Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

js_filebase.c 46.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/* Synchronet JavaScript "FileBase" Object */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
 *																			*
 * 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"
#include "filedat.h"
#include "js_request.h"
#include <stdbool.h>

typedef struct
{
	smb_t	smb;
	int		smb_result;

} private_t;

/* Destructor */

static void js_finalize_filebase(JSContext *cx, JSObject *obj)
{
	private_t* p;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return;

	if(SMB_IS_OPEN(&(p->smb)))
		smb_close(&(p->smb));

	free(p);

	JS_SetPrivate(cx, obj, NULL);
}

/* Methods */

extern JSClass js_filebase_class;

static JSBool
js_open(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject* obj = JS_THIS_OBJECT(cx, arglist);
	private_t* p;
	jsrefcount	rc;
	scfg_t*		scfg;

	scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL) {
		return JS_FALSE;
	}

	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);

	if(p->smb.dirnum==INVALID_DIR
		&& strchr(p->smb.file,'/')==NULL
		&& strchr(p->smb.file,'\\')==NULL) {
		JS_ReportError(cx,"Unrecognized filebase code: %s",p->smb.file);
		return JS_TRUE;
	}

	rc=JS_SUSPENDREQUEST(cx);
	if((p->smb_result = smb_open_dir(scfg, &(p->smb), p->smb.dirnum)) != SMB_SUCCESS) {
		JS_RESUMEREQUEST(cx, rc);
		return JS_TRUE;
	}
	JS_RESUMEREQUEST(cx, rc);

	JS_SET_RVAL(cx, arglist, JSVAL_TRUE);
	return JS_TRUE;
}

static JSBool
js_close(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject* obj = JS_THIS_OBJECT(cx, arglist);
	private_t* p;
	jsrefcount	rc;

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL) {
		return JS_FALSE;
	}

	JS_SET_RVAL(cx, arglist, JSVAL_VOID);

	rc=JS_SUSPENDREQUEST(cx);
	smb_close(&(p->smb));
	JS_RESUMEREQUEST(cx, rc);

	return JS_TRUE;
}

static JSBool
js_dump_file(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	char*		filename = NULL;

	JS_SET_RVAL(cx, arglist, JSVAL_NULL);

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	if(!SMB_IS_OPEN(&(p->smb))) {
		JS_ReportError(cx, "FileBase is not open");
		return JS_FALSE;
	}

	uintN argn = 0;
	if(argn < argc)	{
		JSVALUE_TO_MSTRING(cx, argv[argn], filename, NULL);
		HANDLE_PENDING(cx, filename);
		argn++;
	}
	if(filename == NULL)
		return JS_FALSE;

	file_t file;
	if((p->smb_result = smb_loadfile(&p->smb, filename, &file, file_detail_normal)) == SMB_SUCCESS) {
		str_list_t list = smb_msghdr_str_list(&file);
		if(list != NULL) {
			JSObject* array;
			if((array = JS_NewArrayObject(cx, 0, NULL)) == NULL) {
				free(filename);
147
				strListFree(&list);
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 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
				JS_ReportError(cx, "JS_NewArrayObject failure");
				return JS_FALSE;
			}
			JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(array));
			for(int i = 0; list[i] != NULL; i++) {
				JSString* js_str = JS_NewStringCopyZ(cx, list[i]);
				if(js_str == NULL)
					break;
				JS_DefineElement(cx, array, i, STRING_TO_JSVAL(js_str), NULL, NULL, JSPROP_ENUMERATE);
			}
			strListFree(&list);
		}
	}
	smb_freefilemem(&file);
	free(filename);
	return JS_TRUE;
}

static bool
set_file_properties(JSContext *cx, JSObject* obj, file_t* f, enum file_detail detail)
{
	jsval		val;
	JSString*	js_str;
	const uintN flags = JSPROP_ENUMERATE;

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}
	if(f->name == NULL
		|| (js_str = JS_NewStringCopyZ(cx, f->name)) == NULL
		|| !JS_DefineProperty(cx, obj, "name", STRING_TO_JSVAL(js_str), NULL, NULL, flags))
		return false;

	if(f->from != NULL
		&& ((js_str = JS_NewStringCopyZ(cx, f->from)) == NULL
			|| !JS_DefineProperty(cx, obj, "from", STRING_TO_JSVAL(js_str), NULL, NULL, flags)))
		return false;

	val = BOOLEAN_TO_JSVAL(f->idx.attr & FILE_ANONYMOUS);
	if((val == JSVAL_TRUE || detail > file_detail_extdesc)
		&& !JS_DefineProperty(cx, obj, "anon", val, NULL, NULL, flags))
		return false;

	if(f->desc != NULL
		&& ((js_str = JS_NewStringCopyZ(cx, f->desc)) == NULL
			|| !JS_DefineProperty(cx, obj, "desc", STRING_TO_JSVAL(js_str), NULL, NULL, flags)))
		return false;

	if(f->extdesc != NULL && *f->extdesc != '\0'
		&& ((js_str = JS_NewStringCopyZ(cx, f->extdesc)) == NULL
			|| !JS_DefineProperty(cx, obj, "extdesc", STRING_TO_JSVAL(js_str), NULL, NULL, flags)))
		return false;

	if(f->cost > 0 || detail > file_detail_extdesc) {
		val = UINT_TO_JSVAL(f->cost);
		if(!JS_DefineProperty(cx, obj, "cost", val, NULL, NULL, flags))
			return false;
	}
	val = UINT_TO_JSVAL(f->idx.size);
	if(!JS_DefineProperty(cx, obj, "size", val, NULL, NULL, flags))
		return false;

	val = UINT_TO_JSVAL(f->hdr.when_written.time);
	if(!JS_DefineProperty(cx, obj, "time", val, NULL, NULL, flags))
		return false;
	if(f->hdr.when_imported.time > 0 || detail > file_detail_extdesc) {
		val = UINT_TO_JSVAL(f->hdr.when_imported.time);
		if(!JS_DefineProperty(cx, obj, "added", val, NULL, NULL, flags))
			return false;
	}
	if(f->hdr.last_downloaded > 0 || detail > file_detail_extdesc) {
		val = UINT_TO_JSVAL(f->hdr.last_downloaded);
		if(!JS_DefineProperty(cx, obj, "last_downloaded", val, NULL, NULL, flags))
			return false;
	}
	if(f->hdr.times_downloaded > 0 || detail > file_detail_extdesc) {
		val = UINT_TO_JSVAL(f->hdr.times_downloaded);
		if(!JS_DefineProperty(cx, obj, "times_downloaded", val, NULL, NULL, flags))
			return false;
	}
	if(f->file_idx.hash.flags & SMB_HASH_CRC16) {
		val = UINT_TO_JSVAL(f->file_idx.hash.data.crc16);
		if(!JS_DefineProperty(cx, obj, "crc16", val, NULL, NULL, flags))
			return false;
	}
	if(f->file_idx.hash.flags & SMB_HASH_CRC32) {
		val = UINT_TO_JSVAL(f->file_idx.hash.data.crc32);
		if(!JS_DefineProperty(cx, obj, "crc32", val, NULL, NULL, flags))
			return false;
	}
	if(f->file_idx.hash.flags & SMB_HASH_MD5) {
		char hex[128];
		if((js_str = JS_NewStringCopyZ(cx, MD5_hex(hex, f->file_idx.hash.data.md5))) == NULL
			|| !JS_DefineProperty(cx, obj, "md5", STRING_TO_JSVAL(js_str), NULL, NULL, flags))
			return false;
	}
	if(f->file_idx.hash.flags & SMB_HASH_SHA1) {
		char hex[128];
		if((js_str = JS_NewStringCopyZ(cx, SHA1_hex(hex, f->file_idx.hash.data.sha1))) == NULL
			|| !JS_DefineProperty(cx, obj, "sha1", STRING_TO_JSVAL(js_str), NULL, NULL, flags))
			return false;
	}
	if(f->tags != NULL
		&& ((js_str = JS_NewStringCopyZ(cx, f->tags)) == NULL
			|| !JS_DefineProperty(cx, obj, "tags", STRING_TO_JSVAL(js_str), NULL, NULL, flags)))
		return false;

	return true;
}

