addfiles.c 21.9 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 42 43

scfg_t scfg;

int cur_altpath=0;

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

44
char lib[LEN_GSNAME+1];
45
const char* datefmt = NULL;
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#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
62
#define CHECK_DATE	(1L<<15)
63 64

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

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

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

90
	sprintf(str,"%sdsts.dab",scfg.ctrl_dir);
91
	if((file=nopen(str,O_RDWR|O_BINARY))==-1) {
92
		printf("ERR_OPEN %s\n",str);
93
		return;
94 95 96 97 98 99 100 101 102 103 104 105 106
	}
	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);
}

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

117

118 119 120 121 122 123 124
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));
125
		return false;
126 127
	}
	printf("Parsing DIZ: %s\n", diz_fpath);
128 129 130
	char* lines = read_diz(diz_fpath, NULL);
	format_diz(lines, ext, maxlen, 0, false);
	free(lines);
131
	remove(diz_fpath);
132 133 134

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

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

144
void addlist(char *inpath, uint dirnum, const char* uploader, uint dskip, uint sskip)
145
{
146 147 148 149 150 151
	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];
152
	char *p;
153 154
	char ext[1024];
	int i;
155
	off_t l;
156
	BOOL exist;
157 158 159
	FILE *stream;
	DIR*	dir;
	DIRENT*	dirent;
160 161
	smb_t smb;
	file_t f;
162

163 164 165 166 167 168
	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);
169
	if(mode&SEARCH_DIR) {
170
		SAFECOPY(str,cur_altpath ? scfg.altpath[cur_altpath-1] : scfg.dir[dirnum]->path);
171 172 173 174
		printf("Searching %s\n\n",str);
		dir=opendir(str);

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

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

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

232
			prep_file_desc(fdesc, fdesc);
233
			if(mode&ASCII_ONLY)
234 235 236 237 238 239 240 241
				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);
242
			if(exist) {
243
				result = smb_updatemsg(&smb, &f);
244
				if(!(mode&NO_NEWDATE))
245
					reupload(&smb, &f);
246
			}
247
			else
248
				result = smb_addfile(&smb, &f, SMB_SELFPACK, ext_desc, NULL, filepath);
249 250 251
			smb_freefilemem(&f);
			if(result != SMB_SUCCESS)
				fprintf(stderr, "!Error %d (%s) adding file to %s", result, smb.last_error, smb.file);
252
			if(mode&UL_STATS)
253 254
				updatestats(cdt);
			files++; 
255 256 257
		}
		if(dir!=NULL)
			closedir(dir);
258 259 260
		smb_close(&smb);
		strListFree(&fname_list);
		return; 
261 262 263
	}


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

	printf("Adding %s to %s %s\n\n"
282
		,listpath,scfg.lib[scfg.dir[dirnum]->lib]->sname,scfg.dir[dirnum]->sname);
283 284

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

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

307 308
		sprintf(filepath, "%s%s", cur_altpath ? scfg.altpath[cur_altpath-1]
			: scfg.dir[dirnum]->path,fname);
309

310 311
		if(strcspn(fname,"\\/|<>+[]:=\";,")!=strlen(fname)) {
			fprintf(stderr, "!Illegal filename: %s\n", fname);
312
			continue;
313 314
		}

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

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

		if(mode&TODAYS_DATE) {		/* put today's date in desc */
			l=time32(NULL);
			unixtodstr(&scfg,(time32_t)l,fdesc);
			strcat(fdesc, "  "); 
338
		}
339 340 341

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

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

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

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

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

401
		if(mode&FILE_ID)
402
			get_file_diz(&f, ext, sizeof(ext));
403

404
		prep_file_desc(fdesc, fdesc);
405
		if(mode&ASCII_ONLY)
406 407 408 409 410 411
			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);
412
		if(exist) {
413
			result = smb_updatemsg(&smb, &f);
414
			if(!(mode&NO_NEWDATE))
415
				reupload(&smb, &f);
416
		}
417
		else
418
			result = smb_addfile(&smb, &f, SMB_SELFPACK, ext_desc, NULL, filepath);
419 420 421 422
		smb_freefilemem(&f);
		if(result != SMB_SUCCESS)
			fprintf(stderr, "!ERROR %d (%s) writing to %s\n"
				, result, smb.last_error, smb.file);
423 424

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

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

449
	SAFECOPY(listpath,inpath);
450
	if((stream=fopen(listpath,"r"))==NULL) {
451 452
		sprintf(listpath,"%s%s",cur_altpath ? scfg.altpath[cur_altpath-1]
				: scfg.dir[dirnum]->path,inpath);
453
		if((stream=fopen(listpath,"r"))==NULL) {
454 455
			printf("Can't open: %s\n"
				   "        or: %s\n",inpath,listpath);
456 457
			return;
		}
458
	}
