Diff

Differences From Artifact [7ee6ccc5a2]:

To Artifact [45ff02ebc8]:


44
45
46
47
48
49
50

51
52
53
54
55
56
57
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58







+







	char name[256];
};

struct appfs_pathinfo {
	appfs_pathtype_t type;
	time_t time;
	char hostname[256];
	unsigned long long inode;
	union {
		struct {
			int childcount;
		} dir;
		struct {
			int executable;
			off_t size;
271
272
273
274
275
276
277


278
279
280
281
282
283
284
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287







+
+







	APPFS_DEBUG("SQL: %s", sql);
	sqlite_ret = sqlite3_exec(globalThread.db, sql, appfs_getchildren_cb, &head, NULL);
	sqlite3_free(sql);

	if (sqlite_ret != SQLITE_OK) {
		APPFS_DEBUG("Call to sqlite3_exec failed.");

		appfs_free_list_children(head);

		return(NULL);
	}

	if (head != NULL) {
		*children_count_p = head->counter + 1;
	}

398
399
400
401
402
403
404
405

406
407
408
409
410
411
412

413
414



415
416
417
418
419
420
421
401
402
403
404
405
406
407

408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428







-
+







+


+
+
+







	}

	return(retval);
}

static int appfs_getfileinfo_cb(void *_pathinfo, int columns, char **values, char **names) {
	struct appfs_pathinfo *pathinfo = _pathinfo;
	const char *type, *time, *source, *size, *perms, *sha1;
	const char *type, *time, *source, *size, *perms, *sha1, *rowid;

	type = values[0];
	time = values[1];
	source = values[2];
	size = values[3];
	perms = values[4];
	sha1 = values[5];
	rowid = values[6];

	pathinfo->time = strtoull(time, NULL, 10);

	/* Package file inodes start at 2^32, fake inodes are before then */
	pathinfo->inode = strtoull(rowid, NULL, 10) + 4294967296ULL;

	if (strcmp(type, "file") == 0) {
		pathinfo->type = APPFS_PATHTYPE_FILE;

		if (!size) {
			size = "0";
		}
486
487
488
489
490
491
492
493

494
495
496
497
498
499
500
493
494
495
496
497
498
499

500
501
502
503
504
505
506
507







-
+







		file = path;
		directory = "";
	} else {
		*file = '\0';
		file++;
	}

	sql = sqlite3_mprintf("SELECT type, time, source, size, perms, file_sha1 FROM files WHERE package_sha1 = %Q AND file_directory = %Q AND file_name = %Q;", package_hash, directory, file);
	sql = sqlite3_mprintf("SELECT type, time, source, size, perms, file_sha1, rowid FROM files WHERE package_sha1 = %Q AND file_directory = %Q AND file_name = %Q;", package_hash, directory, file);
	if (sql == NULL) {
		APPFS_DEBUG("Call to sqlite3_mprintf failed.");

		free(path);

		return(-EIO);
	}
561
562
563
564
565
566
567



















568
569
570
571
572
573
574
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







		snprintf(new_child->name, sizeof(new_child->name), "%s", name);

		*children = new_child;
	}

	return(0);
}

/* Generate an inode for a given path */
static long long appfs_get_path_inode(const char *path) {
	long long retval;
	const char *p;

	retval = 10;

	for (p = path; *p; p++) {
		retval %= 4290960290ULL;
		retval += *p;
		retval <<= 7;
	}

	retval += 10;
	retval %= 4294967296ULL;

	return(retval);
}

/* Get information about a path, and optionally list children */
static int appfs_get_path_info(const char *_path, struct appfs_pathinfo *pathinfo, struct appfs_children **children) {
	struct appfs_children *dir_children;
	char *hostname, *packagename, *os_cpuArch, *os, *cpuArch, *version;
	char *path, *path_s;
	char *package_hash;
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
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







+



















+
+







	if (_path[0] != '/') {
		return(-ENOENT);
	}

	if (_path[1] == '\0') {
		/* Request for the root directory */
		pathinfo->hostname[0] = '\0';
		pathinfo->inode = 1;

		sql = sqlite3_mprintf("SELECT DISTINCT hostname FROM packages;");

		retval = appfs_get_path_info_sql(sql, 1, NULL, pathinfo, children);

		/* The root directory always exists, even if it has no subordinates */
		if (retval != 0) {
			pathinfo->type = APPFS_PATHTYPE_DIRECTORY;
			pathinfo->typeinfo.dir.childcount = 0;
			pathinfo->time = globalThread.boottime;

			retval = 0;
		}

		return(retval);
	}

	path = strdup(_path);
	path_s = path;

	pathinfo->inode = appfs_get_path_inode(path);

	hostname = path + 1;
	packagename = strchr(hostname, '/');

	if (packagename != NULL) {
		*packagename = '\0';
		packagename++;
737
738
739
740
741
742
743


744
745
746
747
748
749
750
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781







+
+








		if (dir_children != NULL) {
			pathinfo->typeinfo.dir.childcount = files_count;
		}

		if (children) {
			*children = dir_children;
		} else {
			appfs_free_list_children(dir_children);
		}
	}

	free(path_s);

	return(0);
}
789
790
791
792
793
794
795

796
797
798
799
800
801
802
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834







+







	}

	memset(stbuf, 0, sizeof(struct stat));

	stbuf->st_mtime = pathinfo.time;
	stbuf->st_ctime = pathinfo.time;
	stbuf->st_atime = pathinfo.time;
	stbuf->st_ino   = pathinfo.inode;

	switch (pathinfo.type) {
		case APPFS_PATHTYPE_DIRECTORY:
			stbuf->st_mode = S_IFDIR | 0555;
			stbuf->st_nlink = 2 + pathinfo.typeinfo.dir.childcount;
			break;
		case APPFS_PATHTYPE_FILE: