Debian Codegraph @

src/plugins/fs/exfat.c

raw
1 /*
2 * Copyright (C) 2020 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Vojtech Trefny <[email protected]>
18 */
19
20 #include <blockdev/utils.h>
21 #include <check_deps.h>
22
23 #include "exfat.h"
24 #include "fs.h"
25 #include "common.h"
26
27 static volatile guint avail_deps = 0;
28 static GMutex deps_check_lock;
29
30 G_GNUC_INTERNAL
31 void _fs_exfat_reset_avail_deps (void) {
32 g_atomic_int_set (&avail_deps, 0);
33 }
34
35 #define DEPS_MKEXFAT 0
36 #define DEPS_MKEXFAT_MASK (1 << DEPS_MKEXFAT)
37 #define DEPS_FSCKEXFAT 1
38 #define DEPS_FSCKEXFAT_MASK (1 << DEPS_FSCKEXFAT)
39 #define DEPS_TUNEEXFAT 2
40 #define DEPS_TUNEEXFAT_MASK (1 << DEPS_TUNEEXFAT)
41
42 #define DEPS_LAST 4
43
44 static const UtilDep deps[DEPS_LAST] = {
45 {"mkfs.exfat", NULL, NULL, NULL},
46 {"fsck.exfat", NULL, NULL, NULL},
47 {"tune.exfat", NULL, NULL, NULL},
48 {"tune.exfat", NULL, NULL, NULL},
49 };
50
51 static guint32 fs_mode_util[BD_FS_MODE_LAST+1] = {
52 DEPS_MKEXFAT_MASK, /* mkfs */
53 0, /* wipe */
54 DEPS_FSCKEXFAT_MASK, /* check */
55 DEPS_FSCKEXFAT_MASK, /* repair */
56 DEPS_TUNEEXFAT_MASK, /* set-label */
57 DEPS_TUNEEXFAT_MASK, /* query */
58 0, /* resize */
59 DEPS_TUNEEXFAT_MASK, /* set-uuid */
60 };
61
62 /* line prefixes in tune.exfat output for parsing */
63 #define BLOCK_SIZE_PREFIX "Block sector size : "
64 #define BLOCK_SIZE_PREFIX_LEN 20
65 #define SECTORS_PREFIX "Number of the sectors : "
66 #define SECTORS_PREFIX_LEN 24
67 #define CLUSTERS_PREFIX "Number of the clusters : "
68 #define CLUSTERS_PREFIX_LEN 25
69
70
71
72 /**
73 * bd_fs_exfat_is_tech_avail:
74 * @tech: the queried tech
75 * @mode: a bit mask of queried modes of operation (#BDFSTechMode) for @tech
76 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
77 *
78 * Returns: whether the @tech-@mode combination is available -- supported by the
79 * plugin implementation and having all the runtime dependencies available
80 */
81 G_GNUC_INTERNAL gboolean
82 bd_fs_exfat_is_tech_avail (BDFSTech tech G_GNUC_UNUSED, guint64 mode, GError **error) {
83 guint32 required = 0;
84 guint i = 0;
85
86 if (mode & BD_FS_TECH_MODE_RESIZE) {
87 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_TECH_UNAVAIL,
88 "exFAT currently doesn't support resizing.");
89 return FALSE;
90 }
91
92 for (i = 0; i <= BD_FS_MODE_LAST; i++)
93 if (mode & (1 << i))
94 required |= fs_mode_util[i];
95
96 return check_deps (&avail_deps, required, deps, DEPS_LAST, &deps_check_lock, error);
97 }
98
99 /**
100 * bd_fs_exfat_info_copy: (skip)
101 * @data: (nullable): %BDFSExfatInfo to copy
102 *
103 * Creates a new copy of @data.
104 */
105 BDFSExfatInfo* bd_fs_exfat_info_copy (BDFSExfatInfo *data) {
106 if (data == NULL)
107 return NULL;
108
109 BDFSExfatInfo *ret = g_new0 (BDFSExfatInfo, 1);
110
111 ret->label = g_strdup (data->label);
112 ret->uuid = g_strdup (data->uuid);
113 ret->sector_size = data->sector_size;
114 ret->sector_count = data->sector_count;
115 ret->cluster_count = data->cluster_count;
116
117 return ret;
118 }
119
120 /**
121 * bd_fs_exfat_info_free: (skip)
122 * @data: (nullable): %BDFSExfatInfo to free
123 *
124 * Frees @data.
125 */
126 void bd_fs_exfat_info_free (BDFSExfatInfo *data) {
127 if (data == NULL)
128 return;
129
130 g_free (data->label);
131 g_free (data->uuid);
132 g_free (data);
133 }
134
135 G_GNUC_INTERNAL BDExtraArg **
136 bd_fs_exfat_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) {
137 GPtrArray *options_array = g_ptr_array_new ();
138 const BDExtraArg **extra_p = NULL;
139
140 if (options->label && g_strcmp0 (options->label, "") != 0)
141 g_ptr_array_add (options_array, bd_extra_arg_new ("-n", options->label));
142
143 if (extra) {
144 for (extra_p = extra; *extra_p; extra_p++)
145 g_ptr_array_add (options_array, bd_extra_arg_copy ((BDExtraArg *) *extra_p));
146 }
147
148 g_ptr_array_add (options_array, NULL);
149
150 return (BDExtraArg **) g_ptr_array_free (options_array, FALSE);
151 }
152
153 /**
154 * bd_fs_exfat_mkfs:
155 * @device: the device to create a new exfat fs on
156 * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
157 * passed to the 'mkfs.exfat' utility)
158 * @error: (out) (optional): place to store error (if any)
159 *
160 * Returns: whether a new exfat fs was successfully created on @device or not
161 *
162 * Tech category: %BD_FS_TECH_EXFAT-%BD_FS_TECH_MODE_MKFS
163 */
164 gboolean bd_fs_exfat_mkfs (const gchar *device, const BDExtraArg **extra, GError **error) {
165 const gchar *args[3] = {"mkfs.exfat", device, NULL};
166
167 if (!check_deps (&avail_deps, DEPS_MKEXFAT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
168 return FALSE;
169
170 return bd_utils_exec_and_report_error (args, extra, error);
171 }
172
173 /**
174 * bd_fs_exfat_check:
175 * @device: the device containing the file system to check
176 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
177 * passed to the 'fsck.exfat' utility)
178 * @error: (out) (optional): place to store error (if any)
179 *
180 * Returns: whether the exfat file system on the @device is clean or not
181 *
182 * Tech category: %BD_FS_TECH_EXFAT-%BD_FS_TECH_MODE_CHECK
183 */
184 gboolean bd_fs_exfat_check (const gchar *device, const BDExtraArg **extra, GError **error) {
185 const gchar *args[4] = {"fsck.exfat", "-n", device, NULL};
186 gint status = 0;
187 gboolean ret = FALSE;
188
189 if (!check_deps (&avail_deps, DEPS_FSCKEXFAT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
190 return FALSE;
191
192 ret = bd_utils_exec_and_report_status_error (args, extra, &status, error);
193 if (!ret && (status == 1)) {
194 /* no error should be reported for exit code 1 */
195 g_clear_error (error);
196 }
197 return ret;
198 }
199
200 /**
201 * bd_fs_exfat_repair:
202 * @device: the device containing the file system to repair
203 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
204 * passed to the 'fsck.exfat' utility)
205 * @error: (out) (optional): place to store error (if any)
206 *
207 * Returns: whether the exfat file system on the @device was successfully repaired
208 * (if needed) or not (error is set in that case)
209 *
210 * Tech category: %BD_FS_TECH_EXFAT-%BD_FS_TECH_MODE_REPAIR
211 */
212 gboolean bd_fs_exfat_repair (const gchar *device, const BDExtraArg **extra, GError **error) {
213 const gchar *args[4] = {"fsck.exfat", "-y", device, NULL};
214 gint status = 0;
215 gboolean ret = FALSE;
216
217 if (!check_deps (&avail_deps, DEPS_FSCKEXFAT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
218 return FALSE;
219
220 ret = bd_utils_exec_and_report_status_error (args, extra, &status, error);
221 if (!ret && (status == 1)) {
222 /* no error should be reported for exit code 1 */
223 g_clear_error (error);
224 ret = TRUE;
225 }
226
227 return ret;
228 }
229
230 /**
231 * bd_fs_exfat_set_label:
232 * @device: the device containing the file system to set label for
233 * @label: label to set
234 * @error: (out) (optional): place to store error (if any)
235 *
236 * Returns: whether the label of exfat file system on the @device was
237 * successfully set or not
238 *
239 * Tech category: %BD_FS_TECH_EXFAT-%BD_FS_TECH_MODE_SET_LABEL
240 */
241 gboolean bd_fs_exfat_set_label (const gchar *device, const gchar *label, GError **error) {
242 const gchar *args[5] = {"tune.exfat", "-L", label, device, NULL};
243
244 if (!check_deps (&avail_deps, DEPS_TUNEEXFAT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
245 return FALSE;
246
247 return bd_utils_exec_and_report_error (args, NULL, error);
248 }
249
250 /**
251 * bd_fs_exfat_check_label:
252 * @label: label to check
253 * @error: (out) (optional): place to store error
254 *
255 * Returns: whether @label is a valid label for the exfat file system or not
256 * (reason is provided in @error)
257 *
258 * Tech category: always available
259 */
260 gboolean bd_fs_exfat_check_label (const gchar *label, GError **error) {
261 gsize bytes_written;
262 g_autofree gchar *str = NULL;
263
264 if (g_utf8_validate (label, -1, NULL)) {
265 str = g_convert (label, -1, "UTF-16LE", "UTF-8", NULL, &bytes_written, NULL);
266 }
267
268 if (!str) {
269 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
270 "Label for exFAT filesystem must be a valid UTF-8 string.");
271 return FALSE;
272 }
273
274 if (bytes_written > 22) {
275 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
276 "Label for exFAT filesystem is too long.");
277 return FALSE;
278 }
279
280 return TRUE;
281 }
282
283 /**
284 * bd_fs_exfat_set_uuid:
285 * @device: the device containing the file system to set uuid for
286 * @uuid: (nullable): volume ID to set or %NULL to generate a new one
287 * @error: (out) (optional): place to store error (if any)
288 *
289 * Returns: whether the volume ID of exFAT file system on the @device was
290 * successfully set or not
291 *
292 * Tech category: %BD_FS_TECH_EXFAT-%BD_FS_TECH_MODE_SET_UUID
293 */
294 gboolean bd_fs_exfat_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
295 const gchar *args[5] = {"tune.exfat", "-I", NULL, device, NULL};
296 g_autofree gchar *new_uuid = NULL;
297 size_t len = 0;
298 guint32 rand = 0;
299
300 if (!check_deps (&avail_deps, DEPS_TUNEEXFAT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
301 return FALSE;
302
303 if (!uuid || g_strcmp0 (uuid, "") == 0) {
304 rand = g_random_int ();
305 new_uuid = g_strdup_printf ("0x%08x", rand);
306 args[2] = new_uuid;
307 } else {
308 if (g_str_has_prefix (uuid, "0x")) {
309 /* already format taken by tune.exfat: hexadecimal number with the 0x prefix */
310 args[2] = uuid;
311 } else {
312 len = strlen (uuid);
313 if (len == 9 && uuid[4] == '-') {
314 /* we want to support vol ID in the "udev format", e.g. "2E24-EC82" */
315 new_uuid = g_new0 (gchar, 11);
316 memcpy (new_uuid, "0x", 2);
317 memcpy (new_uuid + 2, uuid, 4);
318 memcpy (new_uuid + 6, uuid + 5, 4);
319 args[2] = new_uuid;
320 } else {
321 new_uuid = g_strdup_printf ("0x%s", uuid);
322 args[2] = new_uuid;
323 }
324 }
325 }
326
327 return bd_utils_exec_and_report_error (args, NULL, error);
328 }
329
330 /**
331 * bd_fs_exfat_check_uuid:
332 * @uuid: UUID to check
333 * @error: (out) (optional): place to store error
334 *
335 * Returns: whether @uuid is a valid UUID for the exFAT file system or not
336 * (reason is provided in @error)
337 *
338 * Tech category: always available
339 */
340 gboolean bd_fs_exfat_check_uuid (const gchar *uuid, GError **error) {
341 guint64 vol_id;
342 gchar *new_uuid = NULL;
343 gchar *endptr = NULL;
344 size_t len = 0;
345
346 if (!uuid)
347 return TRUE;
348
349 len = strlen (uuid);
350 if (len == 9 && uuid[4] == '-') {
351 new_uuid = g_new0 (gchar, 9);
352 memcpy (new_uuid, uuid, 4);
353 memcpy (new_uuid + 4, uuid + 5, 4);
354 } else
355 new_uuid = g_strdup (uuid);
356
357 vol_id = g_ascii_strtoull (new_uuid, &endptr, 16);
358 if ((vol_id == 0 && endptr == new_uuid) || (endptr && *endptr)) {
359 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID,
360 "UUID for exFAT filesystem must be a hexadecimal number.");
361 g_free (new_uuid);
362 return FALSE;
363 }
364
365 if (vol_id > G_MAXUINT32) {
366 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID,
367 "UUID for exFAT filesystem must fit into 32 bits.");
368 g_free (new_uuid);
369 return FALSE;
370 }
371
372 g_free (new_uuid);
373 return TRUE;
374 }
375
376 /**
377 * bd_fs_exfat_get_info:
378 * @device: the device containing the file system to get info for
379 * @error: (out) (optional): place to store error (if any)
380 *
381 * Returns: (transfer full): information about the file system on @device or
382 * %NULL in case of error
383 *
384 * Tech category: %BD_FS_TECH_EXFAT-%BD_FS_TECH_MODE_QUERY
385 */
386 BDFSExfatInfo* bd_fs_exfat_get_info (const gchar *device, GError **error) {
387 const gchar *args[4] = {"tune.exfat", "-v", device, NULL};
388 gboolean success = FALSE;
389 gchar *output = NULL;
390 BDFSExfatInfo *ret = NULL;
391 gchar **lines = NULL;
392 gchar **line_p = NULL;
393 gchar *val_start = NULL;
394
395 if (!check_deps (&avail_deps, DEPS_TUNEEXFAT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
396 return NULL;
397
398 ret = g_new0 (BDFSExfatInfo, 1);
399
400 success = get_uuid_label (device, &(ret->uuid), &(ret->label), error);
401 if (!success) {
402 /* error is already populated */
403 bd_fs_exfat_info_free (ret);
404 return NULL;
405 }
406
407 success = bd_utils_exec_and_capture_output (args, NULL, &output, error);
408 if (!success) {
409 /* error is already populated */
410 bd_fs_exfat_info_free (ret);
411 return NULL;
412 }
413
414 lines = g_strsplit (output, "\n", 0);
415 g_free (output);
416
417 for (line_p=lines; *line_p; line_p++) {
418 if (ret->sector_size == 0) {
419 val_start = g_strrstr (*line_p, BLOCK_SIZE_PREFIX);
420 if (val_start)
421 ret->sector_size = g_ascii_strtoull (val_start + BLOCK_SIZE_PREFIX_LEN, NULL, 0);
422 }
423
424 if (ret->sector_count == 0) {
425 val_start = g_strrstr (*line_p, SECTORS_PREFIX);
426 if (val_start)
427 ret->sector_count = g_ascii_strtoull (val_start + SECTORS_PREFIX_LEN, NULL, 0);
428 }
429
430 if (ret->cluster_count == 0) {
431 val_start = g_strrstr (*line_p, CLUSTERS_PREFIX);
432 if (val_start)
433 ret->cluster_count = g_ascii_strtoull (val_start + CLUSTERS_PREFIX_LEN, NULL, 0);
434 }
435
436 if (ret->sector_size > 0 && ret->sector_count > 0 && ret->cluster_count > 0)
437 break;
438 }
439 g_strfreev (lines);
440
441 if (ret->sector_size == 0 || ret->sector_count == 0 || ret->cluster_count == 0) {
442 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
443 "Failed to parse exFAT info.");
444 bd_fs_exfat_info_free (ret);
445 return NULL;
446 }
447
448 return ret;
449 }