459

460 461 462
	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
463
		fclose(stream);
464 465 466
		return;
	}
	size_t file_count;
Rob Swindell's avatar
Rob Swindell committed
467
	file_list = loadfiles(&smb, NULL, 0, file_detail_normal, FILE_SORT_NATURAL, &file_count);
468

469 470 471
	printf("\nSynchronizing %s with %s %s\n\n"
		,listpath,scfg.lib[scfg.dir[dirnum]->lib]->sname,scfg.dir[dirnum]->sname);

472 473
	for(l = 0; l < (long)file_count; l++) {
		f = &file_list[l];
474 475 476 477 478 479
		rewind(stream);
		found=0;
		while(!found) {
			if(!fgets(str,1000,stream))
				break;
			truncsp(str);
480
			p=strchr(str,' ');
481
			if(p) *p=0;
482 483
			if(!stricmp(str, f->name))
				found=1; 
484
		}
485 486
		if(found)
			continue;
487 488 489 490 491 492 493 494
		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");
495 496
		}
	}
497
	freefiles(file_list, file_count);
498 499 500

	if(mode&DEL_LIST) {
		printf("\nDeleting %s\n",listpath);
501
		remove(listpath);
502
	}
503
	smb_close(&smb);
504
	fclose(stream);
505 506
}

507
char *usage="\nusage: addfiles code [.alt_path] [-opts] +list "
508
			 "[desc_off] [size_off]"
509
	"\n   or: addfiles code [.alt_path] [-opts] file "
510
		"[\"description\"]\n"
511
	"\navailable opts:"
512 513 514 515 516 517
	"\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"
518
	"\n      -F <fmt>   include file date in descriptions, using strftime format"
519 520 521 522
	"\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
523
	"\n      -p         compare file date with upload date for existing files"
524 525 526 527 528 529
	"\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)"
530
	"\n"
531 532
	"\nAuto-ADD:   use - in place of code for Auto-ADD of FILES.BBS"
	"\n            use -filename to Auto-ADD a different filename"
533
	"\n            use -l \"libname\" to only Auto-ADD files to a specific library"
534 535 536 537 538 539 540 541
	;

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

554
	fprintf(stderr,"\nADDFILES v%s-%s %s/%s - Adds Files to Synchronet "
555 556 557
		"Filebase\n"
		,ADDFILES_VER
		,PLATFORM_DESC
558 559
		,GIT_BRANCH
		,GIT_HASH
560 561 562
		);

	if(argc<2) {
563
		puts(usage);
564
		return(1);
565
	}
566

567
	p = get_ctrl_dir(/* warn: */true);
568

569 570
	memset(&scfg,0,sizeof(scfg));
	scfg.size=sizeof(scfg);
571
	SAFECOPY(scfg.ctrl_dir,p);
572 573 574 575 576

	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);
577
	if(!load_cfg(&scfg, /* text: */NULL, /* prep: */TRUE, /* node: */FALSE, error, sizeof(error))) {
578 579 580
		fprintf(stderr,"!ERROR loading configuration files: %s\n",error);
		exit(1);
	}
rswindell's avatar
rswindell committed
581 582
	SAFECOPY(scfg.temp_dir,"../temp");
	prep_dir(scfg.ctrl_dir, scfg.temp_dir, sizeof(scfg.temp_dir));
583
	memset(&smb, 0, sizeof(smb));
584

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

600
		for(i=0;i<scfg.total_dirs;i++)
601
			if(!stricmp(scfg.dir[i]->code,argv[1])) /* use matchmatchi() instead? */
602 603 604 605
				break;

		if(i>=scfg.total_dirs) {
			printf("Directory code '%s' not found.\n",argv[1]);
606 607 608
			exit(1); 
		} 
		dirnum = i;
609
	}
610

611 612
	memset(&f,0,sizeof(f));
	const char* uploader = "-> ADDFILES <-";
