From c55e2ca9c0ebfac13cac4f7531121700a373e8a0 Mon Sep 17 00:00:00 2001 From: calvinwilliams Date: Fri, 1 Feb 2019 16:36:30 +0800 Subject: [PATCH] UPDATE TO V0.28.1 --- ChangeLog-CN | 3 +++ src/cocker/action_del_image.c | 4 ++-- src/cocker/show_containers.c | 11 ++++++++++- src/cocker/show_images.c | 25 ++++++++++++++++++++----- src/cocker/util.c | 20 +++++++++++++++++++- src/util/file.c | 21 ++++++++++----------- 6 files changed, 64 insertions(+), 20 deletions(-) diff --git a/ChangeLog-CN b/ChangeLog-CN index 8bd52b4..5f71941 100644 --- a/ChangeLog-CN +++ b/ChangeLog-CN @@ -1,3 +1,6 @@ +0.28.1 2019-02-01 calvin + * 函数readdir的返回结构成员d_type在某些文件系统中可能工作不正常,加入stat替代判断 + 0.28.0 2018-11-28 calvin * 修正了cocker指令'-a create'或'-a boot'时单进程启动容器时存在的问题 * 文档新增章节 3.3.5. 单进程启动容器 diff --git a/src/cocker/action_del_image.c b/src/cocker/action_del_image.c index 4165a41..d1cd9ac 100644 --- a/src/cocker/action_del_image.c +++ b/src/cocker/action_del_image.c @@ -41,8 +41,8 @@ int DoAction_del_image( struct CockerEnvironment *env ) if( IsDirectoryEmpty( version_path_base ) == 0 ) { - nret = SnprintfAndSystem( cmd , sizeof(cmd) , "rm -rf %s" , version_path_base ) ; - INTER1( "*** ERROR : SnprintfAndSystem [rm -rf %s] failed[%d] , errno[%d]\n" , version_path_base , nret , errno ) + nret = SnprintfAndSystem( cmd , sizeof(cmd) , "rmdir %s" , version_path_base ) ; + INTER1( "*** ERROR : SnprintfAndSystem [rmdir %s] failed[%d] , errno[%d]\n" , version_path_base , nret , errno ) EIDTI( "system [%s] ok\n" , cmd ) } diff --git a/src/cocker/show_containers.c b/src/cocker/show_containers.c index e8c97f9..20cafa7 100644 --- a/src/cocker/show_containers.c +++ b/src/cocker/show_containers.c @@ -12,6 +12,8 @@ int DoShow_containers( struct CockerEnvironment *cocker_env ) { DIR *dir = NULL ; struct dirent *dirent = NULL ; + char container_path_base[ PATH_MAX + 1 ] ; + struct stat dir_stat ; int count ; char container_image_file[ PATH_MAX + 1 ] ; @@ -45,7 +47,14 @@ int DoShow_containers( struct CockerEnvironment *cocker_env ) if( STRCMP( dirent->d_name , == , "." ) || STRCMP( dirent->d_name , == , ".." ) ) continue; - if( dirent->d_type != DT_DIR ) + + if( Snprintf( container_path_base , sizeof(container_path_base) , "%s/%s" , cocker_env->containers_path_base , dirent->d_name ) == NULL ) + continue; + memset( & dir_stat , 0x00 , sizeof(struct stat) ); + nret = stat( container_path_base , & dir_stat ) ; + if( nret == -1 ) + continue; + if( S_ISDIR(dir_stat.st_mode) ) continue; /* image */ diff --git a/src/cocker/show_images.c b/src/cocker/show_images.c index f07f286..1184a0b 100644 --- a/src/cocker/show_images.c +++ b/src/cocker/show_images.c @@ -14,11 +14,12 @@ int DoShow_images( struct CockerEnvironment *cocker_env ) struct dirent *dirent = NULL ; DIR *dir2 = NULL ; struct dirent *dirent2 = NULL ; - int count ; char version_path_base[ PATH_MAX + 1 ] ; + char image_path_base[ PATH_MAX + 1 ] ; + struct stat dir_stat ; + int count ; char version[ VERSION_LEN_MAX + 1 ] ; char image_id[ IMAGES_ID_LEN_MAX + 1 ] ; - char image_path_base[ PATH_MAX + 1 ] ; struct stat image_path_stat ; struct tm image_path_modifytm ; char image_path_modifytime_buf[ 32 + 1 ] ; @@ -41,12 +42,19 @@ int DoShow_images( struct CockerEnvironment *cocker_env ) if( STRCMP( dirent->d_name , == , "." ) || STRCMP( dirent->d_name , == , ".." ) ) continue; - if( dirent->d_type != DT_DIR ) + + if( Snprintf( version_path_base , sizeof(version_path_base) , "%s/%s" , cocker_env->images_path_base , dirent->d_name ) == NULL ) + continue; + memset( & dir_stat , 0x00 , sizeof(struct stat) ); + nret = stat( version_path_base , & dir_stat ) ; + if( nret == -1 ) + continue; + + if( ! S_ISDIR(dir_stat.st_mode) ) continue; Snprintf( image_id , sizeof(image_id) , "%s" , dirent->d_name ); - Snprintf( version_path_base , sizeof(version_path_base) , "%s/%s" , cocker_env->images_path_base , image_id ); dir2 = opendir( version_path_base ) ; while( dir2 ) { @@ -56,7 +64,14 @@ int DoShow_images( struct CockerEnvironment *cocker_env ) if( STRCMP( dirent2->d_name , == , "." ) || STRCMP( dirent2->d_name , == , ".." ) ) continue; - if( dirent2->d_type != DT_DIR ) + + if( Snprintf( image_path_base , sizeof(image_path_base) , "%s/%s" , cocker_env->version_path_base , dirent2->d_name ) == NULL ) + continue; + memset( & dir_stat , 0x00 , sizeof(struct stat) ); + nret = stat( image_path_base , & dir_stat ) ; + if( nret == -1 ) + continue; + if( ! S_ISDIR(dir_stat.st_mode) ) continue; Snprintf( version , sizeof(version) , "%s" , dirent2->d_name ); diff --git a/src/cocker/util.c b/src/cocker/util.c index ae9f14e..2298d70 100644 --- a/src/cocker/util.c +++ b/src/cocker/util.c @@ -55,10 +55,14 @@ int GetMaxVersionPath( char *version_path_base , char *max_version , int max_ver { DIR *dir = NULL ; struct dirent *dirent = NULL ; + char sub_path[ PATH_MAX + 1 ] ; + struct stat dir_stat ; int max_v1 , max_v2 , max_v3 , max_v4 ; int v1 , v2 , v3 , v4 ; char version[ PATH_MAX + 1 ] = "" ; + int nret = 0 ; + dir = opendir( version_path_base ) ; if( dir == NULL ) return -1; @@ -74,7 +78,21 @@ int GetMaxVersionPath( char *version_path_base , char *max_version , int max_ver break; if( STRCMP( dirent->d_name , == , "." ) || STRCMP( dirent->d_name , == , ".." ) ) continue; - if( dirent->d_type != DT_DIR ) + + if( Snprintf( sub_path , sizeof(sub_path) , "%s/%s" , version_path_base , dirent->d_name ) == NULL ) + { + closedir( dir ); + return -2; + } + + memset( & dir_stat , 0x00 , sizeof(struct stat) ); + nret = stat( sub_path , & dir_stat ) ; + if( nret == -1 ) + { + closedir( dir ); + return -3; + } + if( ! S_ISDIR(dir_stat.st_mode) ) continue; sscanf( dirent->d_name , "%d.%d.%d.%d" , & v1 , & v2 , & v3 , & v4 ); diff --git a/src/util/file.c b/src/util/file.c index 6bb7bc3..f5f8cd9 100644 --- a/src/util/file.c +++ b/src/util/file.c @@ -270,21 +270,22 @@ static int _GetDirectorySize( char *path , int *p_directory_size ) return -2; } - if( dirent->d_type == DT_DIR ) + memset( & file_stat , 0x00 , sizeof(struct stat) ); + nret = stat( sub_path , & file_stat ) ; + if( nret == -1 ) + { + closedir( dir ); + return -3; + } + + if( S_ISDIR(file_stat.st_mode) ) { nret = _GetDirectorySize( sub_path , p_directory_size ) ; if( nret ) return nret; } - else if( dirent->d_type == DT_REG ) + else if( S_ISREG(file_stat.st_mode) ) { - memset( & file_stat , 0x00 , sizeof(struct stat) ); - nret = stat( sub_path , & file_stat ) ; - if( nret == -1 ) - { - closedir( dir ); - return -3; - } (*p_directory_size) += file_stat.st_size ; } } @@ -316,8 +317,6 @@ int IsDirectoryEmpty( char *version_path_base ) break; if( STRCMP( dirent->d_name , == , "." ) || STRCMP( dirent->d_name , == , ".." ) ) continue; - if( dirent->d_type != DT_DIR ) - continue; closedir( dir ); return 1;