2 * ntfslabel - Part of the Linux-NTFS project.
4 * Copyright (c) 2002 Matthew J. Fanto
5 * Copyright (c) 2002 Anton Altaparmakov
6 * Copyright (c) 2002-2003 Richard Russon
8 * This utility will display/change the label on an NTFS partition.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program (in the main directory of the Linux-NTFS
22 * distribution in the file COPYING); if not, write to the Free Software
23 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 static const char *EXEC_NAME = "ntfslabel";
41 static struct options {
42 char *device; /* Device/File to work with */
43 char *label; /* Set the label to this */
44 int quiet; /* Less output */
45 int verbose; /* Extra output */
46 int force; /* Override common sense */
47 int noaction; /* Do not write to disk */
50 GEN_PRINTF (Eprintf, stderr, NULL, FALSE)
51 GEN_PRINTF (Vprintf, stdout, &opts.verbose, TRUE)
52 GEN_PRINTF (Qprintf, stdout, &opts.quiet, FALSE)
55 * version - Print version information about the program
57 * Print a copyright statement and a brief description of the program.
63 printf ("\n%s v%s - Display, or set, the label for an NTFS Volume.\n\n",
65 printf ("Copyright (c)\n");
66 printf (" 2002 Matthew J. Fanto\n");
67 printf (" 2002 Anton Altaparmakov\n");
68 printf (" 2002-2003 Richard Russon\n");
69 printf ("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home);
73 * usage - Print a list of the parameters to the program
75 * Print a list of the parameters and options for the program.
81 printf ("\nUsage: %s [options] device [label]\n"
82 " -n --no-action Do not write to disk\n"
83 " -f --force Use less caution\n"
84 " -q --quiet Less output\n"
85 " -v --verbose More output\n"
86 " -V --version Display version information\n"
87 " -h --help Display this help\n\n",
89 printf ("%s%s\n", ntfs_bugs, ntfs_home);
93 * parse_options - Read and validate the programs command line
95 * Read the command line, verify the syntax and parse the options.
96 * This function is very long, but quite simple.
99 * 0 Error, one or more problems
101 int parse_options (int argc, char *argv[])
103 static const char *sopt = "-fh?nqvV";
104 static const struct option lopt[] = {
105 { "force", no_argument, NULL, 'f' },
106 { "help", no_argument, NULL, 'h' },
107 { "no-action", no_argument, NULL, 'n' },
108 { "quiet", no_argument, NULL, 'q' },
109 { "verbose", no_argument, NULL, 'v' },
110 { "version", no_argument, NULL, 'V' },
111 { NULL, 0, NULL, 0 },
119 opterr = 0; /* We'll handle the errors, thank you. */
121 while ((c = getopt_long (argc, argv, sopt, lopt, NULL)) != -1) {
123 case 1: /* A non-option argument */
124 if (!err && !opts.device)
125 opts.device = argv[optind-1];
126 else if (!err && !opts.label)
127 opts.label = argv[optind-1];
151 Eprintf ("Unknown option '%s'.\n", argv[optind-1]);
160 if (opts.device == NULL) {
162 Eprintf ("You must specify a device.\n");
166 if (opts.quiet && opts.verbose) {
167 Eprintf ("You may not use --quiet and --verbose at the same time.\n");
177 return (!err && !help && !ver);
182 * print_label - display the current label of a mounted ntfs partition.
183 * @dev: device to read the label from
184 * @mnt_flags: mount flags of the device or 0 if not mounted
185 * @mnt_point: mount point of the device or NULL
187 * Print the label of the device @dev to stdout.
189 int print_label (ntfs_volume *vol, unsigned long mnt_flags)
193 if ((mnt_flags & (NTFS_MF_MOUNTED | NTFS_MF_READONLY)) ==
195 Eprintf ("%s is mounted read-write, results may be "
196 "unreliable.\n", opts.device);
200 printf("%s\n", vol->vol_name);
205 * resize_resident_attribute_value - resize a resident attribute
206 * @m: mft record containing attribute to resize
207 * @a: attribute record (inside @m) which to resize
208 * @new_vsize: the new attribute value size to resize the attribute to
210 * Return 0 on success and -1 with errno = ENOSPC if not enough space in the
213 int resize_resident_attribute_value(MFT_RECORD *m, ATTR_RECORD *a,
216 int new_alen, new_muse;
218 /* New attribute length and mft record bytes used. */
219 new_alen = (le16_to_cpu(a->value_offset) + new_vsize + 7) & ~7;
220 new_muse = le32_to_cpu(m->bytes_in_use) - le32_to_cpu(a->length) +
222 /* Check for sufficient space. */
223 if (new_muse > le32_to_cpu(m->bytes_allocated)) {
227 /* Move attributes behind @a to their new location. */
228 memmove((char*)a + new_alen, (char*)a + le32_to_cpu(a->length),
229 le32_to_cpu(m->bytes_in_use) - ((char*)a - (char*)m) -
230 le32_to_cpu(a->length));
231 /* Adjust @m to reflect change in used space. */
232 m->bytes_in_use = cpu_to_le32(new_muse);
233 /* Adjust @a to reflect new value size. */
234 a->length = cpu_to_le32(new_alen);
235 a->value_length = cpu_to_le32(new_vsize);
240 * change_label - change the current label on a device
241 * @dev: device to change the label on
242 * @mnt_flags: mount flags of the device or 0 if not mounted
243 * @mnt_point: mount point of the device or NULL
244 * @label: the new label
246 * Change the label on the device @dev to @label.
248 int change_label(ntfs_volume *vol, unsigned long mnt_flags, char *label, BOOL force)
250 ntfs_attr_search_ctx *ctx = NULL;
251 uchar_t *new_label = NULL;
252 MFT_RECORD *mrec = NULL;
258 if (mnt_flags & NTFS_MF_MOUNTED) {
259 /* If not the root fs or mounted read/write, refuse change. */
260 if (!(mnt_flags & NTFS_MF_ISROOT) ||
261 !(mnt_flags & NTFS_MF_READONLY)) {
263 fprintf(stderr, "Refusing to change label on "
264 "read-%s mounted device %s.\n",
265 mnt_flags & NTFS_MF_READONLY ?
266 "only" : "write", opts.device);
272 if (ntfs_file_record_read(vol, (MFT_REF)FILE_Volume, &mrec, NULL)) {
273 perror("Error reading file record");
276 if (!(mrec->flags & MFT_RECORD_IN_USE)) {
277 fprintf(stderr, "Error: $Volume has been deleted. Run "
278 "chkdsk to fix this.\n");
281 ctx = ntfs_attr_get_search_ctx(NULL, mrec);
283 perror("Failed to get attribute search context");
286 if (ntfs_attr_lookup(AT_VOLUME_NAME, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx)) {
287 perror("Lookup of $VOLUME_NAME attribute failed");
291 if (a->non_resident) {
292 fprintf(stderr, "Error: Attribute $VOLUME_NAME must be "
296 label_len = ntfs_mbstoucs(label, &new_label, 0);
297 if (label_len == -1) {
298 perror("Unable to convert label string to Unicode");
301 label_len *= sizeof(uchar_t);
302 if (label_len > 0x100) {
303 fprintf(stderr, "New label is too long. Maximum %i characters "
304 "allowed. Truncating excess characters.\n",
305 0x100 / sizeof(uchar_t));
307 new_label[label_len / sizeof(uchar_t)] = cpu_to_le16(L'\0');
309 if (resize_resident_attribute_value(mrec, a, label_len)) {
310 perror("Error resizing resident attribute");
313 memcpy((char*)a + le16_to_cpu(a->value_offset), new_label, label_len);
314 if (ntfs_mft_record_write(vol, (MFT_REF)FILE_Volume, mrec)) {
315 perror("Error writing MFT Record to disk");
332 * Return: 0 Success, the program worked
333 * 1 Error, something went wrong
335 int main(int argc, char **argv)
337 unsigned long mnt_flags = 0;
341 if (!parse_options (argc, argv))
346 //XXX need to set and get mount flags
347 vol = utils_mount_volume (opts.device, 0, opts.force);
352 result = change_label (vol, mnt_flags, opts.label, opts.force);
354 result = print_label (vol, mnt_flags);
356 ntfs_umount (vol, FALSE);