]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/discover-image.h
Merge pull request #25615 from DaanDeMeyer/mkosi-kconfig
[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
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 ImageClass class;
38 char *name;
39 char *path;
40 bool read_only;
41
42 usec_t crtime;
43 usec_t mtime;
44
45 uint64_t usage;
46 uint64_t usage_exclusive;
47 uint64_t limit;
48 uint64_t limit_exclusive;
49
50 char *hostname;
51 sd_id128_t machine_id;
52 char **machine_info;
53 char **os_release;
54 char **extension_release;
55
56 bool metadata_valid:1;
57 bool discoverable:1; /* true if we know for sure that image_find() would find the image given just the short name */
58
59 void *userdata;
60 } Image;
61
62 Image *image_unref(Image *i);
63 Image *image_ref(Image *i);
64
65 DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref);
66
67 int image_find(ImageClass class, const char *root, const char *name, Image **ret);
68 int image_from_path(const char *path, Image **ret);
69 int image_find_harder(ImageClass class, const char *root, const char *name_or_path, Image **ret);
70 int image_discover(ImageClass class, const char *root, Hashmap *map);
71
72 int image_remove(Image *i);
73 int image_rename(Image *i, const char *new_name);
74 int image_clone(Image *i, const char *new_name, bool read_only);
75 int image_read_only(Image *i, bool b);
76
77 const char* image_type_to_string(ImageType t) _const_;
78 ImageType image_type_from_string(const char *s) _pure_;
79
80 const char* image_class_to_string(ImageClass cl) _const_;
81 ImageClass image_class_from_string(const char *s) _pure_;
82
83 int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local);
84 int image_name_lock(const char *name, int operation, LockFile *ret);
85
86 int image_set_limit(Image *i, uint64_t referenced_max);
87
88 int image_read_metadata(Image *i);
89
90 bool image_in_search_path(ImageClass class, const char *root, const char *image);
91
92 static inline bool IMAGE_IS_HIDDEN(const struct Image *i) {
93 assert(i);
94
95 return i->name && i->name[0] == '.';
96 }
97
98 static inline bool IMAGE_IS_VENDOR(const struct Image *i) {
99 assert(i);
100
101 return i->path && path_startswith(i->path, "/usr");
102 }
103
104 static inline bool IMAGE_IS_HOST(const struct Image *i) {
105 assert(i);
106
107 if (i->name && streq(i->name, ".host"))
108 return true;
109
110 if (i->path && path_equal(i->path, "/"))
111 return true;
112
113 return false;
114 }
115
116 extern const struct hash_ops image_hash_ops;