int zht_remove(const char *key)
{
//	return c_zht_remove2(key);

	Package package = PACKAGE__INIT; // Package
	package.virtualpath = (char*)key;
	package.realfullpath = "";
	package.has_operation = true;
	package.operation = 2; //1 for look up, 2 for remove, 3 for insert

	char *buf; // Buffer to store serialized data
	unsigned len; // Length of serialized data

	len = package__get_packed_size(&package);
	buf = (char*) calloc(len, sizeof(char));
	package__pack(&package, (uint8_t *)buf);

	int ret = c_zht_remove(buf);
	if (ret)
		fprintf(stderr, "c_zht_remove, return code %d\n", ret);

	free(buf); // Free the allocated serialized buffer

	return 0;
}
/**
 * Desc: get the open mode of the file:
 * Return: 000 - not open; 001 - read only; 010 - write only; 011 - read and write; -1 - failed
 */
int zht_get_openmode(const char *virtualfname)
{
	char *buf; // Buffer to store serialized data
	unsigned len; // Length of serialized data
	char result[ZHT_MAX_BUFF] = {0}; //return result
	size_t ln; //return length
	int rtn;

	Package package = PACKAGE__INIT; // Package
	package.virtualpath = (char*)virtualfname;
	package.realfullpath = "";
	package.has_operation = true;
	package.operation = 1; //1 for look up, 2 for remove, 3 for insert

	len = package__get_packed_size(&package);
	buf = (char*) calloc(len, sizeof(char));
	package__pack(&package, (uint8_t *)buf);

	int lret = c_zht_lookup(buf, result, &ln);
	if (lret == 0 && ln > 0) {
		Package *lkPackage;
		lkPackage = package__unpack(NULL, ln, (const uint8_t *)result);

		if (lkPackage == NULL) {
			fprintf(stderr, "error unpacking lookup result\n");
		}

		rtn = lkPackage->openmode;
		package__free_unpacked(lkPackage, NULL);
	}

	free(buf); // Free the allocated serialized buffer

	return rtn;
}
int zht_lookup(const char *key, char *val)
{
//	return c_zht_lookup2(key, val);
//	size_t len;
//	return c_zht_lookup2(key, val, &len);

	char *buf; // Buffer to store serialized data
	unsigned len; // Length of serialized data
	char result[ZHT_MAX_BUFF] = {0}; //return result
	size_t ln; //return length

	Package package = PACKAGE__INIT; // Package
	package.virtualpath = (char*)key;
//	package.realfullpath = "";
//	package.has_operation = true;
	package.operation = 1; //1 for look up, 2 for remove, 3 for insert

	len = package__get_packed_size(&package);
	buf = (char*) calloc(len, sizeof(char));
	package__pack(&package, (uint8_t *)buf);

	int lret = c_zht_lookup(buf, result, &ln);
	if (lret == 0 && ln > 0) {
		Package *lkPackage;
//		char *lkBuf = (char*) calloc(ln, sizeof(char));
//		char lkBuf[ln + 1] = {0};
//		strncpy(lkBuf, result, ln);
		lkPackage = package__unpack(NULL, ln, (const uint8_t *)result);

		if (lkPackage == NULL) {
			fprintf(stderr, "error unpacking lookup result\n");
		}
//		else {
//			fprintf(stderr,
//					"c_zht_lookup, return {key}:{value} ==>\n{%s}:{%s}\n",
//					lkPackage->virtualpath, lkPackage->realfullpath);
//		}

//		free(lkBuf);

		strcpy(val, lkPackage->realfullpath);
		package__free_unpacked(lkPackage, NULL);
	}

	//fprintf(stderr, "dfz debug: in zht_lookup(): val = %s. \n", val);
	log_msg("DFZ debug: in zht_lookup(): key = %s, val = %s. \n\n", key, val);

	free(buf); // Free the allocated serialized buffer

	return 0;
}
Example #4
0
extern int _c_zht_insert2(char *key, char *value)
{
    char *buf;
    unsigned len;

    Package package = PACKAGE__INIT;
    package.virtualpath = key;
    if (strcmp(value, "") != 0) //tricky: bypass protocol-buf's bug
	package.realfullpath = value;
    package.has_isdir = true;
    package.isdir = false;
    package.has_operation = true;
    package.operation = 3;

    len = package__get_packed_size(&package);
    buf = (char*) calloc(len, sizeof(char));
    package__pack(&package, buf);
    int iret = c_zht_insert(buf);
    free(buf);
    return iret;
}
Example #5
0
extern void _c_zht_lookup2(char *key, char *ret)
{
    Package package2 = PACKAGE__INIT;
    package2.virtualpath = key;
    package2.operation = 1;
    unsigned len = package__get_packed_size(&package2);
    char *buf = (char*) calloc(len, sizeof(char));
    package__pack(&package2, buf);
    size_t ln;
    char *result = (char*)calloc(1024 * 10, sizeof(char));
    if (result != NULL)
    {
	int lret = c_zht_lookup(buf, result, &ln);
	free(buf);
	if (lret == 0 && ln > 0)
	{
	    //printf("OK, seems fine!\n");
	    Package * lPackage;
	    char *lBuf = (char*) calloc(ln, sizeof(char));
	    strncpy(lBuf, result, ln);
	    lPackage = package__unpack(NULL, ln, lBuf);
	    free(lBuf); free(result);
	    if (lPackage == NULL)
	    {
		printf("error unpacking lookup result\n");
	    }
	    else
	    {
		//printf("The value is:%s\n", lPackage->realfullpath);
		strcpy(ret, lPackage->realfullpath);
	    }
	}
	else
	{
	    free(result);
	}
    }
}
int zht_insert(const char *key, const char *value)
{

//	fprintf(stderr, "dfz debug: in zht_insert(): key = %s, value = %s. \n", key, value);
	log_msg("DFZ debug: in zht_insert(): key = %s, value = %s. \n\n", key, value);

	//	return c_zht_insert2(key, value);
	Package package = PACKAGE__INIT; // Package
	package.virtualpath = (char*)key;
	package.realfullpath = (char*)value;
	package.has_isdir = true;
	package.isdir = false;
	package.has_operation = true;
	package.operation = 3; //1 for look up, 2 for remove, 3 for insert

	char *buf; // Buffer to store serialized data
	unsigned len; // Length of serialized data

	len = package__get_packed_size(&package);
	buf = (char*) calloc(len, sizeof(char));
	package__pack(&package, (uint8_t *)buf);

	int ret = c_zht_insert(buf);
	if (ret)
		fprintf(stderr, "c_zht_insert, return code %d. \n", ret);

	/* test ZHT insert */
//	char tmpstr[ZHT_MAX_BUFF] = {0};
//	zht_lookup(key, tmpstr);
//	fprintf(stderr, "dfz debug: tmpstr = %s. \n", tmpstr);


	free(buf); // Free the allocated serialized buffer

	return 0;
}
void test_pass_package() {

	char *key = "hello";
	char *value = "zht";

	Package package = PACKAGE__INIT; // Package
	package.virtualpath = key;
	package.realfullpath = value;
	package.has_isdir = true;
	package.isdir = true;
	package.has_operation = true;
	package.operation = 3; //1 for look up, 2 for remove, 3 for insert

	char *buf; // Buffer to store serialized data
	unsigned len; // Length of serialized data

	len = package__get_packed_size(&package);
	buf = (char*) calloc(len, sizeof(char));
	package__pack(&package, buf);

	/*
	 * test c_zht_insert
	 * */
	int iret = c_zht_insert(buf);
	fprintf(stderr, "c_zht_insert, return code: %d\n", iret);

	/*
	 * test c_zht_lookup
	 * */
	memset(buf, 0, len);
	package.operation = 1; //1 for look up, 2 for remove, 3 for insert
	package.realfullpath = "";
	package__pack(&package, buf);

	size_t ln;
	char *result = (char*) calloc(LOOKUP_SIZE, sizeof(char));
	if (result != NULL) {
		int lret = c_zht_lookup(buf, result, &ln);
		fprintf(stderr, "c_zht_lookup, return code(length): %d(%lu)\n", lret,
				ln);

		if (lret == 0 && ln > 0) {
			Package *lkPackage;
			char *lkBuf = (char*) calloc(ln, sizeof(char));

			strncpy(lkBuf, result, ln);
			lkPackage = package__unpack(NULL, ln, lkBuf);

			if (lkPackage == NULL) {
				fprintf(stderr, "error unpacking lookup result\n");
			} else {
				fprintf(stderr,
						"c_zht_lookup, return {key}:{value} ==>\n{%s}:{%s}\n",
						lkPackage->virtualpath, lkPackage->realfullpath);
			}

			free(lkBuf);
			package__free_unpacked(lkPackage, NULL);
		}
	}
	free(result);

	/*
	 * test c_zht_remove
	 * */
	memset(buf, 0, len);
	package.operation = 2; //1 for look up, 2 for remove, 3 for insert
	package__pack(&package, buf);

	int rret = c_zht_remove(buf);
	fprintf(stderr, "c_zht_remove, return code: %d\n", rret);

	free(buf); // Free the allocated serialized buffer
}
int main(int argc, char *argv[]) {
	int i;
	int fuse_stat;
	struct fusion_state *fusion_data;

	// fusionfs doesn't do any access checking on its own (the comment
	// blocks in fuse.h mention some of the functions that need
	// accesses checked -- but note there are other functions, like
	// chown(), that also need checking!).  Since running fusionfs as root
	// will therefore open Metrodome-sized holes in the system
	// security, we'll check if root is trying to mount the filesystem
	// and refuse if it is.  The somewhat smaller hole of an ordinary
	// user doing it with the allow_other flag is still there because
	// I don't want to parse the options string.
	if ((getuid() == 0) || (geteuid() == 0)) {
		fprintf(stderr,
				"Running BBFS as root opens unnacceptable security holes\n");
		return 1;
	}

	fusion_data = calloc(sizeof(struct fusion_state), 1);
	if (fusion_data == NULL) {
		perror("main calloc");
		abort();
	}

	fusion_data->logfile = log_open();

	// libfuse is able to do most of the command line parsing; all I
	// need to do is to extract the rootdir; this will be the first
	// non-option passed in.  I'm using the GNU non-standard extension
	// and having realpath malloc the space for the path
	// the string.
	for (i = 1; (i < argc) && (argv[i][0] == '-'); i++)
		if (argv[i][1] == 'o')
			i++; // -o takes a parameter; need to
	// skip it too.  This doesn't
	// handle "squashed" parameters

	if ((argc - i) != 2)
		fusion_usage();

	fusion_data->rootdir = realpath(argv[i], NULL);

	argv[i] = argv[i + 1];
	argc--;

	/*
	 * DFZ: test gbuf starts
	 */
	printf("\n=====Start testing Google Protocol Buffer=====\n\n");
	size_t size = 0, size2 = 0;

	Package pkg = PACKAGE__INIT;
	Package *pkg_rtn;

	/* test data */
	pkg.has_replicano = 1; /* This is in need if this int32 is optional */
	pkg.replicano = 5;
	pkg.has_operation = 1;
	pkg.operation = 3;
	pkg.virtualpath = "mykey";
	pkg.realfullpath = "mypathname";
	pkg.has_isdir = true;
	pkg.isdir = false;

	/* pack the data */
	size = package__get_packed_size(&pkg);
	unsigned char *packed = malloc(size);
	size2 = package__pack(&pkg, packed);
	assert(size == size2);

	printf("\n=====DFZ debug gbuf: packed = %s. \n\n", packed);

	/* unpack the data */
	pkg_rtn = package__unpack(NULL, size, packed);

	/* verify the matchings */
	printf("dfz debug: pkg_rtn->replicano = %d \n", pkg_rtn->replicano);
	printf("dfz debug: pkg_rtn->operation = %d \n", pkg_rtn->operation);
	printf("dfz debug: pkg_rtn->virtualpath = %s \n", pkg_rtn->virtualpath);
	printf("dfz debug: pkg_rtn->realfullpath = %s \n", pkg_rtn->realfullpath);

	package__free_unpacked(pkg_rtn, NULL);
	free(packed);
	printf("\n=====Finish testing Google Protocol Buffer=====\n\n");
	/*
	 *  DFZ: test gbuf ends
	 */

	/* DFZ: initialize the hash table */
	zht_init();
	
	/*
		initialize new spade
	*/	
	fprintf(stderr, "init spade...\n");
	if (spade_init() < 0) 
	{
		perror("spade failed");
		return -1;
	}

	printf("spade done \n");
	
	/*DFZ: add root dir in ZHT*/
	zht_insert("/", " ");

	/*DFZ: test GPU erasure
	printf("start testing GPU erasure.\n");
	gib_context gc;
	int m = 2, n = 2;
	int rc = gib_init(m, n, &gc);
	if (rc)
	{
		printf("error in gib_init.\n");
		exit (1);
	}
	int bs = 1024 * 1024;
	void *data, *dense_data;
	gib_alloc(&data, bs, &bs, gc);
	gib_generate(data, bs, gc);
	char buff_id[256] = {'1'};
	gib_recover(data, bs, buff_id, m, gc);
	gib_free(data, gc);
	gib_destroy(gc);
	printf("GPU erasure successful. \n");
	DFZ: end of GPU erasure test*/

	fprintf(stderr, "about to call fuse_main\n");
	fuse_stat = fuse_main(argc, argv, &fusion_oper, fusion_data);
	fprintf(stderr, "fuse_main returned %d\n", fuse_stat);

	/* DFZ: destruct the hash table */
	zht_free();
	spade_close();

	return fuse_stat;
}