static BOOL
parse_file_index_properties(JSContext *cx, JSObject* obj, fileidxrec_t* idx)
{
	char*		cp = NULL;
	size_t		cp_sz = 0;
	jsval		val;
	const char* prop_name;

	if(JS_GetProperty(cx, obj, prop_name = "name", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx, cp);
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid '%s' string in file object", prop_name);
			return FALSE;
		}
		SAFECOPY(idx->name, cp);
	}
	if(JS_GetProperty(cx, obj, prop_name = "size", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if(!JS_ValueToECMAUint32(cx, val, &idx->idx.size)) {
279
			free(cp);
280 281 282 283 284 285 286 287 288 289
			JS_ReportError(cx, "Error converting adding '%s' property to Uint32", prop_name);
			return FALSE;
		}
	}
	if(JS_GetProperty(cx, obj, prop_name = "crc16", &val) && !JSVAL_NULL_OR_VOID(val)) {
		idx->hash.data.crc16 = JSVAL_TO_INT(val);
		idx->hash.flags |= SMB_HASH_CRC16;
	}
	if(JS_GetProperty(cx, obj, prop_name = "crc32", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if(!JS_ValueToECMAUint32(cx, val, &idx->hash.data.crc32)) {
290
			free(cp);
291 292 293 294 295 296 297 298 299 300 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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
			JS_ReportError(cx, "Error converting adding '%s' property to Uint32", prop_name);
			return FALSE;
		}
		idx->hash.flags |= SMB_HASH_CRC32;
	}
	if(JS_GetProperty(cx, obj, prop_name = "md5", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx, cp);
		if(cp==NULL || strlen(cp) != MD5_DIGEST_SIZE * 2) {
			free(cp);
			JS_ReportError(cx, "Invalid '%s' string in file object", prop_name);
			return FALSE;
		}
		for(int i = 0; i < MD5_DIGEST_SIZE * 2; i += 2) {
			idx->hash.data.md5[i/2] = HEX_CHAR_TO_INT(*(cp + i)) * 16;
			idx->hash.data.md5[i/2] += HEX_CHAR_TO_INT(*(cp + i + 1));
		}
		idx->hash.flags |= SMB_HASH_MD5;
	}
	if(JS_GetProperty(cx, obj, prop_name = "sha1", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx, cp);
		if(cp==NULL || strlen(cp) != SHA1_DIGEST_SIZE * 2) {
			free(cp);
			JS_ReportError(cx, "Invalid '%s' string in file object", prop_name);
			return FALSE;
		}
		for(int i = 0; i < SHA1_DIGEST_SIZE * 2; i += 2) {
			idx->hash.data.sha1[i/2] = HEX_CHAR_TO_INT(*(cp + i)) * 16;
			idx->hash.data.sha1[i/2] += HEX_CHAR_TO_INT(*(cp + i + 1));
		}
		idx->hash.flags |= SMB_HASH_SHA1;
	}
	free(cp);
	return TRUE;
}

static int
parse_file_properties(JSContext *cx, JSObject* obj, file_t* file, char** extdesc)
{
	char*		cp = NULL;
	size_t		cp_sz = 0;
	jsval		val;
	int result = SMB_ERR_NOT_FOUND;

	const char* prop_name = "name";
	if(JS_GetProperty(cx, obj, prop_name, &val) && !JSVAL_NULL_OR_VOID(val)) {
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx, cp);
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid '%s' string in file object", prop_name);
			return SMB_FAILURE;
		}
		if((result = smb_new_hfield_str(file, SMB_FILENAME, cp)) != SMB_SUCCESS) {
			free(cp);
			JS_ReportError(cx, "Error %d adding '%s' property to file object", result, prop_name);
			return result;
		}
	}

	prop_name = "from";
	if(JS_GetProperty(cx, obj, prop_name, &val) && !JSVAL_NULL_OR_VOID(val)) {
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx, cp);
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid '%s' string in file object", prop_name);
			return SMB_FAILURE;
		}
		if((result = smb_new_hfield_str(file, SMB_FILEUPLOADER, cp)) != SMB_SUCCESS) {
			free(cp);
			JS_ReportError(cx, "Error %d adding '%s' property to file object", result, prop_name);
			return result;
		}
	}

	prop_name = "desc";
	if(JS_GetProperty(cx, obj, prop_name, &val) && !JSVAL_NULL_OR_VOID(val)) {
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx, cp);
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid '%s' string in file object", prop_name);
			return SMB_FAILURE;
		}
		if((result = smb_new_hfield_str(file, SMB_FILEDESC, cp)) != SMB_SUCCESS) {
			free(cp);
			JS_ReportError(cx, "Error %d adding '%s' property to file object", result, prop_name);
			return result;
		}
	}
	prop_name = "extdesc";
	if(extdesc != NULL && JS_GetProperty(cx, obj, prop_name, &val) && !JSVAL_NULL_OR_VOID(val)) {
		FREE_AND_NULL(*extdesc);
		JSVALUE_TO_MSTRING(cx, val, *extdesc, NULL);
		HANDLE_PENDING(cx, *extdesc);
		if(*extdesc == NULL) {
386
			free(cp);
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
			JS_ReportError(cx, "Invalid '%s' string in file object", prop_name);
			return SMB_ERR_MEM;
		}
		truncsp(*extdesc);
	}
	prop_name = "tags";
	if(JS_GetProperty(cx, obj, prop_name, &val) && !JSVAL_NULL_OR_VOID(val)) {
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx, cp);
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid '%s' string in file object", prop_name);
			return SMB_FAILURE;
		}
		if((result = smb_new_hfield_str(file, SMB_TAGS, cp)) != SMB_SUCCESS) {
			free(cp);
			JS_ReportError(cx, "Error %d adding '%s' property to file object", result, prop_name);
			return result;
		}
	}
	prop_name = "cost";
	if(JS_GetProperty(cx, obj, prop_name, &val) && !JSVAL_NULL_OR_VOID(val)) {
		uint32_t cost = 0;
		if(!JS_ValueToECMAUint32(cx, val, &cost)) {
410
			free(cp);
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
			JS_ReportError(cx, "Error converting adding '%s' property to Uint32", prop_name);
			return SMB_FAILURE;
		}
		if((result = smb_new_hfield(file, SMB_COST, sizeof(cost), &cost)) != SMB_SUCCESS) {
			free(cp);
			JS_ReportError(cx, "Error %d adding '%s' property to file object", result, prop_name);
			return result;
		}
	}

	if(JS_GetProperty(cx, obj, "anon", &val) && val == JSVAL_TRUE)
		file->hdr.attr |= FILE_ANONYMOUS;

	if(!parse_file_index_properties(cx, obj, &file->file_idx))
		result = SMB_FAILURE;

	free(cp);
	return result;
}

static JSBool
js_hash_file(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	char		path[MAX_PATH + 1];
	char*		filename = NULL;
	enum file_detail detail = file_detail_normal;
	jsrefcount	rc;

	JS_SET_RVAL(cx, arglist, JSVAL_NULL);

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	file_t file;
	ZERO_VAR(file);

	uintN argn = 0;
	if(argn < argc && JSVAL_IS_STRING(argv[argn]))	{
		JSVALUE_TO_MSTRING(cx, argv[argn], filename, NULL);
		HANDLE_PENDING(cx, filename);
		argn++;
	}
	if(filename == NULL) {
		JS_ReportError(cx, "No filename argument");
		return JS_TRUE;
	}
	rc=JS_SUSPENDREQUEST(cx);
	if(getfname(filename) != filename)
		SAFECOPY(path, filename);
	else {
		file.name = filename;
		getfilepath(scfg, &file, path);
	}
	off_t size = flength(path);
	if(size == -1)
		JS_ReportError(cx, "File does not exist: %s", path);
	else {
		file.idx.size = (uint32_t)size;
		if((p->smb_result = smb_hashfile(path, size, &file.file_idx.hash.data)) > 0) {
			file.file_idx.hash.flags = p->smb_result;
			file.hdr.when_written.time = (uint32_t)fdate(path);
			JSObject* fobj;
			if((fobj = JS_NewObject(cx, NULL, NULL, obj)) == NULL)
				JS_ReportError(cx, "object allocation failure, line %d", __LINE__);
			else {
				set_file_properties(cx, fobj, &file, detail);
				JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(fobj));
			}
		}
	}
	JS_RESUMEREQUEST(cx, rc);
	free(filename);
	smb_freefilemem(&file);

	return JS_TRUE;
}

static JSBool
js_get_file(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	char*		filename = NULL;
	enum file_detail detail = file_detail_normal;
	jsrefcount	rc;

	JS_SET_RVAL(cx, arglist, JSVAL_NULL);

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	if(!SMB_IS_OPEN(&(p->smb))) {
		JS_ReportError(cx, "FileBase is not open");
		return JS_FALSE;
	}

	file_t file;
	ZERO_VAR(file);

	uintN argn = 0;
	if(argn < argc && JSVAL_IS_STRING(argv[argn]))	{
		JSVALUE_TO_MSTRING(cx, argv[argn], filename, NULL);
		HANDLE_PENDING(cx, filename);
		if(filename == NULL)
			return JS_FALSE;
		argn++;
	}
	if(argn < argc && JSVAL_IS_OBJECT(argv[argn])) {
535
		free(filename);
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
		if(!parse_file_index_properties(cx, JSVAL_TO_OBJECT(argv[argn]), &file.file_idx))
			return JS_TRUE;
		filename = strdup(file.file_idx.name);
		argn++;
	}
	else if(filename == NULL)
		return JS_TRUE;
	if(argn < argc && JSVAL_IS_NUMBER(argv[argn])) {
		detail = JSVAL_TO_INT(argv[argn]);
		argn++;
	}
	rc=JS_SUSPENDREQUEST(cx);
	if((p->smb_result = smb_findfile(&p->smb, filename, &file)) == SMB_SUCCESS
		&& (p->smb_result = smb_getfile(&p->smb, &file, detail)) == SMB_SUCCESS) {
		JSObject* fobj;
		if((fobj = JS_NewObject(cx, NULL, NULL, obj)) == NULL)
			JS_ReportError(cx, "object allocation failure, line %d", __LINE__);
		else {
			set_file_properties(cx, fobj, &file, detail);
		    JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(fobj));
		}
	}
	JS_RESUMEREQUEST(cx, rc);
	free(filename);
	smb_freefilemem(&file);

	return JS_TRUE;
}

static JSBool
js_get_file_list(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	time_t		t = 0;
	char*		filespec = NULL;
	enum file_detail detail = file_detail_normal;
	enum file_sort sort = FILE_SORT_NAME_A;
	jsrefcount	rc;

	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	if(!SMB_IS_OPEN(&(p->smb))) {
		JS_ReportError(cx, "FileBase is not open");
		return JS_FALSE;
	}

	if(p->smb.dirnum != INVALID_DIR)
		sort = scfg->dir[p->smb.dirnum]->sort;

	uintN argn = 0;
	if(argn < argc && JSVAL_IS_STRING(argv[argn]))	{
		JSVALUE_TO_MSTRING(cx, argv[argn], filespec, NULL);
		HANDLE_PENDING(cx, filespec);
		if(filespec == NULL)
			return JS_FALSE;
		argn++;
	}
	if(argn < argc && JSVAL_IS_NUMBER(argv[argn])) {
		detail = JSVAL_TO_INT(argv[argn]);
		argn++;
	}
	if(argn < argc && JSVAL_IS_NUMBER(argv[argn]))	{
		t = JSVAL_TO_INT(argv[argn]);
		argn++;
	}
	if(argn < argc && JSVAL_IS_BOOLEAN(argv[argn])) {
		if(argv[argn++] == JSVAL_FALSE)
			sort = FILE_SORT_NATURAL;
		else if(argn < argc && JSVAL_IS_NUMBER(argv[argn])) {
			sort = JSVAL_TO_INT(argv[argn]);
			argn++;
		}
	}

	rc=JS_SUSPENDREQUEST(cx);
	JSObject* array;
    if((array = JS_NewArrayObject(cx, 0, NULL)) == NULL) {
		free(filespec);
		JS_RESUMEREQUEST(cx, rc);
		JS_ReportError(cx, "JS_NewArrayObject failure");
		return JS_FALSE;
	}

	size_t file_count;
	file_t* file_list = loadfiles(&p->smb, filespec, t, detail, sort, &file_count);
	if(file_list != NULL) {
		for(size_t i = 0; i < file_count; i++) {
			JSObject* fobj;
			if((fobj = JS_NewObject(cx, NULL, NULL, array)) == NULL) {
				JS_ReportError(cx, "object allocation failure, line %d", __LINE__);
				break;
			}
			set_file_properties(cx, fobj, &file_list[i], detail);
			JS_DefineElement(cx, array, i, OBJECT_TO_JSVAL(fobj), NULL, NULL, JSPROP_ENUMERATE);
		}
		freefiles(file_list, file_count);
	}
    JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(array));
	JS_RESUMEREQUEST(cx, rc);
	free(filespec);

	return JS_TRUE;
}

