static int trunk_client_trunk_confirm_or_free(TrackerServerInfo *pTrunkServer,\ const FDFSTrunkFullInfo *pTrunkInfo, const int cmd, \ const int status) { TrackerHeader *pHeader; FDFSTrunkInfoBuff *pTrunkBuff; int64_t in_bytes; int result; char out_buff[sizeof(TrackerHeader) \ + STORAGE_TRUNK_ALLOC_CONFIRM_REQ_BODY_LEN]; pHeader = (TrackerHeader *)out_buff; pTrunkBuff = (FDFSTrunkInfoBuff *)(out_buff + sizeof(TrackerHeader) \ + FDFS_GROUP_NAME_MAX_LEN); memset(out_buff, 0, sizeof(out_buff)); snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - \ sizeof(TrackerHeader), "%s", g_group_name); long2buff(STORAGE_TRUNK_ALLOC_CONFIRM_REQ_BODY_LEN, pHeader->pkg_len); pHeader->cmd = cmd; pHeader->status = status; pTrunkBuff->store_path_index = pTrunkInfo->path.store_path_index; pTrunkBuff->sub_path_high = pTrunkInfo->path.sub_path_high; pTrunkBuff->sub_path_low = pTrunkInfo->path.sub_path_low; int2buff(pTrunkInfo->file.id, pTrunkBuff->id); int2buff(pTrunkInfo->file.offset, pTrunkBuff->offset); int2buff(pTrunkInfo->file.size, pTrunkBuff->size); if ((result=tcpsenddata_nb(pTrunkServer->sock, out_buff, \ sizeof(out_buff), g_fdfs_network_timeout)) != 0) { logError("file: "__FILE__", line: %d, " \ "send data to storage server %s:%d fail, " \ "errno: %d, error info: %s", __LINE__, \ pTrunkServer->ip_addr, pTrunkServer->port, \ result, STRERROR(result)); return result; } if ((result=fdfs_recv_header(pTrunkServer, &in_bytes)) != 0) { return result; } if (in_bytes != 0) { logError("file: "__FILE__", line: %d, " \ "storage server %s:%d response data " \ "length: "INT64_PRINTF_FORMAT" is invalid, " \ "should == 0", __LINE__, pTrunkServer->ip_addr, \ pTrunkServer->port, in_bytes); return EINVAL; } return 0; }
static int storage_do_fetch_binlog(ConnectionInfo *pSrcStorage, \ const int store_path_index) { char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + 1]; char full_binlog_filename[MAX_PATH_SIZE]; TrackerHeader *pHeader; char *pBasePath; int64_t in_bytes; int64_t file_bytes; int result; pBasePath = g_fdfs_store_paths.paths[store_path_index]; recovery_get_binlog_filename(pBasePath, full_binlog_filename); memset(out_buff, 0, sizeof(out_buff)); pHeader = (TrackerHeader *)out_buff; long2buff(FDFS_GROUP_NAME_MAX_LEN + 1, pHeader->pkg_len); pHeader->cmd = STORAGE_PROTO_CMD_FETCH_ONE_PATH_BINLOG; strcpy(out_buff + sizeof(TrackerHeader), g_group_name); *(out_buff + sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN) = \ store_path_index; if((result=tcpsenddata_nb(pSrcStorage->sock, out_buff, \ sizeof(out_buff), g_fdfs_network_timeout)) != 0) { logError("file: "__FILE__", line: %d, " \ "tracker server %s:%d, send data fail, " \ "errno: %d, error info: %s.", \ __LINE__, pSrcStorage->ip_addr, pSrcStorage->port, \ result, STRERROR(result)); return result; } if ((result=fdfs_recv_header(pSrcStorage, &in_bytes)) != 0) { return result; } if ((result=tcprecvfile(pSrcStorage->sock, full_binlog_filename, \ in_bytes, 0, g_fdfs_network_timeout, \ &file_bytes)) != 0) { logError("file: "__FILE__", line: %d, " \ "tracker server %s:%d, tcprecvfile fail, " \ "errno: %d, error info: %s.", \ __LINE__, pSrcStorage->ip_addr, pSrcStorage->port, \ result, STRERROR(result)); return result; } logInfo("file: "__FILE__", line: %d, " \ "recovery binlog file size: %"PRId64, \ __LINE__, file_bytes); return 0; }
int fdfs_deal_no_body_cmd(ConnectionInfo *pTrackerServer, const int cmd) { TrackerHeader header; int result; int64_t in_bytes; memset(&header, 0, sizeof(header)); header.cmd = cmd; result = tcpsenddata_nb(pTrackerServer->sock, &header, \ sizeof(header), g_fdfs_network_timeout); if(result != 0) { logError("file: "__FILE__", line: %d, " \ "tracker server ip: %s, send data fail, " \ "errno: %d, error info: %s", \ __LINE__, pTrackerServer->ip_addr, \ result, STRERROR(result)); return result; } result = fdfs_recv_header(pTrackerServer, &in_bytes); if (result != 0) { return result; } if (in_bytes == 0) { return 0; } else { logError("file: "__FILE__", line: %d, " \ "server ip: %s, expect body length 0, " \ "but received: "INT64_PRINTF_FORMAT, __LINE__, \ pTrackerServer->ip_addr, in_bytes); return EINVAL; } }
int fdfs_recv_response(ConnectionInfo *pTrackerServer, \ char **buff, const int buff_size, \ int64_t *in_bytes) { int result; bool bMalloced; result = fdfs_recv_header(pTrackerServer, in_bytes); if (result != 0) { return result; } if (*in_bytes == 0) { return 0; } if (*buff == NULL) { *buff = (char *)malloc((*in_bytes) + 1); if (*buff == NULL) { *in_bytes = 0; logError("file: "__FILE__", line: %d, " \ "malloc "INT64_PRINTF_FORMAT" bytes fail", \ __LINE__, (*in_bytes) + 1); return errno != 0 ? errno : ENOMEM; } bMalloced = true; } else { if (*in_bytes > buff_size) { logError("file: "__FILE__", line: %d, " \ "server: %s:%d, recv body bytes: " \ INT64_PRINTF_FORMAT" exceed max: %d", \ __LINE__, pTrackerServer->ip_addr, \ pTrackerServer->port, *in_bytes, buff_size); *in_bytes = 0; return ENOSPC; } bMalloced = false; } if ((result=tcprecvdata_nb(pTrackerServer->sock, *buff, \ *in_bytes, g_fdfs_network_timeout)) != 0) { logError("file: "__FILE__", line: %d, " \ "tracker server: %s:%d, recv data fail, " \ "errno: %d, error info: %s", \ __LINE__, pTrackerServer->ip_addr, \ pTrackerServer->port, \ result, STRERROR(result)); *in_bytes = 0; if (bMalloced) { free(*buff); *buff = NULL; } return result; } return 0; }