예제 #1
0
파일: kpfs_conf.c 프로젝트: liqingqiya/kpfs
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;
}
예제 #2
0
파일: kpfs_node.c 프로젝트: wyingquan/kpfs
kpfs_node *kpfs_node_get_by_path(kpfs_node * node, const char *path)
{
	kpfs_node *ret = NULL;
	GHashTableIter iter;

	if (NULL == path || NULL == node)
		return NULL;

	if (1 == strlen(path) && path[0] == '/')
		return kpfs_node_root_get();

	ret = g_hash_table_lookup(node->sub_nodes, path);
	if (NULL == ret) {
		char *key = NULL;
		kpfs_node *value = NULL;

		KPFS_LOG("[%s:%d] could not find %s in node->sub_nodes.\n", __FUNCTION__, __LINE__, path);
		g_hash_table_iter_init(&iter, node->sub_nodes);
		while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value)) {
			KPFS_LOG("[%s:%d] iter, key: %s, path: %s\n", __FUNCTION__, __LINE__, key, path);
			ret = kpfs_node_get_by_path(value, path);
			if (ret)
				return ret;
		}
	} else {
		return ret;
	}
	return ret;
}
예제 #3
0
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;
}
예제 #4
0
파일: kpfs_conf.c 프로젝트: liqingqiya/kpfs
void kpfs_conf_dump()
{
	KPFS_LOG("kpfs conf:\n");
	KPFS_LOG("\tconsumer_key: %s\n", g_kpfs_conf.consumer_key);
	KPFS_LOG("\tconsumer_secret: %s\n", g_kpfs_conf.consumer_secret);
	KPFS_LOG("\troot: %s\n", g_kpfs_conf.root);
	KPFS_LOG("\tmount_point: %s\n", g_kpfs_conf.mount_point);
	KPFS_LOG("\toauth_json_file: %s\n", g_kpfs_conf.oauth_json_file);
	KPFS_LOG("\twritable_tmp_path: %s\n", g_kpfs_conf.writable_tmp_path);
}
예제 #5
0
파일: kpfs_node.c 프로젝트: wyingquan/kpfs
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);
						}
					}
				}
			}
		}