file.cpp 9.16 KB
Newer Older
1
2
3
4
5
6
/* Synchronet file transfer-related functions */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"

/****************************************************************************/
/* Prints all information of file in file_t structure 'f'					*/
/****************************************************************************/
void sbbs_t::fileinfo(file_t* f)
{
29
	char	ext[513];
30
	char 	tmp[512];
31
	char	tmp2[64];
32
	char	path[MAX_PATH+1];
33
	char	fname[MAX_PATH+1];
34
	char*	real_fname;
35
	uint	i,j;
36

37
	current_file = f;
38
39
40
41
42
43
	for(i=0;i<usrlibs;i++)
		if(usrlib[i]==cfg.dir[f->dir]->lib)
			break;
	for(j=0;j<usrdirs[i];j++)
		if(usrdir[i][j]==f->dir)
			break;
44

45
	getfilepath(&cfg,f,path);
46
47
	real_fname = getfname(path);
	unpadfname(f->name, fname);
48
49
	bprintf(text[FiLib],i+1,cfg.lib[cfg.dir[f->dir]->lib]->lname);
	bprintf(text[FiDir],j+1,cfg.dir[f->dir]->lname);
50
51
52
	bprintf(text[FiFilename],fname);
	if(strcmp(real_fname, fname) && strcmp(f->desc, real_fname))	/* Different "actual" filename */
		bprintf(text[FiFilename], real_fname);
53

54
	if(f->size!=-1L)
55
56
		bprintf(text[FiFileSize],ultoac(f->size,tmp)
			, byte_estimate_to_str(f->size, tmp2, sizeof(tmp2), /* units: */1024, /* precision: */1));
57
58
59
60
61
	bprintf(text[FiCredits]
		,(cfg.dir[f->dir]->misc&DIR_FREE || !f->cdt) ? "FREE" : ultoac(f->cdt,tmp));
	bprintf(text[FiDescription],f->desc);
	bprintf(text[FiUploadedBy],f->misc&FM_ANON ? text[UNKNOWN_USER] : f->uler);
	if(f->date)
62
63
64
		bprintf(text[FiFileDate],timestr(f->date));
	bprintf(text[FiDateUled],timestr(f->dateuled));
	bprintf(text[FiDateDled],f->datedled ? timestr(f->datedled) : "Never");
65
	bprintf(text[FiTimesDled],f->timesdled);
66
	if(f->size>0 && f->timetodl>0)
67
68
69
70
		bprintf(text[FiTransferTime],sectostr(f->timetodl,tmp));
	if(f->altpath) {
		if(f->altpath<=cfg.altpaths) {
			if(SYSOP)
71
72
				bprintf(text[FiAlternatePath],cfg.altpath[f->altpath-1]); 
		}
73
		else
74
75
			bprintf(text[InvalidAlternatePathN],f->altpath); 
	}
76
	bputs(text[FileHdrDescSeparator]);
77
	if(f->misc&FM_EXTDESC) {
78
		getextdesc(&cfg,f->dir,f->datoffset,ext);
79
		putmsg(ext,P_NOATCODES);
80
81
		CRLF; 
	}
82
	if(f->size==-1L) {
83
		bprintf(text[FileIsNotOnline],f->name);
84
		if(SYSOP)
85
			bprintf("%s\r\n",path);
86
	}
87
88
	if(f->opencount)
		bprintf(text[FileIsOpen],f->opencount,f->opencount>1 ? "s" : nulstr);
89
	current_file = NULL;
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
}


