Keep track of the position of the slash character separately, and
restore the slash character at a single place, at the end of the while
loop.  This makes the next change easier to implement.

Signed-off-by: Michael Haggerty <mhag...@alum.mit.edu>
---
 sha1_file.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/sha1_file.c b/sha1_file.c
index cc9957e..dcfd35a 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -107,40 +107,40 @@ int mkdir_in_gitdir(const char *path)
 
 int safe_create_leading_directories(char *path)
 {
-       char *pos = path + offset_1st_component(path);
+       char *next_component = path + offset_1st_component(path);
+       int retval = 0;
 
-       while (pos) {
+       while (!retval && next_component) {
                struct stat st;
+               char *slash = strchr(next_component, '/');
 
-               pos = strchr(pos, '/');
-               if (!pos)
-                       break;
-               while (*++pos == '/')
-                       ;
-               if (!*pos)
-                       break;
-               *--pos = '\0';
+               if (!slash)
+                       return 0;
+               while (*(slash + 1) == '/')
+                       slash++;
+               next_component = slash + 1;
+               if (!*next_component)
+                       return 0;
+
+               *slash = '\0';
                if (!stat(path, &st)) {
                        /* path exists */
                        if (!S_ISDIR(st.st_mode)) {
-                               *pos = '/';
-                               return -3;
+                               retval = -3;
                        }
                } else if (mkdir(path, 0777)) {
                        if (errno == EEXIST &&
                            !stat(path, &st) && S_ISDIR(st.st_mode)) {
                                ; /* somebody created it since we checked */
                        } else {
-                               *pos = '/';
-                               return -1;
+                               retval = -1;
                        }
                } else if (adjust_shared_perm(path)) {
-                       *pos = '/';
-                       return -2;
+                       retval = -2;
                }
-               *pos++ = '/';
+               *slash = '/';
        }
-       return 0;
+       return retval;
 }
 
 int safe_create_leading_directories_const(const char *path)
-- 
1.8.5.1

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to