smbfile.c 13.3 KB
Newer Older
1
/* Synchronet message base (SMB) FILE stream and FileBase routines  */
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 22 23
 *																			*
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.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 "smblib.h"

24
int smb_feof(FILE* fp)
25 26 27 28
{
	return(feof(fp));
}

29
int smb_ferror(FILE* fp)
30 31 32 33
{
	return(ferror(fp));
}

34
int smb_fflush(FILE* fp)
35 36 37 38
{
	return(fflush(fp));
}

39
int smb_fgetc(FILE* fp)
40 41 42 43
{
	return(fgetc(fp));
}

44
int smb_fputc(int ch, FILE* fp)
45 46 47 48
{
	return(fputc(ch,fp));
}

49
int smb_fseek(FILE* fp, off_t offset, int whence)
50
{
51
	return(fseeko(fp,offset,whence));
52 53
}

54
off_t smb_ftell(FILE* fp)
55
{
56
	return(ftello(fp));
57 58
}

59
off_t smb_fgetlength(FILE* fp)
60 61 62 63
{
	return(filelength(fileno(fp)));
}

64
int smb_fsetlength(FILE* fp, long length)
65 66 67 68
{
	return(chsize(fileno(fp),length));
}

69
void smb_rewind(FILE* fp)
70 71 72 73
{
	rewind(fp);
}

74
void smb_clearerr(FILE* fp)
75 76 77 78
{
	clearerr(fp);
}

79
size_t smb_fread(smb_t* smb, void* buf, size_t bytes, FILE* fp)
80 81 82 83 84 85 86
{
	size_t ret;
	time_t start=0;

	while(1) {
		if((ret=fread(buf,sizeof(char),bytes,fp))==bytes)
			return(ret);
87
		if(feof(fp) || (get_errno()!=EDEADLOCK && get_errno()!=EACCES))
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
			return(ret);
		if(!start)
			start=time(NULL);
		else
			if(time(NULL)-start>=(time_t)smb->retry_time)
				break;
		SLEEP(smb->retry_delay);
	}
	return(ret);
}

#if defined(__BORLANDC__)
	#pragma argsused
#endif

103
size_t smb_fwrite(smb_t* smb, const void* buf, size_t bytes, FILE* fp)
104 105 106 107 108 109
{
	return(fwrite(buf,1,bytes,fp));
}

/****************************************************************************/
/* Opens a non-shareable file (FILE*) associated with a message base		*/
rswindell's avatar
rswindell committed
110
/* Retries for retry_time number of seconds									*/
111 112
/* Return 0 on success, non-zero otherwise									*/
/****************************************************************************/
113
int smb_open_fp(smb_t* smb, FILE** fp, int share)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
{
	int 	file;
	char	path[MAX_PATH+1];
	char*	ext;
	time_t	start=0;

	if(fp==&smb->shd_fp)
		ext="shd";
	else if(fp==&smb->sid_fp)
		ext="sid";
	else if(fp==&smb->sdt_fp)
		ext="sdt";
	else if(fp==&smb->sda_fp)
		ext="sda";
	else if(fp==&smb->sha_fp)
		ext="sha";
	else if(fp==&smb->hash_fp)
		ext="hash";
	else {
		safe_snprintf(smb->last_error,sizeof(smb->last_error)
134
			,"%s opening %s: Illegal FILE* pointer argument: %p", __FUNCTION__
135 136 137 138 139 140 141
			,smb->file, fp);
		return(SMB_ERR_OPEN);
	}

	if(*fp!=NULL)	/* Already open! */
		return(SMB_SUCCESS);

142 143
	SAFEPRINTF2(path,"%s.%s",smb->file,ext);

144
	while(1) {
145
		if((file=sopen(path,O_RDWR|O_CREAT|O_BINARY,share,DEFFILEMODE))!=-1)
146
			break;
147
		if(get_errno()!=EACCES && get_errno()!=EAGAIN) {
148
			safe_snprintf(smb->last_error,sizeof(smb->last_error)
149
				,"%s %d '%s' opening %s", __FUNCTION__
150
				,get_errno(),STRERROR(get_errno()),path);
151 152 153 154 155 156 157
			return(SMB_ERR_OPEN);
		}
		if(!start)
			start=time(NULL);
		else
			if(time(NULL)-start>=(time_t)smb->retry_time) {
				safe_snprintf(smb->last_error,sizeof(smb->last_error)
158 159
					,"%s timeout opening %s (errno=%d, retry_time=%lu)", __FUNCTION__
					,path, get_errno(), (ulong)smb->retry_time);
160 161 162 163 164 165
				return(SMB_ERR_TIMEOUT); 
			}
		SLEEP(smb->retry_delay);
	}
	if((*fp=fdopen(file,"r+b"))==NULL) {
		safe_snprintf(smb->last_error,sizeof(smb->last_error)
166
			,"%s %d '%s' fdopening %s (%d)", __FUNCTION__
167
			,get_errno(),STRERROR(get_errno()),path,file);
168 169 170
		close(file);
		return(SMB_ERR_OPEN); 
	}
171
	setvbuf(*fp,NULL,_IOFBF, 2*1024);
172 173 174 175 176
	return(SMB_SUCCESS);
}