static JSBool
js_get_file_names(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	time_t		t = 0;
	char*		filespec = NULL;
	enum file_sort sort = FILE_SORT_NAME_A;
	jsrefcount	rc;

	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	if(!SMB_IS_OPEN(&(p->smb))) {
		JS_ReportError(cx, "FileBase is not open");
		return JS_FALSE;
	}

	if(p->smb.dirnum != INVALID_DIR)
		sort = scfg->dir[p->smb.dirnum]->sort;

	uintN argn = 0;
	if(argn < argc && JSVAL_IS_STRING(argv[argn]))	{
		JSVALUE_TO_MSTRING(cx, argv[argn], filespec, NULL);
		HANDLE_PENDING(cx, filespec);
		if(filespec == NULL)
			return JS_FALSE;
		argn++;
	}
	if(argn < argc && JSVAL_IS_NUMBER(argv[argn]))	{
		t = JSVAL_TO_INT(argv[argn]);
		argn++;
	}
	if(argn < argc && JSVAL_IS_BOOLEAN(argv[argn])) {
		if(argv[argn++] == JSVAL_FALSE)
			sort = FILE_SORT_NATURAL;
		else if(argn < argc && JSVAL_IS_NUMBER(argv[argn])) {
			sort = JSVAL_TO_INT(argv[argn]);
			argn++;
		}
	}

	rc=JS_SUSPENDREQUEST(cx);
	JSObject* array;
    if((array = JS_NewArrayObject(cx, 0, NULL)) == NULL) {
		free(filespec);
		JS_RESUMEREQUEST(cx, rc);
		JS_ReportError(cx, "JS_NewArrayObject failure");
		return JS_FALSE;
	}

	str_list_t file_list = loadfilenames(&p->smb, filespec, t, sort, NULL);
	if(file_list != NULL) {
		for(size_t i = 0; file_list[i] != NULL; i++) {
			JSString* js_str;
			if((js_str = JS_NewStringCopyZ(cx, file_list[i]))==NULL)
				return JS_FALSE;
			JS_DefineElement(cx, array, i, STRING_TO_JSVAL(js_str), NULL, NULL, JSPROP_ENUMERATE);
		}
		strListFree(&file_list);
	}
    JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(array));
	JS_RESUMEREQUEST(cx, rc);
	free(filespec);

	return JS_TRUE;
}

static JSBool
js_get_file_name(JSContext *cx, uintN argc, jsval *arglist)
{
	jsval*		argv = JS_ARGV(cx, arglist);
	char*		filepath = NULL;
	char		filename[SMB_FILEIDX_NAMELEN + 1] = "";

	JS_SET_RVAL(cx, arglist, JSVAL_NULL);

 	if(!js_argc(cx, argc, 1))
		return JS_FALSE;

	uintN argn = 0;
	JSVALUE_TO_MSTRING(cx, argv[argn], filepath, NULL);
	HANDLE_PENDING(cx, filepath);
	JSString* js_str;
	if((js_str = JS_NewStringCopyZ(cx, smb_fileidxname(getfname(filepath), filename, sizeof(filename)))) != NULL)
		JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(js_str));

	return JS_TRUE;
}

static JSBool
js_get_file_path(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	char*		filename = NULL;
	jsrefcount	rc;
	file_t file;

	ZERO_VAR(file);
	JS_SET_RVAL(cx, arglist, JSVAL_NULL);

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	uintN argn = 0;
	if(argn < argc && JSVAL_IS_STRING(argv[argn]))	{
		JSVALUE_TO_MSTRING(cx, argv[argn], filename, NULL);
		HANDLE_PENDING(cx, filename);
		argn++;
	}
	if(argn < argc && JSVAL_IS_OBJECT(argv[argn])) {
		if(!parse_file_index_properties(cx, JSVAL_TO_OBJECT(argv[argn]), &file.file_idx))
			return JS_TRUE;
		free(filename);
		filename = strdup(file.file_idx.name);
		argn++;
	}
	else if(filename == NULL)
		return JS_TRUE;

	rc=JS_SUSPENDREQUEST(cx);
	if((p->smb_result = smb_loadfile(&p->smb, filename, &file, file_detail_index)) == SMB_SUCCESS) {
		char path[MAX_PATH + 1];
		JSString* js_str;
		if((js_str = JS_NewStringCopyZ(cx, getfilepath(scfg, &file, path))) != NULL)
			JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(js_str));
		smb_freefilemem(&file);
	}
	JS_RESUMEREQUEST(cx, rc);
	free(filename);

	return JS_TRUE;
}

static JSBool
js_get_file_size(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	char*		filename = NULL;
	jsrefcount	rc;
	file_t	file;

	ZERO_VAR(file);
	JS_SET_RVAL(cx, arglist, INT_TO_JSVAL(-1));

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	uintN argn = 0;
	if(argn < argc && JSVAL_IS_STRING(argv[argn]))	{
		JSVALUE_TO_MSTRING(cx, argv[argn], filename, NULL);
		HANDLE_PENDING(cx, filename);
		argn++;
	}
	if(argn < argc && JSVAL_IS_OBJECT(argv[argn])) {
		if(!parse_file_index_properties(cx, JSVAL_TO_OBJECT(argv[argn]), &file.file_idx))
			return JS_TRUE;
		free(filename);
		filename = strdup(file.file_idx.name);
		argn++;
	}
	else if(filename == NULL)
		return JS_TRUE;

	rc=JS_SUSPENDREQUEST(cx);
	if((p->smb_result = smb_loadfile(&p->smb, filename, &file, file_detail_index)) == SMB_SUCCESS) {
		char path[MAX_PATH + 1];
		getfilepath(scfg, &file, path);
	    JS_SET_RVAL(cx, arglist, DOUBLE_TO_JSVAL((jsdouble)getfilesize(scfg, &file)));
		smb_freefilemem(&file);
	}
	JS_RESUMEREQUEST(cx, rc);
	free(filename);

	return JS_TRUE;
}

static JSBool
js_get_file_time(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	char*		filename = NULL;
	jsrefcount	rc;
	file_t file;

	ZERO_VAR(file);
	JS_SET_RVAL(cx, arglist, INT_TO_JSVAL(-1));

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	uintN argn = 0;
	if(argn < argc && JSVAL_IS_STRING(argv[argn]))	{
		JSVALUE_TO_MSTRING(cx, argv[argn], filename, NULL);
		HANDLE_PENDING(cx, filename);
		argn++;
	}
	if(argn < argc && JSVAL_IS_OBJECT(argv[argn])) {
		if(!parse_file_index_properties(cx, JSVAL_TO_OBJECT(argv[argn]), &file.file_idx))
			return JS_TRUE;
		free(filename);
		filename = strdup(file.file_idx.name);
		argn++;
	}
	else if(filename == NULL)
		return JS_TRUE;

	rc=JS_SUSPENDREQUEST(cx);
	if((p->smb_result = smb_loadfile(&p->smb, filename, &file, file_detail_index)) == SMB_SUCCESS) {
		char path[MAX_PATH + 1];
		getfilepath(scfg, &file, path);
	    JS_SET_RVAL(cx, arglist, UINT_TO_JSVAL((uint32)getfiletime(scfg, &file)));
		smb_freefilemem(&file);
	}
	JS_RESUMEREQUEST(cx, rc);
	free(filename);

	return JS_TRUE;
}

