Check-in [654957c655]
Overview
Comment:Updated to print packages
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:654957c65509f97c65b189fc808abe2d3fbc79e4
User & Date: rkeene on 2014-09-08 06:12:46
Other Links: manifest | tags
Context
2014-09-08
06:48
More work towards reading package data check-in: e37bc482a9 user: rkeene tags: trunk
06:12
Updated to print packages check-in: 654957c655 user: rkeene tags: trunk
04:34
More work towards SQLite3 integration check-in: 76ac4557f6 user: rkeene tags: trunk
Changes

Modified appfs.c from [d2a493f839] to [0ffa90a2b2].

    37     37   	APPFS_CPU_I386,
    38     38   	APPFS_CPU_ARM
    39     39   } appfs_cpuArch_t;
    40     40   
    41     41   struct appfs_package {
    42     42   	char name[128];
    43     43   	char version[64];
           44  +	char sha1[41];
    44     45   	appfs_os_t os;
    45     46   	appfs_cpuArch_t cpuArch;
    46     47   	int isLatest;
           48  +
           49  +	int counter;
           50  +	struct appfs_package *_next;
    47     51   };
    48     52   
    49     53   static appfs_os_t appfs_convert_os_fromString(const char *os) {
    50     54   	if (strcasecmp(os, "Linux") == 0) {
    51     55   		return(APPFS_OS_LINUX);
    52     56   	}
    53     57   
................................................................................
    79     83   		case APPFS_CPU_UNKNOWN:
    80     84   			return("unknown");
    81     85   	}
    82     86   
    83     87   	return("unknown");
    84     88   }
    85     89   
    86         -static appfs_cpuArch_t appfs_convert_cpu_fromString(const char *cpu) {
           90  +static appfs_cpuArch_t appfs_convert_cpuArch_fromString(const char *cpu) {
    87     91   	if (strcasecmp(cpu, "amd64") == 0 || strcasecmp(cpu, "x86_64") == 0) {
    88     92   		return(APPFS_CPU_AMD64);
    89     93   	}
    90     94   
    91     95   	if (strcasecmp(cpu, "i386") == 0 || \
    92     96   	    strcasecmp(cpu, "i486") == 0 || \
    93     97   	    strcasecmp(cpu, "i586") == 0 || \
................................................................................
   103    107   	if (strcasecmp(cpu, "noarch") == 0) {
   104    108   		return(APPFS_CPU_ALL);
   105    109   	}
   106    110   
   107    111   	return(APPFS_CPU_UNKNOWN);
   108    112   }
   109    113   
   110         -static const char *appfs_convert_cpu_toString(appfs_cpuArch_t cpu) {
          114  +static const char *appfs_convert_cpuArch_toString(appfs_cpuArch_t cpu) {
   111    115   	switch (cpu) {
   112    116   		case APPFS_CPU_ALL:
   113    117   			return("noarch");
   114    118   		case APPFS_CPU_AMD64:
   115    119   			return("amd64");
   116    120   		case APPFS_CPU_I386:
   117    121   			return("ix86");
................................................................................
   143    147   
   144    148   	retval = Tcl_EvalObjv(interp, objc, objv, 0);
   145    149   
   146    150   	ckfree((void *) objv);
   147    151   
   148    152   	return(retval);
   149    153   }
          154  +
          155  +int appfs_getindex_cb(void *_head, int columns, char **values, char **names) {
          156  +	struct appfs_package **head = _head, *obj;
          157  +
          158  +	obj = (void *) ckalloc(sizeof(*obj));
          159  +
          160  +	snprintf(obj->name, sizeof(obj->name), "%s", values[0]);
          161  +	snprintf(obj->version, sizeof(obj->version), "%s", values[1]);
          162  +	snprintf(obj->sha1, sizeof(obj->sha1), "%s", values[2]);
          163  +	obj->os = appfs_convert_os_fromString(values[3]);
          164  +	obj->cpuArch = appfs_convert_cpuArch_fromString(values[4]);
          165  +	if (values[5][0] == '1') {
          166  +		obj->isLatest = 1;
          167  +	} else {
          168  +		obj->isLatest = 0;
          169  +	}
          170  +
          171  +	if (*head == NULL) {
          172  +		obj->counter = 0;
          173  +	} else {
          174  +		obj->counter = (*head)->counter + 1;
          175  +	}
          176  +
          177  +	obj->_next = *head;
          178  +	*head = obj;
          179  +
          180  +	return(0);
          181  +}
   150    182   
   151    183   static struct appfs_package *appfs_getindex(const char *hostname, int *package_count_p) {
   152         -	int tcl_ret;
          184  +	struct appfs_package *head = NULL;
          185  +	char *sql;
          186  +	int tcl_ret, sqlite_ret;
   153    187   
   154    188   	if (package_count_p == NULL) {
   155    189   		return(NULL);
   156    190   	}
   157    191   
   158    192   	tcl_ret = appfs_Tcl_Eval(globalThread.interp, 2, "::appfs::getindex", hostname);
   159    193   	if (tcl_ret != TCL_OK) {
   160    194   		APPFS_DEBUG("Call to ::appfs::getindex failed: %s", Tcl_GetStringResult(globalThread.interp));
   161    195   
   162    196   		return(NULL);
   163    197   	}
   164    198   
   165         -	return(NULL);
          199  +	sql = sqlite3_mprintf("SELECT package, version, sha1, os, cpuArch, isLatest FROM packages WHERE hostname = %Q;", hostname);
          200  +	if (sql == NULL) {
          201  +		APPFS_DEBUG("Call to sqlite3_mprintf failed.");
          202  +
          203  +		return(NULL);
          204  +	}
          205  +
          206  +	sqlite_ret = sqlite3_exec(globalThread.db, sql, appfs_getindex_cb, &head, NULL);
          207  +	sqlite3_free(sql);
          208  +
          209  +	if (sqlite_ret != SQLITE_OK) {
          210  +		APPFS_DEBUG("Call to sqlite3_exec failed.");
          211  +
          212  +		return(NULL);
          213  +	}
          214  +
          215  +	if (head != NULL) {
          216  +		*package_count_p = head->counter + 1;
          217  +	}
          218  +
          219  +	return(head);
   166    220   }
   167    221   
   168    222   static int appfs_getfile(const char *hostname, const char *sha1) {
          223  +	return(0);
   169    224   }
   170    225   
   171    226   static int appfs_getmanifest(const char *hostname, const char *sha1) {
          227  +	return(0);
   172    228   }
   173    229   
   174    230   static int appfs_fuse_getattr(const char *path, struct stat *stbuf) {
          231  +	char *hostname;
   175    232   	int res = 0;
          233  +
          234  +	if (path == NULL) {
          235  +		return(-ENOENT);
          236  +	}
   176    237   
   177    238   	APPFS_DEBUG("Enter (path = %s, ...)", path);
          239  +
          240  +	if (path[0] != '/') {
          241  +		return(-ENOENT);
          242  +	}
   178    243   
   179    244   	memset(stbuf, 0, sizeof(struct stat));
   180    245   
   181    246   	stbuf->st_mode = S_IFDIR | 0755;
   182    247   	stbuf->st_nlink = 2;
   183    248   
   184    249   	return res;
................................................................................
   199    264   
   200    265   static int appfs_fuse_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
   201    266   	return(-ENOENT);
   202    267   }
   203    268   
   204    269   #ifdef APPFS_TEST_DRIVER
   205    270   static int appfs_test_driver(void) {
   206         -	struct appfs_package *packages;
          271  +	struct appfs_package *packages, *package;
   207    272   	int packages_count = 0;
   208    273   
   209    274   	packages = appfs_getindex("rkeene.org", &packages_count);
   210    275   	if (packages == NULL || packages_count == 0) {
   211    276   		fprintf(stderr, "Unable to fetch package index from rkeene.org.\n");
   212    277   
   213    278   		return(1);
   214    279   	}
          280  +
          281  +	for (package = packages; package; package = package->_next) {
          282  +		printf("Package:\n\tname = %s\n\tversion = %s\n\tsha1 = %s\n\tos = %s\n\tcpuArch = %s\n",
          283  +			package->name,
          284  +			package->version,
          285  +			package->sha1,
          286  +			appfs_convert_os_toString(package->os),
          287  +			appfs_convert_cpuArch_toString(package->cpuArch)
          288  +		);
          289  +	}
   215    290   
   216    291   	return(0);
   217    292   }
   218         -#endif
   219         -
          293  +#else
   220    294   static struct fuse_operations appfs_oper = {
   221    295   	.getattr	= appfs_fuse_getattr,
   222    296   	.readdir	= appfs_fuse_readdir,
   223    297   	.open		= appfs_fuse_open,
   224    298   	.read		= appfs_fuse_read
   225    299   };
          300  +#endif
   226    301   
   227    302   int main(int argc, char **argv) {
   228    303   	const char *cachedir = APPFS_CACHEDIR;
   229    304   	char dbfilename[1024];
   230    305   	int tcl_ret, snprintf_ret, sqlite_ret;
   231    306   
   232    307   	globalThread.interp = Tcl_CreateInterp();