1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <inttypes.h>
5 #include <stdbool.h>
6 #include <sys/types.h>
7 
8 #include "sd-id128.h"
9 
10 #include "hashmap.h"
11 #include "macro.h"
12 
13 /* Forward declare this type so that the headers below can use it */
14 typedef struct Resource Resource;
15 
16 #include "sysupdate-instance.h"
17 
18 typedef enum ResourceType {
19         RESOURCE_URL_FILE,
20         RESOURCE_URL_TAR,
21         RESOURCE_TAR,
22         RESOURCE_PARTITION,
23         RESOURCE_REGULAR_FILE,
24         RESOURCE_DIRECTORY,
25         RESOURCE_SUBVOLUME,
26         _RESOURCE_TYPE_MAX,
27         _RESOURCE_TYPE_INVALID = -EINVAL,
28 } ResourceType;
29 
RESOURCE_IS_SOURCE(ResourceType t)30 static inline bool RESOURCE_IS_SOURCE(ResourceType t) {
31         return IN_SET(t,
32                       RESOURCE_URL_FILE,
33                       RESOURCE_URL_TAR,
34                       RESOURCE_TAR,
35                       RESOURCE_REGULAR_FILE,
36                       RESOURCE_DIRECTORY,
37                       RESOURCE_SUBVOLUME);
38 }
39 
RESOURCE_IS_TARGET(ResourceType t)40 static inline bool RESOURCE_IS_TARGET(ResourceType t) {
41         return IN_SET(t,
42                       RESOURCE_PARTITION,
43                       RESOURCE_REGULAR_FILE,
44                       RESOURCE_DIRECTORY,
45                       RESOURCE_SUBVOLUME);
46 }
47 
48 /* Returns true for all resources that deal with file system objects, i.e. where we operate on top of the
49  * file system layer, instead of below. */
RESOURCE_IS_FILESYSTEM(ResourceType t)50 static inline bool RESOURCE_IS_FILESYSTEM(ResourceType t) {
51         return IN_SET(t,
52                       RESOURCE_TAR,
53                       RESOURCE_REGULAR_FILE,
54                       RESOURCE_DIRECTORY,
55                       RESOURCE_SUBVOLUME);
56 }
57 
RESOURCE_IS_TAR(ResourceType t)58 static inline bool RESOURCE_IS_TAR(ResourceType t) {
59         return IN_SET(t,
60                       RESOURCE_TAR,
61                       RESOURCE_URL_TAR);
62 }
63 
RESOURCE_IS_URL(ResourceType t)64 static inline bool RESOURCE_IS_URL(ResourceType t) {
65         return IN_SET(t,
66                       RESOURCE_URL_TAR,
67                       RESOURCE_URL_FILE);
68 }
69 
70 struct Resource {
71         ResourceType type;
72 
73         /* Where to look for instances, and what to match precisely */
74         char *path;
75         bool path_auto; /* automatically find root path (only available if target resource, not source resource) */
76         char **patterns;
77         sd_id128_t partition_type;
78         bool partition_type_set;
79 
80         /* All instances of this resource we found */
81         Instance **instances;
82         size_t n_instances;
83 
84         /* If this is a partition resource (RESOURCE_PARTITION), then how many partition slots are currently unassigned, that we can use */
85         size_t n_empty;
86 };
87 
88 void resource_destroy(Resource *rr);
89 
90 int resource_load_instances(Resource *rr, bool verify, Hashmap **web_cache);
91 
92 Instance* resource_find_instance(Resource *rr, const char *version);
93 
94 int resource_resolve_path(Resource *rr, const char *root, const char *node);
95 
96 ResourceType resource_type_from_string(const char *s) _pure_;
97 const char *resource_type_to_string(ResourceType t) _const_;
98