static void get_diz(scfg_t* scfg, file_t* file, char** extdesc)
{
	char diz_fpath[MAX_PATH + 1];
	if(extract_diz(scfg, file, /* diz_fnames: */NULL, diz_fpath, sizeof(diz_fpath))) {
		char extbuf[LEN_EXTDESC + 1] = "";
		str_list_t lines = read_diz(diz_fpath, /* max_line_len: */80);
		if(lines != NULL) {
			format_diz(lines, extbuf, sizeof(extbuf), /* allow_ansi: */false);
			strListFree(&lines);
			free(*extdesc);
			*extdesc = strdup(extbuf);
			if(file->desc == NULL)
				smb_new_hfield_str(file, SMB_FILEDESC, prep_file_desc(extbuf, extbuf));
		}
		(void)remove(diz_fpath);
	}
}

static JSBool
js_add_file(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	char*		extdesc = NULL;
	file_t	file;
	bool		use_diz_always = false;
	jsrefcount	rc;

	ZERO_VAR(file);
	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	if(!SMB_IS_OPEN(&(p->smb))) {
		JS_ReportError(cx, "FileBase is not open");
		return JS_FALSE;
	}

	uintN argn = 0;
	if(argn < argc && JSVAL_IS_OBJECT(argv[argn])) {
		p->smb_result = parse_file_properties(cx, JSVAL_TO_OBJECT(argv[argn]), &file, &extdesc);
		if(p->smb_result != SMB_SUCCESS)
			return JS_TRUE;
		argn++;
	}
	if(argn < argc && JSVAL_IS_BOOLEAN(argv[argn])) {
		use_diz_always = JSVAL_TO_BOOLEAN(argv[argn]);
		argn++;
	}

	file.dir = p->smb.dirnum;
	rc=JS_SUSPENDREQUEST(cx);
	if(file.name != NULL) {
		if((extdesc == NULL	|| use_diz_always == true)
			&& file.dir < scfg->total_dirs
			&& (scfg->dir[file.dir]->misc & DIR_DIZ)) {
			get_diz(scfg, &file, &extdesc);
		}
		char fpath[MAX_PATH + 1];
		getfilepath(scfg, &file, fpath);
		p->smb_result = smb_addfile(&p->smb, &file, SMB_SELFPACK, extdesc, fpath);
		JS_SET_RVAL(cx, arglist, BOOLEAN_TO_JSVAL(p->smb_result == SMB_SUCCESS));
	}
	JS_RESUMEREQUEST(cx, rc);
	smb_freefilemem(&file);
	free(extdesc);

	return JS_TRUE;
}

static JSBool
js_update_file(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	file_t	file;
	char*		filename = NULL;
	JSObject*	fileobj = NULL;
	bool		use_diz_always = false;
	jsrefcount	rc;

	ZERO_VAR(file);
	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	if(!SMB_IS_OPEN(&(p->smb))) {
		JS_ReportError(cx, "FileBase is not open");
		return JS_FALSE;
	}

	uintN argn = 0;
	if(argn < argc) {
		JSVALUE_TO_MSTRING(cx, argv[argn], filename, NULL);
		HANDLE_PENDING(cx, filename);
		argn++;
	}
	if(argn < argc && JSVAL_IS_OBJECT(argv[argn])) {
		fileobj = JSVAL_TO_OBJECT(argv[argn]);
		argn++;
	}
	if(argn < argc && JSVAL_IS_BOOLEAN(argv[argn])) {
		use_diz_always = JSVAL_TO_BOOLEAN(argv[argn]);
		argn++;
	}

	JSBool result = JS_TRUE;
	char* extdesc = NULL;
	rc=JS_SUSPENDREQUEST(cx);
	if(filename != NULL && fileobj != NULL
		&& (p->smb_result = smb_loadfile(&p->smb, filename, &file, file_detail_extdesc)) == SMB_SUCCESS) {
		p->smb_result = parse_file_properties(cx, fileobj, &file, &extdesc);
		if((extdesc == NULL	|| use_diz_always == true)
			&& file.dir < scfg->total_dirs
			&& (scfg->dir[file.dir]->misc & DIR_DIZ)) {
			get_diz(scfg, &file, &extdesc);
		}
		if(p->smb_result == SMB_SUCCESS) {
			char orgfname[MAX_PATH + 1];
			char newfname[MAX_PATH + 1];
			getfilepath(scfg, &file, newfname);
			SAFECOPY(orgfname, newfname);
			*getfname(orgfname) = '\0';
			SAFECAT(orgfname, filename);
			if(strcmp(orgfname, newfname) != 0 && fexistcase(orgfname) && rename(orgfname, newfname) != 0) {
				JS_ReportError(cx, "%d renaming '%s' to '%s'", errno, orgfname, newfname);
				result = JS_FALSE;
				p->smb_result = SMB_ERR_RENAME;
			} else {
				if(file.extdesc != NULL)
					truncsp(file.extdesc);
				if(strcmp(extdesc ? extdesc : "", file.extdesc ? file.extdesc : "") == 0)
					p->smb_result = smb_putfile(&p->smb, &file);
				else {
					if((p->smb_result = smb_removefile(&p->smb, &file)) == SMB_SUCCESS)
						p->smb_result = smb_addfile(&p->smb, &file, SMB_SELFPACK, extdesc, newfname);
				}
			}
		}
		JS_SET_RVAL(cx, arglist, BOOLEAN_TO_JSVAL(p->smb_result == SMB_SUCCESS));
	}
	JS_RESUMEREQUEST(cx, rc);
	smb_freefilemem(&file);
	free(filename);
	free(extdesc);

	return result;
}

static JSBool
js_renew_file(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	char*		fname = NULL;
	jsrefcount	rc;

	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	if(!SMB_IS_OPEN(&(p->smb))) {
		JS_ReportError(cx, "FileBase is not open");
		return JS_FALSE;
	}

	uintN argn = 0;
	if(argn < argc) {
		JSVALUE_TO_MSTRING(cx, argv[argn], fname, NULL);
		HANDLE_PENDING(cx, fname);
		argn++;
	}
	if(fname == NULL)
		return JS_TRUE;

	rc=JS_SUSPENDREQUEST(cx);
	file_t file;
	if((p->smb_result = smb_loadfile(&p->smb, fname, &file, file_detail_index)) == SMB_SUCCESS) {
		char path[MAX_PATH + 1];
		p->smb_result = smb_renewfile(&p->smb, &file, SMB_SELFPACK, getfilepath(scfg, &file, path));
		smb_freefilemem(&file);
	}
	JS_SET_RVAL(cx, arglist, BOOLEAN_TO_JSVAL(p->smb_result == SMB_SUCCESS));
	JS_RESUMEREQUEST(cx, rc);
	free(fname);

	return JS_TRUE;
}