/****************************************************************************/
/* Increments the opencount on the file data 'f' and adds the transaction 	*/
/* to the backout.dab														*/
/****************************************************************************/
void sbbs_t::openfile(file_t* f)
{
	char str1[256],str2[4],str3[4],ch;
	int file;

	/************************************/
	/* Increment open count in dat file */
	/************************************/
105
	sprintf(str1,"%s%s.dat",cfg.dir[f->dir]->data_dir,cfg.dir[f->dir]->code);
106
107
	if((file=nopen(str1,O_RDWR))==-1) {
		errormsg(WHERE,ERR_OPEN,str1,O_RDWR);
108
109
		return; 
	}
Rob Swindell's avatar
Rob Swindell committed
110
	(void)lseek(file,f->datoffset+F_OPENCOUNT,SEEK_SET);
111
112
113
	if(read(file,str2,3)!=3) {
		close(file);
		errormsg(WHERE,ERR_READ,str1,3);
114
115
		return; 
	}
116
	str2[3]=0;
117
	ultoa(atoi(str2)+1,str3,10);
118
	putrec(str2,0,3,str3);
Rob Swindell's avatar
Rob Swindell committed
119
	(void)lseek(file,f->datoffset+F_OPENCOUNT,SEEK_SET);
120
121
122
	if(write(file,str2,3)!=3) {
		close(file);
		errormsg(WHERE,ERR_WRITE,str1,3);
123
124
		return; 
	}
125
126
127
128
	close(file);
	/**********************************/
	/* Add transaction to BACKOUT.DAB */
	/**********************************/
129
	sprintf(str1,"%sbackout.dab",cfg.node_dir);
130
131
	if((file=nopen(str1,O_WRONLY|O_APPEND|O_CREAT))==-1) {
		errormsg(WHERE,ERR_OPEN,str1,O_WRONLY|O_APPEND|O_CREAT);
132
133
		return; 
	}
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	ch=BO_OPENFILE;
	write(file,&ch,1);				/* backout type */
	write(file,cfg.dir[f->dir]->code,8); /* directory code */
	write(file,&f->datoffset,4);		/* offset into .dat file */
	write(file,&ch,BO_LEN-(1+8+4)); /* pad it */
	close(file);
}

/****************************************************************************/
/* Decrements the opencount on the file data 'f' and removes the backout  	*/
/* from the backout.dab														*/
/****************************************************************************/
void sbbs_t::closefile(file_t* f)
{
	char	str1[256],str2[4],str3[4],ch,*buf;
	int		file;
	long	length,l,offset;

	/************************************/
	/* Decrement open count in dat file */
	/************************************/
155
	sprintf(str1,"%s%s.dat",cfg.dir[f->dir]->data_dir,cfg.dir[f->dir]->code);
156
157
	if((file=nopen(str1,O_RDWR))==-1) {
		errormsg(WHERE,ERR_OPEN,str1,O_RDWR);
158
159
		return; 
	}
Rob Swindell's avatar
Rob Swindell committed
160
	(void)lseek(file,f->datoffset+F_OPENCOUNT,SEEK_SET);
161
162
163
	if(read(file,str2,3)!=3) {
		close(file);
		errormsg(WHERE,ERR_READ,str1,3);
164
165
		return; 
	}
166
167
168
	str2[3]=0;
	ch=atoi(str2);
	if(ch) ch--;
169
	ultoa(ch,str3,10);
170
	putrec(str2,0,3,str3);
Rob Swindell's avatar
Rob Swindell committed
171
	(void)lseek(file,f->datoffset+F_OPENCOUNT,SEEK_SET);
172
173
174
	if(write(file,str2,3)!=3) {
		close(file);
		errormsg(WHERE,ERR_WRITE,str1,3);
175
176
		return; 
	}
177
178
179
180
	close(file);
	/*****************************************/
	/* Removing transaction from BACKOUT.DAB */
	/*****************************************/
181
	sprintf(str1,"%sbackout.dab",cfg.node_dir);
182
183
184
185
	if(flength(str1)<1L)	/* file is not there or empty */
		return;
	if((file=nopen(str1,O_RDONLY))==-1) {
		errormsg(WHERE,ERR_OPEN,str1,O_RDONLY);
186
187
		return; 
	}
188
	length=(long)filelength(file);
deuce's avatar
deuce committed
189
	if((buf=(char *)malloc(length))==NULL) {
190
191
		close(file);
		errormsg(WHERE,ERR_ALLOC,str1,length);
192
193
		return; 
	}
194
195
	if(read(file,buf,length)!=length) {
		close(file);
deuce's avatar
deuce committed
196
		free(buf);
197
		errormsg(WHERE,ERR_READ,str1,length);
198
199
		return; 
	}
200
201
	close(file);
	if((file=nopen(str1,O_WRONLY|O_TRUNC))==-1) {
202
		free(buf);
203
		errormsg(WHERE,ERR_OPEN,str1,O_WRONLY|O_TRUNC);
204
205
		return; 
	}
206
207
208
209
210
211
212
213
	ch=0;								/* 'ch' is a 'file already removed' flag */
	for(l=0;l<length;l+=BO_LEN) {       /* in case file is in backout.dab > 1 */
		if(!ch && buf[l]==BO_OPENFILE) {
			memcpy(str1,buf+l+1,8);
			str1[8]=0;
			memcpy(&offset,buf+l+9,4);
			if(!stricmp(str1,cfg.dir[f->dir]->code) && offset==f->datoffset) {
				ch=1;
214
215
216
217
218
				continue; 
			}
		}
		write(file,buf+l,BO_LEN); 
	}
deuce's avatar
deuce committed
219
	free(buf);
220
221
222
223
224
225
226
227
228
229
230
	close(file);
}

