]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/machine-image.h
tree-wide: fix a few missing includes
[thirdparty/systemd.git] / src / shared / machine-image.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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_CLASS_MAX,
20 _IMAGE_CLASS_INVALID = -1
21 } ImageClass;
22
23 typedef enum ImageType {
24 IMAGE_DIRECTORY,
25 IMAGE_SUBVOLUME,
26 IMAGE_RAW,
27 IMAGE_BLOCK,
28 _IMAGE_TYPE_MAX,
29 _IMAGE_TYPE_INVALID = -1
30 } ImageType;
31
32 typedef struct Image {
33 unsigned n_ref;
34
35 ImageType type;
36 char *name;
37 char *path;
38 bool read_only;
39
40 usec_t crtime;
41 usec_t mtime;
42
43 uint64_t usage;
44 uint64_t usage_exclusive;
45 uint64_t limit;
46 uint64_t limit_exclusive;
47
48 char *hostname;
49 sd_id128_t machine_id;
50 char **machine_info;
51 char **os_release;
52
53 bool metadata_valid:1;
54 bool discoverable:1; /* true if we know for sure that image_find() would find the image given just the short name */
55
56 void *userdata;
57 } Image;
58
59 Image *image_unref(Image *i);
60 Image *image_ref(Image *i);
61
62 static inline Hashmap* image_hashmap_free(Hashmap *map) {
63 return hashmap_free_with_destructor(map, image_unref);
64 }
65
66 DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref);
67 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, image_hashmap_free);
68
69 int image_find(ImageClass class, const char *name, Image **ret);
70 int image_from_path(const char *path, Image **ret);
71 int image_find_harder(ImageClass class, const char *name_or_path, Image **ret);
72 int image_discover(ImageClass class, Hashmap *map);
73
74 int image_remove(Image *i);
75 int image_rename(Image *i, const char *new_name);
76 int image_clone(Image *i, const char *new_name, bool read_only);
77 int image_read_only(Image *i, bool b);
78
79 const char* image_type_to_string(ImageType t) _const_;
80 ImageType image_type_from_string(const char *s) _pure_;
81
82 bool image_name_is_valid(const char *s) _pure_;
83
84 int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local);
85 int image_name_lock(const char *name, int operation, LockFile *ret);
86
87 int image_set_limit(Image *i, uint64_t referenced_max);
88
89 int image_read_metadata(Image *i);
90
91 bool image_in_search_path(ImageClass class, const char *image);
92
93 static inline bool IMAGE_IS_HIDDEN(const struct Image *i) {
94 assert(i);
95
96 return i->name && i->name[0] == '.';
97 }
98
99 static inline bool IMAGE_IS_VENDOR(const struct Image *i) {
100 assert(i);
101
102 return i->path && path_startswith(i->path, "/usr");
103 }
104
105 static inline bool IMAGE_IS_HOST(const struct Image *i) {
106 assert(i);
107
108 if (i->name && streq(i->name, ".host"))
109 return true;
110
111 if (i->path && path_equal(i->path, "/"))
112 return true;
113
114 return false;
115 }