Beispiel #1
0
int upload(lua_State *L)
{
	char *conf_filename;
	char *local_filename;
	char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
	ConnectionInfo *pTrackerServer;
	int result;
	int store_path_index;
	ConnectionInfo storageServer;
	char file_id[128];
        size_t n;	
        char response[255];

        conf_filename = luaL_checklstring(L, 1, &n);
        local_filename = luaL_checklstring(L, 2, &n);
	log_init();
	g_log_context.log_level = LOG_ERR;
	ignore_signal_pipe();
        

	if ((result=fdfs_client_init(conf_filename)) != 0)
	{
		return result;
	}

	pTrackerServer = tracker_get_connection();
	if (pTrackerServer == NULL)
	{
		fdfs_client_destroy();
		return errno != 0 ? errno : ECONNREFUSED;
	}

	*group_name = '\0';
	if ((result=tracker_query_storage_store(pTrackerServer, \
	                &storageServer, group_name, &store_path_index)) != 0)
	{
		fdfs_client_destroy();
		return result;
	}

	result = storage_upload_by_filename1(pTrackerServer, \
			&storageServer, store_path_index, \
			local_filename, NULL, \
			NULL, 0, group_name, file_id);
	if (result == 0)
	{
            sprintf(response, "http://%s/%s\n", storageServer.ip_addr, file_id);
            lua_pushstring(L, response);
	}

	tracker_disconnect_server_ex(pTrackerServer, true);
	fdfs_client_destroy();
        if (result == 0)
            return 1;
	return result;
}
int CFDFSClient::fastfdfs_client_init(const char* sFDFSConfig)
{
	int result = 0;
	if ((result=fdfs_client_init(m_strConfigPath.c_str())) != 0)
	{
		logErrorEx(&g_log_context, "CFDFSClient::init() fdfs_client_init is failed, result:%d", result);
		return FSC_ERROR_CODE_INIT_FAILED;
	}

	return result;
}
Beispiel #3
0
int main(int argc, char *argv[])
{
	char *conf_filename;
	char file_id[128];
	int result;
	FDFSFileInfo file_info;
	
	if (argc < 3)
	{
		printf("Usage: %s <config_file> <file_id>\n", argv[0]);
		return 1;
	}

	log_init();
	g_log_context.log_level = LOG_ERR;
	ignore_signal_pipe();

	conf_filename = argv[1];
	if ((result=fdfs_client_init(conf_filename)) != 0)
	{
		return result;
	}

	snprintf(file_id, sizeof(file_id), "%s", argv[2]);
	memset(&file_info, 0, sizeof(file_info));
	result = fdfs_get_file_info_ex1(file_id, true, &file_info);
	if (result != 0)
	{
		printf("query file info fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
	}
	else
	{
		char szDatetime[32];

		printf("source storage id: %d\n", file_info.source_id);
		printf("source ip address: %s\n", file_info.source_ip_addr);
		printf("file create timestamp: %s\n", formatDatetime(
			file_info.create_timestamp, "%Y-%m-%d %H:%M:%S", \
			szDatetime, sizeof(szDatetime)));
		printf("file size: %"PRId64"\n", \
			file_info.file_size);
		printf("file crc32: %u (0x%08X)\n", \
			file_info.crc32, file_info.crc32);
	}

	tracker_close_all_connections();
	fdfs_client_destroy();

	return 0;
}
Beispiel #4
0
int main(int argc, char *argv[])
{
	char *conf_filename;
	int result;
	char *op_type;
	char *tracker_server;
	int arg_index;
	char *group_name;

	if (argc < 2)
	{
		usage(argv);
		return 1;
	}

	tracker_server = NULL;
	conf_filename = argv[1];
	arg_index = 2;

	if (arg_index >= argc)
	{
		op_type = "list";
	}
	else
	{
		int len;

		len = strlen(argv[arg_index]); 
		if (len >= 2 && strncmp(argv[arg_index], "-h", 2) == 0)
		{
			if (len == 2)
			{
				arg_index++;
				if (arg_index >= argc)
				{
					usage(argv);
					return 1;
				}

				tracker_server = argv[arg_index++];
			}
			else
			{
				tracker_server = argv[arg_index] + 2;
				arg_index++;
			}

			if (arg_index < argc)
			{
				op_type = argv[arg_index++];
			}
			else
			{
				op_type = "list";
			}
		}
		else
		{
			op_type = argv[arg_index++];
		}
	}

	log_init();
	g_log_context.log_level = LOG_DEBUG;
	ignore_signal_pipe();

	if ((result=fdfs_client_init(conf_filename)) != 0)
	{
		return result;
	}
	load_log_level_ex(conf_filename);

	if (tracker_server == NULL)
	{
		if (g_tracker_group.server_count > 1)
		{
			srand(time(NULL));
			rand();  //discard the first
			g_tracker_group.server_index = (int)( \
				(g_tracker_group.server_count * (double)rand()) \
				/ (double)RAND_MAX);
		}
	}
	else
	{
		int i;
		char ip_addr[IP_ADDRESS_SIZE];

		*ip_addr = '\0';
		if (getIpaddrByName(tracker_server, ip_addr, sizeof(ip_addr)) \
			 == INADDR_NONE)
		{
			printf("resolve ip address of tracker server: %s " \
				"fail!\n", tracker_server);
			return 2;
		}

		for (i=0; i<g_tracker_group.server_count; i++)
		{
			if (strcmp(g_tracker_group.servers[i].ip_addr, \
					ip_addr) == 0)
			{
				g_tracker_group.server_index = i;
				break;
			}
		}

		if (i == g_tracker_group.server_count)
		{
			printf("tracker server: %s not exists!\n", tracker_server);
			return 2;
		}
	}

	printf("server_count=%d, server_index=%d\n", g_tracker_group.server_count, g_tracker_group.server_index);

	pTrackerServer = tracker_get_connection();
	if (pTrackerServer == NULL)
	{
		fdfs_client_destroy();
		return errno != 0 ? errno : ECONNREFUSED;
	}
	printf("\ntracker server is %s:%d\n\n", pTrackerServer->ip_addr, pTrackerServer->port);

	if (arg_index < argc)
	{
		group_name = argv[arg_index++];
	}
	else
	{
		group_name = NULL;
	}

	if (strcmp(op_type, "list") == 0)
	{
		if (group_name == NULL)
		{
			result = list_all_groups(NULL);
		}
		else
		{
			result = list_all_groups(group_name);
		}
	}
	else if (strcmp(op_type, "delete") == 0)
	{
		char *storage_id;
		if (arg_index >= argc)
		{
			usage(argv);
			return 1;
		}

		storage_id = argv[arg_index++];

		if ((result=tracker_delete_storage(&g_tracker_group, \
				group_name, storage_id)) == 0)
		{
			printf("delete storage server %s::%s success\n", \
				group_name, storage_id);
		}
		else
		{
			printf("delete storage server %s::%s fail, " \
				"error no: %d, error info: %s\n", \
				group_name, storage_id, \
				result, STRERROR(result));
		}
	}
	else if (strcmp(op_type, "set_trunk_server") == 0)
	{
		char *storage_id;
		char new_trunk_server_id[FDFS_STORAGE_ID_MAX_SIZE];

		if (group_name == NULL)
		{
			usage(argv);
			return 1;
		}
		if (arg_index >= argc)
		{
			storage_id = "";
		}
		else
		{
			storage_id = argv[arg_index++];
		}

		if ((result=tracker_set_trunk_server(&g_tracker_group, \
			group_name, storage_id, new_trunk_server_id)) == 0)
		{
			printf("set trunk server %s::%s success, " \
				"new trunk server: %s\n", group_name, \
				storage_id, new_trunk_server_id);
		}
		else
		{
			printf("set trunk server %s::%s fail, " \
				"error no: %d, error info: %s\n", \
				group_name, storage_id, \
				result, STRERROR(result));
		}
	}
	else
	{
		printf("Invalid command %s\n\n", op_type);
		usage(argv);
	}

	tracker_disconnect_server_ex(pTrackerServer, true);
	fdfs_client_destroy();
	return 0;
}
int main(int argc, char *argv[])
{
	char *conf_filename;
	char *local_filename;
	char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
	ConnectionInfo *pTrackerServer;
	int result;
	int store_path_index;
	ConnectionInfo storageServer;
	char file_id[128];
	
	if (argc < 3)
	{
		printf("Usage: %s <config_file> <local_filename>\n", argv[0]);
		return 1;
	}

	log_init();
	g_log_context.log_level = LOG_ERR;
	ignore_signal_pipe();

	conf_filename = argv[1];
	if ((result=fdfs_client_init(conf_filename)) != 0)
	{
		return result;
	}

	pTrackerServer = tracker_get_connection();
	if (pTrackerServer == NULL)
	{
		fdfs_client_destroy();
		return errno != 0 ? errno : ECONNREFUSED;
	}


	*group_name = '\0';
	store_path_index = 0;
	if ((result=tracker_query_storage_store(pTrackerServer, \
	                &storageServer, group_name, &store_path_index)) != 0)
	{
		fdfs_client_destroy();
		fprintf(stderr, "tracker_query_storage fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
		return result;
	}

	local_filename = argv[2];
	result = storage_upload_appender_by_filename1(pTrackerServer, \
			&storageServer, store_path_index, \
			local_filename, NULL, \
			NULL, 0, group_name, file_id);
	if (result != 0)
	{
		fprintf(stderr, "upload file fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));

		tracker_disconnect_server_ex(pTrackerServer, true);
		fdfs_client_destroy();
		return result;
	}

	printf("%s\n", file_id);

	tracker_disconnect_server_ex(pTrackerServer, true);
	fdfs_client_destroy();

	return 0;
}
Beispiel #6
0
int main(int argc, char *argv[])
{
	char *conf_filename;
	char *local_filename;
	ConnectionInfo *pTrackerServer;
	ConnectionInfo *pStorageServer;
	int result;
	ConnectionInfo storageServer;
	char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
	char file_id[256];
	char appender_file_id[256];
	FDFSMetaData meta_list[32];
	int meta_count;
	char token[32 + 1];
	char file_url[256];
	char szDatetime[20];
	char szPortPart[16];
	int url_len;
	time_t ts;
	int64_t file_offset;
	int64_t file_size = 0;
	int store_path_index;
	FDFSFileInfo file_info;
	int upload_type;
	const char *file_ext_name;
	struct stat stat_buf;

	printf("This is FastDFS client test program v%d.%02d\n" \
"\nCopyright (C) 2008, Happy Fish / YuQing\n" \
"\nFastDFS may be copied only under the terms of the GNU General\n" \
"Public License V3, which may be found in the FastDFS source kit.\n" \
"Please visit the FastDFS Home Page http://www.csource.org/ \n" \
"for more detail.\n\n" \
, g_fdfs_version.major, g_fdfs_version.minor);

	if (argc < 3)
	{
		printf("Usage: %s <config_file> <local_filename> " \
			"[FILE | BUFF | CALLBACK]\n", argv[0]);
		return 1;
	}

	log_init();
	g_log_context.log_level = LOG_DEBUG;

	conf_filename = argv[1];
	if ((result=fdfs_client_init(conf_filename)) != 0)
	{
		return result;
	}

	pTrackerServer = tracker_get_connection();
	if (pTrackerServer == NULL)
	{
		fdfs_client_destroy();
		return errno != 0 ? errno : ECONNREFUSED;
	}

	local_filename = argv[2];
	if (argc == 3)
	{
		upload_type = FDFS_UPLOAD_BY_FILE;
	}
	else
	{
		if (strcmp(argv[3], "BUFF") == 0)
		{
			upload_type = FDFS_UPLOAD_BY_BUFF;
		}
		else if (strcmp(argv[3], "CALLBACK") == 0)
		{
			upload_type = FDFS_UPLOAD_BY_CALLBACK;
		}
		else
		{
			upload_type = FDFS_UPLOAD_BY_FILE;
		}
	}

	store_path_index = 0;
	*group_name = '\0';
	if ((result=tracker_query_storage_store(pTrackerServer, \
			&storageServer, group_name, &store_path_index)) != 0)
	{
		fdfs_client_destroy();
		printf("tracker_query_storage fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
		return result;
	}

	printf("group_name=%s, ip_addr=%s, port=%d\n", \
		group_name, storageServer.ip_addr, \
		storageServer.port);

	if ((pStorageServer=tracker_connect_server(&storageServer, \
		&result)) == NULL)
	{
		fdfs_client_destroy();
		return result;
	}

	memset(&meta_list, 0, sizeof(meta_list));
	meta_count = 0;
	strcpy(meta_list[meta_count].name, "ext_name");
	strcpy(meta_list[meta_count].value, "jpg");
	meta_count++;
	strcpy(meta_list[meta_count].name, "width");
	strcpy(meta_list[meta_count].value, "160");
	meta_count++;
	strcpy(meta_list[meta_count].name, "height");
	strcpy(meta_list[meta_count].value, "80");
	meta_count++;
	strcpy(meta_list[meta_count].name, "file_size");
	strcpy(meta_list[meta_count].value, "115120");
	meta_count++;

	file_ext_name = fdfs_get_file_ext_name(local_filename);
	if (upload_type == FDFS_UPLOAD_BY_FILE)
	{
		if (stat(local_filename, &stat_buf) == 0 && \
				S_ISREG(stat_buf.st_mode))
		{
			file_size = stat_buf.st_size;
			result = storage_upload_appender_by_filename1( \
				pTrackerServer, pStorageServer, \
				store_path_index, local_filename, \
				file_ext_name, meta_list, meta_count, \
				group_name, file_id);
		}
		else
		{
			result = errno != 0 ? errno : ENOENT;
		}

		printf("storage_upload_appender_by_filename1\n");
	}
	else if (upload_type == FDFS_UPLOAD_BY_BUFF)
	{
		char *file_content;
		if ((result=getFileContent(local_filename, \
					&file_content, &file_size)) == 0)
		{
			result = storage_upload_appender_by_filebuff1( \
					pTrackerServer, pStorageServer, \
					store_path_index, file_content, \
					file_size, file_ext_name, \
					meta_list, meta_count, \
					group_name, file_id);
			free(file_content);
		}

		printf("storage_upload_appender_by_filebuff1\n");
	}
	else
	{
		if (stat(local_filename, &stat_buf) == 0 && \
				S_ISREG(stat_buf.st_mode))
		{
			file_size = stat_buf.st_size;
			result = storage_upload_appender_by_callback1( \
					pTrackerServer, pStorageServer, \
					store_path_index, uploadFileCallback, \
					local_filename, file_size, \
					file_ext_name, meta_list, meta_count, \
					group_name, file_id);
		}
		else
		{
			result = errno != 0 ? errno : ENOENT;
		}

		printf("storage_upload_appender_by_callback1\n");
	}

	if (result != 0)
	{
		printf("upload file fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
		tracker_disconnect_server_ex(pStorageServer, true);
		fdfs_client_destroy();
		return result;
	}

	if (g_tracker_server_http_port == 80)
	{
		*szPortPart = '\0';
	}
	else
	{
		sprintf(szPortPart, ":%d", g_tracker_server_http_port);
	}

	url_len = sprintf(file_url, "http://%s%s/%s", \
			pTrackerServer->ip_addr, szPortPart, file_id);
	if (g_anti_steal_token)
	{
		ts = time(NULL);
		fdfs_http_gen_token(&g_anti_steal_secret_key, file_id, \
				ts, token);
		sprintf(file_url + url_len, "?token=%s&ts=%d", token, (int)ts);
	}

	printf("fild_id=%s\n", file_id);

	fdfs_get_file_info1(file_id, &file_info);
	printf("source ip address: %s\n", file_info.source_ip_addr);
	printf("file timestamp=%s\n", formatDatetime(
		file_info.create_timestamp, "%Y-%m-%d %H:%M:%S", \
		szDatetime, sizeof(szDatetime)));
	printf("file size="INT64_PRINTF_FORMAT"\n", file_info.file_size);
	printf("file crc32=%u\n", file_info.crc32);
	printf("file url: %s\n", file_url);


	strcpy(appender_file_id, file_id);
	if (storage_truncate_file1(pTrackerServer, pStorageServer, \
			appender_file_id, 0) != 0)
	{
		printf("truncate file fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
		tracker_disconnect_server_ex(pStorageServer, true);
		fdfs_client_destroy();
		return result;
	}

	fdfs_get_file_info1(file_id, &file_info);
	printf("source ip address: %s\n", file_info.source_ip_addr);
	printf("file timestamp=%s\n", formatDatetime(
		file_info.create_timestamp, "%Y-%m-%d %H:%M:%S", \
		szDatetime, sizeof(szDatetime)));
	printf("file size="INT64_PRINTF_FORMAT"\n", file_info.file_size);
	printf("file crc32=%u\n", file_info.crc32);
	printf("file url: %s\n", file_url);
	if (file_info.file_size != 0)
	{
		fprintf(stderr, "file size: "INT64_PRINTF_FORMAT \
			" != 0!!!", file_info.file_size);
	}

	//sleep(70);
	if (upload_type == FDFS_UPLOAD_BY_FILE)
	{
		result = storage_append_by_filename1(pTrackerServer, \
				pStorageServer, local_filename, 
				appender_file_id);

		printf("storage_append_by_filename\n");
	}
	else if (upload_type == FDFS_UPLOAD_BY_BUFF)
	{
		char *file_content;
		if ((result=getFileContent(local_filename, \
				&file_content, &file_size)) == 0)
		{
			result = storage_append_by_filebuff1(pTrackerServer, \
				pStorageServer, file_content, \
				file_size, appender_file_id);
			free(file_content);
		}

		printf("storage_append_by_filebuff1\n");
	}
	else
	{
		if (stat(local_filename, &stat_buf) == 0 && \
			S_ISREG(stat_buf.st_mode))
		{
			file_size = stat_buf.st_size;
			result = storage_append_by_callback1(pTrackerServer, \
					pStorageServer, uploadFileCallback, \
					local_filename, file_size, \
					appender_file_id);
		}
		else
		{
			result = errno != 0 ? errno : ENOENT;
		}

		printf("storage_append_by_callback1\n");
	}

	if (result != 0)
	{
		printf("append file fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
		tracker_disconnect_server_ex(pStorageServer, true);
		fdfs_client_destroy();
		return result;
	}
	printf("append file successfully.\n");
	fdfs_get_file_info1(appender_file_id, &file_info);
	printf("source ip address: %s\n", file_info.source_ip_addr);
	printf("file timestamp=%s\n", formatDatetime(
		file_info.create_timestamp, "%Y-%m-%d %H:%M:%S", \
		szDatetime, sizeof(szDatetime)));
	printf("file size="INT64_PRINTF_FORMAT"\n", file_info.file_size);
	if (file_info.file_size != file_size)
	{
		fprintf(stderr, "file size: "INT64_PRINTF_FORMAT \
			" != "INT64_PRINTF_FORMAT"!!!", file_info.file_size, \
			file_size);
	}

	file_offset = file_size;
	if (upload_type == FDFS_UPLOAD_BY_FILE)
	{
		result = storage_modify_by_filename1(pTrackerServer, \
				pStorageServer, local_filename, 
				file_offset, appender_file_id);

		printf("storage_modify_by_filename\n");
	}
	else if (upload_type == FDFS_UPLOAD_BY_BUFF)
	{
		char *file_content;
		if ((result=getFileContent(local_filename, \
				&file_content, &file_size)) == 0)
		{
			result = storage_modify_by_filebuff1( \
				pTrackerServer, pStorageServer, \
				file_content, file_offset, file_size, \
				appender_file_id);
			free(file_content);
		}

		printf("storage_modify_by_filebuff1\n");
	}
	else
	{
		if (stat(local_filename, &stat_buf) == 0 && \
			S_ISREG(stat_buf.st_mode))
		{
			file_size = stat_buf.st_size;
			result = storage_modify_by_callback1( \
					pTrackerServer, pStorageServer, \
					uploadFileCallback, \
					local_filename, file_offset, \
					file_size, appender_file_id);
		}
		else
		{
			result = errno != 0 ? errno : ENOENT;
		}

		printf("storage_modify_by_callback1\n");
	}

	if (result != 0)
	{
		printf("modify file fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
		tracker_disconnect_server_ex(pStorageServer, true);
		fdfs_client_destroy();
		return result;
	}
	printf("modify file successfully.\n");
	fdfs_get_file_info1(appender_file_id, &file_info);
	printf("source ip address: %s\n", file_info.source_ip_addr);
	printf("file timestamp=%s\n", formatDatetime(
		file_info.create_timestamp, "%Y-%m-%d %H:%M:%S", \
		szDatetime, sizeof(szDatetime)));
	printf("file size="INT64_PRINTF_FORMAT"\n", file_info.file_size);
	if (file_info.file_size != 2 * file_size)
	{
		fprintf(stderr, "file size: "INT64_PRINTF_FORMAT \
			" != "INT64_PRINTF_FORMAT"!!!", file_info.file_size, \
			2 * file_size);
	}

	tracker_disconnect_server_ex(pStorageServer, true);
	tracker_disconnect_server_ex(pTrackerServer, true);

	fdfs_client_destroy();

	return result;
}
Beispiel #7
0
int dfs_init(const int proccess_index, const char *conf_filename)
{
	return fdfs_client_init(conf_filename);
}
Beispiel #8
0
int main(int argc, char *argv[])
{
	char *conf_filename;
	char *local_filename;
	TrackerServerInfo *pTrackerServer;
	int result;
	char file_id[128];
	int64_t file_size;
	
	if (argc < 3)
	{
		printf("Usage: %s <config_file> <remote file id> " \
			"[local filename]\n", argv[0]);
		return 1;
	}

	log_init();
	g_log_context.log_level = LOG_ERR;

	conf_filename = argv[1];
	if ((result=fdfs_client_init(conf_filename)) != 0)
	{
		return result;
	}

	pTrackerServer = tracker_get_connection();
	if (pTrackerServer == NULL)
	{
		fdfs_client_destroy();
		return errno != 0 ? errno : ECONNREFUSED;
	}

	snprintf(file_id, sizeof(file_id), "%s", argv[2]);

	if (argc >= 4)
	{
		local_filename = argv[3];
	}
	else
	{
		local_filename = strrchr(file_id, '/');
		if (local_filename != NULL)
		{
			local_filename++;  //skip /
		}
		else
		{
			local_filename = file_id;
		}
	}

	result = storage_download_file_to_file1( \
			pTrackerServer, NULL, \
			file_id, local_filename, &file_size);
	if (result != 0)
	{
		printf("download file fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
	}

	fdfs_quit(pTrackerServer);
	tracker_close_all_connections();
	fdfs_client_destroy();

	return 0;
}