Check-in [041086f3fb]
Overview
Comment:Reduced redundant code
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:041086f3fbf180deb7f0498bef34df357e0ac12f
User & Date: rkeene on 2014-09-10 04:01:07
Other Links: manifest | tags
Context
2014-09-10
04:02
Reorganized check-in: 1f01cf90b5 user: rkeene tags: trunk
04:01
Reduced redundant code check-in: 041086f3fb user: rkeene tags: trunk
2014-09-09
08:23
Added a default time to psuedo entries check-in: ef5acff5c9 user: rkeene tags: trunk
Changes

Modified appfs.c from [799a556739] to [3c07efd857].

   326    326   	type *obj, *next; \
   327    327   	for (obj = head; obj; obj = next) { \
   328    328   		next = obj->_next; \
   329    329   		ckfree((void *) obj); \
   330    330   	} \
   331    331   }
   332    332   
   333         -appfs_free_list_type(site, struct appfs_site)
   334         -appfs_free_list_type(package, struct appfs_package)
   335    333   appfs_free_list_type(children, struct appfs_children)
   336    334   
   337         -static int appfs_getsites_cb(void *_head, int columns, char **values, char **names) {
   338         -	struct appfs_site **head_p, *obj;
   339         -
   340         -	head_p = _head;
   341         -
   342         -	obj = (void *) ckalloc(sizeof(*obj));
   343         -	snprintf(obj->name, sizeof(obj->name), "%s", values[0]);
   344         -
   345         -	if (*head_p == NULL) {
   346         -		obj->counter = 0;
   347         -	} else {
   348         -		obj->counter = (*head_p)->counter + 1;
   349         -	}
   350         -
   351         -	obj->_next = *head_p;
   352         -	*head_p = obj;
   353         -
   354         -	return(0);
   355         -}
   356         -
   357         -static struct appfs_site *appfs_getsites(int *site_count_p) {
   358         -	struct appfs_site *head = NULL;
   359         -	int sqlite_ret;
   360         -
   361         -	if (site_count_p == NULL) {
   362         -		return(NULL);
   363         -	}
   364         -
   365         -	sqlite_ret = sqlite3_exec(globalThread.db, "SELECT DISTINCT hostname FROM packages;", appfs_getsites_cb, &head, NULL);
   366         -	if (sqlite_ret != SQLITE_OK) {
   367         -		APPFS_DEBUG("Call to sqlite3_exec failed.");
   368         -
   369         -		return(NULL);
   370         -	}
   371         -
   372         -	if (head != NULL) {
   373         -		*site_count_p = head->counter + 1;
   374         -	}
   375         -
   376         -	return(head);
   377         -}
   378         -
   379         -static int appfs_getindex_cb(void *_head, int columns, char **values, char **names) {
   380         -	struct appfs_package **head_p, *obj;
   381         -
   382         -	head_p = _head;
   383         -
   384         -	obj = (void *) ckalloc(sizeof(*obj));
   385         -
   386         -	snprintf(obj->name, sizeof(obj->name), "%s", values[0]);
   387         -	snprintf(obj->version, sizeof(obj->version), "%s", values[1]);
   388         -	snprintf(obj->sha1, sizeof(obj->sha1), "%s", values[2]);
   389         -	obj->os = appfs_convert_os_fromString(values[3]);
   390         -	obj->cpuArch = appfs_convert_cpuArch_fromString(values[4]);
   391         -	snprintf(obj->os_str, sizeof(obj->os_str), "%s", values[3]);
   392         -	snprintf(obj->cpuArch_str, sizeof(obj->cpuArch_str), "%s", values[4]);
   393         -	if (values[5][0] == '1') {
   394         -		obj->isLatest = 1;
   395         -	} else {
   396         -		obj->isLatest = 0;
   397         -	}
   398         -
   399         -	if (*head_p == NULL) {
   400         -		obj->counter = 0;
   401         -	} else {
   402         -		obj->counter = (*head_p)->counter + 1;
   403         -	}
   404         -
   405         -	obj->_next = *head_p;
   406         -	*head_p = obj;
   407         -
   408         -	return(0);
   409         -}
   410         -
   411         -static struct appfs_package *appfs_getindex(const char *hostname, int *package_count_p) {
   412         -	struct appfs_package *head = NULL;
   413         -	char *sql;
   414         -	int sqlite_ret;
   415         -
   416         -	if (package_count_p == NULL) {
   417         -		return(NULL);
   418         -	}
   419         -
   420         -	appfs_update_index(hostname);
   421         -
   422         -	sql = sqlite3_mprintf("SELECT package, version, sha1, os, cpuArch, isLatest FROM packages WHERE hostname = %Q;", hostname);
   423         -	if (sql == NULL) {
   424         -		APPFS_DEBUG("Call to sqlite3_mprintf failed.");
   425         -
   426         -		return(NULL);
   427         -	}
   428         -
   429         -	sqlite_ret = sqlite3_exec(globalThread.db, sql, appfs_getindex_cb, &head, NULL);
   430         -	sqlite3_free(sql);
   431         -
   432         -	if (sqlite_ret != SQLITE_OK) {
   433         -		APPFS_DEBUG("Call to sqlite3_exec failed.");
   434         -
   435         -		return(NULL);
   436         -	}
   437         -
   438         -	if (head != NULL) {
   439         -		*package_count_p = head->counter + 1;
   440         -	}
   441         -
   442         -	return(head);
   443         -}
   444         -
   445    335   static int appfs_getchildren_cb(void *_head, int columns, char **values, char **names) {
   446    336   	struct appfs_children **head_p, *obj;
   447    337   
   448    338   	head_p = _head;
   449    339   
   450    340   	obj = (void *) ckalloc(sizeof(*obj));
   451    341   
................................................................................
   495    385   
   496    386   	if (head != NULL) {
   497    387   		*children_count_p = head->counter + 1;
   498    388   	}
   499    389   
   500    390   	return(head);
   501    391   }
          392  +
          393  +struct appfs_sqlite3_query_cb_handle {
          394  +	struct appfs_children *head;
          395  +	int argc;
          396  +	const char *fmt;
          397  +};
          398  +
          399  +static int appfs_sqlite3_query_cb(void *_cb_handle, int columns, char **values, char **names) {
          400  +	struct appfs_sqlite3_query_cb_handle *cb_handle;
          401  +	struct appfs_children *obj;
          402  +
          403  +	cb_handle = _cb_handle;
          404  +
          405  +	obj = (void *) ckalloc(sizeof(*obj));
          406  +
          407  +	switch (cb_handle->argc) {
          408  +		case 1:
          409  +			snprintf(obj->name, sizeof(obj->name), cb_handle->fmt, values[0]);
          410  +			break;
          411  +		case 2:
          412  +			snprintf(obj->name, sizeof(obj->name), cb_handle->fmt, values[0], values[1]);
          413  +			break;
          414  +		case 3:
          415  +			snprintf(obj->name, sizeof(obj->name), cb_handle->fmt, values[0], values[1], values[2]);
          416  +			break;
          417  +		case 4:
          418  +			snprintf(obj->name, sizeof(obj->name), cb_handle->fmt, values[0], values[1], values[2], values[3]);
          419  +			break;
          420  +	}
          421  +
          422  +	if (cb_handle->head == NULL) {
          423  +		obj->counter = 0;
          424  +	} else {
          425  +		obj->counter = cb_handle->head->counter + 1;
          426  +	}
          427  +
          428  +	obj->_next = cb_handle->head;
          429  +	cb_handle->head = obj;
          430  +
          431  +	return(0);
          432  +}
          433  +
          434  +static struct appfs_children *appfs_sqlite3_query(char *sql, int argc, const char *fmt, int *results_count_p) {
          435  +	struct appfs_sqlite3_query_cb_handle cb_handle;
          436  +	int sqlite_ret;
          437  +
          438  +	if (results_count_p == NULL) {
          439  +		return(NULL);
          440  +	}
          441  +
          442  +	if (sql == NULL) {
          443  +		APPFS_DEBUG("Call to sqlite3_mprintf probably failed.");
          444  +
          445  +		return(NULL);
          446  +	}
          447  +
          448  +	if (fmt == NULL) {
          449  +		fmt = "%s";
          450  +	}
          451  +
          452  +	cb_handle.head = NULL;
          453  +	cb_handle.argc = argc;
          454  +	cb_handle.fmt  = fmt;
          455  +
          456  +	APPFS_DEBUG("SQL: %s", sql);
          457  +	sqlite_ret = sqlite3_exec(globalThread.db, sql, appfs_sqlite3_query_cb, &cb_handle, NULL);
          458  +	sqlite3_free(sql);
          459  +
          460  +	if (sqlite_ret != SQLITE_OK) {
          461  +		APPFS_DEBUG("Call to sqlite3_exec failed.");
          462  +
          463  +		return(NULL);
          464  +	}
          465  +
          466  +	if (cb_handle.head != NULL) {
          467  +		*results_count_p = cb_handle.head->counter + 1;
          468  +	}
          469  +
          470  +	return(cb_handle.head);
          471  +}
   502    472   
   503    473   static int appfs_lookup_package_hash_cb(void *_retval, int columns, char **values, char **names) {
   504    474   	char **retval = _retval;
   505    475   
   506    476   	*retval = strdup(values[0]);
   507    477   
   508    478   	return(0);
................................................................................
   643    613   	if (pathinfo->type == APPFS_PATHTYPE_INVALID) {
   644    614   		return(-ENOENT);
   645    615   	}
   646    616   
   647    617   	return(0);
   648    618   }
   649    619   
          620  +static int appfs_get_path_info_sql(char *sql, int argc, const char *fmt, struct appfs_pathinfo *pathinfo, struct appfs_children **children) {
          621  +	struct appfs_children *node, *dir_children, *dir_child;
          622  +	int dir_children_count = 0;
          623  +
          624  +	dir_children = appfs_sqlite3_query(sql, argc, fmt, &dir_children_count);
          625  +
          626  +	if (dir_children == NULL || dir_children_count == 0) {
          627  +		return(-ENOENT);
          628  +	}
          629  +
          630  +	/* Request for a single hostname */
          631  +	pathinfo->type = APPFS_PATHTYPE_DIRECTORY;
          632  +	pathinfo->typeinfo.dir.childcount = dir_children_count;
          633  +	pathinfo->time = globalThread.boottime;
          634  +
          635  +	if (children) {
          636  +		for (dir_child = dir_children; dir_child; dir_child = dir_child->_next) {
          637  +			node = (void *) ckalloc(sizeof(*node));
          638  +			node->_next = *children;
          639  +			strcpy(node->name, dir_child->name);
          640  +			*children = node;
          641  +		}
          642  +	}
          643  +
          644  +	appfs_free_list_children(dir_children);
          645  +
          646  +	return(0);
          647  +}
   650    648   /* Get information about a path, and optionally list children */
   651    649   static int appfs_get_path_info(const char *_path, struct appfs_pathinfo *pathinfo, struct appfs_children **children) {
   652         -	struct appfs_site *sites, *site;
   653         -	struct appfs_children *node, *dir_children;
   654         -	struct appfs_package *packages, *package;
          650  +	struct appfs_children *dir_children;
   655    651   	appfs_os_t os_val;
   656    652   	appfs_cpuArch_t cpuArch_val;
   657    653   	char *hostname, *packagename, *os_cpuArch, *os, *cpuArch, *version;
   658    654   	char *path, *path_s;
   659    655   	char *package_hash;
   660         -	int sites_count, packages_count, os_cpuArch_count, version_count, files_count;
   661         -	int fileinfo_ret;
          656  +	char *sql;
          657  +	int files_count;
          658  +	int fileinfo_ret, retval;
   662    659   
   663    660   	if (children) {
   664    661   		*children = NULL;
   665    662   	}
   666    663   
   667    664   	if (_path == NULL) {
   668    665   		return(-ENOENT);
................................................................................
   670    667   
   671    668   	if (_path[0] != '/') {
   672    669   		return(-ENOENT);
   673    670   	}
   674    671   
   675    672   	if (_path[1] == '\0') {
   676    673   		/* Request for the root directory */
   677         -		sites = appfs_getsites(&sites_count);
   678         -
   679         -		pathinfo->type = APPFS_PATHTYPE_DIRECTORY;
   680         -		pathinfo->typeinfo.dir.childcount = sites_count;
   681    674   		pathinfo->hostname[0] = '\0';
   682         -		pathinfo->time = globalThread.boottime;
   683    675   
   684         -		if (children) {
   685         -			for (site = sites; site; site = site->_next) {
   686         -				node = (void *) ckalloc(sizeof(*node));
   687         -				node->_next = *children;
   688         -				strcpy(node->name, site->name);
   689         -				*children = node;
   690         -			}
          676  +		sql = sqlite3_mprintf("SELECT DISTINCT hostname FROM packages;");
          677  +
          678  +		retval = appfs_get_path_info_sql(sql, 1, NULL, pathinfo, children);
          679  +
          680  +		/* The root directory always exists, even if it has no subordinates */
          681  +		if (retval != 0) {
          682  +			pathinfo->type = APPFS_PATHTYPE_DIRECTORY;
          683  +			pathinfo->typeinfo.dir.childcount = 0;
          684  +			pathinfo->time = globalThread.boottime;
          685  +
          686  +			retval = 0;
   691    687   		}
   692    688   
   693         -		appfs_free_list_site(sites);
   694         -
   695         -		return(0);
          689  +		return(retval);
   696    690   	}
   697    691   
   698    692   	path = strdup(_path);
   699    693   	path_s = path;
   700    694   
   701    695   	hostname = path + 1;
   702    696   	packagename = strchr(hostname, '/');
................................................................................
   704    698   	if (packagename != NULL) {
   705    699   		*packagename = '\0';
   706    700   		packagename++;
   707    701   	}
   708    702   
   709    703   	snprintf(pathinfo->hostname, sizeof(pathinfo->hostname), "%s", hostname);
   710    704   
   711         -	packages = appfs_getindex(hostname, &packages_count);
          705  +	if (packagename == NULL) {
          706  +		appfs_update_index(hostname);
   712    707   
   713         -	if (packages == NULL || packages_count == 0) {
   714         -		APPFS_DEBUG("Unable to fetch package index from %s", hostname);
          708  +		sql = sqlite3_mprintf("SELECT DISTINCT package FROM packages WHERE hostname = %Q;", hostname);
   715    709   
   716    710   		free(path_s);
   717    711   
   718         -		return(-ENOENT);
          712  +		return(appfs_get_path_info_sql(sql, 1, NULL, pathinfo, children));
   719    713   	}
   720    714   
   721         -	if (packagename == NULL) {
   722         -		/* Request for a single hostname */
   723         -		pathinfo->type = APPFS_PATHTYPE_DIRECTORY;
   724         -		pathinfo->typeinfo.dir.childcount = packages_count;
   725         -		pathinfo->time = globalThread.boottime;
          715  +	os_cpuArch = strchr(packagename, '/');
   726    716   
   727         -		if (children) {
   728         -			for (package = packages; package; package = package->_next) {
   729         -				node = (void *) ckalloc(sizeof(*node));
   730         -				node->_next = *children;
   731         -				strcpy(node->name, package->name);
   732         -				*children = node;
   733         -			}
   734         -		}
          717  +	if (os_cpuArch != NULL) {
          718  +		*os_cpuArch = '\0';
          719  +		os_cpuArch++;
          720  +	}
   735    721   
   736         -		appfs_free_list_package(packages);
          722  +	if (os_cpuArch == NULL) {
          723  +		appfs_update_index(hostname);
          724  +
          725  +		sql = sqlite3_mprintf("SELECT DISTINCT os, cpuArch FROM packages WHERE hostname = %Q AND package = %Q;", hostname, packagename);
   737    726   
   738    727   		free(path_s);
   739    728   
   740         -		return(0);
          729  +		return(appfs_get_path_info_sql(sql, 2, "%s-%s", pathinfo, children));
   741    730   	}
   742    731   
   743         -	os_cpuArch = strchr(packagename, '/');
   744         -	if (os_cpuArch == NULL) {
   745         -		/* Request for OS and CPU Arch for a specific package */
   746         -		pathinfo->type = APPFS_PATHTYPE_DIRECTORY;
   747         -		pathinfo->time = globalThread.boottime;
   748         -
   749         -		os_cpuArch_count = 0;
   750         -		for (package = packages; package; package = package->_next) {
   751         -			if (strcmp(package->name, packagename) != 0) {
   752         -				APPFS_DEBUG("Skipping package named \"%s\", does not match requested package: \"%s\"", package->name, packagename);
   753         -
   754         -				continue;
   755         -			}
   756         -
   757         -			os_cpuArch_count++;
   758         -
   759         -			if (children) {
   760         -				node = (void *) ckalloc(sizeof(*node));
   761         -				node->_next = *children;
   762         -				snprintf(node->name, sizeof(node->name), "%s-%s", package->os_str, package->cpuArch_str);
   763         -				*children = node;
   764         -			}
   765         -		}
   766         -
   767         -		appfs_free_list_package(packages);
   768         -
   769         -		free(path_s);
   770         -
   771         -		pathinfo->typeinfo.dir.childcount = os_cpuArch_count;
   772         -
   773         -		return(0);
   774         -	}
   775         -
   776         -	*os_cpuArch = '\0';
   777         -	os_cpuArch++;
   778         -
   779    732   	version = strchr(os_cpuArch, '/');
   780    733   
   781    734   	if (version != NULL) {
   782    735   		*version = '\0';
   783    736   		version++;
   784    737   	}
   785    738   
................................................................................
   794    747   		cpuArch_val = APPFS_CPU_UNKNOWN;
   795    748   	}
   796    749   
   797    750   	os_val = appfs_convert_os_fromString(os);
   798    751   
   799    752   	if (version == NULL) {
   800    753   		/* Request for version list for a package on an OS/CPU */
   801         -		pathinfo->type = APPFS_PATHTYPE_DIRECTORY;
   802         -		pathinfo->time = globalThread.boottime;
          754  +		appfs_update_index(hostname);
   803    755   
   804         -		version_count = 0;
   805         -		for (package = packages; package; package = package->_next) {
   806         -			if (strcmp(package->name, packagename) != 0) {
   807         -				APPFS_DEBUG("Skipping package named \"%s\", does not match requested package: \"%s\"", package->name, packagename);
   808         -
   809         -				continue;
   810         -			}
   811         -
   812         -			if (package->os != os_val) {
   813         -				continue;
   814         -			}
   815         -
   816         -			if (package->cpuArch != cpuArch_val) {
   817         -				continue;
   818         -			}
   819         -
   820         -			version_count++;
   821         -
   822         -			if (children) {
   823         -				node = (void *) ckalloc(sizeof(*node));
   824         -				node->_next = *children;
   825         -				strcpy(node->name, package->version);
   826         -				*children = node;
   827         -			}
   828         -		}
   829         -
   830         -		appfs_free_list_package(packages);
          756  +		sql = sqlite3_mprintf("SELECT DISTINCT version FROM packages WHERE hostname = %Q AND package = %Q AND os = %Q and cpuArch = %Q;", hostname, packagename, os, cpuArch);
   831    757   
   832    758   		free(path_s);
   833    759   
   834         -		pathinfo->typeinfo.dir.childcount = version_count;
   835         -
   836         -		return(0);
          760  +		return(appfs_get_path_info_sql(sql, 1, NULL, pathinfo, children));
   837    761   	}
   838    762   
   839         -	appfs_free_list_package(packages);
   840         -
   841    763   	path = strchr(version, '/');
   842    764   	if (path == NULL) {
   843    765   		path = "";
   844    766   	} else {
   845    767   		*path = '\0';
   846    768   		path++;
   847    769   	}
................................................................................
   855    777   	if (package_hash == NULL) {
   856    778   		free(path_s);
   857    779   
   858    780   		return(-ENOENT);
   859    781   	}
   860    782   
   861    783   	APPFS_DEBUG("  ... which hash a hash of %s", package_hash);
          784  +
          785  +	appfs_update_manifest(hostname, package_hash);
   862    786   
   863    787   	if (strcmp(path, "") == 0) {
   864    788   		pathinfo->type = APPFS_PATHTYPE_DIRECTORY;
   865    789   		pathinfo->time = globalThread.boottime;
   866    790   	} else {
   867    791   		fileinfo_ret = appfs_getfileinfo(hostname, package_hash, path, pathinfo);
   868    792   		if (fileinfo_ret != 0) {