1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <sys/stat.h>
5
6 #include "sysupdate-instance.h"
7
instance_metadata_destroy(InstanceMetadata * m)8 void instance_metadata_destroy(InstanceMetadata *m) {
9 assert(m);
10 free(m->version);
11 }
12
instance_new(Resource * rr,const char * path,const InstanceMetadata * f,Instance ** ret)13 int instance_new(
14 Resource *rr,
15 const char *path,
16 const InstanceMetadata *f,
17 Instance **ret) {
18
19 _cleanup_(instance_freep) Instance *i = NULL;
20 _cleanup_free_ char *p = NULL, *v = NULL;
21
22 assert(rr);
23 assert(path);
24 assert(f);
25 assert(f->version);
26 assert(ret);
27
28 p = strdup(path);
29 if (!p)
30 return log_oom();
31
32 v = strdup(f->version);
33 if (!v)
34 return log_oom();
35
36 i = new(Instance, 1);
37 if (!i)
38 return log_oom();
39
40 *i = (Instance) {
41 .resource = rr,
42 .metadata = *f,
43 .path = TAKE_PTR(p),
44 .partition_info = PARTITION_INFO_NULL,
45 };
46
47 i->metadata.version = TAKE_PTR(v);
48
49 *ret = TAKE_PTR(i);
50 return 0;
51 }
52
instance_free(Instance * i)53 Instance *instance_free(Instance *i) {
54 if (!i)
55 return NULL;
56
57 instance_metadata_destroy(&i->metadata);
58
59 free(i->path);
60 partition_info_destroy(&i->partition_info);
61
62 return mfree(i);
63 }
64