Debian Codegraph @

src/plugins/fs/vfat.c

raw
1 /*
2 * Copyright (C) 2017 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: Vratislav Podzimek <[email protected]>
18 */
19
20 #include <blockdev/utils.h>
21 #include <check_deps.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26
27 #include "vfat.h"
28 #include "fs.h"
29 #include "common.h"
30
31 static volatile guint avail_deps = 0;
32 static GMutex deps_check_lock;
33
34 G_GNUC_INTERNAL
35 void _fs_vfat_reset_avail_deps (void) {
36 g_atomic_int_set (&avail_deps, 0);
37 }
38
39 #define DEPS_MKFSVFAT 0
40 #define DEPS_MKFSVFAT_MASK (1 << DEPS_MKFSVFAT)
41 #define DEPS_FATLABEL 1
42 #define DEPS_FATLABEL_MASK (1 << DEPS_FATLABEL)
43 #define DEPS_FSCKVFAT 2
44 #define DEPS_FSCKVFAT_MASK (1 << DEPS_FSCKVFAT)
45 #define DEPS_RESIZEVFAT 3
46 #define DEPS_RESIZEVFAT_MASK (1 << DEPS_RESIZEVFAT)
47 #define DEPS_FATLABELUUID 4
48 #define DEPS_FATLABELUUID_MASK (1 << DEPS_FATLABELUUID)
49
50 #define DEPS_LAST 5
51
52 static const UtilDep deps[DEPS_LAST] = {
53 {"mkfs.vfat", NULL, NULL, NULL},
54 {"fatlabel", NULL, NULL, NULL},
55 {"fsck.vfat", NULL, NULL, NULL},
56 {"vfat-resize", NULL, NULL, NULL},
57 {"fatlabel", "4.2", "--version", "fatlabel\\s+([\\d\\.]+).+"},
58 };
59
60 static guint32 fs_mode_util[BD_FS_MODE_LAST+1] = {
61 DEPS_MKFSVFAT_MASK, /* mkfs */
62 0, /* wipe */
63 DEPS_FSCKVFAT_MASK, /* check */
64 DEPS_FSCKVFAT_MASK, /* repair */
65 DEPS_FATLABEL_MASK, /* set-label */
66 DEPS_FSCKVFAT_MASK, /* query */
67 DEPS_RESIZEVFAT_MASK, /* resize */
68 DEPS_FATLABELUUID_MASK, /* set-uuid */
69 };
70
71
72 #ifdef __clang__
73 #define ZERO_INIT {}
74 #else
75 #define ZERO_INIT {0}
76 #endif
77
78 /**
79 * bd_fs_vfat_is_tech_avail:
80 * @tech: the queried tech
81 * @mode: a bit mask of queried modes of operation (#BDFSTechMode) for @tech
82 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
83 *
84 * Returns: whether the @tech-@mode combination is available -- supported by the
85 * plugin implementation and having all the runtime dependencies available
86 */
87 G_GNUC_INTERNAL gboolean
88 bd_fs_vfat_is_tech_avail (BDFSTech tech G_GNUC_UNUSED, guint64 mode, GError **error) {
89 guint32 required = 0;
90 guint i = 0;
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_vfat_info_copy: (skip)
101 * @data: (nullable): %BDFSVfatInfo to copy
102 *
103 * Creates a new copy of @data.
104 */
105 BDFSVfatInfo* bd_fs_vfat_info_copy (BDFSVfatInfo *data) {
106 if (data == NULL)
107 return NULL;
108
109 BDFSVfatInfo *ret = g_new0 (BDFSVfatInfo, 1);
110
111 ret->label = g_strdup (data->label);
112 ret->uuid = g_strdup (data->uuid);
113 ret->cluster_size = data->cluster_size;
114 ret->cluster_count = data->cluster_count;
115 ret->free_cluster_count = data->free_cluster_count;
116
117 return ret;
118 }
119
120 /**
121 * bd_fs_vfat_info_free: (skip)
122 * @data: (nullable): %BDFSVfatInfo to free
123 *
124 * Frees @data.
125 */
126 void bd_fs_vfat_info_free (BDFSVfatInfo *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 /* we want to support vol ID in the "udev format", e.g. "2E24-EC82" */
136 static gchar *_fix_uuid (const gchar *uuid) {
137 gchar *new_uuid = NULL;
138 size_t len = 0;
139
140 len = strlen (uuid);
141 if (len == 9 && uuid[4] == '-') {
142 new_uuid = g_new0 (gchar, 9);
143 memcpy (new_uuid, uuid, 4);
144 memcpy (new_uuid + 4, uuid + 5, 4);
145 } else
146 new_uuid = g_strdup (uuid);
147
148 return new_uuid;
149 }
150
151 G_GNUC_INTERNAL BDExtraArg **
152 bd_fs_vfat_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) {
153 GPtrArray *options_array = g_ptr_array_new ();
154 const BDExtraArg **extra_p = NULL;
155 gchar *label;
156 UtilDep dep = {"mkfs.vfat", "4.2", "--help", "mkfs.fat\\s+([\\d\\.]+).+"};
157 gboolean new_vfat = FALSE;
158 gchar *new_uuid = NULL;
159
160 if (options->label && g_strcmp0 (options->label, "") != 0) {
161 /* convert the label uppercase */
162 label = g_ascii_strup (options->label, -1);
163 g_ptr_array_add (options_array, bd_extra_arg_new ("-n", label));
164 g_free (label);
165 }
166
167 if (options->uuid && g_strcmp0 (options->uuid, "") != 0) {
168 new_uuid = _fix_uuid (options->uuid);
169 g_ptr_array_add (options_array, bd_extra_arg_new ("-i", new_uuid));
170 g_free (new_uuid);
171 }
172
173 if (options->force)
174 g_ptr_array_add (options_array, bd_extra_arg_new ("-I", ""));
175
176 if (options->no_pt) {
177 /* only mkfs.vfat >= 4.2 (sometimes) creates the partition table */
178 new_vfat = bd_utils_check_util_version (dep.name, dep.version,
179 dep.ver_arg, dep.ver_regexp,
180 NULL);
181 if (new_vfat)
182 g_ptr_array_add (options_array, bd_extra_arg_new ("--mbr=no", ""));
183 }
184
185 if (extra) {
186 for (extra_p = extra; *extra_p; extra_p++)
187 g_ptr_array_add (options_array, bd_extra_arg_copy ((BDExtraArg *) *extra_p));
188 }
189
190 g_ptr_array_add (options_array, NULL);
191
192 return (BDExtraArg **) g_ptr_array_free (options_array, FALSE);
193 }
194
195 /**
196 * bd_fs_vfat_mkfs:
197 * @device: the device to create a new vfat fs on
198 * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
199 * passed to the 'mkfs.vfat' utility)
200 * @error: (out) (optional): place to store error (if any)
201 *
202 * Please remember that FAT labels should always be uppercase.
203 *
204 * Returns: whether a new vfat fs was successfully created on @device or not
205 *
206 * Tech category: %BD_FS_TECH_VFAT-%BD_FS_TECH_MODE_MKFS
207 */
208 gboolean bd_fs_vfat_mkfs (const gchar *device, const BDExtraArg **extra, GError **error) {
209 const gchar *args[3] = {"mkfs.vfat", device, NULL};
210
211 if (!check_deps (&avail_deps, DEPS_MKFSVFAT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
212 return FALSE;
213
214 return bd_utils_exec_and_report_error (args, extra, error);
215 }
216
217 /**
218 * bd_fs_vfat_check:
219 * @device: the device containing the file system to check
220 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
221 * passed to the 'fsck.vfat' utility)
222 * @error: (out) (optional): place to store error (if any)
223 *
224 * Returns: whether an vfat file system on the @device is clean or not
225 *
226 * Tech category: %BD_FS_TECH_VFAT-%BD_FS_TECH_MODE_CHECK
227 */
228 gboolean bd_fs_vfat_check (const gchar *device, const BDExtraArg **extra, GError **error) {
229 const gchar *args[4] = {"fsck.vfat", "-n", device, NULL};
230 gint status = 0;
231 gboolean ret = FALSE;
232
233 if (!check_deps (&avail_deps, DEPS_FSCKVFAT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
234 return FALSE;
235
236 ret = bd_utils_exec_and_report_status_error (args, extra, &status, error);
237 if (!ret && (status == 1)) {
238 /* no error should be reported for exit code 1 -- Recoverable errors have been detected */
239 g_clear_error (error);
240 }
241 return ret;
242 }
243
244 /**
245 * bd_fs_vfat_repair:
246 * @device: the device containing the file system to repair
247 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
248 * passed to the 'fsck.vfat' utility)
249 * @error: (out) (optional): place to store error (if any)
250 *
251 * Returns: whether an vfat file system on the @device was successfully repaired
252 * (if needed) or not (error is set in that case)
253 *
254 * Tech category: %BD_FS_TECH_VFAT-%BD_FS_TECH_MODE_REPAIR
255 */
256 gboolean bd_fs_vfat_repair (const gchar *device, const BDExtraArg **extra, GError **error) {
257 const gchar *args[4] = {"fsck.vfat", "-a", device, NULL};
258 gint status = 0;
259 gboolean ret = FALSE;
260
261 if (!check_deps (&avail_deps, DEPS_FSCKVFAT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
262 return FALSE;
263
264 ret = bd_utils_exec_and_report_status_error (args, extra, &status, error);
265 if (!ret) {
266 if (status == 1) {
267 /* exit code 1 can also mean "errors have been detected and corrected" so we need
268 to run fsck again to make sure the filesystem is now clean */
269 g_clear_error (error);
270 ret = bd_utils_exec_and_report_status_error (args, extra, &status, error);
271 } else
272 /* FALSE and exit code other than 1 always means error */
273 ret = FALSE;
274 }
275
276 return ret;
277 }
278
279 /**
280 * bd_fs_vfat_set_label:
281 * @device: the device containing the file system to set label for
282 * @label: label to set
283 * @error: (out) (optional): place to store error (if any)
284 *
285 * Returns: whether the label of vfat file system on the @device was
286 * successfully set or not
287 *
288 * Tech category: %BD_FS_TECH_VFAT-%BD_FS_TECH_MODE_SET_LABEL
289 */
290 gboolean bd_fs_vfat_set_label (const gchar *device, const gchar *label, GError **error) {
291 const gchar *args[4] = {"fatlabel", device, NULL, NULL};
292 UtilDep dep = {"fatlabel", "4.2", "--version", "fatlabel\\s+([\\d\\.]+).+"};
293 gchar *label_up = NULL;
294 gboolean new_vfat = FALSE;
295 gboolean ret;
296
297 if (!check_deps (&avail_deps, DEPS_FATLABEL_MASK, deps, DEPS_LAST, &deps_check_lock, error))
298 return FALSE;
299
300 if (!label || g_strcmp0 (label, "") == 0) {
301 /* fatlabel >= 4.2 refuses to set empty label */
302 new_vfat = bd_utils_check_util_version (dep.name, dep.version,
303 dep.ver_arg, dep.ver_regexp,
304 NULL);
305 if (new_vfat)
306 args[2] = "--reset";
307 }
308
309 /* forcefully convert the label uppercase in case no reset was requested */
310 if (label && args[2] == NULL) {
311 label_up = g_ascii_strup (label, -1);
312 args[2] = label_up;
313 }
314 ret = bd_utils_exec_and_report_error (args, NULL, error);
315 g_free (label_up);
316
317 return ret;
318 }
319
320 /**
321 * bd_fs_vfat_check_label:
322 * @label: label to check
323 * @error: (out) (optional): place to store error
324 *
325 * Returns: whether @label is a valid label for the vfat file system or not
326 * (reason is provided in @error)
327 *
328 * Tech category: always available
329 */
330 gboolean bd_fs_vfat_check_label (const gchar *label, GError **error) {
331 const gchar *forbidden = "\"*/:<>?\\|";
332 guint n;
333
334 if (strlen (label) > 11) {
335 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
336 "Label for VFAT filesystem must be at most 11 characters long.");
337 return FALSE;
338 }
339
340 /* VFAT does not allow some characters; as dosfslabel does not enforce this,
341 * check in advance; also, VFAT only knows upper-case characters, dosfslabel
342 * enforces this */
343 for (n = 0; forbidden[n] != 0; n++)
344 if (strchr (label, forbidden[n]) != NULL) {
345 g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
346 "Invalid label: character '%c' not supported in VFAT labels.",
347 forbidden[n]);
348 return FALSE;
349 }
350
351 return TRUE;
352 }
353
354 /**
355 * bd_fs_vfat_set_uuid:
356 * @device: the device containing the file system to set uuid for
357 * @uuid: (nullable): volume ID to set or %NULL to generate a new one
358 * @error: (out) (optional): place to store error (if any)
359 *
360 * Returns: whether the volume ID of vfat file system on the @device was
361 * successfully set or not
362 *
363 * Tech category: %BD_FS_TECH_VFAT-%BD_FS_TECH_MODE_SET_UUID
364 */
365 gboolean bd_fs_vfat_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
366 const gchar *args[5] = {"fatlabel", "-i", device, NULL, NULL};
367 g_autofree gchar *new_uuid = NULL;
368
369 if (!check_deps (&avail_deps, DEPS_FATLABELUUID_MASK, deps, DEPS_LAST, &deps_check_lock, error))
370 return FALSE;
371
372 if (!uuid || g_strcmp0 (uuid, "") == 0)
373 args[3] = "--reset";
374 else {
375 new_uuid = _fix_uuid (uuid);
376 args[3] = new_uuid;
377 }
378
379 return bd_utils_exec_and_report_error (args, NULL, error);
380 }
381
382 /**
383 * bd_fs_vfat_check_uuid:
384 * @uuid: UUID to check
385 * @error: (out) (optional): place to store error
386 *
387 * Returns: whether @uuid is a valid UUID for the vfat file system or not
388 * (reason is provided in @error)
389 *
390 * Tech category: always available
391 */
392 gboolean bd_fs_vfat_check_uuid (const gchar *uuid, GError **error) {
393 guint64 vol_id;
394 gchar *new_uuid = NULL;
395 gchar *endptr = NULL;
396
397 if (!uuid)
398 return TRUE;
399
400 new_uuid = _fix_uuid (uuid);
401
402 vol_id = g_ascii_strtoull (new_uuid, &endptr, 16);
403 if ((vol_id == 0 && endptr == new_uuid) || (endptr && *endptr)) {
404 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID,
405 "UUID for VFAT filesystem must be a hexadecimal number.");
406 g_free (new_uuid);
407 return FALSE;
408 }
409
410 if (vol_id > G_MAXUINT32) {
411 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_UUID_INVALID,
412 "UUID for VFAT filesystem must fit into 32 bits.");
413 g_free (new_uuid);
414 return FALSE;
415 }
416
417 g_free (new_uuid);
418 return TRUE;
419 }
420
421 /**
422 * bd_fs_vfat_get_info:
423 * @device: the device containing the file system to get info for
424 * @error: (out) (optional): place to store error (if any)
425 *
426 * Returns: (transfer full): information about the file system on @device or
427 * %NULL in case of error
428 *
429 * Tech category: %BD_FS_TECH_VFAT-%BD_FS_TECH_MODE_QUERY
430 */
431 BDFSVfatInfo* bd_fs_vfat_get_info (const gchar *device, GError **error) {
432 const gchar *args[4] = {"fsck.vfat", "-nv", device, NULL};
433 gboolean success = FALSE;
434 BDFSVfatInfo *ret = NULL;
435 gchar *output = NULL;
436 gchar **lines = NULL;
437 gchar **line_p = NULL;
438 gboolean have_cluster_size = FALSE;
439 gboolean have_cluster_count = FALSE;
440 guint64 full_cluster_count = 0;
441 guint64 cluster_count = 0;
442 gchar **key_val = NULL;
443 gint scanned = 0;
444
445 if (!check_deps (&avail_deps, DEPS_FSCKVFAT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
446 return NULL;
447
448 ret = g_new0 (BDFSVfatInfo, 1);
449
450 success = get_uuid_label (device, &(ret->uuid), &(ret->label), error);
451 if (!success) {
452 /* error is already populated */
453 bd_fs_vfat_info_free (ret);
454 return NULL;
455 }
456
457 success = bd_utils_exec_and_capture_output (args, NULL, &output, error);
458 if (!success) {
459 /* error is already populated */
460 bd_fs_vfat_info_free (ret);
461 return NULL;
462 }
463
464 lines = g_strsplit (output, "\n", 0);
465 g_free (output);
466 for (line_p=lines; *line_p && (!have_cluster_size || !have_cluster_count); line_p++) {
467 if (!have_cluster_size && g_str_has_suffix (*line_p, "bytes per cluster")) {
468 ret->cluster_size = g_ascii_strtoull (*line_p, NULL, 0);
469 have_cluster_size = TRUE;
470 } else if (!have_cluster_count && g_str_has_prefix (*line_p, device)) {
471 key_val = g_strsplit (*line_p, ",", 2);
472 scanned = sscanf (key_val[1], " %" G_GUINT64_FORMAT "/" "%" G_GUINT64_FORMAT " clusters",
473 &full_cluster_count, &cluster_count);
474 if (scanned != 2) {
475 g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
476 "Failed to get number of FAT clusters for '%s'", device);
477 bd_fs_vfat_info_free (ret);
478 g_strfreev (key_val);
479 g_strfreev (lines);
480 return NULL;
481 }
482 ret->cluster_count = cluster_count;
483 ret->free_cluster_count = cluster_count - full_cluster_count;
484 have_cluster_count = TRUE;
485 g_strfreev (key_val);
486 }
487 }
488 g_strfreev (lines);
489
490 return ret;
491 }
492
493 /**
494 * bd_fs_vfat_resize:
495 * @device: the device the file system of which to resize
496 * @new_size: new requested size for the file system (if 0, the file system is
497 * adapted to the underlying block device)
498 * @error: (out) (optional): place to store error (if any)
499 *
500 * Returns: whether the file system on @device was successfully resized or not
501 *
502 * Tech category: %BD_FS_TECH_VFAT-%BD_FS_TECH_MODE_RESIZE
503 */
504 gboolean bd_fs_vfat_resize (const gchar *device, guint64 new_size, GError **error) {
505 g_autofree gchar *size_str = NULL;
506 const gchar *args[4] = {"vfat-resize", device, NULL, NULL};
507
508 if (!check_deps (&avail_deps, DEPS_RESIZEVFAT_MASK, deps, DEPS_LAST, &deps_check_lock, error))
509 return FALSE;
510
511 if (new_size != 0) {
512 size_str = g_strdup_printf ("%"G_GUINT64_FORMAT, new_size);
513 args[2] = size_str;
514 }
515
516 return bd_utils_exec_and_report_error (args, NULL, error);
517 }