@@ -330,7 +330,7 @@ int git_futils_mkdir_withperf(
330330{
331331 int error = -1 ;
332332 git_buf make_path = GIT_BUF_INIT ;
333- ssize_t root = 0 , min_root_len ;
333+ ssize_t root = 0 , min_root_len , root_len ;
334334 char lastch = '/' , * tail ;
335335 struct stat st ;
336336
@@ -343,22 +343,29 @@ int git_futils_mkdir_withperf(
343343 goto done ;
344344 }
345345
346- /* remove trailing slashes on path */
347- while (make_path .ptr [make_path .size - 1 ] == '/' ) {
348- make_path .size -- ;
349- make_path .ptr [make_path .size ] = '\0' ;
350- }
346+ /* Trim trailing slashes (except the root) */
347+ if ((root_len = git_path_root (make_path .ptr )) < 0 )
348+ root_len = 0 ;
349+ else
350+ root_len ++ ;
351+
352+ while (make_path .size > (size_t )root_len &&
353+ make_path .ptr [make_path .size - 1 ] == '/' )
354+ make_path .ptr [-- make_path .size ] = '\0' ;
351355
352356 /* if we are not supposed to made the last element, truncate it */
353357 if ((flags & GIT_MKDIR_SKIP_LAST2 ) != 0 ) {
354- git_buf_rtruncate_at_char (& make_path , '/' );
358+ git_path_dirname_r (& make_path , make_path . ptr );
355359 flags |= GIT_MKDIR_SKIP_LAST ;
356360 }
357- if ((flags & GIT_MKDIR_SKIP_LAST ) != 0 )
358- git_buf_rtruncate_at_char (& make_path , '/' );
361+ if ((flags & GIT_MKDIR_SKIP_LAST ) != 0 ) {
362+ git_path_dirname_r (& make_path , make_path .ptr );
363+ }
359364
360- /* if nothing left after truncation, then we're done! */
361- if (!make_path .size ) {
365+ /* We were either given the root path (or trimmed it to
366+ * the root), we don't have anything to do.
367+ */
368+ if (make_path .size <= (size_t )root_len ) {
362369 error = 0 ;
363370 goto done ;
364371 }
0 commit comments