/****************************************************************************/
/****************************************************************************/
177
void smb_close_fp(FILE** fp)
178 179 180 181 182 183 184
{
	if(fp!=NULL) {
		if(*fp!=NULL)
			fclose(*fp);
		*fp=NULL;
	}
}
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

/****************************************************************************/
/* maxlen includes the NUL terminator										*/
/****************************************************************************/
char* smb_fileidxname(const char* filename, char* buf, size_t maxlen)
{
	size_t fnlen = strlen(filename);
	char* ext = getfext(filename);
	if(ext != NULL) {
		size_t extlen = strlen(ext);
		if(extlen >= maxlen - 1) {
			strncpy(buf, filename, maxlen);
			buf[maxlen - 1] = '\0';
		}
		else {
			fnlen -= extlen;
			if(fnlen > (maxlen - 1) - extlen)
				fnlen = (maxlen - 1) - extlen;
			safe_snprintf(buf, maxlen, "%-.*s%s", (int)fnlen, filename, ext);
		}
	} else {	/* no extension */
		strncpy(buf, filename, maxlen);
		buf[maxlen - 1] = '\0';
	}
	return buf;
}

/****************************************************************************/
/* Find file in index via either/or:										*/
/* -  CASE-INSENSITIVE 'filename' search through index (no wildcards)		*/
/* -  file content size and hash details found in 'file'					*/
/****************************************************************************/
int smb_findfile(smb_t* smb, const char* filename, smbfile_t* file)
{
	long offset = 0;
	smbfile_t* f = file;
	smbfile_t file_ = {0};
	if(f == NULL)
		f = &file_;
	char fname[SMB_FILEIDX_NAMELEN + 1] = "";
	if(filename != NULL)
		smb_fileidxname(filename, fname, sizeof(fname));

	if(smb->sid_fp == NULL) {
		safe_snprintf(smb->last_error, sizeof(smb->last_error), "%s msgbase not open", __FUNCTION__);
		return SMB_ERR_NOT_OPEN;
	}
	f->dir = smb->dirnum;
	rewind(smb->sid_fp);
	while(!feof(smb->sid_fp)) {
		fileidxrec_t fidx;

		if(smb_fread(smb, &fidx, sizeof(fidx), smb->sid_fp) != sizeof(fidx))
			break;

		f->idx_offset = offset++;

		if(filename != NULL) {
			if(stricmp(fidx.name, fname) != 0)
				continue;
			f->file_idx = fidx;
			return SMB_SUCCESS;
		}

		if(file == NULL)
			continue;

		if((f->file_idx.hash.flags & SMB_HASH_MASK) != 0 || f->file_idx.idx.size > 0) {
			if(f->file_idx.idx.size > 0 && f->file_idx.idx.size != fidx.idx.size)
				continue;
			if((f->file_idx.hash.flags & SMB_HASH_CRC16) && f->file_idx.hash.data.crc16 != fidx.hash.data.crc16)
				continue;
			if((f->file_idx.hash.flags & SMB_HASH_CRC32) && f->file_idx.hash.data.crc32 != fidx.hash.data.crc32)
				continue;
			if((f->file_idx.hash.flags & SMB_HASH_MD5)
				&& memcmp(f->file_idx.hash.data.md5, fidx.hash.data.md5, sizeof(fidx.hash.data.md5)) !=0)
				continue;
			if((f->file_idx.hash.flags & SMB_HASH_SHA1)
				&& memcmp(f->file_idx.hash.data.sha1, fidx.hash.data.sha1, sizeof(fidx.hash.data.sha1)) !=0)
				continue;
			f->file_idx = fidx;
			return SMB_SUCCESS;
		}
	}
	return SMB_ERR_NOT_FOUND;
}

