Setting toplevel from a non-existant path fails
authorJ. Ali Harlow <ali@juiblex.co.uk>
Tue, 30 Sep 2014 15:19:55 +0000 (16:19 +0100)
committerJ. Ali Harlow <ali@juiblex.co.uk>
Tue, 30 Sep 2014 15:19:55 +0000 (16:19 +0100)
librazor/atomic-emulate.c

index dae72ff..da65008 100644 (file)
@@ -240,7 +240,7 @@ razor_atomic_set_toplevel_from_path(struct razor_atomic *atomic,
                 * unwritable directory and take one step back.
                 */
                char *s, *abspath, saved;
-               int len;
+               int len, can_step_back = 0;
 
                abspath = absolute_path(path);
                if (!abspath) {
@@ -249,11 +249,15 @@ razor_atomic_set_toplevel_from_path(struct razor_atomic *atomic,
                }
 
                if (stat(abspath, &buf) < 0) {
-                       atomic->error = razor_error_new_posix(abspath);
-                       free(abspath);
-                       return -1;
-               }
-               filesystem = buf.st_dev;
+                       if (errno == ENOENT)
+                               filesystem = 0;
+                       else {
+                               atomic->error = razor_error_new_posix(abspath);
+                               free(abspath);
+                               return -1;
+                       }
+               } else
+                       filesystem = buf.st_dev;
 
                len = strlen(abspath);
                while(len > 1 && (s = strrchr(abspath, '/'))) {
@@ -267,19 +271,27 @@ razor_atomic_set_toplevel_from_path(struct razor_atomic *atomic,
                        }
 
                        if (stat(abspath, &buf) < 0) {
-                               atomic->error = razor_error_new_posix(abspath);
-                               free(abspath);
-                               return -1;
-                       }
+                               if (errno == ENOENT)
+                                       continue;
+                               else {
+                                   atomic->error = razor_error_new_posix(abspath);
+                                   free(abspath);
+                                   return -1;
+                               }
+                       } else if (!filesystem)
+                               filesystem = buf.st_dev;
 
                        if (buf.st_dev != filesystem || access(abspath, W_OK)) {
-                               if (s == abspath)
-                                       s[1] = saved;
-                               else
-                                       s[0] = '/';
+                               if (can_step_back) {
+                                       if (s == abspath)
+                                               s[1] = saved;
+                                       else
+                                               s[0] = '/';
+                               }
                                len = strlen(abspath);
                                break;
-                       }
+                       } else
+                               can_step_back = 1;
                }
 
                if (len == 1)