make subdirs recursively
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | /**
*@brief 递归的创建一个路径下的所有å目录. 内å˜è€ƒè™‘, fullpath长度为 MAX_PATH_LEN, 对于n级目录, 需è¦çš„è°ƒç”¨æ ˆå°±
?,
* 并ä¸ä¼šç‰æ¯”级数的扩å¼
*
*@param[in] root æ ¹ç›®å½•,必须是文件件路径
*/
static int create_subdir(char *root, mode_t mode, int depth){
if(0 == depth){
return EYOU_FILE_TRUE;
}
int i=0;
char fname[2] = {0};
char fullpath[MAX_PATH_LEN];
while(i < 16){
snprintf(fname, 2, "%x", i);
if('/' == root[strlen(root)-1] ){
snprintf(fullpath, MAX_PATH_LEN, "%s%s",root,fname);
}
else{
snprintf(fullpath, MAX_PATH_LEN, "%s/%s", root, fname);
}
eyou_syslog("simple filed :%s\n", fullpath);
if( -1 == mkdir(fullpath, mode)){
eyou_syslog("simple filed :create dir %s : %s", fullpath, strerror(errno));
return EYOU_FILE_FALSE;
}
/* 一旦出错就返回, 以é¿å…é‡å¤åˆ›å»ºã€‚ 其他错误需è¦äººå·¥å¤„ç† */
if ( EYOU_FILE_FALSE == create_subdir(fullpath,mode,depth-1)){
return EYOU_FILE_FALSE;
}
i++;
}
return EYOU_FILE_TRUE;
}
/**
*@brief 在é…置文件指定的路径下,当这个路径被第一次分é…时, 创建å¯èƒ½åˆ†é…到的所有的å文件夹, å³æ–‡ä»¶å¤¹0-9a-z.
* 这个功能由一个daemon负责, ä¸éœ€è¦åœ¨ç¨‹åºä¸åšï¼Œ 以å‡å°‘i/0æ“作.
*
* 这个程åºä»»ç„¶å¯ä»¥ç”¨åšåˆ›å»ºç›®å½•
*
*
*@param[in] year_monæŸå¹´æŸæœˆçš„目录, 比如200904. 当为空是, 设定为当å‰å¹´æœˆ
*/
int eyou_create_dir(char *year_mon)
{
SIMPLE_FILED_PATH_CONFIG *configs = NULL;
char path[MAX_PATH_LEN];
char date[7] = {0};
mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO ;
if ( NULL == glb_flink_cfg){
glb_flink_cfg = get_path_config();;
}
configs = get_path_config();
if(NULL == configs){
return EYOU_FILE_FALSE;
}
if(NULL == year_mon){
/* 先创建文件夹 */
time_t epoch_time = time(NULL);
struct tm *cur_time = localtime(&epoch_time);
cur_time->tm_year += 1900;
snprintf(date, 7, "%04d%02d", cur_time->tm_year, cur_time->tm_mon);
year_mon = date;
}
while(configs){
snprintf(path, MAX_PATH_LEN, "%s/%s", configs->str_path, year_mon);
/* 创建以日期分类的目录 */
if( -1 != mkdir(path, mode) ){
create_subdir(path, mode, configs->hash_level);
}
configs = configs->next;
}
return EYOU_FILE_TRUE;
}
|
Tags: mkdir