/****************************************************************************/
/****************************************************************************/
int smb_loadfile(smb_t* smb, const char* filename, smbfile_t* file, enum file_detail detail)
{
	int result;

	memset(file, 0, sizeof(*file));

	if((result = smb_findfile(smb, filename, file)) != SMB_SUCCESS)
		return result;

	return smb_getfile(smb, file, detail);
}

/****************************************************************************/
/****************************************************************************/
int smb_getfile(smb_t* smb, smbfile_t* file, enum file_detail detail)
{
	int result;

	file->name = file->file_idx.name;
	file->hdr.when_written.time = file->idx.time;
	if(detail > file_detail_index) {
		if((result = smb_getmsghdr(smb, file)) != SMB_SUCCESS)
			return result;
		if(detail >= file_detail_extdesc)
298
			file->extdesc = smb_getmsgtxt(smb, file, GETMSGTXT_BODY_ONLY);
299 300
		if(detail >= file_detail_metadata)
			file->metadata = smb_getmsgtxt(smb, file, GETMSGTXT_TAIL_ONLY);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	}
	file->dir = smb->dirnum;

	return SMB_SUCCESS;
}

/****************************************************************************/
/* Writes both header and index information for file 'file'                 */
/* Like smb_putmsg() but doesn't (re)-initialize index (idx)				*/
/****************************************************************************/
int smb_putfile(smb_t* smb, smbfile_t* file)
{
	int result;

	if((result = smb_putmsghdr(smb, file))!=SMB_SUCCESS)
		return result;

	return smb_putmsgidx(smb, file);
}

/****************************************************************************/
/****************************************************************************/
void smb_freefilemem(smbfile_t* file)
{
	smb_freemsgmem(file);
}

/****************************************************************************/
/****************************************************************************/
330
int smb_addfile(smb_t* smb, smbfile_t* file, int storage, const char* extdesc, const char* metadata, const char* path)
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
{
	if(file->name == NULL || *file->name == '\0') {
		safe_snprintf(smb->last_error, sizeof(smb->last_error), "%s missing name", __FUNCTION__);
		return SMB_ERR_HDR_FIELD;
	}
	if(smb_findfile(smb, file->name, NULL) == SMB_SUCCESS) {
		safe_snprintf(smb->last_error, sizeof(smb->last_error), "%s duplicate name found: %s", __FUNCTION__, file->name);
		return SMB_DUPE_MSG;
	}
	if(path != NULL) {
		file->size = flength(path);
		file->hdr.when_written.time = (uint32_t)fdate(path);
		if(!(smb->status.attr & SMB_NOHASH) && file->file_idx.hash.flags == 0)
			file->file_idx.hash.flags = smb_hashfile(path, file->size, &file->file_idx.hash.data);
	}
	file->hdr.attr |= MSG_FILE;
	file->hdr.type = SMB_MSG_TYPE_FILE;
348
	return smb_addmsg(smb, file, storage, SMB_HASH_SOURCE_NONE, XLAT_NONE
349
		,/* body: */(const uchar*)extdesc, /* tail: */(const uchar*)metadata);
350 351 352
}

/****************************************************************************/
353
/* Like smb_addfile(), except 'metadata' is a str_list_t: 'list'			*/
354 355 356
/****************************************************************************/
int smb_addfile_withlist(smb_t* smb, smbfile_t* file, int storage, const char* extdesc, str_list_t list, const char* path)
{
357
	char* metadata = NULL;
358 359
	int result;

360
	if(list != NULL && *list != NULL) {
361
		size_t size = strListCount(list) * 1024;
362 363
		metadata = calloc(1, size);
		if(metadata == NULL)
364
			return SMB_ERR_MEM;
365
		strListCombine(list, metadata, size - 1, "\r\n");
366
	}
367 368
	result = smb_addfile(smb, file, storage, extdesc, metadata, path);
	free(metadata);
369
	return result;
370 371 372 373 374 375 376 377 378
}

/****************************************************************************/
/****************************************************************************/
int smb_renewfile(smb_t* smb, smbfile_t* file, int storage, const char* path)
{
	int result;
	if((result = smb_removefile(smb, file)) != SMB_SUCCESS)
		return result;
379
	return smb_addfile(smb, file, storage, file->extdesc, file->metadata, path);
380 381 382 383 384 385 386 387
}

/****************************************************************************/
/****************************************************************************/
int smb_removefile(smb_t* smb, smbfile_t* file)
{
	int result;
	int removed = 0;
388
	char fname[SMB_FILEIDX_NAMELEN + 1] = "";
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414

	if(!smb->locked && smb_locksmbhdr(smb) != SMB_SUCCESS)
		return SMB_ERR_LOCK;

	file->hdr.attr |= MSG_DELETE;
	if((result = smb_putmsghdr(smb, file)) != SMB_SUCCESS) {
		smb_unlocksmbhdr(smb);
		return result;
	}
	if((result = smb_getstatus(smb)) != SMB_SUCCESS) {
		smb_unlocksmbhdr(smb);
		return result;
	}
	if((result = smb_open_ha(smb)) != SMB_SUCCESS) {
		smb_unlocksmbhdr(smb);
		return result;
	}
	if((result = smb_open_da(smb)) != SMB_SUCCESS) {
		smb_unlocksmbhdr(smb);
		return result;
	}
	result = smb_freemsg(smb, file);
	smb_close_ha(smb);
	smb_close_da(smb);

	// Now remove from index:
415
	smb_fileidxname(file->name, fname, sizeof(fname));
416 417 418 419 420 421 422 423 424 425 426 427 428 429
	if(result == SMB_SUCCESS) {
		rewind(smb->sid_fp);
		fileidxrec_t* fidx = malloc(smb->status.total_files * sizeof(*fidx));
		if(fidx == NULL) {
			smb_unlocksmbhdr(smb);
			return SMB_ERR_MEM;
		}
		if(fread(fidx, sizeof(*fidx), smb->status.total_files, smb->sid_fp) != smb->status.total_files) {
			free(fidx);
			smb_unlocksmbhdr(smb);
			return SMB_ERR_READ;
		}
		rewind(smb->sid_fp);
		for(uint32_t i = 0; i < smb->status.total_files; i++) {
430
			if(stricmp(fidx[i].name, fname) == 0) {
431 432 433 434 435 436 437 438 439 440 441 442 443
				removed++;
				continue;
			}
			if(fwrite(fidx + i, sizeof(*fidx), 1, smb->sid_fp) != 1) {
				safe_snprintf(smb->last_error, sizeof(smb->last_error), "%s re-writing index"
					,__FUNCTION__);
				result = SMB_ERR_WRITE;
				break;
			}
		}
		free(fidx);
		if(result == SMB_SUCCESS) {
			if(removed < 1) {
444 445
				safe_snprintf(smb->last_error, sizeof(smb->last_error), "%s name not found: %s"
					,__FUNCTION__, fname);
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
				result = SMB_ERR_NOT_FOUND;
			} else {
				fflush(smb->sid_fp);
				smb->status.total_files -= removed;
				if(chsize(fileno(smb->sid_fp), smb->status.total_files * sizeof(*fidx)) != 0) {
					safe_snprintf(smb->last_error, sizeof(smb->last_error), "%s error %d truncating index"
						,__FUNCTION__, errno);
					result = SMB_ERR_DELETE;
				} else
					result = smb_putstatus(smb);
			}
		}
	}

	smb_unlocksmbhdr(smb);
	return result;
}