static JSBool
js_remove_file(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject*	obj = JS_THIS_OBJECT(cx, arglist);
	jsval*		argv = JS_ARGV(cx, arglist);
	private_t*	p;
	char*		fname = NULL;
	bool		delfile = false;
	jsrefcount	rc;

	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);

	scfg_t* scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	if(scfg == NULL) {
		JS_ReportError(cx, "JS_GetRuntimePrivate returned NULL");
		return JS_FALSE;
	}

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL)
		return JS_FALSE;

	if(!SMB_IS_OPEN(&(p->smb))) {
		JS_ReportError(cx, "FileBase is not open");
		return JS_FALSE;
	}

	uintN argn = 0;
	if(argn < argc)	{
		JSVALUE_TO_MSTRING(cx, argv[argn], fname, NULL);
		HANDLE_PENDING(cx, fname);
		argn++;
	}
	if(argn < argc && JSVAL_IS_BOOLEAN(argv[argn])) {
		delfile = JSVAL_TO_BOOLEAN(argv[argn]);
		argn++;
	}
	if(fname == NULL)
		return JS_TRUE;

	JSBool result = JS_TRUE;
	rc=JS_SUSPENDREQUEST(cx);
	file_t file;
	if((p->smb_result = smb_loadfile(&p->smb, fname, &file, file_detail_index)) == SMB_SUCCESS) {
		char path[MAX_PATH + 1];
		if(delfile && remove(getfilepath(scfg, &file, path)) != 0) {
			JS_ReportError(cx, "%d removing '%s'", errno, path);
			p->smb_result = SMB_ERR_DELETE;
			result = JS_FALSE;
		} else
			p->smb_result = smb_removefile(&p->smb, &file);
		smb_freefilemem(&file);
	}
	JS_SET_RVAL(cx, arglist, BOOLEAN_TO_JSVAL(p->smb_result == SMB_SUCCESS));
	JS_RESUMEREQUEST(cx, rc);
	free(fname);

	return result;
}

/* FileBase Object Properties */
enum {
	 FB_PROP_LAST_ERROR
	,FB_PROP_FILE
	,FB_PROP_DEBUG
	,FB_PROP_RETRY_TIME
	,FB_PROP_RETRY_DELAY
	,FB_PROP_FIRST_FILE		/* first file number */
	,FB_PROP_LAST_FILE		/* last file number */
	,FB_PROP_LAST_FILE_TIME	/* last file index time */
	,FB_PROP_FILES 			/* total files */
	,FB_PROP_UPDATE_TIME
    ,FB_PROP_MAX_FILES		/* Maximum number of file to keep in dir */
    ,FB_PROP_MAX_AGE		/* Maximum age of file to keep in dir (in days) */
	,FB_PROP_ATTR			/* Attributes for this file base (SMB_HYPER,etc) */
	,FB_PROP_DIRNUM			/* Directory number */
	,FB_PROP_IS_OPEN
	,FB_PROP_STATUS			/* Last SMBLIB returned status value (e.g. retval) */
};

static JSBool js_filebase_set(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp)
{
	time32_t t;
	jsval idval;
    jsint       tiny;
	private_t*	p;

	if((p=(private_t*)js_GetClassPrivate(cx, obj, &js_filebase_class))==NULL) {
		return JS_FALSE;
	}

    JS_IdToValue(cx, id, &idval);
    tiny = JSVAL_TO_INT(idval);

	switch(tiny) {
		case FB_PROP_RETRY_TIME:
			if(!JS_ValueToInt32(cx,*vp,(int32*)&(p->smb).retry_time))
				return JS_FALSE;
			break;
		case FB_PROP_RETRY_DELAY:
			if(!JS_ValueToInt32(cx,*vp,(int32*)&(p->smb).retry_delay))
				return JS_FALSE;
			break;
		case FB_PROP_UPDATE_TIME:
			if(!JS_ValueToInt32(cx, *vp, (int32*)&t))
				return JS_FALSE;
			update_newfiletime(&(p->smb), t);
			break;
	}

	return JS_TRUE;
}

static JSBool js_filebase_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
{
	jsval idval;
	char*		s=NULL;
	JSString*	js_str;
    jsint       tiny;
	idxrec_t	idx;
	private_t*	p;
	jsrefcount	rc;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return JS_FALSE;

    JS_IdToValue(cx, id, &idval);
    tiny = JSVAL_TO_INT(idval);

	switch(tiny) {
		case FB_PROP_FILE:
			s=p->smb.file;
			break;
		case FB_PROP_LAST_ERROR:
			s=p->smb.last_error;
			break;
		case FB_PROP_STATUS:
			*vp = INT_TO_JSVAL(p->smb_result);
			break;
		case FB_PROP_RETRY_TIME:
			*vp = INT_TO_JSVAL(p->smb.retry_time);
			break;
		case FB_PROP_RETRY_DELAY:
			*vp = INT_TO_JSVAL(p->smb.retry_delay);
			break;
		case FB_PROP_FIRST_FILE:
			rc=JS_SUSPENDREQUEST(cx);
			memset(&idx,0,sizeof(idx));
			smb_getfirstidx(&(p->smb),&idx);
			JS_RESUMEREQUEST(cx, rc);
			*vp=UINT_TO_JSVAL(idx.number);
			break;
		case FB_PROP_LAST_FILE:
			rc=JS_SUSPENDREQUEST(cx);
			smb_getstatus(&(p->smb));
			JS_RESUMEREQUEST(cx, rc);
			*vp=UINT_TO_JSVAL(p->smb.status.last_file);
			break;
		case FB_PROP_LAST_FILE_TIME:
			rc=JS_SUSPENDREQUEST(cx);
			*vp = UINT_TO_JSVAL((uint32_t)lastfiletime(&p->smb));
			JS_RESUMEREQUEST(cx, rc);
			break;
		case FB_PROP_FILES:
			rc=JS_SUSPENDREQUEST(cx);
			smb_getstatus(&(p->smb));
			JS_RESUMEREQUEST(cx, rc);
			*vp=UINT_TO_JSVAL(p->smb.status.total_files);
			break;
		case FB_PROP_UPDATE_TIME:
			*vp = UINT_TO_JSVAL((uint32_t)newfiletime(&p->smb));
			break;
		case FB_PROP_MAX_FILES:
			*vp=UINT_TO_JSVAL(p->smb.status.max_files);
			break;
		case FB_PROP_MAX_AGE:
			*vp=UINT_TO_JSVAL(p->smb.status.max_age);
			break;
		case FB_PROP_ATTR:
			*vp=UINT_TO_JSVAL(p->smb.status.attr);
			break;
		case FB_PROP_DIRNUM:
			*vp = INT_TO_JSVAL(p->smb.dirnum);
			break;
		case FB_PROP_IS_OPEN:
			*vp = BOOLEAN_TO_JSVAL(SMB_IS_OPEN(&(p->smb)));
			break;
	}

	if(s!=NULL) {
		if((js_str=JS_NewStringCopyZ(cx, s))==NULL)
			return JS_FALSE;
		*vp = STRING_TO_JSVAL(js_str);
	}

	return JS_TRUE;
}

