📄 storage_func.c
字号:
"errno: %d, error info: %s", \ __LINE__, data_path, dir_name, \ errno, strerror(errno)); return errno != 0 ? errno : ENOENT; } } if (chdir(dir_name) != 0) { logError("file: "__FILE__", line: %d, " \ "chdir \"%s/%s\" fail, " \ "errno: %d, error info: %s", \ __LINE__, data_path, dir_name, \ errno, strerror(errno)); return errno != 0 ? errno : ENOENT; } for (k=0; k<g_subdir_count_per_path; k++) { sprintf(sub_name, STORAGE_DATA_DIR_FORMAT, k); if (mkdir(sub_name, 0755) != 0) { if (!(errno == EEXIST && isDir(sub_name))) { logError("file: "__FILE__", line: %d," \ " mkdir \"%s/%s/%s\" fail, " \ "errno: %d, error info: %s", \ __LINE__, data_path, \ dir_name, sub_name, \ errno, strerror(errno)); return errno != 0 ? errno : ENOENT; } } } if (chdir("..") != 0) { logError("file: "__FILE__", line: %d, " \ "chdir \"%s\" fail, " \ "errno: %d, error info: %s", \ __LINE__, data_path, \ errno, strerror(errno)); return errno != 0 ? errno : ENOENT; } } fprintf(stderr, "data path: %s, mkdir sub dir done.\n", data_path); return 0;}static int storage_cmp_by_ip_and_port(const void *p1, const void *p2){ int res; res = strcmp(((TrackerServerInfo *)p1)->ip_addr, \ ((TrackerServerInfo *)p2)->ip_addr); if (res != 0) { return res; } return ((TrackerServerInfo *)p1)->port - \ ((TrackerServerInfo *)p2)->port;}static void insert_into_sorted_servers(TrackerServerInfo *pInsertedServer){ TrackerServerInfo *pDestServer; for (pDestServer=g_tracker_servers+g_tracker_server_count; \ pDestServer>g_tracker_servers; pDestServer--) { if (storage_cmp_by_ip_and_port(pInsertedServer, \ pDestServer-1) > 0) { memcpy(pDestServer, pInsertedServer, \ sizeof(TrackerServerInfo)); return; } memcpy(pDestServer, pDestServer-1, sizeof(TrackerServerInfo)); } memcpy(pDestServer, pInsertedServer, sizeof(TrackerServerInfo));}static int copy_tracker_servers(const char *filename, char **ppTrackerServers){ char **ppSrc; char **ppEnd; TrackerServerInfo destServer; char *pSeperator; char szHost[128]; int nHostLen; memset(&destServer, 0, sizeof(TrackerServerInfo)); ppEnd = ppTrackerServers + g_tracker_server_count; g_tracker_server_count = 0; for (ppSrc=ppTrackerServers; ppSrc<ppEnd; ppSrc++) { if ((pSeperator=strchr(*ppSrc, ':')) == NULL) { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\", " \ "tracker_server \"%s\" is invalid, " \ "correct format is host:port", \ __LINE__, filename, *ppSrc); return EINVAL; } nHostLen = pSeperator - (*ppSrc); if (nHostLen >= sizeof(szHost)) { nHostLen = sizeof(szHost) - 1; } memcpy(szHost, *ppSrc, nHostLen); szHost[nHostLen] = '\0'; if (getIpaddrByName(szHost, destServer.ip_addr, \ sizeof(destServer.ip_addr)) == INADDR_NONE) { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\", " \ "host \"%s\" is invalid", \ __LINE__, filename, szHost); return EINVAL; } if (strcmp(destServer.ip_addr, "127.0.0.1") == 0) { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\", " \ "host \"%s\" is invalid, " \ "ip addr can't be 127.0.0.1", \ __LINE__, filename, szHost); return EINVAL; } destServer.port = atoi(pSeperator+1); if (destServer.port <= 0) { destServer.port = FDFS_TRACKER_SERVER_DEF_PORT; } if (bsearch(&destServer, g_tracker_servers, \ g_tracker_server_count, \ sizeof(TrackerServerInfo), \ storage_cmp_by_ip_and_port) == NULL) { insert_into_sorted_servers(&destServer); g_tracker_server_count++; } } /* { TrackerServerInfo *pServer; for (pServer=g_tracker_servers; pServer<g_tracker_servers+ \ g_tracker_server_count; pServer++) { //printf("server=%s:%d\n", \ pServer->ip_addr, pServer->port); } } */ return 0;}/*static int init_fsync_pthread_cond(){ int result; pthread_condattr_t thread_condattr; if ((result=pthread_condattr_init(&thread_condattr)) != 0) { logError("file: "__FILE__", line: %d, " \ "pthread_condattr_init failed, " \ "errno: %d, error info: %s", \ __LINE__, result, strerror(result)); return result; } if ((result=pthread_cond_init(&fsync_thread_cond, &thread_condattr)) != 0) { logError("file: "__FILE__", line: %d, " \ "pthread_cond_init failed, " \ "errno: %d, error info: %s", \ __LINE__, result, strerror(result)); return result; } pthread_condattr_destroy(&thread_condattr); return 0;}*/int storage_load_paths(IniItemInfo *items, const int nItemCount){ char item_name[64]; char *pPath; int i; pPath = iniGetStrValue("base_path", items, nItemCount); if (pPath == NULL) { logError("file: "__FILE__", line: %d, " \ "conf file must have item \"base_path\"!", __LINE__); return ENOENT; } snprintf(g_base_path, sizeof(g_base_path), "%s", pPath); chopPath(g_base_path); if (!fileExists(g_base_path)) { logError("file: "__FILE__", line: %d, " \ "\"%s\" can't be accessed, error info: %s", \ __LINE__, strerror(errno), g_base_path); return errno != 0 ? errno : ENOENT; } if (!isDir(g_base_path)) { logError("file: "__FILE__", line: %d, " \ "\"%s\" is not a directory!", \ __LINE__, g_base_path); return ENOTDIR; } g_path_count = iniGetIntValue("store_path_count", \ items, nItemCount, 1); if (g_path_count <= 0) { logError("file: "__FILE__", line: %d, " \ "store_path_count: %d is invalid!", \ __LINE__, g_path_count); return EINVAL; } g_store_paths = (char **)malloc(sizeof(char *) *g_path_count); if (g_store_paths == NULL) { logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, errno: %d, error info: %s", \ __LINE__, sizeof(char *) *g_path_count, \ errno, strerror(errno)); return errno != 0 ? errno : ENOMEM; } memset(g_store_paths, 0, sizeof(char *) *g_path_count); pPath = iniGetStrValue("store_path0", items, nItemCount); if (pPath == NULL) { pPath = g_base_path; } g_store_paths[0] = strdup(pPath); if (g_store_paths[0] == NULL) { logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, errno: %d, error info: %s", \ __LINE__, strlen(pPath), errno, strerror(errno)); return errno != 0 ? errno : ENOMEM; } for (i=1; i<g_path_count; i++) { sprintf(item_name, "store_path%d", i); pPath = iniGetStrValue(item_name, items, nItemCount); if (pPath == NULL) { logError("file: "__FILE__", line: %d, " \ "conf file must have item \"%s\"!", \ __LINE__, item_name); return ENOENT; } chopPath(pPath); if (!fileExists(pPath)) { logError("file: "__FILE__", line: %d, " \ "\"%s\" can't be accessed, error info: %s", \ __LINE__, strerror(errno), pPath); return errno != 0 ? errno : ENOENT; } if (!isDir(pPath)) { logError("file: "__FILE__", line: %d, " \ "\"%s\" is not a directory!", \ __LINE__, pPath); return ENOTDIR; } g_store_paths[i] = strdup(pPath); if (g_store_paths[i] == NULL) { logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, " \ "errno: %d, error info: %s", __LINE__, \ strlen(pPath), errno, strerror(errno)); return errno != 0 ? errno : ENOMEM; } } return 0;}static int storage_get_time(IniItemInfo *items, const int nItemCount, \ const char *item_name, FDFSTimeInfo *pTimeInfo, \ const byte default_hour, const byte default_minute){ char *pValue; int hour; int minute; pValue = iniGetStrValue(item_name, items, nItemCount); if (pValue == NULL) { pTimeInfo->hour = default_hour; pTimeInfo->minute = default_minute; return 0; } if (sscanf(pValue, "%d:%d", &hour, &minute) != 2) { logError("file: "__FILE__", line: %d, " \ "item \"%s\" 's value \"%s\" is not an valid time", \ __LINE__, item_name, pValue); return EINVAL; } if ((hour < 0 || hour > 23) || (minute < 0 || minute > 59)) { logError("file: "__FILE__", line: %d, " \ "item \"%s\" 's value \"%s\" is not an valid time", \ __LINE__, item_name, pValue); return EINVAL; } pTimeInfo->hour = (byte)hour; pTimeInfo->minute = (byte)minute; return 0;}int storage_func_init(const char *filename, \ char *bind_addr, const int addr_size){ char *pBindAddr; char *pGroupName; char *pRunByGroup; char *pRunByUser; char *pFsyncAfterWrittenBytes; char *ppTrackerServers[FDFS_MAX_TRACKERS]; IniItemInfo *items; int nItemCount; int result; int64_t fsync_after_written_bytes; /* while (nThreadCount > 0) { sleep(1); } */ if ((result=iniLoadItems(filename, &items, &nItemCount)) != 0) { logError("file: "__FILE__", line: %d, " \ "load conf file \"%s\" fail, ret code: %d", \ __LINE__, filename, result); return result; } while (1) { if (iniGetBoolValue("disabled", items, nItemCount)) { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\" disabled=true, exit", \ __LINE__, filename); result = ECANCELED; break; } g_subdir_count_per_path=iniGetIntValue("subdir_count_per_path", items, nItemCount, DEFAULT_DATA_DIR_COUNT_PER_PATH); if (g_subdir_count_per_path <= 0 || \ g_subdir_count_per_path > 256) { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\", invalid subdir_count: %d", \ __LINE__, filename, g_subdir_count_per_path); result = EINVAL; break; } if ((result=storage_load_paths(items, nItemCount)) != 0) { break; } load_log_level(items, nItemCount); if ((result=log_init(STORAGE_ERROR_LOG_FILENAME)) != 0) { break; } g_network_timeout = iniGetIntValue("network_timeout", \ items, nItemCount, DEFAULT_NETWORK_TIMEOUT); if (g_network_timeout <= 0) { g_network_timeout = DEFAULT_NETWORK_TIMEOUT; } g_server_port = iniGetIntValue("port", items, nItemCount, \ FDFS_STORAGE_SERVER_DEF_PORT); if (g_server_port <= 0) { g_server_port = FDFS_STORAGE_SERVER_DEF_PORT; } g_heart_beat_interval = iniGetIntValue("heart_beat_interval", \ items, nItemCount, STORAGE_BEAT_DEF_INTERVAL); if (g_heart_beat_interval <= 0) { g_heart_beat_interval = STORAGE_BEAT_DEF_INTERVAL; } g_stat_report_interval = iniGetIntValue("stat_report_interval",\ items, nItemCount, STORAGE_REPORT_DEF_INTERVAL); if (g_stat_report_interval <= 0) { g_stat_report_interval = STORAGE_REPORT_DEF_INTERVAL; } pBindAddr = iniGetStrValue("bind_addr", items, nItemCount); if (pBindAddr == NULL) { bind_addr[0] = '\0'; } else { snprintf(bind_addr, addr_size, "%s", pBindAddr); } pGroupName = iniGetStrValue("group_name", items, nItemCount); if (pGroupName == NULL) { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\" must have item " \ "\"group_name\"!", \ __LINE__, filename); result = ENOENT; break; } if (pGroupName[0] == '\0') { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\", " \ "group_name is empty!", \ __LINE__, filename); result = EINVAL; break; } snprintf(g_group_name, sizeof(g_group_name), "%s", pGroupName); if ((result=fdfs_validate_group_name(g_group_name)) != 0) \ { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\", " \ "the group name \"%s\" is invalid!", \ __LINE__, filename, g_group_name); result = EINVAL; break; } if ((g_tracker_server_count=iniGetValues("tracker_server", \ items, nItemCount, ppTrackerServers, \ FDFS_MAX_TRACKERS)) <= 0) { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\", " \ "get item \"tracker_server\" fail", \ __LINE__, filename); result = ENOENT; break; } g_tracker_servers = (TrackerServerInfo *)malloc( \ sizeof(TrackerServerInfo) * g_tracker_server_count); if (g_tracker_servers == NULL) { logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail", __LINE__, \ sizeof(TrackerServerInfo)*g_tracker_server_count); result = errno != 0 ? errno : ENOMEM;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -