int trunk_client_trunk_alloc_space(const int file_size, \ FDFSTrunkFullInfo *pTrunkInfo) { int result; TrackerServerInfo trunk_server; if (g_if_trunker_self) { return trunk_alloc_space(file_size, pTrunkInfo); } if (*(g_trunk_server.ip_addr) == '\0') { logError("file: "__FILE__", line: %d, " \ "no trunk server", __LINE__); return EAGAIN; } memcpy(&trunk_server, &g_trunk_server, sizeof(TrackerServerInfo)); if ((result=tracker_connect_server(&trunk_server)) != 0) { return result; } result = trunk_client_trunk_do_alloc_space(&trunk_server, \ file_size, pTrunkInfo); tracker_disconnect_server(&trunk_server); return result; }
int trunk_client_trunk_alloc_confirm(const FDFSTrunkFullInfo *pTrunkInfo, \ const int status) { int result; TrackerServerInfo trunk_server; if (g_if_trunker_self) { return trunk_alloc_confirm(pTrunkInfo, status); } if (*(g_trunk_server.ip_addr) == '\0') { return EAGAIN; } memcpy(&trunk_server, &g_trunk_server, sizeof(TrackerServerInfo)); if ((result=tracker_connect_server(&trunk_server)) != 0) { return result; } result = trunk_client_trunk_do_alloc_confirm(&trunk_server, \ pTrunkInfo, status); tracker_disconnect_server(&trunk_server); return result; }
int trunk_client_trunk_free_space(const FDFSTrunkFullInfo *pTrunkInfo) { int result; TrackerServerInfo trunk_server; if (g_if_trunker_self) { return trunk_free_space(pTrunkInfo, true); } if (*(g_trunk_server.ip_addr) == '\0') { return EAGAIN; } memcpy(&trunk_server, &g_trunk_server, sizeof(TrackerServerInfo)); if ((result=tracker_connect_server(&trunk_server)) != 0) { return result; } result = trunk_client_trunk_do_free_space(&trunk_server, pTrunkInfo); tracker_disconnect_server(&trunk_server); return result; }
int upload_file(const char *file_buff, const int file_size, char *file_id, char *storage_ip) { int result; TrackerServerInfo *pTrackerServer; TrackerServerInfo storageServer; int store_path_index; pTrackerServer = tracker_get_connection(); if (pTrackerServer == NULL) { return errno != 0 ? errno : ECONNREFUSED; } if ((result=tracker_query_storage_store(pTrackerServer, &storageServer, &store_path_index)) != 0) { fdfs_quit(pTrackerServer); tracker_disconnect_server(pTrackerServer); return result; } if ((result=tracker_connect_server(&storageServer)) != 0) { fdfs_quit(pTrackerServer); tracker_disconnect_server(pTrackerServer); return result; } strcpy(storage_ip, storageServer.ip_addr); result = storage_upload_by_filebuff1(pTrackerServer, &storageServer, store_path_index, file_buff, file_size, NULL, NULL, 0, "", file_id); fdfs_quit(pTrackerServer); tracker_disconnect_server(pTrackerServer); fdfs_quit(&storageServer); tracker_disconnect_server(&storageServer); return result; }
int download_file(const char *file_id, int *file_size, char *storage_ip) { int result; TrackerServerInfo *pTrackerServer; TrackerServerInfo storageServer; int64_t file_bytes; pTrackerServer = tracker_get_connection(); if (pTrackerServer == NULL) { return errno != 0 ? errno : ECONNREFUSED; } if ((result=tracker_query_storage_fetch1(pTrackerServer, &storageServer, file_id)) != 0) { fdfs_quit(pTrackerServer); tracker_disconnect_server(pTrackerServer); return result; } if ((result=tracker_connect_server(&storageServer)) != 0) { fdfs_quit(pTrackerServer); tracker_disconnect_server(pTrackerServer); return result; } strcpy(storage_ip, storageServer.ip_addr); result = storage_download_file_ex1(pTrackerServer, &storageServer, file_id, 0, 0, downloadFileCallback, NULL, &file_bytes); *file_size = file_bytes; fdfs_quit(pTrackerServer); tracker_disconnect_server(pTrackerServer); fdfs_quit(&storageServer); tracker_disconnect_server(&storageServer); return result; }
int delete_file(const char *file_id, char *storage_ip) { int result; TrackerServerInfo *pTrackerServer; TrackerServerInfo storageServer; pTrackerServer = tracker_get_connection(); if (pTrackerServer == NULL) { return errno != 0 ? errno : ECONNREFUSED; } if ((result=tracker_query_storage_update1(pTrackerServer, &storageServer, file_id)) != 0) { fdfs_quit(pTrackerServer); tracker_disconnect_server(pTrackerServer); return result; } if ((result=tracker_connect_server(&storageServer)) != 0) { fdfs_quit(pTrackerServer); tracker_disconnect_server(pTrackerServer); return result; } strcpy(storage_ip, storageServer.ip_addr); result = storage_delete_file1(pTrackerServer, &storageServer, file_id); fdfs_quit(pTrackerServer); tracker_disconnect_server(pTrackerServer); fdfs_quit(&storageServer); tracker_disconnect_server(&storageServer); return result; }
static int recovery_get_src_storage_server(ConnectionInfo *pSrcStorage) { int result; int storage_count; ConnectionInfo trackerServer; ConnectionInfo *pTrackerConn; FDFSGroupStat groupStat; FDFSStorageInfo storageStats[FDFS_MAX_SERVERS_EACH_GROUP]; FDFSStorageInfo *pStorageStat; FDFSStorageInfo *pStorageEnd; memset(pSrcStorage, 0, sizeof(ConnectionInfo)); pSrcStorage->sock = -1; logDebug("file: "__FILE__", line: %d, " \ "disk recovery: get source storage server", \ __LINE__); while (g_continue_flag) { result = tracker_get_storage_max_status(&g_tracker_group, \ g_group_name, g_tracker_client_ip, \ g_my_server_id_str, &saved_storage_status); if (result == ENOENT) { logWarning("file: "__FILE__", line: %d, " \ "current storage: %s does not exist " \ "in tracker server", __LINE__, \ g_tracker_client_ip); return ENOENT; } if (result == 0) { if (saved_storage_status == FDFS_STORAGE_STATUS_INIT) { logInfo("file: "__FILE__", line: %d, " \ "current storage: %s 's status is %d" \ ", does not need recovery", __LINE__, \ g_tracker_client_ip, \ saved_storage_status); return ENOENT; } if (saved_storage_status == FDFS_STORAGE_STATUS_IP_CHANGED || \ saved_storage_status == FDFS_STORAGE_STATUS_DELETED) { logWarning("file: "__FILE__", line: %d, " \ "current storage: %s 's status is %d" \ ", does not need recovery", __LINE__, \ g_tracker_client_ip, saved_storage_status); return ENOENT; } break; } sleep(1); } while (g_continue_flag) { if ((pTrackerConn=tracker_get_connection_r(&trackerServer, \ &result)) == NULL) { sleep(5); continue; } result = tracker_list_one_group(pTrackerConn, \ g_group_name, &groupStat); if (result != 0) { tracker_disconnect_server_ex(pTrackerConn, true); sleep(1); continue; } if (groupStat.count <= 0) { logWarning("file: "__FILE__", line: %d, " \ "storage server count: %d in the group <= 0!",\ __LINE__, groupStat.count); tracker_disconnect_server(pTrackerConn); sleep(1); continue; } if (groupStat.count == 1) { logInfo("file: "__FILE__", line: %d, " \ "storage server count in the group = 1, " \ "does not need recovery", __LINE__); tracker_disconnect_server(pTrackerConn); return ENOENT; } if (g_fdfs_store_paths.count > groupStat.store_path_count) { logInfo("file: "__FILE__", line: %d, " \ "storage store path count: %d > " \ "which of the group: %d, " \ "does not need recovery", __LINE__, \ g_fdfs_store_paths.count, groupStat.store_path_count); tracker_disconnect_server(pTrackerConn); return ENOENT; } if (groupStat.active_count <= 0) { tracker_disconnect_server(pTrackerConn); sleep(5); continue; } result = tracker_list_servers(pTrackerConn, \ g_group_name, NULL, storageStats, \ FDFS_MAX_SERVERS_EACH_GROUP, &storage_count); tracker_disconnect_server_ex(pTrackerConn, result != 0); if (result != 0) { sleep(5); continue; } if (storage_count <= 1) { logWarning("file: "__FILE__", line: %d, " \ "storage server count: %d in the group <= 1!",\ __LINE__, storage_count); sleep(5); continue; } pStorageEnd = storageStats + storage_count; for (pStorageStat=storageStats; pStorageStat<pStorageEnd; \ pStorageStat++) { if (strcmp(pStorageStat->id, g_my_server_id_str) == 0) { continue; } if (pStorageStat->status == FDFS_STORAGE_STATUS_ACTIVE) { strcpy(pSrcStorage->ip_addr, \ pStorageStat->ip_addr); pSrcStorage->port = pStorageStat->storage_port; break; } } if (pStorageStat < pStorageEnd) //found src storage server { break; } sleep(5); } if (!g_continue_flag) { return EINTR; } logDebug("file: "__FILE__", line: %d, " \ "disk recovery: get source storage server %s:%d", \ __LINE__, pSrcStorage->ip_addr, pSrcStorage->port); return 0; }
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 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_size; int store_path_index; FDFSFileInfo file_info; int upload_type; const char *file_ext_name; 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; 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; } 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); *group_name = '\0'; if (upload_type == FDFS_UPLOAD_BY_FILE) { result = storage_upload_appender_by_filename1(pTrackerServer, \ &storageServer, store_path_index, \ local_filename, file_ext_name, \ meta_list, meta_count, group_name, file_id); 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, &storageServer, \ 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 { struct stat stat_buf; 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, &storageServer, \ store_path_index, uploadFileCallback, \ local_filename, file_size, \ file_ext_name, meta_list, meta_count, \ group_name, file_id); } printf("storage_upload_appender_by_callback1\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; } 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); //sleep(70); strcpy(appender_file_id, file_id); if (upload_type == FDFS_UPLOAD_BY_FILE) { result = storage_append_by_filename1(pTrackerServer, \ &storageServer, 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, \ &storageServer, file_content, \ file_size, appender_file_id); free(file_content); } printf("storage_append_by_filebuff1\n"); } else { struct stat stat_buf; 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, \ &storageServer, uploadFileCallback, \ local_filename, file_size, \ appender_file_id); } printf("storage_append_by_callback1\n"); } if (result != 0) { printf("append 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("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); fdfs_quit(&storageServer); tracker_disconnect_server(&storageServer); fdfs_quit(pTrackerServer); tracker_close_all_connections(); fdfs_client_destroy(); return result; }