⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 storage_func.c

📁 文件系统源代码!!!!! 文件系统源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*** Copyright (C) 2008 Happy Fish / YuQing** FastDFS may be copied only under the terms of the GNU General* Public License V3, which may be found in the FastDFS source kit.* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.**///storage_func.c#include <sys/types.h>#include <sys/stat.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <time.h>#include "fdfs_define.h"#include "logger.h"#include "fdfs_global.h"#include "sockopt.h"#include "shared_func.h"#include "ini_file_reader.h"#include "tracker_types.h"#include "tracker_proto.h"#include "storage_global.h"#include "storage_func.h"#define DATA_DIR_INITED_FILENAME	".data_init_flag"#define STORAGE_STAT_FILENAME		"storage_stat.dat"#define INIT_ITEM_STORAGE_JOIN_TIME	"storage_join_time"#define INIT_ITEM_SYNC_OLD_DONE		"sync_old_done"#define INIT_ITEM_SYNC_SRC_SERVER	"sync_src_server"#define INIT_ITEM_SYNC_UNTIL_TIMESTAMP	"sync_until_timestamp"#define STAT_ITEM_TOTAL_UPLOAD		"total_upload_count"#define STAT_ITEM_SUCCESS_UPLOAD	"success_upload_count"#define STAT_ITEM_TOTAL_DOWNLOAD	"total_download_count"#define STAT_ITEM_SUCCESS_DOWNLOAD	"success_download_count"#define STAT_ITEM_LAST_SOURCE_UPD	"last_source_update"#define STAT_ITEM_LAST_SYNC_UPD		"last_sync_update"#define STAT_ITEM_TOTAL_SET_META	"total_set_meta_count"#define STAT_ITEM_SUCCESS_SET_META	"success_set_meta_count"#define STAT_ITEM_TOTAL_DELETE		"total_delete_count"#define STAT_ITEM_SUCCESS_DELETE	"success_delete_count"#define STAT_ITEM_TOTAL_GET_META	"total_get_meta_count"#define STAT_ITEM_SUCCESS_GET_META	"success_get_meta_count"#define STAT_ITEM_DIST_PATH_INDEX_HIGH	"dist_path_index_high"#define STAT_ITEM_DIST_PATH_INDEX_LOW	"dist_path_index_low"#define STAT_ITEM_DIST_WRITE_FILE_COUNT	"dist_write_file_count"static int storage_stat_fd = -1;/*static pthread_mutex_t fsync_thread_mutex;static pthread_cond_t fsync_thread_cond;static int fsync_thread_count = 0;*/static int storage_open_storage_stat();static int storage_close_storage_stat();static int storage_make_data_dirs(const char *pBasePath);static int storage_check_and_make_data_dirs();static char *get_storage_stat_filename(const void *pArg, char *full_filename){	static char buff[MAX_PATH_SIZE];	if (full_filename == NULL)	{		full_filename = buff;	}	snprintf(full_filename, MAX_PATH_SIZE, \			"%s/data/%s", g_base_path, STORAGE_STAT_FILENAME);	return full_filename;}int storage_write_to_fd(int fd, get_filename_func filename_func, \		const void *pArg, const char *buff, const int len){	if (ftruncate(fd, 0) != 0)	{		logError("file: "__FILE__", line: %d, " \			"truncate file \"%s\" to empty fail, " \			"error no: %d, error info: %s", \			__LINE__, filename_func(pArg, NULL), \			errno, strerror(errno));		return errno != 0 ? errno : ENOENT;	}	if (lseek(fd, 0, SEEK_SET) != 0)	{		logError("file: "__FILE__", line: %d, " \			"rewind file \"%s\" to start fail, " \			"error no: %d, error info: %s", \			__LINE__, filename_func(pArg, NULL), \			errno, strerror(errno));		return errno != 0 ? errno : ENOENT;	}	if (write(fd, buff, len) != len)	{		logError("file: "__FILE__", line: %d, " \			"write to file \"%s\" fail, " \			"error no: %d, error info: %s", \			__LINE__, filename_func(pArg, NULL), \			errno, strerror(errno));		return errno != 0 ? errno : ENOENT;	}	if (fsync(fd) != 0)	{		logError("file: "__FILE__", line: %d, " \			"sync file \"%s\" to disk fail, " \			"error no: %d, error info: %s", \			__LINE__, filename_func(pArg, NULL), \			errno, strerror(errno));		return errno != 0 ? errno : ENOENT;	}	return 0;}static int storage_open_storage_stat(){	char full_filename[MAX_PATH_SIZE];	IniItemInfo *items;	int nItemCount;	int result;	get_storage_stat_filename(NULL, full_filename);	if (fileExists(full_filename))	{		if ((result=iniLoadItems(full_filename, &items, &nItemCount)) \			 != 0)		{			logError("file: "__FILE__", line: %d, " \				"load from stat file \"%s\" fail, " \				"error code: %d", \				__LINE__, full_filename, result);			return result;		}		if (nItemCount < 12)		{			iniFreeItems(items);			logError("file: "__FILE__", line: %d, " \				"in stat file \"%s\", item count: %d < 12", \				__LINE__, full_filename, nItemCount);			return ENOENT;		}		g_storage_stat.total_upload_count = iniGetInt64Value( \				STAT_ITEM_TOTAL_UPLOAD, \				items, nItemCount, 0);		g_storage_stat.success_upload_count = iniGetInt64Value( \				STAT_ITEM_SUCCESS_UPLOAD, \				items, nItemCount, 0);		g_storage_stat.total_download_count = iniGetInt64Value( \				STAT_ITEM_TOTAL_DOWNLOAD, \				items, nItemCount, 0);		g_storage_stat.success_download_count = iniGetInt64Value( \				STAT_ITEM_SUCCESS_DOWNLOAD, \				items, nItemCount, 0);		g_storage_stat.last_source_update = iniGetIntValue( \				STAT_ITEM_LAST_SOURCE_UPD, \				items, nItemCount, 0);		g_storage_stat.last_sync_update = iniGetIntValue( \				STAT_ITEM_LAST_SYNC_UPD, \				items, nItemCount, 0);		g_storage_stat.total_set_meta_count = iniGetInt64Value( \				STAT_ITEM_TOTAL_SET_META, \				items, nItemCount, 0);		g_storage_stat.success_set_meta_count = iniGetInt64Value( \				STAT_ITEM_SUCCESS_SET_META, \				items, nItemCount, 0);		g_storage_stat.total_delete_count = iniGetInt64Value( \				STAT_ITEM_TOTAL_DELETE, \				items, nItemCount, 0);		g_storage_stat.success_delete_count = iniGetInt64Value( \				STAT_ITEM_SUCCESS_DELETE, \				items, nItemCount, 0);		g_storage_stat.total_get_meta_count = iniGetInt64Value( \				STAT_ITEM_TOTAL_GET_META, \				items, nItemCount, 0);		g_storage_stat.success_get_meta_count = iniGetInt64Value( \				STAT_ITEM_SUCCESS_GET_META, \				items, nItemCount, 0);		g_dist_path_index_high = iniGetIntValue( \				STAT_ITEM_DIST_PATH_INDEX_HIGH, \				items, nItemCount, 0);		g_dist_path_index_low = iniGetIntValue( \				STAT_ITEM_DIST_PATH_INDEX_LOW, \				items, nItemCount, 0);		g_dist_write_file_count = iniGetIntValue( \				STAT_ITEM_DIST_WRITE_FILE_COUNT, \				items, nItemCount, 0);		iniFreeItems(items);	}	else	{		memset(&g_storage_stat, 0, sizeof(g_storage_stat));	}	storage_stat_fd = open(full_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);	if (storage_stat_fd < 0)	{		logError("file: "__FILE__", line: %d, " \			"open stat file \"%s\" fail, " \			"error no: %d, error info: %s", \			__LINE__, full_filename, errno, strerror(errno));		return errno != 0 ? errno : ENOENT;	}	return storage_write_to_stat_file();}static int storage_close_storage_stat(){	int result;	result = 0;	if (storage_stat_fd >= 0)	{		result = storage_write_to_stat_file();		if (close(storage_stat_fd) != 0)		{			result += errno != 0 ? errno : ENOENT;		}		storage_stat_fd = -1;	}	return result;}int storage_write_to_stat_file(){	char buff[512];	int len;	len = sprintf(buff, 		"%s="INT64_PRINTF_FORMAT"\n"  \		"%s="INT64_PRINTF_FORMAT"\n"  \		"%s="INT64_PRINTF_FORMAT"\n"  \		"%s="INT64_PRINTF_FORMAT"\n"  \		"%s=%d\n"  \		"%s=%d\n"  \		"%s="INT64_PRINTF_FORMAT"\n"  \		"%s="INT64_PRINTF_FORMAT"\n"  \		"%s="INT64_PRINTF_FORMAT"\n"  \		"%s="INT64_PRINTF_FORMAT"\n"  \		"%s="INT64_PRINTF_FORMAT"\n"  \		"%s="INT64_PRINTF_FORMAT"\n"  \		"%s=%d\n"  \		"%s=%d\n"  \		"%s=%d\n", \		STAT_ITEM_TOTAL_UPLOAD, g_storage_stat.total_upload_count, \		STAT_ITEM_SUCCESS_UPLOAD, g_storage_stat.success_upload_count, \		STAT_ITEM_TOTAL_DOWNLOAD, g_storage_stat.total_download_count, \		STAT_ITEM_SUCCESS_DOWNLOAD, \		g_storage_stat.success_download_count, \		STAT_ITEM_LAST_SOURCE_UPD, \		(int)g_storage_stat.last_source_update, \		STAT_ITEM_LAST_SYNC_UPD, (int)g_storage_stat.last_sync_update,\		STAT_ITEM_TOTAL_SET_META, g_storage_stat.total_set_meta_count, \		STAT_ITEM_SUCCESS_SET_META, \		g_storage_stat.success_set_meta_count, \		STAT_ITEM_TOTAL_DELETE, g_storage_stat.total_delete_count, \		STAT_ITEM_SUCCESS_DELETE, g_storage_stat.success_delete_count, \		STAT_ITEM_TOTAL_GET_META, g_storage_stat.total_get_meta_count, \		STAT_ITEM_SUCCESS_GET_META, \		g_storage_stat.success_get_meta_count,  \		STAT_ITEM_DIST_PATH_INDEX_HIGH, g_dist_path_index_high, \		STAT_ITEM_DIST_PATH_INDEX_LOW, g_dist_path_index_low, \		STAT_ITEM_DIST_WRITE_FILE_COUNT, g_dist_write_file_count	    );	return storage_write_to_fd(storage_stat_fd, \			get_storage_stat_filename, NULL, buff, len);}int storage_write_to_sync_ini_file(){	char full_filename[MAX_PATH_SIZE];	char buff[256];	int fd;	int len;	snprintf(full_filename, sizeof(full_filename), \		"%s/data/%s", g_base_path, DATA_DIR_INITED_FILENAME);	if ((fd=open(full_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)	{		logError("file: "__FILE__", line: %d, " \			"open file \"%s\" fail, " \			"errno: %d, error info: %s", \			__LINE__, full_filename, \			errno, strerror(errno));		return errno != 0 ? errno : ENOENT;	}	len = sprintf(buff, "%s=%d\n" \		"%s=%d\n"  \		"%s=%s\n"  \		"%s=%d\n", \		INIT_ITEM_STORAGE_JOIN_TIME, g_storage_join_time, \		INIT_ITEM_SYNC_OLD_DONE, g_sync_old_done, \		INIT_ITEM_SYNC_SRC_SERVER, g_sync_src_ip_addr, \		INIT_ITEM_SYNC_UNTIL_TIMESTAMP, g_sync_until_timestamp \	    );	if (write(fd, buff, len) != len)	{		logError("file: "__FILE__", line: %d, " \			"write to file \"%s\" fail, " \			"errno: %d, error info: %s", \			__LINE__, full_filename, \			errno, strerror(errno));		close(fd);		return errno != 0 ? errno : EIO;	}	close(fd);	return 0;}static int storage_check_and_make_data_dirs(){	int result;	int i;	char data_path[MAX_PATH_SIZE];	char full_filename[MAX_PATH_SIZE];	snprintf(data_path, sizeof(data_path), "%s/data", \			g_base_path);	snprintf(full_filename, sizeof(full_filename), "%s/%s", \			data_path, DATA_DIR_INITED_FILENAME);	if (fileExists(full_filename))	{		IniItemInfo *items;		int nItemCount;		char *pValue;		if ((result=iniLoadItems(full_filename, \				&items, &nItemCount)) \			 != 0)		{			logError("file: "__FILE__", line: %d, " \				"load from file \"%s/%s\" fail, " \				"error code: %d", \				__LINE__, data_path, \				full_filename, result);			return result;		}				pValue = iniGetStrValue(INIT_ITEM_STORAGE_JOIN_TIME, \				items, nItemCount);		if (pValue == NULL)		{			iniFreeItems(items);			logError("file: "__FILE__", line: %d, " \				"in file \"%s/%s\", item \"%s\" not exists", \				__LINE__, data_path, full_filename, \				INIT_ITEM_STORAGE_JOIN_TIME);			return ENOENT;		}		g_storage_join_time = atoi(pValue);		pValue = iniGetStrValue(INIT_ITEM_SYNC_OLD_DONE, \				items, nItemCount);		if (pValue == NULL)		{			iniFreeItems(items);			logError("file: "__FILE__", line: %d, " \				"in file \"%s/%s\", item \"%s\" not exists", \				__LINE__, data_path, full_filename, \				INIT_ITEM_SYNC_OLD_DONE);			return ENOENT;		}		g_sync_old_done = atoi(pValue);		pValue = iniGetStrValue(INIT_ITEM_SYNC_SRC_SERVER, \				items, nItemCount);		if (pValue == NULL)		{			iniFreeItems(items);			logError("file: "__FILE__", line: %d, " \				"in file \"%s/%s\", item \"%s\" not exists", \				__LINE__, data_path, full_filename, \				INIT_ITEM_SYNC_SRC_SERVER);			return ENOENT;		}		snprintf(g_sync_src_ip_addr, sizeof(g_sync_src_ip_addr), \				"%s", pValue);		g_sync_until_timestamp = iniGetIntValue( \				INIT_ITEM_SYNC_UNTIL_TIMESTAMP, \				items, nItemCount, 0);		iniFreeItems(items);		//printf("g_sync_old_done = %d\n", g_sync_old_done);		//printf("g_sync_src_ip_addr = %s\n", g_sync_src_ip_addr);		//printf("g_sync_until_timestamp = %d\n", g_sync_until_timestamp);	}	else	{		if (!fileExists(data_path))		{			if (mkdir(data_path, 0755) != 0)			{				logError("file: "__FILE__", line: %d, " \					"mkdir \"%s\" fail, " \					"errno: %d, error info: %s", \					__LINE__, data_path, \					errno, strerror(errno));				return errno != 0 ? errno : EPERM;			}		}		g_storage_join_time = time(NULL);		if ((result=storage_write_to_sync_ini_file()) != 0)		{			return result;		}	}	for (i=0; i<g_path_count; i++)	{		if ((result=storage_make_data_dirs(g_store_paths[i])) != 0)		{			return result;		}	}	return 0;}static int storage_make_data_dirs(const char *pBasePath){	char data_path[MAX_PATH_SIZE];	char dir_name[9];	char sub_name[9];	char min_sub_path[16];	char max_sub_path[16];	int i, k;	snprintf(data_path, sizeof(data_path), "%s/data", pBasePath);	if (!fileExists(data_path))	{		if (mkdir(data_path, 0755) != 0)		{			logError("file: "__FILE__", line: %d, " \				"mkdir \"%s\" fail, " \				"errno: %d, error info: %s", \				__LINE__, data_path, errno, strerror(errno));			return errno != 0 ? errno : EPERM;		}	}	if (chdir(data_path) != 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;	}	sprintf(min_sub_path, STORAGE_DATA_DIR_FORMAT"/"STORAGE_DATA_DIR_FORMAT,			0, 0);	sprintf(max_sub_path, STORAGE_DATA_DIR_FORMAT"/"STORAGE_DATA_DIR_FORMAT,			g_subdir_count_per_path-1, g_subdir_count_per_path-1);	if (fileExists(min_sub_path) && fileExists(max_sub_path))	{		return 0;	}	fprintf(stderr, "data path: %s, mkdir sub dir...\n", data_path);	for (i=0; i<g_subdir_count_per_path; i++)	{		sprintf(dir_name, STORAGE_DATA_DIR_FORMAT, i);		fprintf(stderr, "mkdir data path: %s ...\n", dir_name);		if (mkdir(dir_name, 0755) != 0)		{			if (!(errno == EEXIST && isDir(dir_name)))			{				logError("file: "__FILE__", line: %d, " \					"mkdir \"%s/%s\" fail, " \

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -