addfiles.c 21.4 KB
Newer Older
1
/* Add files to a Synchronet file database(s) */
2
3
4
5
6

/****************************************************************************
 * @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
 *																			*
 * 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.	*
 ****************************************************************************/

22
23
24
25
26
27
28
#include "nopen.h"
#include "str_util.h"
#include "datewrap.h"
#include "date_str.h"
#include "userdat.h"
#include "filedat.h"
#include "load_cfg.h"
29
30
31
32
#include "smblib.h"
#include "git_branch.h"
#include "git_hash.h"

33
#include <stdbool.h>
34
#include <stdarg.h>
35

36
#define ADDFILES_VER "3.19"
37
38
39
40
41

scfg_t scfg;

long files=0,removed=0,mode=0;

42
char lib[LEN_GSNAME+1];
43
const char* datefmt = NULL;
44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#define DEL_LIST	(1L<<0)
#define NO_EXTEND	(1L<<1)
#define FILE_DATE	(1L<<2)
#define TODAYS_DATE (1L<<3)
#define FILE_ID 	(1L<<4)
#define NO_UPDATE	(1L<<5)
#define NO_NEWDATE	(1L<<6)
#define ASCII_ONLY	(1L<<7)
#define UL_STATS	(1L<<8)
#define ULDATE_ONLY (1L<<9)
#define KEEP_DESC	(1L<<10)
#define AUTO_ADD	(1L<<11)
#define SEARCH_DIR	(1L<<12)
#define SYNC_LIST	(1L<<13)
#define KEEP_SPACE	(1L<<14)
rswindell's avatar
rswindell committed
60
#define CHECK_DATE	(1L<<15)
61
62

/****************************************************************************/
63
/* This is needed by load_cfg.c												*/
64
/****************************************************************************/
65
int lprintf(int level, const char *fmat, ...)
66
67
{
	va_list argptr;
68
	char sbuf[512];
69
70
71
72
73
	int chcount;

	va_start(argptr,fmat);
	chcount=vsprintf(sbuf,fmat,argptr);
	va_end(argptr);
74
75
	truncsp(sbuf);
	printf("%s\n",sbuf);
76
77
78
79
80
81
82
83
84
85
	return(chcount);
}

/****************************************************************************/
/* Updates dstst.dab file													*/
/****************************************************************************/
void updatestats(ulong size)
{
    char	str[MAX_PATH+1];
    int		file;
deuce's avatar
64-bit    
deuce committed
86
	uint32_t	l;
87

88
	SAFEPRINTF(str,"%sdsts.dab",scfg.ctrl_dir);
89
	if((file=nopen(str,O_RDWR|O_BINARY))==-1) {
90
		printf("ERR_OPEN %s\n",str);
91
		return;
92
93
94
95
96
97
98
99
100
101
102
103
104
	}
	lseek(file,20L,SEEK_SET);	/* Skip timestamp, logons and logons today */
	read(file,&l,4);			/* Uploads today		 */
	l++;
	lseek(file,-4L,SEEK_CUR);
	write(file,&l,4);
	read(file,&l,4);			/* Upload bytes today	 */
	l+=size;
	lseek(file,-4L,SEEK_CUR);
	write(file,&l,4);
	close(file);
}

105
bool reupload(smb_t* smb, file_t* f)
106
{
107
	char path[MAX_PATH + 1];
108
	if(smb_renewfile(smb, f, SMB_SELFPACK, getfilepath(&scfg, f, path)) != SMB_SUCCESS) {
109
		fprintf(stderr, "!Error renewing: %s\n", f->name);
110
		return false;
111
	}
112
113
114
	return true;
}

115