#define FB_PROP_FLAGS JSPROP_ENUMERATE|JSPROP_READONLY

static jsSyncPropertySpec js_filebase_properties[] = {
/*		 name				,tinyid					,flags,				ver	*/

	{	"error"				,FB_PROP_LAST_ERROR		,FB_PROP_FLAGS,		31900 },
	{	"last_error"		,FB_PROP_LAST_ERROR		,JSPROP_READONLY,	31900 },	/* alias */
	{	"status"			,FB_PROP_STATUS			,FB_PROP_FLAGS,		31900 },
	{	"file"				,FB_PROP_FILE			,FB_PROP_FLAGS,		31900 },
	{	"debug"				,FB_PROP_DEBUG			,0,					31900 },
	{	"retry_time"		,FB_PROP_RETRY_TIME		,JSPROP_ENUMERATE,	31900 },
	{	"retry_delay"		,FB_PROP_RETRY_DELAY	,JSPROP_ENUMERATE,	31900 },
	{	"first_file"		,FB_PROP_FIRST_FILE		,FB_PROP_FLAGS,		31900 },
	{	"last_file"			,FB_PROP_LAST_FILE		,FB_PROP_FLAGS,		31900 },
	{	"last_file_time"	,FB_PROP_LAST_FILE_TIME	,FB_PROP_FLAGS,		31900 },
	{	"files"				,FB_PROP_FILES			,FB_PROP_FLAGS,		31900 },
	{	"update_time"		,FB_PROP_UPDATE_TIME	,JSPROP_ENUMERATE,	31900 },
	{	"max_files"			,FB_PROP_MAX_FILES  	,FB_PROP_FLAGS,		31900 },
	{	"max_age"			,FB_PROP_MAX_AGE   		,FB_PROP_FLAGS,		31900 },
	{	"attributes"		,FB_PROP_ATTR			,FB_PROP_FLAGS,		31900 },
	{	"dirnum"			,FB_PROP_DIRNUM			,FB_PROP_FLAGS,		31900 },
	{	"is_open"			,FB_PROP_IS_OPEN		,FB_PROP_FLAGS,		31900 },
	{0}
};

#ifdef BUILD_JSDOCS
static char* filebase_prop_desc[] = {

	 "last occurred file base error - <small>READ ONLY</small>"
	,"return value of last <i>SMB Library</i> function call - <small>READ ONLY</small>"
	,"base path and filename of file base - <small>READ ONLY</small>"
	,"file base open/lock retry timeout (in seconds)"
	,"delay between file base open/lock retries (in milliseconds)"
	,"first file number - <small>READ ONLY</small>"
	,"last file number - <small>READ ONLY</small>"
	,"timestamp of last file - <small>READ ONLY</small>"
	,"total number of files - <small>READ ONLY</small>"
	,"timestamp of file base index (only writable when file base is closed)"
	,"maximum number of files before expiration - <small>READ ONLY</small>"
	,"maximum age (in days) of files to store - <small>READ ONLY</small>"
	,"file base attributes - <small>READ ONLY</small>"
	,"directory number (0-based, -1 if invalid) - <small>READ ONLY</small>"
	,"<i>true</i> if the file base has been opened successfully - <small>READ ONLY</small>"
	,NULL
};
#endif

static jsSyncMethodSpec js_filebase_functions[] = {
	{"open",			js_open,			0, JSTYPE_BOOLEAN
		,JSDOCSTR("")
		,JSDOCSTR("open file base")
		,31900
	},
	{"close",			js_close,			0, JSTYPE_BOOLEAN
		,JSDOCSTR("")
		,JSDOCSTR("close file base (if open)")
		,31900
	},
	{"get",				js_get_file,		2, JSTYPE_OBJECT
		,JSDOCSTR("filename or file-meta-object [,detail=FileBase.DETAIL.NORM]")
		,JSDOCSTR("get a file metadata object")
		,31900
	},
	{"get_list",		js_get_file_list,	4, JSTYPE_ARRAY
		,JSDOCSTR("[filespec] [,detail=FileBase.DETAIL.NORM] [,since-time=0] [,sort=true [,order]]")
		,JSDOCSTR("get a list (array) of file metadata objects"
			", the default sort order is the sysop-configured order or <tt>FileBase.SORT.NAME_AI</tt>"
			)
		,31900
	},
	{"get_name",		js_get_file_name,	1, JSTYPE_STRING
		,JSDOCSTR("path/filename")
		,JSDOCSTR("returns index-formatted (e.g. shortened) version of filename without path (file base does not have to be open)")
		,31900
	},
	{"get_names",		js_get_file_names,	3, JSTYPE_ARRAY
		,JSDOCSTR("[filespec] [,since-time=0] [,sort=true [,order]]")
		,JSDOCSTR("get a list of index-formatted (e.g. shortened) filenames (strings) from file base index"
			", the default sort order is the sysop-configured order or <tt>FileBase.SORT.NAME_A</tt>")
		,31900
	},
	{"get_path",		js_get_file_path,	1, JSTYPE_STRING
		,JSDOCSTR("filename")
		,JSDOCSTR("get the full path to the local file")
		,31900
	},
	{"get_size",		js_get_file_size,	1, JSTYPE_NUMBER
		,JSDOCSTR("filename")
		,JSDOCSTR("get the size of the local file, in bytes, or -1 if it does not exist")
		,31900
	},
	{"get_time",		js_get_file_time,	1, JSTYPE_NUMBER
		,JSDOCSTR("filename")
		,JSDOCSTR("get the modification date/time stamp of the local file")
		,31900
	},
	{"add",				js_add_file,		1, JSTYPE_BOOLEAN
		,JSDOCSTR("file-meta-object [,use_diz_always=false]")
		,JSDOCSTR("add a file to the file base")
		,31900
	},
	{"remove",			js_remove_file,		2, JSTYPE_BOOLEAN
		,JSDOCSTR("filename [,delete=false]")
		,JSDOCSTR("remove an existing file from the file base and optionally delete file"
				", may throw exception on errors (e.g. file remove failure)")
		,31900
	},
	{"update",			js_update_file,		3, JSTYPE_BOOLEAN
		,JSDOCSTR("filename, file-meta-object [,use_diz_always=false]")
		,JSDOCSTR("update an existing file in the file base"
				", may throw exception on errors (e.g. file rename failure)")
		,31900
	},
	{"renew",			js_renew_file,		1, JSTYPE_BOOLEAN
		,JSDOCSTR("filename")
		,JSDOCSTR("remove and re-add (as new) an existing file in the file base")
		,31900
	},
	{"hash",			js_hash_file,		1, JSTYPE_OBJECT
		,JSDOCSTR("filename_or_fullpath")
		,JSDOCSTR("calculate hashes of a file's contents (file base does not have to be open)")
		,31900
	},
	{"dump",			js_dump_file,		1, JSTYPE_ARRAY
		,JSDOCSTR("filename")
		,JSDOCSTR("dump file metadata to an array of strings for diagnostic uses")
		,31900
	},
	{0}
};

