]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/label.c
xfsprogs: Release v6.7.0
[thirdparty/xfsprogs-dev.git] / io / label.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2018 Red Hat, Inc. All Rights Reserved.
4 */
5
6 #include <sys/ioctl.h>
7 #include "platform_defs.h"
8 #include "libxfs.h"
9 #include "path.h"
10 #include "command.h"
11 #include "init.h"
12 #include "io.h"
13
14 #ifndef FS_IOC_GETFSLABEL
15 /* Max chars for the interface; fs limits may differ */
16 #define FSLABEL_MAX 256
17 #define FS_IOC_GETFSLABEL _IOR(0x94, 49, char[FSLABEL_MAX])
18 #define FS_IOC_SETFSLABEL _IOW(0x94, 50, char[FSLABEL_MAX])
19 #endif
20
21 static cmdinfo_t label_cmd;
22
23 static void
24 label_help(void)
25 {
26 printf(_(
27 "\n"
28 " Manipulate or query the filesystem label while mounted.\n"
29 "\n"
30 " With no arguments, displays the current filesystem label.\n"
31 " -s newlabel -- set the filesystem label to newlabel\n"
32 " -c -- clear the filesystem label (sets to NULL string)\n"
33 "\n"));
34 }
35
36 static int
37 label_f(
38 int argc,
39 char **argv)
40 {
41 int c;
42 int error;
43 char label[FSLABEL_MAX + 1];
44
45 if (argc == 1) {
46 memset(label, 0, sizeof(label));
47 error = ioctl(file->fd, FS_IOC_GETFSLABEL, &label);
48 goto out;
49 }
50
51 while ((c = getopt(argc, argv, "cs:")) != EOF) {
52 switch (c) {
53 case 'c':
54 label[0] = '\0';
55 break;
56 case 's':
57 if (strlen(optarg) > FSLABEL_MAX) {
58 errno = EINVAL;
59 error = 1;
60 goto out;
61 }
62 strncpy(label, optarg, sizeof(label) - 1);
63 label[sizeof(label) - 1] = 0;
64 break;
65 default:
66 return command_usage(&label_cmd);
67 }
68 }
69
70 /* Check for trailing arguments */
71 if (argc != optind)
72 return command_usage(&label_cmd);
73
74 error = ioctl(file->fd, FS_IOC_SETFSLABEL, label);
75 out:
76 if (error) {
77 perror("label");
78 exitcode = 1;
79 } else {
80 printf("label = \"%s\"\n", label);
81 }
82
83 return 0;
84 }
85
86 void
87 label_init(void)
88 {
89 label_cmd.name = "label";
90 label_cmd.cfunc = label_f;
91 label_cmd.argmin = 0;
92 label_cmd.argmax = 3;
93 label_cmd.args = _("[-s label|-c]");
94 label_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
95 label_cmd.oneline =
96 _("query, set, or clear the filesystem label while mounted");
97 label_cmd.help = label_help;
98
99 add_command(&label_cmd);
100 }