/****************************************************************************/
/* Prompts user for file specification. <CR> is *.* and .* is assumed.      */
/* Returns padded file specification.                                       */
/* Returns NULL if input was aborted.                                       */
/****************************************************************************/
char * sbbs_t::getfilespec(char *str)
{
	bputs(text[FileSpecStarDotStar]);
231
	if(!getstr(str,64,K_NONE))
232
		strcpy(str,ALLFILES);
233
#if 0
234
235
	else if(!strchr(str,'.') && strlen(str)<=8)
		strcat(str,".*");
236
#endif
237
238
239
240
241
242
243
244
	if(sys_status&SS_ABORT)
		return(0);
	return(str);
}

/****************************************************************************/
/* Checks to see if filename matches filespec. Returns 1 if yes, 0 if no    */
/****************************************************************************/
245
extern "C" BOOL filematch(const char *filename, const char *filespec)
246
247
248
249
250
251
252
{
    char c;

	for(c=0;c<8;c++) /* Handle Name */
		if(filespec[c]=='*') break;
		else if(filespec[c]=='?') continue;
		else if(toupper(filename[c])!=toupper(filespec[c])) return(FALSE);
253
254
	if(filespec[8]==' ')	/* no extension specified */
		return(TRUE);
255
256
257
258
259
260
261
262
263
264
265
266
267
268
	for(c=9;c<12;c++)
		if(filespec[c]=='*') break;
		else if(filespec[c]=='?') continue;
		else if(toupper(filename[c])!=toupper(filespec[c])) return(FALSE);
	return(TRUE);
}

/*****************************************************************************/
/* Checks the filename 'fname' for invalid symbol or character sequences     */
/*****************************************************************************/
bool sbbs_t::checkfname(char *fname)
{
    int		c=0,d;

269
270
	if(fname[0]=='-'
		|| strcspn(fname,ILLEGAL_FILENAME_CHARS)!=strlen(fname)) {
271
		lprintf(LOG_WARNING,"Suspicious filename attempt: '%s'",fname);
deuce's avatar
deuce committed
272
		hacklog((char *)"Filename", fname);
273
274
		return(false); 
	}
275
276
	if(strstr(fname,".."))
		return(false);
277
#if 0	/* long file name support */
278
279
	if(strcspn(fname,".")>8)
		return(false);
280
#endif
281
282
	d=strlen(fname);
	while(c<d) {
283
		if(fname[c]<=' ' || fname[c]&0x80)
284
			return(false);
285
286
		c++; 
	}
287
288
	return(true);
}
289
290
291
292
293
294
295
296

long sbbs_t::delfiles(const char *inpath, const char *spec, size_t keep)
{
	long result = ::delfiles(inpath, spec, keep);
	if(result < 0)
		errormsg(WHERE, ERR_REMOVE, inpath, result, spec);
	return result;
}