コード例 #1
0
ファイル: tracker_client.c プロジェクト: alemic/fastdfs
ConnectionInfo *tracker_get_connection_ex(TrackerServerGroup *pTrackerGroup)
{
	ConnectionInfo *conn;
	ConnectionInfo *pCurrentServer;
	ConnectionInfo *pServer;
	ConnectionInfo *pEnd;
	int server_index;
	int result;

	server_index = pTrackerGroup->server_index;
	if (server_index >= pTrackerGroup->server_count)
	{
		server_index = 0;
	}

	do
	{
	pCurrentServer = pTrackerGroup->servers + server_index;
	if ((conn=tracker_connect_server(pCurrentServer, &result)) != NULL)
	{
		break;
	}

	pEnd = pTrackerGroup->servers + pTrackerGroup->server_count;
	for (pServer=pCurrentServer+1; pServer<pEnd; pServer++)
	{
		if ((conn=tracker_connect_server(pServer, &result)) != NULL)
		{
			pTrackerGroup->server_index = pServer - \
							pTrackerGroup->servers;
			break;
		}
	}

	if (conn != NULL)
	{
		break;
	}

	for (pServer=pTrackerGroup->servers; pServer<pCurrentServer; pServer++)
	{
		if ((conn=tracker_connect_server(pServer, &result)) != NULL)
		{
			pTrackerGroup->server_index = pServer - \
							pTrackerGroup->servers;
			break;
		}
	}
	} while (0);

	pTrackerGroup->server_index++;
	if (pTrackerGroup->server_index >= pTrackerGroup->server_count)
	{
		pTrackerGroup->server_index = 0;
	}

	return conn;
}
コード例 #2
0
ファイル: trunk_client.c プロジェクト: aducode/fastdfs
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;
}
コード例 #3
0
static int storage_convert_src_server_id()
{
	ConnectionInfo *pTrackerServer;
	ConnectionInfo *pServerEnd;
	ConnectionInfo *pTrackerConn;
	ConnectionInfo tracker_server;
	int result;

	result = ENOENT;
	pServerEnd = g_tracker_group.servers + g_tracker_group.server_count;
	for (pTrackerServer=g_tracker_group.servers; \
		pTrackerServer<pServerEnd; pTrackerServer++)
	{
		memcpy(&tracker_server, pTrackerServer, \
			sizeof(ConnectionInfo));
		tracker_server.sock = -1;
                if ((pTrackerConn=tracker_connect_server(&tracker_server, \
			&result)) == NULL)
		{
			continue;
		}

		result = tracker_get_storage_id(pTrackerConn, \
			g_group_name, g_sync_src_id, g_sync_src_id);
		tracker_disconnect_server_ex(pTrackerConn, \
			result != 0 && result != ENOENT);
		if (result == 0)
		{
			return 0;
		}
	}

	return result;
}
コード例 #4
0
ファイル: trunk_client.c プロジェクト: aducode/fastdfs
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;
}
コード例 #5
0
ファイル: trunk_client.c プロジェクト: aducode/fastdfs
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;
}
コード例 #6
0
ファイル: tracker_client.c プロジェクト: alemic/fastdfs
int tracker_get_storage_max_status(TrackerServerGroup *pTrackerGroup, \
		const char *group_name, const char *ip_addr, \
		char *storage_id, int *status)
{
	ConnectionInfo *conn;
	ConnectionInfo tracker_server;
	ConnectionInfo *pServer;
	ConnectionInfo *pEnd;
	FDFSStorageBrief storage_brief;
	int result;

	memset(&storage_brief, 0, sizeof(FDFSStorageBrief));
	storage_brief.status = -1;

	*storage_id = '\0';
	*status = -1;
	pEnd = pTrackerGroup->servers + pTrackerGroup->server_count;
	for (pServer=pTrackerGroup->servers; pServer<pEnd; pServer++)
	{
		memcpy(&tracker_server, pServer, sizeof(ConnectionInfo));
		tracker_server.sock = -1;
		if ((conn=tracker_connect_server(&tracker_server, &result)) == NULL)
		{
			return result;
		}

		result = tracker_get_storage_status(conn, group_name, \
				ip_addr, &storage_brief);
		tracker_disconnect_server_ex(conn, result != 0);

		if (result != 0)
		{
			if (result == ENOENT)
			{
				continue;
			}
			return result;
		}

		strcpy(storage_id, storage_brief.id);
		if (storage_brief.status > *status)
		{
			*status = storage_brief.status;
		}
	}

	if (*status == -1)
	{
		return ENOENT;
	}

	return 0;
}
コード例 #7
0
int CFDFSClient::fdfs_dowloadfile( BufferInfo* pBuff, const char *group_name, const char* remote_filename)
{
	char *file_buff = NULL;
    int64_t file_size = 0;
    int result = 0;
    
    ConnectionInfo *pTrackerServer = tracker_get_connection();
	if (pTrackerServer == NULL)
	{
		result = (errno != 0 ? errno : ECONNREFUSED);
		logErrorEx(&g_log_context, "CFDFSClient::init() tracker_get_connection is failed, result:%d", result);

		return FSC_ERROR_CODE_CONNECT_TRACKER_FAIL;
	}

    // 查询storage 服务地址
	ConnectionInfo storageServer;
	ConnectionInfo* pStorageServer;
    result = tracker_query_storage_fetch(pTrackerServer, \
		&storageServer, group_name, remote_filename);
    if (result != 0)
	{
		logErrorEx(&g_log_context, "CFDFSClient::fdfs_dowloadfile() tracker_query_storage_fetch fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
            
        tracker_disconnect_server_ex(pTrackerServer, true);

		return FSC_ERROR_CODE_QUERY_STORAGE_FAIL;
	}

	logDebugEx(&g_log_context, "CFDFSClient::fdfs_dowloadfile() storage=%s:%d\n", storageServer.ip_addr, \
		storageServer.port);

	if ((pStorageServer=tracker_connect_server(&storageServer, \
		&result)) == NULL)
	{
		logErrorEx(&g_log_context, "CFDFSClient::fdfs_dowloadfile() \
			tracker_connect_server failed, result:%d, storage=%s:%d\n", 
			result, storageServer.ip_addr, \
			storageServer.port);
        
        tracker_disconnect_server_ex(pTrackerServer, true);

		return FSC_ERROR_CODE_CONNECT_STORAGE_FAIL;
    }
コード例 #8
0
ファイル: tracker_proto.c プロジェクト: HappyTomas/fastdfs
int fdfs_deal_no_body_cmd_ex(const char *ip_addr, const int port, const int cmd)
{
	ConnectionInfo *conn;
	ConnectionInfo server_info;
	int result;

	strcpy(server_info.ip_addr, ip_addr);
	server_info.port = port;
	server_info.sock = -1;
	if ((conn=tracker_connect_server(&server_info, &result)) == NULL)
	{
		return result;
	}

	result = fdfs_deal_no_body_cmd(conn, cmd);
	tracker_disconnect_server_ex(conn, result != 0);
	return result;
}
コード例 #9
0
ファイル: dfs_func.c プロジェクト: aducode/fastdfs
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;
}
コード例 #10
0
ファイル: dfs_func.c プロジェクト: aducode/fastdfs
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;
}
コード例 #11
0
ファイル: dfs_func.c プロジェクト: aducode/fastdfs
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;
}
コード例 #12
0
static int storage_do_recovery(const char *pBasePath, StorageBinLogReader *pReader, \
		ConnectionInfo *pSrcStorage)
{
	ConnectionInfo *pTrackerServer;
	ConnectionInfo *pStorageConn;
	FDFSTrunkFullInfo trunk_info;
	StorageBinLogRecord record;
	int record_length;
	int result;
	int log_level;
	int count;
	int store_path_index;
	int64_t file_size;
	int64_t total_count;
	int64_t success_count;
	bool bContinueFlag;
	char local_filename[MAX_PATH_SIZE];
	char src_filename[MAX_PATH_SIZE];

	pTrackerServer = g_tracker_group.servers;
	count = 0;
	total_count = 0;
	success_count = 0;
	result = 0;

	logInfo("file: "__FILE__", line: %d, " \
		"disk recovery: recovering files of data path: %s ...", \
		__LINE__, pBasePath);

	bContinueFlag = true;
	while (bContinueFlag)
	{
	if ((pStorageConn=tracker_connect_server(pSrcStorage, &result)) == NULL)
	{
		sleep(5);
		continue;
	}

	while (g_continue_flag)
	{
		result=storage_binlog_read(pReader, &record, &record_length);
		if (result != 0)
		{
			if (result == ENOENT)
			{
				result = 0;
			}
			bContinueFlag = false;
			break;
		}

		total_count++;
		if (record.op_type == STORAGE_OP_TYPE_SOURCE_CREATE_FILE
		 || record.op_type == STORAGE_OP_TYPE_REPLICA_CREATE_FILE)
		{
			bool bTrunkFile;

			if (fdfs_is_trunk_file(record.filename, \
					record.filename_len))
			{
			char *pTrunkPathEnd;
			char *pLocalFilename;

			bTrunkFile = true;
			if (fdfs_decode_trunk_info(record.store_path_index, \
				record.true_filename, record.true_filename_len,\
				&trunk_info) != 0)
			{
				pReader->binlog_offset += record_length;
				count++;
				continue;
			}

			trunk_get_full_filename(&trunk_info, \
                		local_filename, sizeof(local_filename));

			pTrunkPathEnd = strrchr(record.filename, '/');
			pLocalFilename = strrchr(local_filename, '/');
			if (pTrunkPathEnd == NULL || pLocalFilename == NULL)
			{
				pReader->binlog_offset += record_length;
				count++;
				continue;
			}
			sprintf(pTrunkPathEnd + 1, "%s", pLocalFilename + 1);
			}
			else
			{
			bTrunkFile = false;
			sprintf(local_filename, "%s/data/%s", \
				g_fdfs_store_paths.paths[record.store_path_index], \
				record.true_filename);
			}

			result = storage_download_file_to_file(pTrackerServer, \
					pStorageConn, g_group_name, \
					record.filename, local_filename, \
					&file_size);
			if (result == 0)
			{
				if (!bTrunkFile)
				{
					set_file_utimes(local_filename, \
						record.timestamp);
				}

				success_count++;
			}
			else if (result != ENOENT)
			{
				break;
			}
		}
		else if (record.op_type == STORAGE_OP_TYPE_SOURCE_CREATE_LINK
		      || record.op_type == STORAGE_OP_TYPE_REPLICA_CREATE_LINK)
		{
			if (record.src_filename_len == 0)
			{
				logError("file: "__FILE__", line: %d, " \
					"invalid binlog line, filename: %s, " \
					"expect src filename", __LINE__, \
					record.filename);
				result = EINVAL;
				bContinueFlag = false;
				break;
			}

			if ((result=storage_split_filename_ex(record.filename, \
				&record.filename_len, record.true_filename, \
				&store_path_index)) != 0)
			{
				bContinueFlag = false;
				break;
			}
			sprintf(local_filename, "%s/data/%s", \
				g_fdfs_store_paths.paths[store_path_index], \
				record.true_filename);

			if ((result=storage_split_filename_ex( \
				record.src_filename, &record.src_filename_len,\
				record.true_filename, &store_path_index)) != 0)
			{
				bContinueFlag = false;
				break;
			}
			sprintf(src_filename, "%s/data/%s", \
				g_fdfs_store_paths.paths[store_path_index], \
				record.true_filename);
			if (symlink(src_filename, local_filename) == 0)
			{
				success_count++;
			}
			else
			{
				result = errno != 0 ? errno : ENOENT;
				if (result == ENOENT || result == EEXIST)
				{
					log_level = LOG_DEBUG;
				}
				else
				{
					log_level = LOG_ERR;
				}

				log_it_ex(&g_log_context, log_level, \
					"file: "__FILE__", line: %d, " \
					"link file %s to %s fail, " \
					"errno: %d, error info: %s", __LINE__,\
					src_filename, local_filename, \
					result, STRERROR(result));

				if (result != ENOENT && result != EEXIST)
				{
					bContinueFlag = false;
					break;
				}
			}
		}
		else
		{
			logError("file: "__FILE__", line: %d, " \
				"invalid file op type: %d", \
				__LINE__, record.op_type);
			result = EINVAL;
			bContinueFlag = false;
			break;
		}

		pReader->binlog_offset += record_length;
		count++;
		if (count == 1000)
		{
			logDebug("file: "__FILE__", line: %d, " \
				"disk recovery: recover path: %s, " \
				"file count: %"PRId64 \
				", success count: %"PRId64, \
				__LINE__, pBasePath, total_count, \
				success_count);
			recovery_write_to_mark_file(pBasePath, pReader);
			count = 0;
		}
	}

	tracker_disconnect_server_ex(pStorageConn, result != 0);
	if (count > 0)
	{
		recovery_write_to_mark_file(pBasePath, pReader);
		count = 0;

		logInfo("file: "__FILE__", line: %d, " \
			"disk recovery: recover path: %s, " \
			"file count: %"PRId64 \
			", success count: %"PRId64, \
			__LINE__, pBasePath, total_count, success_count);
	}
	else
	{
		sleep(5);
	}
	}

	if (result == 0)
	{
		logInfo("file: "__FILE__", line: %d, " \
			"disk recovery: recover files of data path: %s done", \
			__LINE__, pBasePath);
	}

	return result;
}
コード例 #13
0
ファイル: tracker_client.c プロジェクト: alemic/fastdfs
int tracker_delete_storage(TrackerServerGroup *pTrackerGroup, \
		const char *group_name, const char *storage_id)
{
	ConnectionInfo *conn;
	TrackerHeader *pHeader;
	ConnectionInfo tracker_server;
	ConnectionInfo *pServer;
	ConnectionInfo *pEnd;
	FDFSStorageInfo storage_infos[1];
	char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + \
			FDFS_STORAGE_ID_MAX_SIZE];
	char in_buff[1];
	char *pInBuff;
	int64_t in_bytes;
	int result;
	int storage_id_len;
	int storage_count;
	int enoent_count;

	enoent_count = 0;
	pEnd = pTrackerGroup->servers + pTrackerGroup->server_count;
	for (pServer=pTrackerGroup->servers; pServer<pEnd; pServer++)
	{
		memcpy(&tracker_server, pServer, sizeof(ConnectionInfo));
		tracker_server.sock = -1;
		if ((conn=tracker_connect_server(&tracker_server, &result)) == NULL)
		{
			return result;
		}

		result = tracker_list_servers(conn, group_name, storage_id, \
				storage_infos, 1, &storage_count);
		tracker_disconnect_server_ex(conn, result != 0 && result != ENOENT);
		if (result != 0 && result != ENOENT)
		{
			return result;
		}

		if (result == ENOENT || storage_count == 0)
		{
			enoent_count++;
			continue;
		}

		if (storage_infos[0].status == FDFS_STORAGE_STATUS_ONLINE
		   || storage_infos[0].status == FDFS_STORAGE_STATUS_ACTIVE)
		{
			return EBUSY;
		}
	}
	if (enoent_count == pTrackerGroup->server_count)
	{
		return ENOENT;
	}

	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;
	snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - \
			sizeof(TrackerHeader),  "%s", group_name);
	storage_id_len = snprintf(out_buff + sizeof(TrackerHeader) + \
			FDFS_GROUP_NAME_MAX_LEN, \
			sizeof(out_buff) - sizeof(TrackerHeader) - \
			FDFS_GROUP_NAME_MAX_LEN,  "%s", storage_id);
	
	long2buff(FDFS_GROUP_NAME_MAX_LEN + storage_id_len, pHeader->pkg_len);
	pHeader->cmd = TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE;

	enoent_count = 0;
	result = 0;
	for (pServer=pTrackerGroup->servers; pServer<pEnd; pServer++)
	{
		memcpy(&tracker_server, pServer, sizeof(ConnectionInfo));
		tracker_server.sock = -1;
		if ((conn=tracker_connect_server(&tracker_server, &result)) == NULL)
		{
			return result;
		}

		if ((result=tcpsenddata_nb(conn->sock, out_buff, \
			sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + 
			storage_id_len, g_fdfs_network_timeout)) != 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"send data to tracker server %s:%d fail, " \
				"errno: %d, error info: %s", __LINE__, \
				tracker_server.ip_addr, tracker_server.port, \
				result, STRERROR(result));
		}
		else
		{
			pInBuff = in_buff;
			result = fdfs_recv_response(conn, &pInBuff, 0, &in_bytes);
		}

		tracker_disconnect_server_ex(conn, result != 0 && result != ENOENT);
		if (result != 0)
		{
			if (result == ENOENT)
			{
				enoent_count++;
			}
			else if (result == EALREADY)
			{
			}
			else
			{
				return result;
			}
		}
	}

	if (enoent_count == pTrackerGroup->server_count)
	{
		return ENOENT;
	}

	return result == ENOENT ? 0 : result;
}
コード例 #14
0
static int do_notify_leader_changed(ConnectionInfo *pTrackerServer, \
		ConnectionInfo *pLeader, const char cmd, bool *bConnectFail)
{
	char out_buff[sizeof(TrackerHeader) + FDFS_PROTO_IP_PORT_SIZE];
	char in_buff[1];
	ConnectionInfo *conn;
	TrackerHeader *pHeader;
	char *pInBuff;
	int64_t in_bytes;
	int result;

	pTrackerServer->sock = -1;
	if ((conn=tracker_connect_server(pTrackerServer, &result)) == NULL)
	{
		*bConnectFail = true;
		return result;
	}
	*bConnectFail = false;

	do
	{
	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;
	pHeader->cmd = cmd;
	sprintf(out_buff + sizeof(TrackerHeader), "%s:%d", \
			pLeader->ip_addr, pLeader->port);
	long2buff(FDFS_PROTO_IP_PORT_SIZE, pHeader->pkg_len);
	if ((result=tcpsenddata_nb(conn->sock, out_buff, \
			sizeof(out_buff), g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to tracker server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pTrackerServer->ip_addr, \
			pTrackerServer->port, \
			result, STRERROR(result));

		result = (result == ENOENT ? EACCES : result);
		break;
	}

	pInBuff = in_buff;
	result = fdfs_recv_response(conn, &pInBuff, \
				0, &in_bytes);
	if (result != 0)
	{
		break;
	}

	if (in_bytes != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response data " \
			"length: %"PRId64" is invalid, " \
			"expect length: %d.", __LINE__, \
			pTrackerServer->ip_addr, pTrackerServer->port, \
			in_bytes, 0);
		result = EINVAL;
		break;
	}
	} while (0);

	if (pTrackerServer->port == g_server_port && \
		is_local_host_ip(pTrackerServer->ip_addr))
	{
		tracker_disconnect_server_ex(conn, true);
	}
	else
	{
		tracker_disconnect_server_ex(conn, result != 0);
	}

	return result;
}
コード例 #15
0
int fdfs_get_storage_ids_from_tracker_server(ConnectionInfo *pTrackerServer)
{
#define MAX_REQUEST_LOOP   32
	TrackerHeader *pHeader;
	ConnectionInfo *conn;
	char out_buff[sizeof(TrackerHeader) + sizeof(int)];
	char *p;
	char *response;
	struct data_info {
		char *buffer;  //for free
		char *content;
		int length;
	} data_list[MAX_REQUEST_LOOP];
	int list_count;
	int total_count;
	int current_count;
	int result;
	int i;
	int start_index;
	int64_t in_bytes;

	if ((conn=tracker_connect_server(pTrackerServer, &result)) == NULL)
	{
		return result;
	}

	memset(data_list, 0, sizeof(data_list));
	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;
	p = out_buff + sizeof(TrackerHeader);
	pHeader->cmd = TRACKER_PROTO_CMD_STORAGE_FETCH_STORAGE_IDS;
	long2buff(sizeof(int), pHeader->pkg_len);

	start_index = 0;
	list_count = 0;
	result = 0;
	while (1)
	{
		int2buff(start_index, p);
		if ((result=tcpsenddata_nb(conn->sock, out_buff, \
			sizeof(out_buff), g_fdfs_network_timeout)) != 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"send data to tracker server %s:%d fail, " \
				"errno: %d, error info: %s", __LINE__, \
				pTrackerServer->ip_addr, \
				pTrackerServer->port, \
				result, STRERROR(result));
		}
		else
		{
			response = NULL;
			result = fdfs_recv_response(conn, \
				&response, 0, &in_bytes);
            if (result != 0)
            {
                logError("file: "__FILE__", line: %d, "
                        "fdfs_recv_response fail, result: %d",
                        __LINE__, result);
            }
		}

		if (result != 0)
		{
			break;
		}

		if (in_bytes < 2 * sizeof(int))
		{
			logError("file: "__FILE__", line: %d, " \
				"tracker server %s:%d, recv data length: %d "\
				"is invalid", __LINE__, 
				pTrackerServer->ip_addr, \
				pTrackerServer->port, (int)in_bytes);
			result = EINVAL;
			break;
		}

		total_count = buff2int(response);
		current_count = buff2int(response + sizeof(int));
		if (total_count <= start_index)
		{
			logError("file: "__FILE__", line: %d, " \
				"tracker server %s:%d, total storage " \
				"count: %d is invalid, which <= start " \
				"index: %d", __LINE__, pTrackerServer->ip_addr,\
				pTrackerServer->port, total_count, start_index);
			result = EINVAL;
			break;
		}

		if (current_count <= 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"tracker server %s:%d, current storage " \
				"count: %d is invalid, which <= 0", \
				__LINE__, pTrackerServer->ip_addr,\
				pTrackerServer->port, current_count);
			result = EINVAL;
			break;
		}

		data_list[list_count].buffer = response;
		data_list[list_count].content = response + 2 * sizeof(int);
		data_list[list_count].length = in_bytes - 2 * sizeof(int);
		list_count++;

		/*
		//logInfo("list_count: %d, total_count: %d, current_count: %d", 
			list_count, total_count, current_count);
		*/

		start_index += current_count;
		if (start_index >= total_count)
		{
			break;
		}

		if (list_count == MAX_REQUEST_LOOP)
		{
			logError("file: "__FILE__", line: %d, " \
				"response data from tracker " \
				"server %s:%d is too large", \
				__LINE__, pTrackerServer->ip_addr,\
				pTrackerServer->port);
			result = ENOSPC;
			break;
		}
	}

	tracker_disconnect_server_ex(conn, result != 0);

	if (result == 0)
	{
		do
		{
			int total_length;
			char *content;

			total_length = 0;
			for (i=0; i<list_count; i++)
			{
				total_length += data_list[i].length;
			}

			content = (char *)malloc(total_length + 1);
			if (content == NULL)
			{
				result = errno != 0 ? errno : ENOMEM;
				logError("file: "__FILE__", line: %d, " \
					"malloc %d bytes fail, " \
					"errno: %d, error info: %s", \
					__LINE__, total_length + 1, \
					result, STRERROR(result));
				break;
			}

			p = content;
			for (i=0; i<list_count; i++)
			{
				memcpy(p, data_list[i].content, data_list[i].length);
				p += data_list[i].length;
			}
			*p = '\0';

			//logInfo("list_count: %d, storage ids:\n%s", list_count, content);

			result = fdfs_load_storage_ids(content, \
					"storage-ids-from-tracker");
			free(content);
		} while (0);
	}

	for (i=0; i<list_count; i++)
	{
		free(data_list[i].buffer);
	}

	return result;
}
コード例 #16
0
ファイル: fdfs_appender_test1.c プロジェクト: CCoder123/pproj
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;
}
コード例 #17
0
int storage_disk_recovery_start(const int store_path_index)
{
	ConnectionInfo srcStorage;
	ConnectionInfo *pStorageConn;
	int result;
	char *pBasePath;

	pBasePath = g_fdfs_store_paths.paths[store_path_index];
	if ((result=recovery_init_mark_file(pBasePath, false)) != 0)
	{
		return result;
	}

	if ((result=recovery_init_binlog_file(pBasePath)) != 0)
	{
		return result;
	}

	if ((result=recovery_get_src_storage_server(&srcStorage)) != 0)
	{
		if (result == ENOENT)
		{
			return storage_disk_recovery_finish(pBasePath);
		}
		else
		{
			return result;
		}
	}

	while (g_continue_flag)
	{
		if (storage_report_storage_status(g_my_server_id_str, \
			g_tracker_client_ip, FDFS_STORAGE_STATUS_RECOVERY) == 0)
		{
			break;
		}
	}

	if (!g_continue_flag)
	{
		return EINTR;
	}

	if ((pStorageConn=tracker_connect_server(&srcStorage, &result)) == NULL)
	{
		return result;
	}

	result = storage_do_fetch_binlog(pStorageConn, store_path_index);
	tracker_disconnect_server_ex(pStorageConn, result != 0);
	if (result != 0)
	{
		return result;
	}

	//set fetch binlog done
	if ((result=recovery_init_mark_file(pBasePath, true)) != 0)
	{
		return result;
	}

	if ((result=storage_disk_recovery_split_trunk_binlog( \
			store_path_index)) != 0)
	{
		char markFullFilename[MAX_PATH_SIZE];
		unlink(recovery_get_mark_filename(pBasePath, markFullFilename));
		return result;
	}

	return 0;
}
コード例 #18
0
ファイル: tracker_client.c プロジェクト: alemic/fastdfs
int tracker_set_trunk_server(TrackerServerGroup *pTrackerGroup, \
		const char *group_name, const char *storage_id, \
		char *new_trunk_server_id)
{
	TrackerHeader *pHeader;
	ConnectionInfo *conn;
	ConnectionInfo *pServer;
	ConnectionInfo *pEnd;
	ConnectionInfo tracker_server;
	char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + \
			FDFS_STORAGE_ID_MAX_SIZE];
	char in_buff[FDFS_STORAGE_ID_MAX_SIZE];
	char *pInBuff;
	int64_t in_bytes;
	int result;
	int storage_id_len;

	*new_trunk_server_id = '\0';
	memset(out_buff, 0, sizeof(out_buff));
	memset(in_buff, 0, sizeof(in_buff));
	pHeader = (TrackerHeader *)out_buff;
	snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - \
			sizeof(TrackerHeader),  "%s", group_name);
	if (storage_id == NULL)
	{
		storage_id_len = 0;
	}
	else
	{
		storage_id_len = snprintf(out_buff + sizeof(TrackerHeader) + \
				FDFS_GROUP_NAME_MAX_LEN, \
				sizeof(out_buff) - sizeof(TrackerHeader) - \
				FDFS_GROUP_NAME_MAX_LEN,  "%s", storage_id);
	}
	
	long2buff(FDFS_GROUP_NAME_MAX_LEN + storage_id_len, pHeader->pkg_len);
	pHeader->cmd = TRACKER_PROTO_CMD_SERVER_SET_TRUNK_SERVER;

	result = 0;
	pEnd = pTrackerGroup->servers + pTrackerGroup->server_count;
	for (pServer=pTrackerGroup->servers; pServer<pEnd; pServer++)
	{
		memcpy(&tracker_server, pServer, sizeof(ConnectionInfo));
		tracker_server.sock = -1;
		if ((conn=tracker_connect_server(&tracker_server, &result)) == NULL)
		{
			continue;
		}

		if ((result=tcpsenddata_nb(conn->sock, out_buff, \
			sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + 
			storage_id_len, g_fdfs_network_timeout)) != 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"send data to tracker server %s:%d fail, " \
				"errno: %d, error info: %s", __LINE__, \
				tracker_server.ip_addr, tracker_server.port, \
				result, STRERROR(result));

			tracker_disconnect_server_ex(conn, true);
			continue;
		}

		pInBuff = in_buff;
		result = fdfs_recv_response(conn, &pInBuff, \
				sizeof(in_buff) - 1, &in_bytes);

		tracker_disconnect_server_ex(conn, result != 0);
		if (result == 0)
		{
			strcpy(new_trunk_server_id, in_buff);
			return 0;
		}

		if (result == EOPNOTSUPP)
		{
			continue;
		}
		if (result == EALREADY)
		{
			if (storage_id_len > 0)
			{
				strcpy(new_trunk_server_id, storage_id);
			}
			return result;
		}
		else
		{
			return result;
		}
	}

	return result;
}
コード例 #19
0
ファイル: tracker_client.c プロジェクト: alemic/fastdfs
ConnectionInfo *tracker_get_connection_r_ex(TrackerServerGroup *pTrackerGroup, \
		ConnectionInfo *pTrackerServer, int *err_no)
{
	ConnectionInfo *conn;
	ConnectionInfo *pCurrentServer;
	ConnectionInfo *pServer;
	ConnectionInfo *pEnd;
	int server_index;

	server_index = pTrackerGroup->server_index;
	if (server_index >= pTrackerGroup->server_count)
	{
		server_index = 0;
	}

	do
	{
	pCurrentServer = pTrackerGroup->servers + server_index;
	memcpy(pTrackerServer, pCurrentServer, sizeof(ConnectionInfo));
	pTrackerServer->sock = -1;
	if ((conn=tracker_connect_server(pTrackerServer, err_no)) != NULL)
	{
		break;
	}

	pEnd = pTrackerGroup->servers + pTrackerGroup->server_count;
	for (pServer=pCurrentServer+1; pServer<pEnd; pServer++)
	{
		memcpy(pTrackerServer, pServer, sizeof(ConnectionInfo));
		pTrackerServer->sock = -1;
		if ((conn=tracker_connect_server(pTrackerServer, err_no)) != NULL)
		{
			pTrackerGroup->server_index = pServer - \
							pTrackerGroup->servers;
			break;
		}
	}

	if (conn != NULL)
	{
		break;
	}

	for (pServer=pTrackerGroup->servers; pServer<pCurrentServer; pServer++)
	{
		memcpy(pTrackerServer, pServer, sizeof(ConnectionInfo));
		pTrackerServer->sock = -1;
		if ((conn=tracker_connect_server(pTrackerServer, err_no)) != NULL)
		{
			pTrackerGroup->server_index = pServer - \
							pTrackerGroup->servers;
			break;
		}
	}
	} while (0);

	pTrackerGroup->server_index++;
	if (pTrackerGroup->server_index >= pTrackerGroup->server_count)
	{
		pTrackerGroup->server_index = 0;
	}

	return conn;
}