Check-in [a7c7a7a363]
Overview
Comment:Implemented basic open() and read()
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a7c7a7a363cffe0f1037e6b125b9bd76e4f0bf6c
User & Date: rkeene on 2014-09-09 06:46:54
Other Links: manifest | tags
Context
2014-09-09
07:33
Updated to create separate interpreters per thread check-in: cf4ca88d48 user: rkeene tags: trunk
06:46
Implemented basic open() and read() check-in: a7c7a7a363 user: rkeene tags: trunk
06:46
Updated to download files as binary check-in: d0513156ec user: rkeene tags: trunk
Changes

Modified appfs.c from [62dbc078fa] to [4ed6fa78a6].

     1      1   #define FUSE_USE_VERSION 26
     2      2   
            3  +#include <sys/types.h>
     3      4   #include <sqlite3.h>
     4      5   #include <string.h>
     5      6   #include <stdarg.h>
     6      7   #include <stdlib.h>
            8  +#include <unistd.h>
     7      9   #include <errno.h>
     8     10   #include <fcntl.h>
     9     11   #include <stdio.h>
    10     12   #include <fuse.h>
    11     13   #include <tcl.h>
    12     14   
    13     15   #define APPFS_CACHEDIR "/tmp/appfs-cache"
................................................................................
    71     73   
    72     74   	char name[256];
    73     75   };
    74     76   
    75     77   struct appfs_pathinfo {
    76     78   	appfs_pathtype_t type;
    77     79   	time_t time;
           80  +	char hostname[256];
    78     81   	union {
    79     82   		struct {
    80     83   			int childcount;
    81     84   		} dir;
    82     85   		struct {
    83     86   			int executable;
    84     87   			off_t size;
           88  +			char sha1[41];
    85     89   		} file;
    86     90   	} typeinfo;
    87     91   };
    88     92   
    89     93   static appfs_os_t appfs_convert_os_fromString(const char *os) {
    90     94   	if (strcasecmp(os, "Linux") == 0) {
    91     95   		return(APPFS_OS_LINUX);
................................................................................
   203    207   
   204    208   		return(-1);
   205    209   	}
   206    210   
   207    211   	return(0);
   208    212   }
   209    213   
   210         -static int appfs_getfile(const char *hostname, const char *sha1) {
   211         -	return(0);
          214  +static const char *appfs_getfile(const char *hostname, const char *sha1) {
          215  +	char *retval;
          216  +	int tcl_ret;
          217  +
          218  +	tcl_ret = appfs_Tcl_Eval(globalThread.interp, 3, "::appfs::download", hostname, sha1);
          219  +	if (tcl_ret != TCL_OK) {
          220  +		APPFS_DEBUG("Call to ::appfs::download failed: %s", Tcl_GetStringResult(globalThread.interp));
          221  +
          222  +		return(NULL);
          223  +	}
          224  +
          225  +	retval = strdup(Tcl_GetStringResult(globalThread.interp));
          226  +
          227  +	return(retval);
   212    228   }
   213    229   
   214    230   static int appfs_update_manifest(const char *hostname, const char *sha1) {
   215    231   	int tcl_ret;
   216    232   
   217    233   	tcl_ret = appfs_Tcl_Eval(globalThread.interp, 3, "::appfs::getpkgmanifest", hostname, sha1);
   218    234   	if (tcl_ret != TCL_OK) {
................................................................................
   474    490   		if (!size) {
   475    491   			size = "0";
   476    492   		}
   477    493   
   478    494   		if (!perms) {
   479    495   			perms = "";
   480    496   		}
          497  +
          498  +		if (!sha1) {
          499  +			sha1 = "";
          500  +		}
   481    501   
   482    502   		pathinfo->typeinfo.file.size = strtoull(size, NULL, 10);
          503  +		snprintf(pathinfo->typeinfo.file.sha1, sizeof(pathinfo->typeinfo.file.sha1), "%s", sha1);
   483    504   
   484    505   		if (strcmp(perms, "x") == 0) {
   485    506   			pathinfo->typeinfo.file.executable = 1;
   486    507   		} else {
   487    508   			pathinfo->typeinfo.file.executable = 0;
   488    509   		}
   489    510   
................................................................................
   590    611   
   591    612   	if (_path[1] == '\0') {
   592    613   		/* Request for the root directory */
   593    614   		sites = appfs_getsites(&sites_count);
   594    615   
   595    616   		pathinfo->type = APPFS_PATHTYPE_DIRECTORY;
   596    617   		pathinfo->typeinfo.dir.childcount = sites_count;
          618  +		pathinfo->hostname[0] = '\0';
   597    619   
   598    620   		if (children) {
   599    621   			for (site = sites; site; site = site->_next) {
   600    622   				node = (void *) ckalloc(sizeof(*node));
   601    623   				node->_next = *children;
   602    624   				strcpy(node->name, site->name);
   603    625   				*children = node;
................................................................................
   615    637   	hostname = path + 1;
   616    638   	packagename = strchr(hostname, '/');
   617    639   
   618    640   	if (packagename != NULL) {
   619    641   		*packagename = '\0';
   620    642   		packagename++;
   621    643   	}
          644  +
          645  +	snprintf(pathinfo->hostname, sizeof(pathinfo->hostname), "%s", hostname);
   622    646   
   623    647   	packages = appfs_getindex(hostname, &packages_count);
   624    648   
   625    649   	if (packages == NULL || packages_count == 0) {
   626    650   		APPFS_DEBUG("Unable to fetch package index from %s", hostname);
   627    651   
   628    652   		free(path_s);
................................................................................
   811    835   	memset(stbuf, 0, sizeof(struct stat));
   812    836   
   813    837   	stbuf->st_mtime = pathinfo.time;
   814    838   	stbuf->st_ctime = pathinfo.time;
   815    839   	stbuf->st_atime = pathinfo.time;
   816    840   
   817    841   	if (pathinfo.type == APPFS_PATHTYPE_DIRECTORY) {
   818         -		stbuf->st_mode = S_IFDIR | 0755;
          842  +		stbuf->st_mode = S_IFDIR | 0555;
   819    843   		stbuf->st_nlink = 2 + pathinfo.typeinfo.dir.childcount;
   820    844   	} else {
   821    845   		if (pathinfo.typeinfo.file.executable) {
   822         -			stbuf->st_mode = S_IFREG | 0755;
          846  +			stbuf->st_mode = S_IFREG | 0555;
   823    847   		} else {
   824         -			stbuf->st_mode = S_IFREG | 0644;
          848  +			stbuf->st_mode = S_IFREG | 0444;
   825    849   		}
   826    850   
   827    851   		stbuf->st_nlink = 1;
   828    852   		stbuf->st_size = pathinfo.typeinfo.file.size;
   829    853   	}
   830    854   
   831    855   	return res;
   832    856   }
   833    857   
   834    858   static int appfs_fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) {
   835    859   	struct appfs_pathinfo pathinfo;
   836    860   	struct appfs_children *children, *child;
   837         -	int res;
          861  +	int retval;
   838    862   
   839    863   	APPFS_DEBUG("Enter (path = %s, ...)", path);
   840    864   
   841         -	res = appfs_get_path_info(path, &pathinfo, &children);
   842         -	if (res != 0) {
   843         -		return(res);
          865  +	retval = appfs_get_path_info(path, &pathinfo, &children);
          866  +	if (retval != 0) {
          867  +		return(retval);
   844    868   	}
   845    869   
   846    870   	filler(buf, ".", NULL, 0);
   847    871   	filler(buf, "..", NULL, 0);
   848    872   
   849    873   	for (child = children; child; child = child->_next) {
   850    874   		filler(buf, child->name, NULL, 0);
   851    875   	}
   852    876   
   853    877   	appfs_free_list_children(children);
   854    878   
   855         -	return 0;
          879  +	return(0);
   856    880   }
   857    881   
   858    882   static int appfs_fuse_open(const char *path, struct fuse_file_info *fi) {
   859         -	return(-ENOENT);
          883  +	struct appfs_pathinfo pathinfo;
          884  +	const char *real_path;
          885  +	int fh;
          886  +	int gpi_ret;
          887  +
          888  +	APPFS_DEBUG("Enter (path = %s, ...)", path);
          889  +
          890  +	if ((fi->flags & 3) != O_RDONLY) {
          891  +                return(-EACCES);
          892  +	}
          893  +
          894  +	gpi_ret = appfs_get_path_info(path, &pathinfo, NULL);
          895  +	if (gpi_ret != 0) {
          896  +		return(gpi_ret);
          897  +	}
          898  +
          899  +	if (pathinfo.type == APPFS_PATHTYPE_DIRECTORY) {
          900  +		return(-EISDIR);
          901  +	}
          902  +
          903  +	real_path = appfs_getfile(pathinfo.hostname, pathinfo.typeinfo.file.sha1);
          904  +	if (real_path == NULL) {
          905  +		return(-EIO);
          906  +	}
          907  +
          908  +	fh = open(real_path, O_RDONLY);
          909  +	free((void *) real_path);
          910  +	if (fh < 0) {
          911  +		return(-EIO);
          912  +	}
          913  +
          914  +	fi->fh = fh;
          915  +
          916  +	return(0);
   860    917   }
   861    918   
   862    919   static int appfs_fuse_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
   863         -	return(-ENOENT);
          920  +	off_t lseek_ret;
          921  +	ssize_t read_ret;
          922  +
          923  +	APPFS_DEBUG("Enter (path = %s, ...)", path);
          924  +
          925  +	lseek_ret = lseek(fi->fh, offset, SEEK_SET);
          926  +	if (lseek_ret != offset) {
          927  +		return(-EIO);
          928  +	}
          929  +
          930  +	read_ret = read(fi->fh, buf, size);
          931  +
          932  +	return(read_ret);
   864    933   }
   865    934   
   866    935   #ifdef APPFS_TEST_DRIVER
   867    936   static int appfs_test_driver(void) {
   868    937   	struct appfs_site *sites, *site;
   869    938   	struct appfs_package *packages, *package;
   870    939   	struct appfs_children *files, *file;