Beispiel #1
0
kpfs_ret kpfs_conf_load(char *file)
{
	struct stat st;
	int fd = 0;
	char *buf = NULL;
	int ret = 0;

	if (NULL == file)
		return KPFS_RET_FAIL;

	if (0 != stat(file, &st)) {
		return KPFS_RET_FAIL;
	}
	fd = open(file, O_RDONLY);
	if (-1 == fd) {
		KPFS_LOG("fail to open file (%s)\n", file);
		return KPFS_RET_FAIL;
	}

	buf = calloc(st.st_size, 1);
	ret = read(fd, buf, st.st_size);
	if (-1 == ret) {
		KPFS_LOG("fail to read file (%s)\n", file);
	}
	close(fd);
	if (KPFS_RET_FAIL == kpfs_conf_parse_json(buf)) {
		KPFS_LOG("fail to load correct conf params from (%s).\n", file);
		KPFS_SAFE_FREE(buf);
		return KPFS_RET_FAIL;
	}
	KPFS_SAFE_FREE(buf);
	return KPFS_RET_OK;
}
Beispiel #2
0
int kpfs_node_get_root_path()
{
	char *response = NULL;
	json_object *jobj = NULL;
	off_t quota_used = 0;
	long quota_total = 0;
	off_t max_file_size = 0;
	int len = 512;
	char *user_name = NULL;
	char *user_id = NULL;
	char *p = NULL;

	response = (char *)kpfs_api_account_info();
	KPFS_FILE_LOG("access [/] %s:\n", response);

	p = strstr(response, KPFS_ID_QUOTA_TOTAL);
	if (p) {
		sscanf(p, KPFS_ID_QUOTA_TOTAL "\": " CURL_FORMAT_OFF_T ",", &quota_total);
	}

	jobj = json_tokener_parse(response);
	if (NULL == jobj || is_error(jobj)) {
		KPFS_FILE_LOG("%s:%d, json_tokener_parse return error.\n", __FUNCTION__, __LINE__);
		KPFS_SAFE_FREE(response);
		return -1;
	}
	json_object_object_foreach(jobj, key, val) {
		if (!strcmp(key, KPFS_ID_QUOTA_USED)) {
			quota_used = (off_t) json_object_get_double(val);;
		} else if (!strcmp(key, KPFS_ID_MAX_FILE_SIZE)) {
			if (json_type_int == json_object_get_type(val)) {
				max_file_size = (off_t) json_object_get_int(val);
			}
		} else if (!strcmp(key, KPFS_ID_USER_ID)) {
			if (json_type_int == json_object_get_type(val)) {
				user_id = calloc(len, 1);
				snprintf(user_id, len, "%d", json_object_get_int(val));
			}
		} else if (!strcmp(key, KPFS_ID_USER_NAME)) {
			if (json_type_string == json_object_get_type(val)) {
				user_name = calloc(len, 1);
				snprintf(user_name, len, "%s", json_object_get_string(val));
			}
		}
	}
	json_object_put(jobj);
	KPFS_SAFE_FREE(response);

	kpfs_util_account_info_store(user_name, user_id, quota_total, quota_used, max_file_size);

	if (NULL == kpfs_node_root_create(user_id, user_name, quota_used))
		return -1;

	return 0;
}
Beispiel #3
0
void kpfs_node_free(gpointer p)
{
	kpfs_node *node = (kpfs_node *) p;

	if (NULL == p)
		return;

	KPFS_SAFE_FREE(node->fullpath);
	KPFS_SAFE_FREE(node->id);
	KPFS_SAFE_FREE(node->name);
	KPFS_SAFE_FREE(node->revision);
	pthread_mutex_destroy(&(node->mutex));
}
int main(int argc, char *argv[])
{
	kpfs_ret ret = KPFS_RET_OK;
	struct stat st;
	char *response = NULL;

	if (argc < 5) {
		kpfs_test_usage(argv[0]);
		return -1;
	}
	if (0 == strcmp("-c", argv[1])) {
		if (argv[2] == '\0') {
			kpfs_test_usage(argv[0]);
			return -1;
		}
		if (0 != stat(argv[2], &st)) {
			printf("config file of kpfs (%s) is not existed.\n", argv[2]);
			kpfs_test_usage(argv[0]);
			return -1;
		}
		if (KPFS_RET_OK != kpfs_conf_load(argv[2])) {
			kpfs_test_usage(argv[0]);
			return -1;
		}
	} else {
		kpfs_test_usage(argv[0]);
		return -1;
	}

	kpfs_oauth_init();

	ret = kpfs_oauth_load();
	if (KPFS_RET_OK != ret) {
		printf("This is the first time you use kpfs.\n");
		ret = kpfs_oauth_request_token();
		if (KPFS_RET_OK != ret) {
			return -1;
		}
		ret = kpfs_oauth_authorize();
		if (KPFS_RET_OK != ret) {
			return -1;
		}
		ret = kpfs_oauth_access_token();
		if (KPFS_RET_OK != ret) {
			KPFS_LOG("Fail to get oauth token for kpfs.\n");
			return -1;
		}
	}

	curl_global_init(CURL_GLOBAL_ALL);

	response = (char *)kpfs_api_upload_file(argv[3], argv[4]);
	printf("%s\n", response);
	KPFS_SAFE_FREE(response);
	return ret;
}
Beispiel #5
0
void kpfs_node_free_sub_nodes(GHashTable * sub_nodes)
{
	GHashTableIter iter;
	char *key = NULL;
	kpfs_node *value = NULL;

	if (NULL == sub_nodes)
		return;

	g_hash_table_iter_init(&iter, sub_nodes);
	while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value)) {
		if (value) {
			kpfs_node_free_sub_nodes(value->sub_nodes);
			g_hash_table_destroy(value->sub_nodes);
			kpfs_node_free(value);
			KPFS_SAFE_FREE(value);
		}
	}
}
Beispiel #6
0
kpfs_ret kpfs_node_parse_dir(kpfs_node * parent_node, const char *path)
{
	char *response = NULL;
	json_object *jobj;
	json_object *tmp_jobj;
	kpfs_node *node = NULL;
	int len = 0;
	char *parent_path = NULL;
	kpfs_ret ret = KPFS_RET_FAIL;

	if (NULL == parent_node)
		return KPFS_RET_FAIL;

	response = (char *)kpfs_api_metadata(path);
	KPFS_LOG("response %s:\n", response);

	jobj = json_tokener_parse(response);
	if (NULL == jobj || is_error(jobj)) {
		KPFS_FILE_LOG("%s:%d, json_tokener_parse return error.\n", __FUNCTION__, __LINE__);
		KPFS_SAFE_FREE(response);
		return KPFS_RET_FAIL;
	}

	json_object_object_foreach(jobj, key, val) {
		if (!strcmp(key, KPFS_ID_MSG)) {
			if (json_type_string == json_object_get_type(val)) {
				if (0 == strcmp(KPFS_MSG_STR_REUSED_NONCE, json_object_get_string(val))) {
					KPFS_FILE_LOG("%s:%d, receive reused nonce.\n", __FUNCTION__, __LINE__);
					goto error_out;
				}
			}
		} else if (!strcmp(key, KPFS_ID_PATH)) {
		} else if (!strcmp(key, KPFS_ID_ROOT)) {
			if (json_type_string == json_object_get_type(val)) {
			}
		} else if (!strcmp(key, KPFS_ID_HASH)) {
		} else if (!strcmp(key, KPFS_ID_FILES)) {
			if (json_type_array == json_object_get_type(val)) {
				int j = 0, array_len = 0;

				array_len = json_object_array_length(val);
				for (j = 0; j < array_len; j++) {
					tmp_jobj = json_object_array_get_idx(val, j);
					if (tmp_jobj && !is_error(tmp_jobj)) {
						node = calloc(sizeof(kpfs_node), 1);
						if (NULL == node) {
							KPFS_FILE_LOG("%s:%d, fail to calloc node.\n", __FUNCTION__, __LINE__);
							goto error_out;
						}
						errno = pthread_mutex_init(&(node->mutex), NULL);
						if (errno) {
							KPFS_FILE_LOG("%s:%d, pthread_mutex_init fail.\n", __FUNCTION__, __LINE__);
							goto error_out;
						}
						node->sub_nodes = g_hash_table_new(g_str_hash, g_str_equal);
						len = strlen(path) + 1;
						parent_path = calloc(len, 1);
						snprintf(parent_path, len, "%s", path);

						KPFS_FILE_LOG("\n", __FUNCTION__, __LINE__);
						json_object_object_foreach(tmp_jobj, key2, val2) {
							if (!strcmp(key2, KPFS_ID_IS_DELETED)) {
								if (json_type_boolean == json_object_get_type(val2)) {
									node->is_deleted = json_object_get_boolean(val2) == TRUE ? 1 : 0;
									KPFS_FILE_LOG("%s:%d, is_deleted: %d.\n", __FUNCTION__, __LINE__, node->is_deleted);
								}
							} else if (!strcmp(key2, KPFS_ID_NAME)) {
								if (json_type_string == json_object_get_type(val2)) {
									node->name = strdup(json_object_get_string(val2));
									KPFS_FILE_LOG("%s:%d, name: %s.\n", __FUNCTION__, __LINE__, node->name);
								}
							} else if (!strcmp(key2, KPFS_ID_REV)) {
								if (json_type_string == json_object_get_type(val2)) {
									node->revision = strdup(json_object_get_string(val2));
									KPFS_FILE_LOG("%s:%d, revision: %s.\n", __FUNCTION__, __LINE__,
										      json_object_get_string(val2));
								}
							} else if (!strcmp(key2, KPFS_ID_CREATE_TIME)) {
								if (json_type_string == json_object_get_type(val2)) {
									char str[128] = { 0 };
									snprintf(str, sizeof(str), "%s", json_object_get_string(val2));
									node->st.st_ctime = kpfs_node_str2time(str);
									KPFS_FILE_LOG("%s:%d, ctime: %d.\n", __FUNCTION__, __LINE__, node->st.st_ctime);
								}
							} else if (!strcmp(key2, KPFS_ID_MODIFY_TIME)) {
								if (json_type_string == json_object_get_type(val2)) {
									char str[128] = { 0 };
									snprintf(str, sizeof(str), "%s", json_object_get_string(val2));
									node->st.st_mtime = kpfs_node_str2time(str);
									KPFS_FILE_LOG("%s:%d, mtime: %d.\n", __FUNCTION__, __LINE__, node->st.st_mtime);
								}
							} else if (!strcmp(key2, KPFS_ID_SIZE)) {
								if (json_type_int == json_object_get_type(val2)) {
									node->st.st_size = json_object_get_int(val2);
									KPFS_FILE_LOG("%s:%d, size: %d.\n", __FUNCTION__, __LINE__, node->st.st_size);
								}
							} else if (!strcmp(key2, KPFS_ID_TYPE)) {
								if (json_type_string == json_object_get_type(val2)) {
									if (0 == strcasecmp(KPFS_NODE_TYPE_STR_FILE, json_object_get_string(val2))) {
										node->type = KPFS_NODE_TYPE_FILE;
									} else {
										node->type = KPFS_NODE_TYPE_FOLDER;
									}
									KPFS_FILE_LOG("%s:%d, type: %d.\n", __FUNCTION__, __LINE__, node->type);
								}
							} else if (!strcmp(key2, KPFS_ID_FILE_ID)) {
								if (json_type_string == json_object_get_type(val2)) {
									node->id = strdup(json_object_get_string(val2));
									KPFS_FILE_LOG("%s:%d, id: %s.\n", __FUNCTION__, __LINE__, node->id);
								}
							}
						}
						if (node->name) {
							if (1 == strlen(parent_path) && parent_path[0] == '/') {
								len = strlen(parent_path) + strlen(node->name) + 1;
								node->fullpath = calloc(len, 1);
								snprintf(node->fullpath, len, "%s%s", parent_path, node->name);
							} else {
								len = strlen(parent_path) + strlen("/") + strlen(node->name) + 1;
								node->fullpath = calloc(len, 1);
								snprintf(node->fullpath, len, "%s/%s", parent_path, node->name);
							}
							KPFS_FILE_LOG("%s:%d, fullpath: %s.\n", __FUNCTION__, __LINE__, node->fullpath);
							if (KPFS_NODE_TYPE_FOLDER == node->type) {
								kpfs_node_parse_dir(node, node->fullpath);
								node->st.st_mode = S_IFDIR | 0755;
								node->st.st_nlink = 2;
								if (0 == node->st.st_size)
									node->st.st_size = getpagesize();
							} else {
								node->st.st_mode = S_IFREG | 0666;
								node->st.st_nlink = 1;
							}

							KPFS_NODE_LOCK(parent_node);
							g_hash_table_insert(parent_node->sub_nodes, node->fullpath, node);
							KPFS_NODE_UNLOCK(parent_node);
						}
					}
				}
			}
		}