static JSBool js_filebase_resolve(JSContext *cx, JSObject *obj, jsid id)
{
	char*			name=NULL;
	JSBool			ret;

	if(id != JSID_VOID && id != JSID_EMPTY) {
		jsval idval;

		JS_IdToValue(cx, id, &idval);
		if(JSVAL_IS_STRING(idval)) {
			JSVALUE_TO_MSTRING(cx, idval, name, NULL);
			HANDLE_PENDING(cx, name);
		}
	}

	ret=js_SyncResolve(cx, obj, name, js_filebase_properties, js_filebase_functions, NULL, 0);
	if(name)
		free(name);
	return ret;
}

static JSBool js_filebase_enumerate(JSContext *cx, JSObject *obj)
{
	return(js_filebase_resolve(cx, obj, JSID_VOID));
}

JSClass js_filebase_class = {
     "FileBase"				/* name			*/
    ,JSCLASS_HAS_PRIVATE	/* flags		*/
	,JS_PropertyStub		/* addProperty	*/
	,JS_PropertyStub		/* delProperty	*/
	,js_filebase_get		/* getProperty	*/
	,js_filebase_set		/* setProperty	*/
	,js_filebase_enumerate	/* enumerate	*/
	,js_filebase_resolve	/* resolve		*/
	,JS_ConvertStub			/* convert		*/
	,js_finalize_filebase	/* finalize		*/
};

/* FileBase Constructor (open file base) */
static JSBool
js_filebase_constructor(JSContext *cx, uintN argc, jsval *arglist)
{
	JSObject *		obj;
	jsval *			argv=JS_ARGV(cx, arglist);
	char*			base = NULL;
	private_t*		p;
	scfg_t*			scfg;

	scfg=JS_GetRuntimePrivate(JS_GetRuntime(cx));

	obj=JS_NewObject(cx, &js_filebase_class, NULL, NULL);
	JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(obj));
	if((p=(private_t*)malloc(sizeof(private_t)))==NULL) {
		JS_ReportError(cx,"malloc failed");
		return JS_FALSE;
	}

	memset(p,0,sizeof(private_t));
	p->smb.retry_time=scfg->smb_retry_time;

	JSSTRING_TO_MSTRING(cx, JS_ValueToString(cx, argv[0]), base, NULL);
	if(JS_IsExceptionPending(cx)) {
		if(base != NULL)
			free(base);
		free(p);
		return JS_FALSE;
	}
	if(base==NULL) {
		JS_ReportError(cx, "Invalid base parameter");
		free(p);
		return JS_FALSE;
	}

	if(!JS_SetPrivate(cx, obj, p)) {
		JS_ReportError(cx,"JS_SetPrivate failed");
		free(p);
		free(base);
		return JS_FALSE;
	}

#ifdef BUILD_JSDOCS
	js_DescribeSyncObject(cx,obj,"Class used for accessing file databases", 31900);
	js_DescribeSyncConstructor(cx,obj,"To create a new FileBase object: "
		"<tt>var filebase = new FileBase('<i>code</i>')</tt><br>"
		"where <i>code</i> is a directory internal code."
		);
	js_CreateArrayOfStrings(cx, obj, "_property_desc_list", filebase_prop_desc, JSPROP_READONLY);
#endif

	p->smb.dirnum = getdirnum(scfg, base);
1536
	if(p->smb.dirnum < scfg->total_dirs) {
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
		safe_snprintf(p->smb.file, sizeof(p->smb.file), "%s%s"
			,scfg->dir[p->smb.dirnum]->data_dir, scfg->dir[p->smb.dirnum]->code);
	} else { /* unknown code */
		SAFECOPY(p->smb.file, base);
	}

	free(base);
	return JS_TRUE;
}

#ifdef BUILD_JSDOCS
static char* filebase_detail_prop_desc[] = {
	 "Include indexed-filenames only",
	 "Normal level of file detail (e.g. full filenames, minimal meta data)",
	 "Normal level of file detail plus extended descriptions",
	 "Maximum file detail, include undefined/null property values",
	 NULL
};

static char* filebase_sort_prop_desc[] = {
	"Natural sort order (same as DATE_A)",
	"Filename ascending, case insensitive sort order",
	"Filename descending, case insensitive sort order",
	"Filename ascending, case sensitive sort order",
	"Filename descending, case sensitive sort order",
	"Import date/time ascending sort order",
	"Import date/time descending sort order",
	NULL
};
#endif

JSObject* DLLCALL js_CreateFileBaseClass(JSContext* cx, JSObject* parent, scfg_t* cfg)
{
	JSObject*	obj;
	JSObject*	constructor;
	jsval		val;

	obj = JS_InitClass(cx, parent, NULL
		,&js_filebase_class
		,js_filebase_constructor
		,1	/* number of constructor args */
		,NULL
		,NULL
		,NULL, NULL);

	if(JS_GetProperty(cx, parent, js_filebase_class.name, &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToObject(cx, val, &constructor);
		JSObject* detail = JS_DefineObject(cx, constructor, "DETAIL", NULL, NULL, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
		if(detail != NULL) {
			JS_DefineProperty(cx, detail, "MIN", INT_TO_JSVAL(file_detail_index), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, detail, "NORM", INT_TO_JSVAL(file_detail_normal), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, detail, "EXTENDED", INT_TO_JSVAL(file_detail_extdesc), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, detail, "MAX", INT_TO_JSVAL(file_detail_extdesc + 1), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
#ifdef BUILD_JSDOCS
			js_DescribeSyncObject(cx, detail, "Detail level numeric constants", 0);
			js_CreateArrayOfStrings(cx, detail, "_property_desc_list", filebase_detail_prop_desc, JSPROP_READONLY);
#endif
		}
		JSObject* sort = JS_DefineObject(cx, constructor, "SORT", NULL, NULL, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
		if(sort != NULL) {
			JS_DefineProperty(cx, sort, "NATURAL", INT_TO_JSVAL(FILE_SORT_NATURAL), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, sort, "NAME_AI", INT_TO_JSVAL(FILE_SORT_NAME_A), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, sort, "NAME_DI", INT_TO_JSVAL(FILE_SORT_NAME_D), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, sort, "NAME_AS", INT_TO_JSVAL(FILE_SORT_NAME_AC), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, sort, "NAME_DS", INT_TO_JSVAL(FILE_SORT_NAME_DC), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, sort, "DATE_A", INT_TO_JSVAL(FILE_SORT_DATE_A), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
			JS_DefineProperty(cx, sort, "DATE_D", INT_TO_JSVAL(FILE_SORT_DATE_D), NULL, NULL
				, JSPROP_PERMANENT|JSPROP_ENUMERATE|JSPROP_READONLY);
#ifdef BUILD_JSDOCS
			js_DescribeSyncObject(cx, sort, "Sort order numeric constants", 0);
			js_CreateArrayOfStrings(cx, sort, "_property_desc_list", filebase_sort_prop_desc, JSPROP_READONLY);
#endif
		}
	}
	return obj;
}