116
117
118
119
120
121
122
bool get_file_diz(file_t* f, char* ext, size_t maxlen)
{
	char path[MAX_PATH + 1];
	printf("Extracting DIZ from: %s\n", getfilepath(&scfg, f, path));
	char diz_fpath[MAX_PATH + 1];
	if(!extract_diz(&scfg, f, /* diz_fnames */NULL, diz_fpath, sizeof(diz_fpath))) {
		printf("DIZ does not exist in: %s\n", getfilepath(&scfg, f, path));
123
		return false;
124
125
	}
	printf("Parsing DIZ: %s\n", diz_fpath);
126
127
128
	char* lines = read_diz(diz_fpath, NULL);
	format_diz(lines, ext, maxlen, 0, false);
	free(lines);
129
	remove(diz_fpath);
130
131
132

	if(mode&ASCII_ONLY)
		strip_exascii(ext, ext);
133

134
	if(!(mode&KEEP_DESC)) {
135
136
137
138
		char* fdesc = strdup(ext);
		smb_new_hfield_str(f, SMB_FILEDESC, prep_file_desc(fdesc, fdesc));
		free(fdesc);
	}
139
	return true;
140
141
}

142
void addlist(char *inpath, uint dirnum, const char* uploader, uint dskip, uint sskip)
143
{
144
145
146
147
148
149
	char str[MAX_PATH+1];
	char tmp[MAX_PATH+1];
	char fname[MAX_PATH+1];
	char listpath[MAX_PATH+1];
	char filepath[MAX_PATH+1];
	char curline[256],nextline[256];
150
	char *p;
151
152
	char ext[1024];
	int i;
153
	off_t l;
154
	BOOL exist;
155
156
157
	FILE *stream;
	DIR*	dir;
	DIRENT*	dirent;
158
159
	smb_t smb;
	file_t f;
160

161
162
163
164
165
166
	int result = smb_open_dir(&scfg, &smb, dirnum);
	if(result != SMB_SUCCESS) {
		fprintf(stderr, "!Error %d (%s) opening %s\n", result, smb.last_error, smb.file);
		return;
	}
	str_list_t fname_list = loadfilenames(&smb, ALLFILES, /* time: */0, FILE_SORT_NATURAL, /* count: */NULL);
167
	if(mode&SEARCH_DIR) {
168
		SAFECOPY(str, scfg.dir[dirnum]->path);
169
170
171
172
		printf("Searching %s\n\n",str);
		dir=opendir(str);

		while(dir!=NULL && (dirent=readdir(dir))!=NULL) {
173
			SAFEPRINTF2(filepath, "%s%s"
174
				,scfg.dir[dirnum]->path
rswindell's avatar
rswindell committed
175
				,dirent->d_name);
176
			if(isdir(filepath))
177
				continue;
178
179
180
181
182
183
			const char* fname = getfname(filepath);
			printf("%s  ", fname);
			if(strListFind(fname_list, fname, /* case-sensitive: */FALSE) && (mode&NO_UPDATE)) {
				printf("already added.\n");
				continue;
			}
184
			memset(ext, 0, sizeof(ext));
185
186
187
			memset(&f, 0, sizeof(f));
			char fdesc[LEN_FDESC + 1] = {0};
			uint32_t cdt = (uint32_t)flength(filepath);
rswindell's avatar
rswindell committed
188
			time_t file_timestamp = fdate(filepath);
189
190
191
			printf("%10"PRIu32"  %s\n"
				,cdt, unixtodstr(&scfg,(time32_t)file_timestamp,str));
			exist = smb_findfile(&smb, fname, &f) == SMB_SUCCESS;
192
193
194
			if(exist) {
				if(mode&NO_UPDATE)
					continue;
195
				if((mode&CHECK_DATE) && file_timestamp <= f.idx.time)
196
					continue;
197
				if(mode&ULDATE_ONLY) {
198
199
200
					reupload(&smb, &f);
					continue; 
				} 
201
202
203
			}

			if(mode&TODAYS_DATE) {		/* put today's date in desc */
204
205
206
207
				time_t now = time(NULL);
				if(datefmt) {
					struct tm tm = {0};
					localtime_r(&now, &tm);
208
					strftime(fdesc, sizeof(fdesc), datefmt, &tm);
209
				} else
210
					unixtodstr(&scfg, (time32_t)now, fdesc);
211
				SAFECAT(fdesc,"  ");
212
213
214
215
216
			}
			else if(mode&FILE_DATE) {		/* get the file date and put into desc */
				if(datefmt) {
					struct tm tm = {0};
					localtime_r(&file_timestamp, &tm);
217
					strftime(fdesc, sizeof(fdesc), datefmt, &tm);
218
				} else
219
					unixtodstr(&scfg,(time32_t)file_timestamp,fdesc);
220
				SAFECAT(fdesc,"  ");
221
222
			}

223
224
225
226
227
228
			char* ext_desc = NULL;
			if(mode&FILE_ID) {
				if(get_file_diz(&f, ext, sizeof(ext))) {
					ext_desc = ext;
				}
			}
229

230
			prep_file_desc(fdesc, fdesc);
231
			if(mode&ASCII_ONLY)
232
233
234
235
236
237
238
239
				strip_exascii(fdesc, fdesc);

			smb_hfield_str(&f, SMB_FILENAME, fname);
			smb_hfield_str(&f, SMB_FILEDESC, fdesc);
			smb_hfield_str(&f, SENDER, uploader);
			smb_hfield_bin(&f, SMB_COST, cdt);

			truncsp(ext_desc);
240
			if(exist) {
241
				result = smb_updatemsg(&smb, &f);
242
				if(!(mode&NO_NEWDATE))
243
					reupload(&smb, &f);
244
			}
245
			else
246
				result = smb_addfile(&smb, &f, SMB_SELFPACK, ext_desc, NULL, filepath);
247
248
249
			smb_freefilemem(&f);
			if(result != SMB_SUCCESS)
				fprintf(stderr, "!Error %d (%s) adding file to %s", result, smb.last_error, smb.file);
250
			if(mode&UL_STATS)
251
252
				updatestats(cdt);
			files++; 
253
254
255
		}
		if(dir!=NULL)
			closedir(dir);
256
257
258
		smb_close(&smb);
		strListFree(&fname_list);
		return; 
259
260
261
	}


262
	SAFECOPY(listpath,inpath);
263
	fexistcase(listpath);
264
	if((stream=fopen(listpath,"r"))==NULL) {
265
266
		fprintf(stderr,"Error %d (%s) opening %s\n"
			,errno,strerror(errno),listpath);
267
		SAFEPRINTF2(listpath, "%s%s", scfg.dir[dirnum]->path, inpath);
268
		fexistcase(listpath);
269
		if((stream=fopen(listpath,"r"))==NULL) {
270
271
			printf("Can't open: %s\n"
				   "        or: %s\n",inpath,listpath);
272
273
274
275
			smb_close(&smb);
			strListFree(&fname_list);
			return; 
		} 
276
	}
277
278

	printf("Adding %s to %s %s\n\n"
279
		,listpath,scfg.lib[scfg.dir[dirnum]->lib]->sname,scfg.dir[dirnum]->sname);
280
281

	fgets(nextline,255,stream);
282
	do {
283
		char fdesc[LEN_FDESC + 1] = {0};
284
		memset(ext, 0, sizeof(ext));
285
		SAFECOPY(curline,nextline);
286
287
288
		nextline[0]=0;
		fgets(nextline,255,stream);
		truncsp(curline);
289
		if(curline[0]<=' ' || (mode&ASCII_ONLY && (uchar)curline[0]>=0x7e))
290
291
			continue;
		printf("%s\n",curline);
292
		SAFECOPY(fname,curline);
293

294
		p=strchr(fname,' ');
295
		if(p) *p=0;
rswindell's avatar
rswindell committed
296
#if 0 // allow import of bare filename list
297
298
		else				   /* no space after filename? */
			continue;
rswindell's avatar
rswindell committed
299
#endif
300
		if(!IS_ALPHANUMERIC(*fname)) {	// filename doesn't begin with an alpha-numeric char?
301
302
			continue;
		}
303

304
		SAFEPRINTF2(filepath, "%s%s", scfg.dir[dirnum]->path, fname);
305

306
307
		if(strcspn(fname,"\\/|<>+[]:=\";,")!=strlen(fname)) {
			fprintf(stderr, "!Illegal filename: %s\n", fname);
308
			continue;
309
310
		}

rswindell's avatar
rswindell committed
311
		time_t file_timestamp = fdate(filepath);
312
		memset(&f, 0, sizeof(f));
313
		exist = smb_findfile(&smb, fname, &f) == SMB_SUCCESS;
314
315
316
		if(exist) {
			if(mode&NO_UPDATE)
				continue;
317
			if((mode&CHECK_DATE) && file_timestamp <= f.idx.time)
318
				continue;
319
			if(mode&ULDATE_ONLY) {
320
321
322
				reupload(&smb, &f);
				continue; 
			} 
323
		}
324

325
326
327
		if(mode&FILE_DATE) {		/* get the file date and put into desc */
			unixtodstr(&scfg,(time32_t)file_timestamp, fdesc);
			strcat(fdesc, "  "); 
328
		}
329
330
331
332
333

		if(mode&TODAYS_DATE) {		/* put today's date in desc */
			l=time32(NULL);
			unixtodstr(&scfg,(time32_t)l,fdesc);
			strcat(fdesc, "  "); 
334
		}
335
336
337

		if(dskip && strlen(curline)>=dskip) p=curline+dskip;
		else {
338
339
			p = curline;
			FIND_WHITESPACE(p);
340
			SKIP_WHITESPACE(p);
341
		}
342
		SAFECOPY(tmp,p);
343
344
		prep_file_desc(tmp, tmp);
		sprintf(fdesc + strlen(fdesc), "%.*s", (int)(LEN_FDESC-strlen(fdesc)), tmp);
345

346
		char* ext_desc = NULL;
347
		if(nextline[0]==' ' || strlen(p)>LEN_FDESC) {	/* ext desc */
348
			if(!(mode&NO_EXTEND)) {
349
				memset(ext, 0, sizeof(ext));
350
351
				sprintf(ext,"%s\r\n",p); 
				ext_desc = ext;
352
			}
353

354
			if(nextline[0]==' ') {
355
				SAFECOPY(str,nextline);				   /* tack on to end of desc */
356
				p=str+dskip;
357
				while(*p>0 && *p<=' ') p++;
358
				i=LEN_FDESC-strlen(fdesc);
359
360
361
362
				if(i>1) {
					p[i-1]=0;
					truncsp(p);
					if(p[0]) {
363
364
						SAFECAT(fdesc," ");
						SAFECAT(fdesc,p);
365
366
					}
				}
367
			}
368
369

			while(!feof(stream) && !ferror(stream) && strlen(ext)<512) {
370
				if(nextline[0]!=' ')
371
372
373
374
375
					break;
				truncsp(nextline);
				printf("%s\n",nextline);
				if(!(mode&NO_EXTEND)) {
					p=nextline+dskip;
376
					ext_desc = ext;
377
					while(*p==' ') p++;
378
379
					SAFECAT(ext,p);
					SAFECAT(ext,"\r\n");
380
				}
381
				nextline[0]=0;
382
383
				fgets(nextline,255,stream);
			}
384
		}
385

386
387
388
389
		l=flength(filepath);
		if(l<0L) {
			printf("%s not found.\n",filepath);
			continue;
390
		}
391
392
393
394
395
		if(l == 0L) {
			printf("%s is a zero length file.\n",filepath);
			continue;
		}
		if(sskip) l=atol(fname+sskip);
396

397
		if(mode&FILE_ID)
398
			get_file_diz(&f, ext, sizeof(ext));
399

400
		prep_file_desc(fdesc, fdesc);
401
		if(mode&ASCII_ONLY)
402
403
404
405
406
407
			strip_exascii(fdesc, fdesc);
		uint32_t cdt = (uint32_t)l;
		smb_hfield_bin(&f, SMB_COST, cdt);
		smb_hfield_str(&f, SMB_FILENAME, fname);
		smb_hfield_str(&f, SMB_FILEDESC, fdesc);
		smb_hfield_str(&f, SENDER, uploader);
408
		if(exist) {
409
			result = smb_updatemsg(&smb, &f);
410
			if(!(mode&NO_NEWDATE))
411
				reupload(&smb, &f);
412
		}
413
		else
414
			result = smb_addfile(&smb, &f, SMB_SELFPACK, ext_desc, NULL, filepath);
415
416
417
418
		smb_freefilemem(&f);
		if(result != SMB_SUCCESS)
			fprintf(stderr, "!ERROR %d (%s) writing to %s\n"
				, result, smb.last_error, smb.file);
419
420

		if(mode&UL_STATS)
421
			updatestats((ulong)l);
422
		files++;
423
	} while(!feof(stream) && !ferror(stream));
424
425
426
	fclose(stream);
	if(mode&DEL_LIST && !(mode&SYNC_LIST)) {
		printf("\nDeleting %s\n",listpath);
427
		remove(listpath);
428
	}
429
430
	smb_close(&smb);
	strListFree(&fname_list);
431
432
433
434
}

