1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdbool.h>
5 #include <stdint.h>
6 
7 #include "sd-id128.h"
8 
9 #include "hashmap.h"
10 #include "lockfile-util.h"
11 #include "macro.h"
12 #include "path-util.h"
13 #include "string-util.h"
14 #include "time-util.h"
15 
16 typedef enum ImageClass {
17         IMAGE_MACHINE,
18         IMAGE_PORTABLE,
19         IMAGE_EXTENSION,
20         _IMAGE_CLASS_MAX,
21         _IMAGE_CLASS_INVALID = -EINVAL,
22 } ImageClass;
23 
24 typedef enum ImageType {
25         IMAGE_DIRECTORY,
26         IMAGE_SUBVOLUME,
27         IMAGE_RAW,
28         IMAGE_BLOCK,
29         _IMAGE_TYPE_MAX,
30         _IMAGE_TYPE_INVALID = -EINVAL,
31 } ImageType;
32 
33 typedef struct Image {
34         unsigned n_ref;
35 
36         ImageType type;
37         char *name;
38         char *path;
39         bool read_only;
40 
41         usec_t crtime;
42         usec_t mtime;
43 
44         uint64_t usage;
45         uint64_t usage_exclusive;
46         uint64_t limit;
47         uint64_t limit_exclusive;
48 
49         char *hostname;
50         sd_id128_t machine_id;
51         char **machine_info;
52         char **os_release;
53         char **extension_release;
54 
55         bool metadata_valid:1;
56         bool discoverable:1;  /* true if we know for sure that image_find() would find the image given just the short name */
57 
58         void *userdata;
59 } Image;
60 
61 Image *image_unref(Image *i);
62 Image *image_ref(Image *i);
63 
64 DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref);
65 
66 int image_find(ImageClass class, const char *root, const char *name, Image **ret);
67 int image_from_path(const char *path, Image **ret);
68 int image_find_harder(ImageClass class, const char *root, const char *name_or_path, Image **ret);
69 int image_discover(ImageClass class, const char *root, Hashmap *map);
70 
71 int image_remove(Image *i);
72 int image_rename(Image *i, const char *new_name);
73 int image_clone(Image *i, const char *new_name, bool read_only);
74 int image_read_only(Image *i, bool b);
75 
76 const char* image_type_to_string(ImageType t) _const_;
77 ImageType image_type_from_string(const char *s) _pure_;
78 
79 int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local);
80 int image_name_lock(const char *name, int operation, LockFile *ret);
81 
82 int image_set_limit(Image *i, uint64_t referenced_max);
83 
84 int image_read_metadata(Image *i);
85 
86 bool image_in_search_path(ImageClass class, const char *root, const char *image);
87 
IMAGE_IS_HIDDEN(const struct Image * i)88 static inline bool IMAGE_IS_HIDDEN(const struct Image *i) {
89         assert(i);
90 
91         return i->name && i->name[0] == '.';
92 }
93 
IMAGE_IS_VENDOR(const struct Image * i)94 static inline bool IMAGE_IS_VENDOR(const struct Image *i) {
95         assert(i);
96 
97         return i->path && path_startswith(i->path, "/usr");
98 }
99 
IMAGE_IS_HOST(const struct Image * i)100 static inline bool IMAGE_IS_HOST(const struct Image *i) {
101         assert(i);
102 
103         if (i->name && streq(i->name, ".host"))
104                 return true;
105 
106         if (i->path && path_equal(i->path, "/"))
107                 return true;
108 
109         return false;
110 }
111 
112 extern const struct hash_ops image_hash_ops;
113