613 614

	for(j=2;j<argc;j++) {
615
		if(argv[j][0]=='*')     /* set the uploader name (legacy) */
616 617
			uploader = argv[j]+1;
		else 
618 619 620
			if(argv[j][0]=='-'
			|| argv[j][0]=='/'
			) {      /* options */
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
			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;
641 642 643
					case 'L':
						j++;
						if(argv[j]==NULL) {
644
							puts(usage);
645 646 647 648 649
							return(-1);
						}
						SAFECOPY(lib,argv[j]);
						i=strlen(argv[j])-1;
						break;
650 651 652
					case 'X':
						j++;
						if(argv[j]==NULL) {
653
							puts(usage);
654 655
							return(-1);
						}
656
						uploader = argv[j];
657 658
						i=strlen(argv[j])-1;
						break;
659 660 661 662 663 664 665 666 667 668 669 670
					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
671 672 673
					case 'P':
						mode|=CHECK_DATE;
						break;
674 675 676 677 678
					case 'U':
						mode|=NO_NEWDATE;
						break;
					case 'F':
						mode|=FILE_DATE;
679 680 681 682 683 684 685 686 687 688
						if(argv[j][i] == 'F') {
							j++;
							if(argv[j]==NULL) {
								puts(usage);
								return(-1);
							}
							datefmt = argv[j];
							i=strlen(argv[j]) - 1;
							break;
						}
689 690 691 692 693 694 695 696
						break;
					case 'T':
						mode|=TODAYS_DATE;
						break;
					case 'S':
						mode|=SEARCH_DIR;
						break;
					default:
697
						puts(usage);
698 699
						return(1);
			}
700
		}
701
		else if(IS_DIGIT(argv[j][0])) {
702 703 704 705 706 707
			if(desc_offset==0)
				desc_offset=atoi(argv[j]);
			else
				size_offset=atoi(argv[j]);
			continue;
		}
708 709
		else if(argv[j][0]=='+') {      /* filelist - FILES.BBS */
			listgiven=1;
rswindell's avatar
rswindell committed
710
			if(argc > j+1
711
				&& IS_DIGIT(argv[j+1][0])) { /* skip x characters before description */
rswindell's avatar
rswindell committed
712
				if(argc > j+2
713
					&& IS_DIGIT(argv[j+2][0])) { /* skip x characters before size */
714
					addlist(argv[j]+1, dirnum, uploader, atoi(argv[j+1]), atoi(argv[j+2]));
715
					j+=2;
716
				}
717
				else {
718 719 720
					addlist(argv[j]+1, dirnum, uploader, atoi(argv[j+1]), 0);
					j++; 
				} 
721
			}
722
			else
723
				addlist(argv[j]+1, dirnum, uploader, 0, 0);
724
			if(mode&SYNC_LIST)
725
				synclist(argv[j]+1, dirnum); 
726
		}
727 728 729 730
		else if(argv[j][0]=='.') {      /* alternate file path */
			cur_altpath=atoi(argv[j]+1);
			if(cur_altpath>scfg.altpaths) {
				printf("Invalid alternate path.\n");
731 732
				exit(1);
			}
733
		}
734 735
		else {
			namegiven=1;
736 737 738
			const char* fname = argv[j];
			char fdesc[LEN_FDESC + 1] = {0};

739
			if(j+1==argc) {
740 741
				printf("%s no description given.\n",fname);
				SAFECOPY(fdesc, "no description given");
742
			}
743

744
			sprintf(str,"%s%s",cur_altpath ? scfg.altpath[cur_altpath-1]
745 746 747 748 749 750 751 752
				: scfg.dir[dirnum]->path, fname);
			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);
753 754
			if(l==-1) {
				printf("%s not found.\n",str);
755
				continue;
756
			}
757 758 759 760 761 762 763 764 765 766
			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;
767 768 769
			if(exist) {
				if(mode&NO_UPDATE)
					continue;
770
				if((mode&CHECK_DATE) && fdate(str) <= f.idx.time)
771
					continue;
772
				if(mode&ULDATE_ONLY) {
773 774 775
					reupload(&smb, &f);
					continue; 
				} 
776
			}
777 778
			memset(&f, 0, sizeof(f));
			prep_file_desc(fdesc, fdesc);
779
			if(mode&ASCII_ONLY)
780 781 782 783 784 785 786 787 788 789 790
				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;
791 792
			if(exist) {
				if(!(mode&NO_NEWDATE))
793 794 795
					reupload(&smb, &f);
				else
					result = smb_updatemsg(&smb, &f);
796
			}
797
			else
798
				result = smb_addfile(&smb, &f, SMB_SELFPACK, ext_desc, NULL, str);
799 800
			if(mode&UL_STATS)
				updatestats(l);
801 802
			files++;
		}
803
	}
804 805 806

	if(mode&AUTO_ADD) {
		for(i=0;i<scfg.total_dirs;i++) {
807 808
			if(lib[0] && stricmp(scfg.lib[scfg.dir[i]->lib]->sname,lib))
				continue;
809 810
			if(scfg.dir[i]->misc&DIR_NOAUTO)
				continue;
811
			dirnum = i;
812
			if(mode&SEARCH_DIR) {
813 814
				addlist("", dirnum, uploader, desc_offset, size_offset);
				continue;