void synclist(char *inpath, int dirnum)
{
435
	char	str[1024];
436
	char	listpath[MAX_PATH+1];
437
	char*	p;
438
439
	int		found;
	long	l;
440
	FILE*	stream;
441
442
443
	file_t*	f;
	file_t*	file_list;
	smb_t	smb;
444

445
	SAFECOPY(listpath,inpath);
446
	if((stream=fopen(listpath,"r"))==NULL) {
447
		sprintf(listpath,"%s%s", scfg.dir[dirnum]->path,inpath);
448
		if((stream=fopen(listpath,"r"))==NULL) {
449
450
			printf("Can't open: %s\n"
				   "        or: %s\n",inpath,listpath);
451
452
			return;
		}
453
	}
454

455
456
457
	int result = smb_open_dir(&scfg, &smb, dirnum);
	if(result != SMB_SUCCESS) {
		fprintf(stderr, "!Error %d (%s) opening %s\n", result, smb.last_error, smb.file);
Rob Swindell's avatar
Rob Swindell committed
458
		fclose(stream);
459
460
461
		return;
	}
	size_t file_count;
Rob Swindell's avatar
Rob Swindell committed
462
	file_list = loadfiles(&smb, NULL, 0, file_detail_normal, FILE_SORT_NATURAL, &file_count);
463

464
465
466
	printf("\nSynchronizing %s with %s %s\n\n"
		,listpath,scfg.lib[scfg.dir[dirnum]->lib]->sname,scfg.dir[dirnum]->sname);

467
468
	for(l = 0; l < (long)file_count; l++) {
		f = &file_list[l];
469
470
471
472
473
474
		rewind(stream);
		found=0;
		while(!found) {
			if(!fgets(str,1000,stream))
				break;
			truncsp(str);
475
			p=strchr(str,' ');
476
			if(p) *p=0;
477
478
			if(!stricmp(str, f->name))
				found=1; 
479
		}
480
481
		if(found)
			continue;
482
483
484
485
486
487
488
489
		printf("%s not found in list - ", f->name);
		if(smb_removefile(&smb, f) != SMB_SUCCESS)
			fprintf(stderr, "!ERROR (%s) removing file from database\n", smb.last_error);
		else {
			if(remove(getfilepath(&scfg, f, str)))
				printf("Error removing %s\n",str);
			removed++;
			printf("Removed from database\n");
490
491
		}
	}
492
	freefiles(file_list, file_count);
493
494
495

	if(mode&DEL_LIST) {
		printf("\nDeleting %s\n",listpath);
496
		remove(listpath);
497
	}
498
	smb_close(&smb);
499
	fclose(stream);
500
501
}

