]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/machine-image.h
tree-wide: remove Lennart's copyright lines
[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 "hashmap.h"
8 #include "lockfile-util.h"
9 #include "macro.h"
10 #include "path-util.h"
11 #include "string-util.h"
12 #include "time-util.h"
13
14 typedef enum ImageClass {
15 IMAGE_MACHINE,
16 IMAGE_PORTABLE,
17 _IMAGE_CLASS_MAX,
18 _IMAGE_CLASS_INVALID = -1
19 } ImageClass;
20
21 typedef enum ImageType {
22 IMAGE_DIRECTORY,
23 IMAGE_SUBVOLUME,
24 IMAGE_RAW,
25 IMAGE_BLOCK,
26 _IMAGE_TYPE_MAX,
27 _IMAGE_TYPE_INVALID = -1
28 } ImageType;
29
30 typedef struct Image {
31 unsigned n_ref;
32
33 ImageType type;
34 char *name;
35 char *path;
36 bool read_only;
37
38 usec_t crtime;
39 usec_t mtime;
40
41 uint64_t usage;
42 uint64_t usage_exclusive;
43 uint64_t limit;
44 uint64_t limit_exclusive;
45
46 char *hostname;
47 sd_id128_t machine_id;
48 char **machine_info;
49 char **os_release;
50
51 bool metadata_valid:1;
52 bool discoverable:1; /* true if we know for sure that image_find() would find the image given just the short name */
53
54 void *userdata;
55 } Image;
56
57 Image *image_unref(Image *i);
58 Image *image_ref(Image *i);
59
60 static inline Hashmap* image_hashmap_free(Hashmap *map) {
61 return hashmap_free_with_destructor(map, image_unref);
62 }
63
64 DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref);
65 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, image_hashmap_free);
66
67 int image_find(ImageClass class, const char *name, Image **ret);
68 int image_from_path(const char *path, Image **ret);
69 int image_find_harder(ImageClass class, const char *name_or_path, Image **ret);
70 int image_discover(ImageClass class, 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 bool image_name_is_valid(const char *s) _pure_;
81
82 int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local);
83 int image_name_lock(const char *name, int operation, LockFile *ret);
84
85 int image_set_limit(Image *i, uint64_t referenced_max);
86
87 int image_read_metadata(Image *i);
88
89 bool image_in_search_path(ImageClass class, const char *image);
90
91 static inline bool IMAGE_IS_HIDDEN(const struct Image *i) {
92 assert(i);
93
94 return i->name && i->name[0] == '.';
95 }
96
97 static inline bool IMAGE_IS_VENDOR(const struct Image *i) {
98 assert(i);
99
100 return i->path && path_startswith(i->path, "/usr");
101 }
102
103 static inline bool IMAGE_IS_HOST(const struct Image *i) {
104 assert(i);
105
106 if (i->name && streq(i->name, ".host"))
107 return true;
108
109 if (i->path && path_equal(i->path, "/"))
110 return true;
111
112 return false;
113 }