|
|
1.1 root 1: /* smbfile.c */
2:
3: /* Synchronet message base (SMB) FILE stream I/O routines */
4:
5: /* $Id: smbfile.c,v 1.7 2004/12/29 04:27:06 rswindell Exp $ */
6:
7: /****************************************************************************
8: * @format.tab-size 4 (Plain Text/Source Code File Header) *
9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
10: * *
11: * Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html *
12: * *
13: * This library is free software; you can redistribute it and/or *
14: * modify it under the terms of the GNU Lesser General Public License *
15: * as published by the Free Software Foundation; either version 2 *
16: * of the License, or (at your option) any later version. *
17: * See the GNU Lesser General Public License for more details: lgpl.txt or *
18: * http://www.fsf.org/copyleft/lesser.html *
19: * *
20: * Anonymous FTP access to the most recent released source is available at *
21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
22: * *
23: * Anonymous CVS access to the development source and modification history *
24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: *
25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login *
26: * (just hit return, no password is necessary) *
27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src *
28: * *
29: * For Synchronet coding style and modification guidelines, see *
30: * http://www.synchro.net/source.html *
31: * *
32: * You are encouraged to submit any modifications (preferably in Unix diff *
33: * format) via e-mail to [email protected] *
34: * *
35: * Note: If this box doesn't appear square, then you need to fix your tabs. *
36: ****************************************************************************/
37:
38: #include "smblib.h"
39:
40: int SMBCALL smb_feof(FILE* fp)
41: {
42: return(feof(fp));
43: }
44:
45: int SMBCALL smb_ferror(FILE* fp)
46: {
47: return(ferror(fp));
48: }
49:
50: int SMBCALL smb_fflush(FILE* fp)
51: {
52: return(fflush(fp));
53: }
54:
55: int SMBCALL smb_fgetc(FILE* fp)
56: {
57: return(fgetc(fp));
58: }
59:
60: int SMBCALL smb_fputc(int ch, FILE* fp)
61: {
62: return(fputc(ch,fp));
63: }
64:
65: int SMBCALL smb_fseek(FILE* fp, long offset, int whence)
66: {
67: return(fseek(fp,offset,whence));
68: }
69:
70: long SMBCALL smb_ftell(FILE* fp)
71: {
72: return(ftell(fp));
73: }
74:
75: long SMBCALL smb_fgetlength(FILE* fp)
76: {
77: return(filelength(fileno(fp)));
78: }
79:
80: int SMBCALL smb_fsetlength(FILE* fp, long length)
81: {
82: return(chsize(fileno(fp),length));
83: }
84:
85: void SMBCALL smb_rewind(FILE* fp)
86: {
87: rewind(fp);
88: }
89:
90: void SMBCALL smb_clearerr(FILE* fp)
91: {
92: clearerr(fp);
93: }
94:
95: size_t SMBCALL smb_fread(smb_t* smb, void* buf, size_t bytes, FILE* fp)
96: {
97: size_t ret;
98: time_t start=0;
99:
100: while(1) {
101: if((ret=fread(buf,sizeof(char),bytes,fp))==bytes)
102: return(ret);
103: if(get_errno()!=EDEADLOCK)
104: return(ret);
105: if(!start)
106: start=time(NULL);
107: else
108: if(time(NULL)-start>=(time_t)smb->retry_time)
109: break;
110: SLEEP(smb->retry_delay);
111: }
112: return(ret);
113: }
114:
115: #if defined(__BORLANDC__)
116: #pragma argsused
117: #endif
118:
119: size_t SMBCALL smb_fwrite(smb_t* smb, const void* buf, size_t bytes, FILE* fp)
120: {
121: return(fwrite(buf,1,bytes,fp));
122: }
123:
124: /****************************************************************************/
125: /* Opens a non-shareable file (FILE*) associated with a message base */
126: /* Retrys for retry_time number of seconds */
127: /* Return 0 on success, non-zero otherwise */
128: /****************************************************************************/
129: int SMBCALL smb_open_fp(smb_t* smb, FILE** fp, int share)
130: {
131: int file;
132: char path[MAX_PATH+1];
133: char* ext;
134: time_t start=0;
135:
136: if(fp==&smb->shd_fp)
137: ext="shd";
138: else if(fp==&smb->sid_fp)
139: ext="sid";
140: else if(fp==&smb->sdt_fp)
141: ext="sdt";
142: else if(fp==&smb->sda_fp)
143: ext="sda";
144: else if(fp==&smb->sha_fp)
145: ext="sha";
146: else if(fp==&smb->hash_fp)
147: ext="hash";
148: else {
149: safe_snprintf(smb->last_error,sizeof(smb->last_error)
150: ,"opening %s: Illegal FILE* pointer argument: %p"
151: ,smb->file, fp);
152: return(SMB_ERR_OPEN);
153: }
154:
155: if(*fp!=NULL) /* Already open! */
156: return(SMB_SUCCESS);
157:
158: SAFEPRINTF2(path,"%s.%s",smb->file,ext);
159:
160: while(1) {
161: if((file=sopen(path,O_RDWR|O_CREAT|O_BINARY,share,S_IREAD|S_IWRITE))!=-1)
162: break;
163: if(get_errno()!=EACCES && get_errno()!=EAGAIN) {
164: safe_snprintf(smb->last_error,sizeof(smb->last_error)
165: ,"%d '%s' opening %s"
166: ,get_errno(),STRERROR(get_errno()),path);
167: return(SMB_ERR_OPEN);
168: }
169: if(!start)
170: start=time(NULL);
171: else
172: if(time(NULL)-start>=(time_t)smb->retry_time) {
173: safe_snprintf(smb->last_error,sizeof(smb->last_error)
174: ,"timeout opening %s (retry_time=%ld)"
175: ,path,smb->retry_time);
176: return(SMB_ERR_TIMEOUT);
177: }
178: SLEEP(smb->retry_delay);
179: }
180: if((*fp=fdopen(file,"r+b"))==NULL) {
181: safe_snprintf(smb->last_error,sizeof(smb->last_error)
182: ,"%d '%s' fdopening %s (%d)"
183: ,get_errno(),STRERROR(get_errno()),path,file);
184: close(file);
185: return(SMB_ERR_OPEN);
186: }
187: setvbuf(*fp,NULL,_IOFBF,2*1024);
188: return(SMB_SUCCESS);
189: }
190:
191: /****************************************************************************/
192: /****************************************************************************/
193: void SMBCALL smb_close_fp(FILE** fp)
194: {
195: if(fp!=NULL) {
196: if(*fp!=NULL)
197: fclose(*fp);
198: *fp=NULL;
199: }
200: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.