502
char *usage="\nusage: addfiles code [-opts] +list "
503
			 "[desc_off] [size_off]"
504
	"\n   or: addfiles code [-opts] file "
505
		"[\"description\"]\n"
506
	"\navailable opts:"
507
508
509
510
511
512
	"\n      -a         import ASCII only (no extended ASCII)"
	"\n      -b         synchronize database with file list (use with caution)"
	"\n      -c         do not remove extra spaces from file description"
	"\n      -d         delete list after import"
	"\n      -e         do not import extended descriptions"
	"\n      -f         include file date in descriptions"
513
	"\n      -F <fmt>   include file date in descriptions, using strftime format"
514
515
516
517
	"\n      -t         include today's date in descriptions"
	"\n      -i         include added files in upload statistics"
	"\n      -n         do not update information for existing files"
	"\n      -o         update upload date only for existing files"
rswindell's avatar
rswindell committed
518
	"\n      -p         compare file date with upload date for existing files"
519
520
521
522
523
524
	"\n      -u         do not update upload date for existing files"
	"\n      -z         check for and import FILE_ID.DIZ and DESC.SDI"
	"\n      -k         keep original short description (not DIZ)"
	"\n      -s         search directory for files (no file list)"
	"\n      -l <lib>   specify library (short name) to Auto-ADD"
	"\n      -x <name>  specify uploader's user name (may require quotes)"
