@@ -2033,7 +2033,7 @@ static int glob_item_recursively(Item *i, fdaction_t action) {
20332033static int rm_if_wrong_type_safe (
20342034 mode_t mode ,
20352035 int parent_fd ,
2036- const struct stat * parent_st /* Only used if follow is true. */ ,
2036+ const struct stat * parent_st , /* Only used if follow_links below is true. */
20372037 const char * name ,
20382038 int flags ) {
20392039 _cleanup_free_ char * parent_name = NULL ;
@@ -2110,12 +2110,10 @@ static int rm_if_wrong_type_safe(
21102110
21112111/* If child_mode is non-zero, rm_if_wrong_type_safe will be executed for the last path component. */
21122112static int mkdir_parents_rm_if_wrong_type (mode_t child_mode , const char * path ) {
2113- _cleanup_close_ int parent_fd = -1 , next_fd = -1 ;
2114- _cleanup_free_ char * parent_name = NULL ;
2113+ _cleanup_close_ int parent_fd = -1 ;
21152114 struct stat parent_st ;
2116- const char * s , * e ;
2117- int r ;
21182115 size_t path_len ;
2116+ int r ;
21192117
21202118 assert (path );
21212119 assert ((child_mode & ~S_IFMT ) == 0 );
@@ -2141,9 +2139,10 @@ static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) {
21412139 return log_error_errno (errno , "Failed to stat root: %m" );
21422140
21432141 /* Check every parent directory in the path, except the last component */
2144- e = path ;
2145- for (;;) {
2142+ for ( const char * e = path ;;) {
2143+ _cleanup_close_ int next_fd = -1 ;
21462144 char t [path_len + 1 ];
2145+ const char * s ;
21472146
21482147 /* Find the start of the next path component. */
21492148 s = e + strspn (e , "/" );
@@ -2156,38 +2155,41 @@ static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) {
21562155 /* Is this the last component? If so, then check the type */
21572156 if (* e == 0 )
21582157 return child_mode != 0 ? rm_if_wrong_type_safe (child_mode , parent_fd , & parent_st , t , AT_SYMLINK_NOFOLLOW ) : 0 ;
2159- else {
2160- r = rm_if_wrong_type_safe (S_IFDIR , parent_fd , & parent_st , t , 0 );
2161- /* Remove dangling symlinks. */
2162- if (r == - ENOENT )
2163- r = rm_if_wrong_type_safe (S_IFDIR , parent_fd , & parent_st , t , AT_SYMLINK_NOFOLLOW );
2164- }
21652158
2159+ r = rm_if_wrong_type_safe (S_IFDIR , parent_fd , & parent_st , t , 0 );
2160+ /* Remove dangling symlinks. */
2161+ if (r == - ENOENT )
2162+ r = rm_if_wrong_type_safe (S_IFDIR , parent_fd , & parent_st , t , AT_SYMLINK_NOFOLLOW );
21662163 if (r == - ENOENT ) {
21672164 RUN_WITH_UMASK (0000 )
21682165 r = mkdirat_label (parent_fd , t , 0755 );
21692166 if (r < 0 ) {
2167+ _cleanup_free_ char * parent_name = NULL ;
2168+
21702169 (void ) fd_get_path (parent_fd , & parent_name );
2171- return log_error_errno (r , "Failed to mkdir \"%s\" at \"%s\": %m" , t , parent_name );
2170+ return log_error_errno (r , "Failed to mkdir \"%s\" at \"%s\": %m" , t , strnull ( parent_name ) );
21722171 }
21732172 } else if (r < 0 )
21742173 /* rm_if_wrong_type_safe already logs errors. */
21752174 return r ;
21762175
21772176 next_fd = openat (parent_fd , t , O_NOCTTY | O_CLOEXEC | O_DIRECTORY );
21782177 if (next_fd < 0 ) {
2178+ _cleanup_free_ char * parent_name = NULL ;
2179+
21792180 r = - errno ;
21802181 (void ) fd_get_path (parent_fd , & parent_name );
2181- return log_error_errno (r , "Failed to open \"%s\" at \"%s\": %m" , t , parent_name );
2182+ return log_error_errno (r , "Failed to open \"%s\" at \"%s\": %m" , t , strnull ( parent_name ) );
21822183 }
21832184 if (fstat (next_fd , & parent_st ) < 0 ) {
2185+ _cleanup_free_ char * parent_name = NULL ;
2186+
21842187 r = - errno ;
21852188 (void ) fd_get_path (parent_fd , & parent_name );
2186- return log_error_errno (r , "Failed to stat \"%s\" at \"%s\": %m" , t , parent_name );
2189+ return log_error_errno (r , "Failed to stat \"%s\" at \"%s\": %m" , t , strnull ( parent_name ) );
21872190 }
21882191
2189- safe_close (parent_fd );
2190- parent_fd = TAKE_FD (next_fd );
2192+ CLOSE_AND_REPLACE (parent_fd , next_fd );
21912193 }
21922194}
21932195
0 commit comments