]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/discover-image.h
sd-json: make static analyzers shut up
[thirdparty/systemd.git] / src / shared / discover-image.h
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 #include "sd-json.h"
9
10 #include "hashmap.h"
11 #include "image-policy.h"
12 #include "lock-util.h"
13 #include "macro.h"
14 #include "os-util.h"
15 #include "path-util.h"
16 #include "string-util.h"
17 #include "time-util.h"
18
19 typedef enum ImageType {
20 IMAGE_DIRECTORY,
21 IMAGE_SUBVOLUME,
22 IMAGE_RAW,
23 IMAGE_BLOCK,
24 _IMAGE_TYPE_MAX,
25 _IMAGE_TYPE_INVALID = -EINVAL,
26 } ImageType;
27
28 typedef struct Image {
29 unsigned n_ref;
30
31 ImageType type;
32 ImageClass class;
33 char *name;
34 char *path;
35 bool read_only;
36
37 usec_t crtime;
38 usec_t mtime;
39
40 uint64_t usage;
41 uint64_t usage_exclusive;
42 uint64_t limit;
43 uint64_t limit_exclusive;
44
45 char *hostname;
46 sd_id128_t machine_id;
47 char **machine_info;
48 char **os_release;
49 char **sysext_release;
50 char **confext_release;
51
52 bool metadata_valid:1;
53 bool discoverable:1; /* true if we know for sure that image_find() would find the image given just the short name */
54
55 void *userdata;
56 } Image;
57
58 Image *image_unref(Image *i);
59 Image *image_ref(Image *i);
60
61 DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref);
62
63 int image_find(ImageClass class, const char *root, const char *name, Image **ret);
64 int image_from_path(const char *path, Image **ret);
65 int image_find_harder(ImageClass class, const char *root, const char *name_or_path, Image **ret);
66 int image_discover(ImageClass class, const char *root, Hashmap *map);
67
68 int image_remove(Image *i);
69 int image_rename(Image *i, const char *new_name);
70 int image_clone(Image *i, const char *new_name, bool read_only);
71 int image_read_only(Image *i, bool b);
72
73 const char* image_type_to_string(ImageType t) _const_;
74 ImageType image_type_from_string(const char *s) _pure_;
75
76 int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local);
77 int image_name_lock(const char *name, int operation, LockFile *ret);
78
79 int image_set_limit(Image *i, uint64_t referenced_max);
80
81 int image_read_metadata(Image *i, const ImagePolicy *image_policy);
82
83 bool image_in_search_path(ImageClass class, const char *root, const char *image);
84
85 static inline char **image_extension_release(Image *image, ImageClass class) {
86 assert(image);
87
88 if (class == IMAGE_SYSEXT)
89 return image->sysext_release;
90 if (class == IMAGE_CONFEXT)
91 return image->confext_release;
92
93 return NULL;
94 }
95
96 static inline bool IMAGE_IS_HIDDEN(const struct Image *i) {
97 assert(i);
98
99 return i->name && i->name[0] == '.';
100 }
101
102 static inline bool IMAGE_IS_VENDOR(const struct Image *i) {
103 assert(i);
104
105 return i->path && path_startswith(i->path, "/usr");
106 }
107
108 static inline bool IMAGE_IS_HOST(const struct Image *i) {
109 assert(i);
110
111 if (i->name && streq(i->name, ".host"))
112 return true;
113
114 if (i->path && path_equal(i->path, "/"))
115 return true;
116
117 return false;
118 }
119
120 int image_to_json(const struct Image *i, sd_json_variant **ret);
121
122 const char *image_root_to_string(ImageClass c) _const_;
123
124 extern const struct hash_ops image_hash_ops;
125
126 extern const char* const image_search_path[_IMAGE_CLASS_MAX];