/src/fwupd/libfwupdplugin/fu-temporary-directory.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2026 Richard Hughes <richard@hughsie.com> |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | */ |
6 | | |
7 | 0 | #define G_LOG_DOMAIN "FuTemporaryDirectory" |
8 | | |
9 | | #include "config.h" |
10 | | |
11 | | #include "fu-path.h" |
12 | | #include "fu-temporary-directory.h" |
13 | | |
14 | | /** |
15 | | * FuTemporaryDirectory: |
16 | | * |
17 | | * An object to create and destroy a temporary directory. |
18 | | */ |
19 | | |
20 | | struct _FuTemporaryDirectory { |
21 | | GObject parent_instance; |
22 | | gchar *path; |
23 | | }; |
24 | | |
25 | 0 | G_DEFINE_TYPE(FuTemporaryDirectory, fu_temporary_directory, G_TYPE_OBJECT) |
26 | 0 |
|
27 | 0 | /** |
28 | 0 | * fu_temporary_directory_get_path: |
29 | 0 | * @self: a #FuTemporaryDirectory |
30 | 0 | * |
31 | 0 | * Gets the path of the temporary directory. |
32 | 0 | * |
33 | 0 | * Returns: (transfer full): data |
34 | 0 | * |
35 | 0 | * Since: 2.1.1 |
36 | 0 | **/ |
37 | 0 | const gchar * |
38 | 0 | fu_temporary_directory_get_path(FuTemporaryDirectory *self) |
39 | 0 | { |
40 | 0 | g_return_val_if_fail(FU_IS_TEMPORARY_DIRECTORY(self), NULL); |
41 | 0 | return self->path; |
42 | 0 | } |
43 | | |
44 | | /** |
45 | | * fu_temporary_directory_new: |
46 | | * @prefix: (nullable): optional prefix |
47 | | * |
48 | | * Creates a new temporary directory that will be deleted (recursively) when this object is |
49 | | * destroyed. |
50 | | * |
51 | | * Returns: (transfer full): a #FuTemporaryDirectory |
52 | | * |
53 | | * Since: 2.1.1 |
54 | | **/ |
55 | | FuTemporaryDirectory * |
56 | | fu_temporary_directory_new(const gchar *prefix, GError **error) |
57 | 0 | { |
58 | 0 | g_autofree gchar *pattern = NULL; |
59 | 0 | g_autoptr(FuTemporaryDirectory) self = g_object_new(FU_TYPE_TEMPORARY_DIRECTORY, NULL); |
60 | |
|
61 | 0 | pattern = g_strdup_printf("fwupd-%s-XXXXXX", prefix != NULL ? prefix : "tmp"); |
62 | 0 | self->path = g_dir_make_tmp(pattern, error); |
63 | 0 | if (self->path == NULL) { |
64 | 0 | fwupd_error_convert(error); |
65 | 0 | return NULL; |
66 | 0 | } |
67 | 0 | return g_steal_pointer(&self); |
68 | 0 | } |
69 | | |
70 | | /** |
71 | | * fu_temporary_directory_build: |
72 | | * @self: a #FuTemporaryDirectory |
73 | | * @...: pairs of string values, ending with %NULL |
74 | | * |
75 | | * Builds a path within the temporary_directory. |
76 | | * |
77 | | * Returns: a path |
78 | | * |
79 | | * Since: 2.1.1 |
80 | | **/ |
81 | | gchar * |
82 | | fu_temporary_directory_build(FuTemporaryDirectory *self, ...) |
83 | 0 | { |
84 | 0 | va_list args; |
85 | 0 | gchar *path; |
86 | |
|
87 | 0 | g_return_val_if_fail(FU_IS_TEMPORARY_DIRECTORY(self), NULL); |
88 | | |
89 | 0 | va_start(args, self); |
90 | 0 | path = g_build_filename_valist(self->path, &args); |
91 | 0 | va_end(args); |
92 | |
|
93 | 0 | return path; |
94 | 0 | } |
95 | | |
96 | | static void |
97 | | fu_temporary_directory_finalize(GObject *object) |
98 | 0 | { |
99 | 0 | FuTemporaryDirectory *self = FU_TEMPORARY_DIRECTORY(object); |
100 | 0 | if (self->path != NULL) { |
101 | 0 | g_autoptr(GError) error = NULL; |
102 | 0 | if (!fu_path_rmtree(self->path, &error)) |
103 | 0 | g_warning("failed to delete %s: %s", self->path, error->message); |
104 | 0 | g_free(self->path); |
105 | 0 | } |
106 | 0 | G_OBJECT_CLASS(fu_temporary_directory_parent_class)->finalize(object); |
107 | 0 | } |
108 | | |
109 | | static void |
110 | | fu_temporary_directory_class_init(FuTemporaryDirectoryClass *klass) |
111 | 0 | { |
112 | 0 | GObjectClass *object_class = G_OBJECT_CLASS(klass); |
113 | 0 | object_class->finalize = fu_temporary_directory_finalize; |
114 | 0 | } |
115 | | |
116 | | static void |
117 | | fu_temporary_directory_init(FuTemporaryDirectory *self) |
118 | 0 | { |
119 | 0 | } |