525
	"\n"
526
527
	"\nAuto-ADD:   use - in place of code for Auto-ADD of FILES.BBS"
	"\n            use -filename to Auto-ADD a different filename"
528
	"\n            use -l \"libname\" to only Auto-ADD files to a specific library"
529
530
531
532
533
534
535
536
	;

/*********************/
/* Entry point (duh) */
/*********************/
int main(int argc, char **argv)
{
	char error[512];
537
538
	char str[MAX_PATH+1];
	char tmp[MAX_PATH+1];
539
	char *p;
540
	char exist,listgiven=0,namegiven=0,ext[LEN_EXTDESC + 1]
541
		,auto_name[MAX_PATH+1]="FILES.BBS";
542
	int i,j;
543
	uint desc_offset=0, size_offset=0;
544
	long l;
545
	smb_t	smb;
546
	file_t	f;
547
	uint dirnum = INVALID_DIR;
548

549
	fprintf(stderr,"\nADDFILES v%s-%s %s/%s - Adds Files to Synchronet "
550
551
552
		"Filebase\n"
		,ADDFILES_VER
		,PLATFORM_DESC
553
554
		,GIT_BRANCH
		,GIT_HASH
555
556
557
		);

	if(argc<2) {
558
		puts(usage);
559
		return(1);
560
	}
561

562
	p = get_ctrl_dir(/* warn: */true);
563

564
565
	memset(&scfg,0,sizeof(scfg));
	scfg.size=sizeof(scfg);
566
	SAFECOPY(scfg.ctrl_dir,p);
567
568
569
570
571

	if(chdir(scfg.ctrl_dir)!=0)
		fprintf(stderr,"!ERROR changing directory to: %s", scfg.ctrl_dir);

	printf("\nLoading configuration files from %s\n",scfg.ctrl_dir);
572
	if(!load_cfg(&scfg, /* text: */NULL, /* prep: */TRUE, /* node: */FALSE, error, sizeof(error))) {
573
574
575
		fprintf(stderr,"!ERROR loading configuration files: %s\n",error);
		exit(1);
	}
rswindell's avatar
rswindell committed
576
577
	SAFECOPY(scfg.temp_dir,"../temp");
	prep_dir(scfg.ctrl_dir, scfg.temp_dir, sizeof(scfg.temp_dir));
578
	memset(&smb, 0, sizeof(smb));
579

580
581
	if(argv[1][0]=='*' || argv[1][0]=='-') {
		if(argv[1][1]=='?') {
582
			puts(usage);
583
584
			exit(0);
		}
585
		if(argv[1][1])
586
			SAFECOPY(auto_name,argv[1]+1);
587
		mode|=AUTO_ADD;
588
		i=0;
589
	} else {
590
		if(!IS_ALPHANUMERIC(argv[1][0]) && argc==2) {
591
			puts(usage);
592
			return(1);
593
594
		}

595
		for(i=0;i<scfg.total_dirs;i++)
596
			if(!stricmp(scfg.dir[i]->code,argv[1])) /* use matchmatchi() instead? */
597
598
599
600
				break;

		if(i>=scfg.total_dirs) {
			printf("Directory code '%s' not found.\n",argv[1]);
601
602
603
			exit(1); 
		} 
		dirnum = i;
604
	}
605

606
607
	memset(&f,0,sizeof(f));
	const char* uploader = "-> ADDFILES <-";
608
609

	for(j=2;j<argc;j++) {
610
		if(argv[j][0]=='*')     /* set the uploader name (legacy) */
611
612
			uploader = argv[j]+1;
		else 
613
614
615
			if(argv[j][0]=='-'
			|| argv[j][0]=='/'
			) {      /* options */
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
			for(i=1;argv[j][i];i++)
				switch(toupper(argv[j][i])) {
					case 'A':
						mode|=ASCII_ONLY;
						break;
					case 'B':
						mode|=(SYNC_LIST|NO_UPDATE);
						break;
					case 'C':
						mode|=KEEP_SPACE;
						break;
					case 'D':
						mode|=DEL_LIST;
						break;
					case 'E':
						mode|=NO_EXTEND;
						break;
					case 'I':
						mode|=UL_STATS;
						break;
636
637
638
					case 'L':
						j++;
						if(argv[j]==NULL) {
639
							puts(usage);
640
641
642
643
644
							return(-1);
						}
						SAFECOPY(lib,argv[j]);
						i=strlen(argv[j])-1;
						break;
645
646
647
					case 'X':
						j++;
						if(argv[j]==NULL) {
648
							puts(usage);
649
650
							return(-1);
						}
651
						uploader = argv[j];
652
653
						i=strlen(argv[j])-1;
						break;
654
655
656
657
658
659
660
661
662
663
664
665
					case 'Z':
						mode|=FILE_ID;
						break;
					case 'K':
						mode|=KEEP_DESC;	 /* Don't use DIZ for short desc */
						break;
					case 'N':
						mode|=NO_UPDATE;
						break;
					case 'O':
						mode|=ULDATE_ONLY;
						break;
rswindell's avatar
rswindell committed
666
667
668
					case 'P':
						mode|=CHECK_DATE;
						break;
669
670
671
672
673
					case 'U':
						mode|=NO_NEWDATE;
						break;
					case 'F':
						mode|=FILE_DATE;
674
675
676
677
678
679
680
681
682
683
						if(argv[j][i] == 'F') {
							j++;
							if(argv[j]==NULL) {
								puts(usage);
								return(-1);
							}
							datefmt = argv[j];
							i=strlen(argv[j]) - 1;
							break;
						}
684
685
686
687
688
689
690
691
						break;
					case 'T':
						mode|=TODAYS_DATE;
						break;
					case 'S':
						mode|=SEARCH_DIR;
						break;
					default:
692
						puts(usage);
693
694
						return(1);
			}
695
		}
696
		else if(IS_DIGIT(argv[j][0])) {
697
698
699
700
701
702
			if(desc_offset==0)
				desc_offset=atoi(argv[j]);
			else
				size_offset=atoi(argv[j]);
			continue;
		}
703
704
		else if(argv[j][0]=='+') {      /* filelist - FILES.BBS */
			listgiven=1;
rswindell's avatar
rswindell committed
705
			if(argc > j+1
706
				&& IS_DIGIT(argv[j+1][0])) { /* skip x characters before description */
rswindell's avatar
rswindell committed
707
				if(argc > j+2
708
					&& IS_DIGIT(argv[j+2][0])) { /* skip x characters before size */
709
					addlist(argv[j]+1, dirnum, uploader, atoi(argv[j+1]), atoi(argv[j+2]));
710
					j+=2;
711
				}
712
				else {
713
714
715
					addlist(argv[j]+1, dirnum, uploader, atoi(argv[j+1]), 0);
					j++; 
				} 
716
			}
717
			else
718
				addlist(argv[j]+1, dirnum, uploader, 0, 0);
719
			if(mode&SYNC_LIST)
720
				synclist(argv[j]+1, dirnum); 
721
		}
722
723
		else {
			namegiven=1;
724
725
726
			const char* fname = argv[j];
			char fdesc[LEN_FDESC + 1] = {0};

727
			if(j+1==argc) {
728
729
				printf("%s no description given.\n",fname);
				SAFECOPY(fdesc, "no description given");
730
			}
731

732
			sprintf(str,"%s%s", scfg.dir[dirnum]->path, fname);
733
734
735
736
737
738
739
			if(mode&FILE_DATE)
				sprintf(fdesc, "%s  ", unixtodstr(&scfg,(time32_t)fdate(str),tmp));
			if(mode&TODAYS_DATE)
				sprintf(fdesc, "%s  ", unixtodstr(&scfg,time32(NULL),tmp));
			sprintf(tmp, "%.*s", (int)(LEN_FDESC-strlen(fdesc)), argv[++j]);
			SAFECOPY(fdesc, tmp);
			l=(long)flength(str);
740
741
			if(l==-1) {
				printf("%s not found.\n",str);
742
				continue;
743
			}
744
745
746
747
748
749
750
751
752
753
			if(SMB_IS_OPEN(&smb))
				smb_close(&smb);

			int result = smb_open_dir(&scfg, &smb, dirnum);
			if(result != SMB_SUCCESS) {
				fprintf(stderr, "!ERROR %d (%s) opening %s\n", result, smb.last_error, smb.file);
				exit(EXIT_FAILURE);
			}

			exist = smb_findfile(&smb, fname, &f) == SMB_SUCCESS;
754
755
756
			if(exist) {
				if(mode&NO_UPDATE)
					continue;
757
				if((mode&CHECK_DATE) && fdate(str) <= f.idx.time)
758
					continue;
759
				if(mode&ULDATE_ONLY) {
760
761
762
					reupload(&smb, &f);
					continue; 
				} 
763
			}
764
765
			memset(&f, 0, sizeof(f));
			prep_file_desc(fdesc, fdesc);
766
			if(mode&ASCII_ONLY)
767
768
769
770
771
772
773
774
775
776
777
				strip_exascii(fdesc, fdesc);
			smb_hfield_str(&f, SMB_FILENAME, fname);
			smb_hfield_str(&f, SMB_FILEDESC, fdesc);
			smb_hfield_str(&f, SENDER, uploader);
			uint32_t cdt = (uint32_t)l;
			smb_hfield_bin(&f, SMB_COST, cdt);

			printf("%s %7"PRIu64" %s\n",fname, (int64_t)l, fdesc);
			char* ext_desc = NULL;
			if(get_file_diz(&f, ext, sizeof(ext)))
				ext_desc = ext;
778
779
			if(exist) {
				if(!(mode&NO_NEWDATE))
780
781
782
					reupload(&smb, &f);
				else
					result = smb_updatemsg(&smb, &f);
783
			}
784
			else
785
				result = smb_addfile(&smb, &f, SMB_SELFPACK, ext_desc, NULL, str);
786
787
			if(mode&UL_STATS)
				updatestats(l);
788
789
			files++;
		}
790
	}
791
792
793

	if(mode&AUTO_ADD) {
		for(i=0;i<scfg.total_dirs;i++) {
794
795
			if(lib[0] && stricmp(scfg.lib[scfg.dir[i]->lib]->sname,lib))
				continue;
796
797
			if(scfg.dir[i]->misc&DIR_NOAUTO)
				continue;
798
			dirnum = i;
799
			if(mode&SEARCH_DIR) {
800
801
				addlist("", dirnum, uploader, desc_offset, size_offset);
				continue; 
802
			}
803
			sprintf(str,"%s%s.lst",scfg.dir[dirnum]->path,scfg.dir[dirnum]->code);
804
			if(fexistcase(str) && flength(str)>0L) {
805
				printf("Auto-adding %s\n",str);
806
				addlist(str, dirnum, uploader, desc_offset, size_offset);
807
808
				if(mode&SYNC_LIST)
					synclist(str,i);
809
				continue;
810
			}
811
			SAFEPRINTF2(str,"%s%s",scfg.dir[dirnum]->path,auto_name);
812
			if(fexistcase(str) && flength(str)>0L) {
813
				printf("Auto-adding %s\n",str);
814
				addlist(str, dirnum, uploader, desc_offset, size_offset);
815
816
				if(mode&SYNC_LIST)
					synclist(str,i);
817
818
819
				continue;
			}
		}
820
	}
821
822
823

	else {
		if(!listgiven && !namegiven) {
824
			sprintf(str,"%s%s.lst",scfg.dir[dirnum]->path, scfg.dir[dirnum]->code);
825
			if(!fexistcase(str) || flength(str)<=0L)
826
				SAFEPRINTF2(str,"%s%s",scfg.dir[dirnum]->path, auto_name);
827
			addlist(str, dirnum, uploader, desc_offset, size_offset);
828
			if(mode&SYNC_LIST)
829
				synclist(str, dirnum);
830
		}
831
	}
832
833
834
835
836
837
838
839

	printf("\n%lu file(s) added.",files);
	if(removed)
		printf("\n%lu files(s) removed.",removed);
	printf("\n");
	return(0);
}