#include
#include
#include
#include
#include
#include
#include
#include
#include "fdfs_client.h"
#include "fdfs_global.h"
#include "base64.h"
#include "sockopt.h"
#include "logger.h"
int main(int argc, char *argv[])
{
char *conf_filename;
char *local_filename;
TrackerServerInfo *pTrackerServer;
int result;
TrackerServerInfo storageServer;
char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
char remote_filename[256];
char master_filename[256];
FDFSMetaData meta_list[32];
int meta_count;
int i;
FDFSMetaData *pMetaList;
char token[32 + 1];
char file_id[128];
char file_url[256];
char szDatetime[20];
char szPortPart[16];
time_t ts;
char *file_buff;
char *meta_buff;
int64_t file_size;
char *operation;
int store_path_index;
FDFSFileInfo file_info;
if (argc < 3)
{
printf("Usage: %s \n" \
"\toperation: upload, download," \
"delete\n", argv[0]);
return 1;
}
conf_filename = argv[1];
operation = argv[2];
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 = NULL;
if (strcmp(operation, "upload") == 0)
{
int upload_type;
char *prefix_name;
const char *file_ext_name;
char slave_filename[256];
int slave_filename_len;
if (argc < 4)
{
printf("Usage: %s upload " \
"\n",\
argv[0]);
fdfs_client_destroy();
return EINVAL;
}
local_filename = argv[3];
upload_type = FDFS_UPLOAD_BY_FILE;
store_path_index = 0;
{
TrackerServerInfo storageServers[FDFS_MAX_SERVERS_EACH_GROUP];
TrackerServerInfo *pServer;
TrackerServerInfo *pServerEnd;
int storage_count;
if ((result=tracker_query_storage_store_list_without_group( \
pTrackerServer, storageServers, \
FDFS_MAX_SERVERS_EACH_GROUP, &storage_count, \
&store_path_index)) == 0)
{
printf("tracker_query_storage_store_list_without_group: \n");
pServerEnd = storageServers + storage_count;
for (pServer=storageServers; pServer
{
printf("\tserver %d. group_name=%s, " \
"ip_addr=%s, port=%d\n", \
(int)(pServer - storageServers) + 1, \
pServer->group_name, \
pServer->ip_addr, pServer->port);
}
printf("\n");
}
}
puts("1");
if ((result=tracker_query_storage_store(pTrackerServer, \
&storageServer, &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", \
storageServer.group_name, \
storageServer.ip_addr, \
storageServer.port);
if ((result=tracker_connect_server(&storageServer)) != 0)
{
fdfs_client_destroy();
return result;
}
file_ext_name = fdfs_get_file_ext_name(local_filename);
strcpy(group_name, "");
result = storage_upload_by_filename(pTrackerServer, \
&storageServer, store_path_index, \
local_filename, file_ext_name, \
meta_list, meta_count, \
group_name, remote_filename);
printf("storage_upload_by_filename\n");
if (result != 0)
{
printf("upload file fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
fdfs_quit(&storageServer);
tracker_disconnect_server(&storageServer);
fdfs_client_destroy();
return result;
}
printf("group_name=%s, remote_filename=%s\n", \
group_name, remote_filename);
fdfs_get_file_info(group_name, remote_filename, &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);
strcpy(master_filename, remote_filename);
*remote_filename = '\0';
prefix_name = "_slv";
result = storage_upload_slave_by_filename(pTrackerServer,
NULL, local_filename, master_filename, \
prefix_name, file_ext_name, \
meta_list, meta_count, \
group_name, remote_filename);
printf("storage_upload_slave_by_filename\n");
if (result != 0)
{
printf("upload slave file fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
fdfs_quit(&storageServer);
tracker_disconnect_server(&storageServer);
fdfs_client_destroy();
return result;
}
printf("group_name=%s, remote_filename=%s\n", \
group_name, remote_filename);
fdfs_get_file_info(group_name, remote_filename, &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);
if (fdfs_gen_slave_filename(master_filename, \
prefix_name, file_ext_name, \
slave_filename, &slave_filename_len) == 0)
{
if (strcmp(remote_filename, slave_filename) != 0)
{
printf("slave_filename=%s\n" \
"remote_filename=%s\n" \
"not equal!\n", \
slave_filename, remote_filename);
}
}
}
else if (strcmp(operation, "download") == 0 || strcmp(operation, "delete") == 0)
{
if (argc < 5)
{
printf("Usage: %s %s " \
" \n", \
argv[0], operation);
fdfs_client_destroy();
return EINVAL;
}
snprintf(group_name, sizeof(group_name), "%s", argv[3]);
snprintf(remote_filename, sizeof(remote_filename), \
"%s", argv[4]);
result = tracker_query_storage_fetch(pTrackerServer, \
&storageServer, group_name, remote_filename);
if (result != 0)
{
fdfs_client_destroy();
printf("tracker_query_storage_fetch fail, " \
"group_name=%s, filename=%s, " \
"error no: %d, error info: %s\n", \
group_name, remote_filename, \
result, STRERROR(result));
return result;
}
printf("storage=%s:%d\n", storageServer.ip_addr, \
storageServer.port);
if ((result=tracker_connect_server(&storageServer)) != 0)
{
fdfs_client_destroy();
return result;
}
if (strcmp(operation, "download") == 0)
{
if (argc >= 6)
{
local_filename = argv[5];
result = storage_download_file_to_file( \
pTrackerServer, &storageServer, \
group_name, remote_filename, \
local_filename, &file_size);
}
if (result == 0)
{
printf("download file success, " \
"file size="INT64_PRINTF_FORMAT", file save to %s\n", \
file_size, local_filename);
}
else
{
printf("download file fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
}
}
else if(strcmp(operation, "delete") == 0)
{
if ((result=storage_delete_file(pTrackerServer, \
NULL, group_name, remote_filename)) == 0)
{
printf("delete file success\n");
}
else
{
printf("delete file fail, " \
"error no: %d, error info: %s\n", \
result, STRERROR(result));
}
}
}
else
{
fdfs_client_destroy();
printf("invalid operation: %s\n", operation);
return EINVAL;
}
fdfs_quit(&storageServer);
tracker_disconnect_server(&storageServer);
fdfs_quit(pTrackerServer);
tracker_close_all_connections();
fdfs_client_destroy();
return result;
}
阅读(1109) | 评论(0) | 转发(0) |