chiark / gitweb /
machined: fix image search path iteration
[elogind.git] / src / machine / image.c
index 2ffe9444e3e511a4a20a65d15acf1d2681d2ecdf..4f59c578883a5794e97afe8a6e40e76818944b09 100644 (file)
 #include "image.h"
 
 static const char image_search_path[] =
+        "/var/lib/machines\0"
         "/var/lib/container\0"
-        "/var/lib/machine\0";
+        "/usr/local/lib/machines\0"
+        "/usr/lib/machines\0";
 
 Image *image_unref(Image *i) {
         if (!i)
@@ -46,8 +48,8 @@ static int image_new(
                 const char *name,
                 const char *path,
                 bool read_only,
+                usec_t crtime,
                 usec_t mtime,
-                usec_t btime,
                 Image **ret) {
 
         _cleanup_(image_unrefp) Image *i = NULL;
@@ -63,8 +65,8 @@ static int image_new(
 
         i->type = t;
         i->read_only = read_only;
+        i->crtime = crtime;
         i->mtime = mtime;
-        i->btime = btime;
 
         i->name = strdup(name);
         if (!i->name)
@@ -86,6 +88,7 @@ static int image_new(
 
 static int image_make(int dfd, const char *name, const char *path, Image **ret) {
         struct stat st;
+        bool writable;
         int r;
 
         assert(dfd >= 0);
@@ -98,6 +101,8 @@ static int image_make(int dfd, const char *name, const char *path, Image **ret)
         if (fstatat(dfd, name, &st, 0) < 0)
                 return -errno;
 
+        writable = faccessat(dfd, name, W_OK, AT_EACCESS) >= 0;
+
         if (S_ISDIR(st.st_mode)) {
 
                 if (!ret)
@@ -116,25 +121,20 @@ static int image_make(int dfd, const char *name, const char *path, Image **ret)
                                 return -errno;
 
                         if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
-                                usec_t btime = 0;
-                                int ro;
+                                BtrfsSubvolInfo info;
 
                                 /* It's a btrfs subvolume */
 
-                                ro = btrfs_subvol_is_read_only_fd(fd);
-                                if (ro < 0)
-                                        return ro;
-
-                                /* r = btrfs_subvol_get_btime(fd, &btime); */
-                                /* if (r < 0) */
-                                /*         return r; */
+                                r = btrfs_subvol_get_info_fd(fd, &info);
+                                if (r < 0)
+                                        return r;
 
                                 r = image_new(IMAGE_SUBVOLUME,
                                               name,
                                               path,
-                                              ro,
+                                              info.read_only || !writable,
+                                              info.otime,
                                               0,
-                                              btime,
                                               ret);
                                 if (r < 0)
                                         return r;
@@ -148,7 +148,7 @@ static int image_make(int dfd, const char *name, const char *path, Image **ret)
                 r = image_new(IMAGE_DIRECTORY,
                               name,
                               path,
-                              false,
+                              !writable,
                               0,
                               0,
                               ret);
@@ -158,18 +158,24 @@ static int image_make(int dfd, const char *name, const char *path, Image **ret)
                 return 1;
 
         } else if (S_ISREG(st.st_mode) && endswith(name, ".gpt")) {
+                const char *truncated;
+                usec_t crtime = 0;
 
                 /* It's a GPT block device */
 
                 if (!ret)
                         return 1;
 
+                fd_getcrtime_at(dfd, name, &crtime, 0);
+
+                truncated = strndupa(name, strlen(name) - 4);
+
                 r = image_new(IMAGE_GPT,
-                              name,
+                              truncated,
                               path,
-                              !!(st.st_mode & 0222),
+                              !(st.st_mode & 0222) || !writable,
+                              crtime,
                               timespec_load(&st.st_mtim),
-                              0,
                               ret);
                 if (r < 0)
                         return r;
@@ -226,7 +232,7 @@ int image_discover(Hashmap *h) {
                 d = opendir(path);
                 if (!d) {
                         if (errno == ENOENT)
-                                return 0;
+                                continue;
 
                         return -errno;
                 }