1N/A/**
1N/A * ntfslabel - Part of the Linux-NTFS project.
1N/A *
1N/A * Copyright (c) 2002 Matthew J. Fanto
1N/A * Copyright (c) 2002-2005 Anton Altaparmakov
1N/A * Copyright (c) 2002-2003 Richard Russon
1N/A *
1N/A * This utility will display/change the label on an NTFS partition.
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program (in the main directory of the Linux-NTFS
1N/A * distribution in the file COPYING); if not, write to the Free Software
1N/A * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifdef HAVE_STDLIB_H
1N/A#include <stdlib.h>
1N/A#endif
1N/A#ifdef HAVE_STDIO_H
1N/A#include <stdio.h>
1N/A#endif
1N/A#ifdef HAVE_STRING_H
1N/A#include <string.h>
1N/A#endif
1N/A#ifdef HAVE_ERRNO_H
1N/A#include <errno.h>
1N/A#endif
1N/A#ifdef HAVE_LOCALE_H
1N/A#include <locale.h>
1N/A#endif
1N/A#ifdef HAVE_GETOPT_H
1N/A#include <getopt.h>
1N/A#endif
1N/A
1N/A#include "compat.h"
1N/A#include "debug.h"
1N/A#include "mft.h"
1N/A#include "utils.h"
1N/A#include "version.h"
1N/A#include "logging.h"
1N/A
1N/Astatic const char *EXEC_NAME = "ntfslabel";
1N/A
1N/Astatic struct options {
1N/A char *device; /* Device/File to work with */
1N/A char *label; /* Set the label to this */
1N/A int quiet; /* Less output */
1N/A int verbose; /* Extra output */
1N/A int force; /* Override common sense */
1N/A int noaction; /* Do not write to disk */
1N/A} opts;
1N/A
1N/A/**
1N/A * version - Print version information about the program
1N/A *
1N/A * Print a copyright statement and a brief description of the program.
1N/A *
1N/A * Return: none
1N/A */
1N/Astatic void version(void)
1N/A{
1N/A ntfs_log_info("\n%s v%s (libntfs %s) - Display, or set, the label for an "
1N/A "NTFS Volume.\n\n", EXEC_NAME, VERSION,
1N/A ntfs_libntfs_version());
1N/A ntfs_log_info("Copyright (c)\n");
1N/A ntfs_log_info(" 2002 Matthew J. Fanto\n");
1N/A ntfs_log_info(" 2002-2005 Anton Altaparmakov\n");
1N/A ntfs_log_info(" 2002-2003 Richard Russon\n");
1N/A ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home);
1N/A}
1N/A
1N/A/**
1N/A * usage - Print a list of the parameters to the program
1N/A *
1N/A * Print a list of the parameters and options for the program.
1N/A *
1N/A * Return: none
1N/A */
1N/Astatic void usage(void)
1N/A{
1N/A ntfs_log_info("\nUsage: %s [options] device [label]\n"
1N/A " -n, --no-action Do not write to disk\n"
1N/A " -f, --force Use less caution\n"
1N/A " -q, --quiet Less output\n"
1N/A " -v, --verbose More output\n"
1N/A " -V, --version Display version information\n"
1N/A " -h, --help Display this help\n\n",
1N/A EXEC_NAME);
1N/A ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home);
1N/A}
1N/A
1N/A/**
1N/A * parse_options - Read and validate the programs command line
1N/A *
1N/A * Read the command line, verify the syntax and parse the options.
1N/A * This function is very long, but quite simple.
1N/A *
1N/A * Return: 1 Success
1N/A * 0 Error, one or more problems
1N/A */
1N/Astatic int parse_options(int argc, char *argv[])
1N/A{
1N/A static const char *sopt = "-fh?nqvV";
1N/A static const struct option lopt[] = {
1N/A { "force", no_argument, NULL, 'f' },
1N/A { "help", no_argument, NULL, 'h' },
1N/A { "no-action", no_argument, NULL, 'n' },
1N/A { "quiet", no_argument, NULL, 'q' },
1N/A { "verbose", no_argument, NULL, 'v' },
1N/A { "version", no_argument, NULL, 'V' },
1N/A { NULL, 0, NULL, 0 },
1N/A };
1N/A
1N/A int c = -1;
1N/A int err = 0;
1N/A int ver = 0;
1N/A int help = 0;
1N/A int levels = 0;
1N/A
1N/A opterr = 0; /* We'll handle the errors, thank you. */
1N/A
1N/A while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) {
1N/A switch (c) {
1N/A case 1: /* A non-option argument */
1N/A if (!err && !opts.device)
1N/A opts.device = argv[optind-1];
1N/A else if (!err && !opts.label)
1N/A opts.label = argv[optind-1];
1N/A else
1N/A err++;
1N/A break;
1N/A case 'f':
1N/A opts.force++;
1N/A break;
1N/A case 'h':
1N/A case '?':
1N/A if (strncmp (argv[optind-1], "--log-", 6) == 0) {
1N/A if (!ntfs_log_parse_option (argv[optind-1]))
1N/A err++;
1N/A break;
1N/A }
1N/A help++;
1N/A break;
1N/A case 'n':
1N/A opts.noaction++;
1N/A break;
1N/A case 'q':
1N/A opts.quiet++;
1N/A ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET);
1N/A break;
1N/A case 'v':
1N/A opts.verbose++;
1N/A ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE);
1N/A break;
1N/A case 'V':
1N/A ver++;
1N/A break;
1N/A default:
1N/A ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]);
1N/A err++;
1N/A break;
1N/A }
1N/A }
1N/A
1N/A /* Make sure we're in sync with the log levels */
1N/A levels = ntfs_log_get_levels();
1N/A if (levels & NTFS_LOG_LEVEL_VERBOSE)
1N/A opts.verbose++;
1N/A if (!(levels & NTFS_LOG_LEVEL_QUIET))
1N/A opts.quiet++;
1N/A
1N/A if (help || ver) {
1N/A opts.quiet = 0;
1N/A } else {
1N/A if (opts.device == NULL) {
1N/A if (argc > 1)
1N/A ntfs_log_error("You must specify a device.\n");
1N/A err++;
1N/A }
1N/A
1N/A if (opts.quiet && opts.verbose) {
1N/A ntfs_log_error("You may not use --quiet and --verbose at "
1N/A "the same time.\n");
1N/A err++;
1N/A }
1N/A }
1N/A
1N/A if (ver)
1N/A version();
1N/A if (help || err)
1N/A usage();
1N/A
1N/A return (!err && !help && !ver);
1N/A}
1N/A
1N/A
1N/A/**
1N/A * print_label - display the current label of a mounted ntfs partition.
1N/A * @dev: device to read the label from
1N/A * @mnt_flags: mount flags of the device or 0 if not mounted
1N/A * @mnt_point: mount point of the device or NULL
1N/A *
1N/A * Print the label of the device @dev.
1N/A */
1N/Astatic int print_label(ntfs_volume *vol, unsigned long mnt_flags)
1N/A{
1N/A int result = 0;
1N/A //XXX significant?
1N/A if ((mnt_flags & (NTFS_MF_MOUNTED | NTFS_MF_READONLY)) ==
1N/A NTFS_MF_MOUNTED) {
1N/A ntfs_log_error("%s is mounted read-write, results may be "
1N/A "unreliable.\n", opts.device);
1N/A result = 1;
1N/A }
1N/A
1N/A ntfs_log_info("%s\n", vol->vol_name);
1N/A return result;
1N/A}
1N/A
1N/A/**
1N/A * resize_resident_attribute_value - resize a resident attribute
1N/A * @m: mft record containing attribute to resize
1N/A * @a: attribute record (inside @m) which to resize
1N/A * @new_vsize: the new attribute value size to resize the attribute to
1N/A *
1N/A * Return 0 on success and -1 with errno = ENOSPC if not enough space in the
1N/A * mft record.
1N/A */
1N/Astatic int resize_resident_attribute_value(MFT_RECORD *m, ATTR_RECORD *a,
1N/A const u32 new_vsize)
1N/A{
1N/A int new_alen, new_muse;
1N/A
1N/A /* New attribute length and mft record bytes used. */
1N/A new_alen = (le16_to_cpu(a->u.res.value_offset) + new_vsize + 7) & ~7;
1N/A new_muse = le32_to_cpu(m->bytes_in_use) - le32_to_cpu(a->length) +
1N/A new_alen;
1N/A /* Check for sufficient space. */
1N/A if ((u32)new_muse > le32_to_cpu(m->bytes_allocated)) {
1N/A errno = ENOSPC;
1N/A return -1;
1N/A }
1N/A /* Move attributes behind @a to their new location. */
1N/A memmove((char*)a + new_alen, (char*)a + le32_to_cpu(a->length),
1N/A le32_to_cpu(m->bytes_in_use) - ((char*)a - (char*)m) -
1N/A le32_to_cpu(a->length));
1N/A /* Adjust @m to reflect change in used space. */
1N/A m->bytes_in_use = cpu_to_le32(new_muse);
1N/A /* Adjust @a to reflect new value size. */
1N/A a->length = cpu_to_le32(new_alen);
1N/A a->u.res.value_length = cpu_to_le32(new_vsize);
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * change_label - change the current label on a device
1N/A * @dev: device to change the label on
1N/A * @mnt_flags: mount flags of the device or 0 if not mounted
1N/A * @mnt_point: mount point of the device or NULL
1N/A * @label: the new label
1N/A *
1N/A * Change the label on the device @dev to @label.
1N/A */
1N/Astatic int change_label(ntfs_volume *vol, unsigned long mnt_flags, char *label, BOOL force)
1N/A{
1N/A ntfs_attr_search_ctx *ctx;
1N/A ntfschar *new_label = NULL;
1N/A ATTR_RECORD *a;
1N/A int label_len;
1N/A int result = 0;
1N/A
1N/A //XXX significant?
1N/A if (mnt_flags & NTFS_MF_MOUNTED) {
1N/A /* If not the root fs or mounted read/write, refuse change. */
1N/A if (!(mnt_flags & NTFS_MF_ISROOT) ||
1N/A !(mnt_flags & NTFS_MF_READONLY)) {
1N/A if (!force) {
1N/A ntfs_log_error("Refusing to change label on "
1N/A "read-%s mounted device %s.\n",
1N/A mnt_flags & NTFS_MF_READONLY ?
1N/A "only" : "write", opts.device);
1N/A return 1;
1N/A }
1N/A }
1N/A }
1N/A ctx = ntfs_attr_get_search_ctx(vol->vol_ni, NULL);
1N/A if (!ctx) {
1N/A ntfs_log_perror("Failed to get attribute search context");
1N/A goto err_out;
1N/A }
1N/A if (ntfs_attr_lookup(AT_VOLUME_NAME, AT_UNNAMED, 0, 0, 0, NULL, 0,
1N/A ctx)) {
1N/A if (errno != ENOENT) {
1N/A ntfs_log_perror("Lookup of $VOLUME_NAME attribute failed");
1N/A goto err_out;
1N/A }
1N/A /* The volume name attribute does not exist. Need to add it. */
1N/A a = NULL;
1N/A } else {
1N/A a = ctx->attr;
1N/A if (a->non_resident) {
1N/A ntfs_log_error("Error: Attribute $VOLUME_NAME must be "
1N/A "resident.\n");
1N/A goto err_out;
1N/A }
1N/A }
1N/A label_len = ntfs_mbstoucs(label, &new_label, 0);
1N/A if (label_len == -1) {
1N/A ntfs_log_perror("Unable to convert label string to Unicode");
1N/A goto err_out;
1N/A }
1N/A label_len *= sizeof(ntfschar);
1N/A if (label_len > 0x100) {
1N/A ntfs_log_error("New label is too long. Maximum %u characters "
1N/A "allowed. Truncating excess characters.\n",
1N/A (unsigned)(0x100 / sizeof(ntfschar)));
1N/A label_len = 0x100;
1N/A new_label[label_len / sizeof(ntfschar)] = 0;
1N/A }
1N/A if (a) {
1N/A if (resize_resident_attribute_value(ctx->mrec, a, label_len)) {
1N/A ntfs_log_perror("Error resizing resident attribute");
1N/A goto err_out;
1N/A }
1N/A } else {
1N/A /* sizeof(resident attribute record header) == 24 */
1N/A int asize = (24 + label_len + 7) & ~7;
1N/A u32 biu = le32_to_cpu(ctx->mrec->bytes_in_use);
1N/A if (biu + asize > le32_to_cpu(ctx->mrec->bytes_allocated)) {
1N/A errno = ENOSPC;
1N/A ntfs_log_perror("Error adding resident attribute");
1N/A goto err_out;
1N/A }
1N/A a = ctx->attr;
1N/A memmove((u8*)a + asize, a, biu - ((u8*)a - (u8*)ctx->mrec));
1N/A ctx->mrec->bytes_in_use = cpu_to_le32(biu + asize);
1N/A a->type = AT_VOLUME_NAME;
1N/A a->length = cpu_to_le32(asize);
1N/A a->non_resident = 0;
1N/A a->name_length = 0;
1N/A a->name_offset = cpu_to_le16(24);
1N/A a->flags = cpu_to_le16(0);
1N/A a->instance = ctx->mrec->next_attr_instance;
1N/A ctx->mrec->next_attr_instance = cpu_to_le16((le16_to_cpu(
1N/A ctx->mrec->next_attr_instance) + 1) & 0xffff);
1N/A a->u.res.value_length = cpu_to_le32(label_len);
1N/A a->u.res.value_offset = a->name_offset;
1N/A a->u.res.resident_flags = 0;
1N/A a->u.res.reservedR = 0;
1N/A }
1N/A memcpy((u8*)a + le16_to_cpu(a->u.res.value_offset), new_label, label_len);
1N/A if (!opts.noaction && ntfs_inode_sync(vol->vol_ni)) {
1N/A ntfs_log_perror("Error writing MFT Record to disk");
1N/A goto err_out;
1N/A }
1N/A result = 0;
1N/Aerr_out:
1N/A free(new_label);
1N/A return result;
1N/A}
1N/A
1N/A/**
1N/A * main - Begin here
1N/A *
1N/A * Start from here.
1N/A *
1N/A * Return: 0 Success, the program worked
1N/A * 1 Error, something went wrong
1N/A */
1N/Aint main(int argc, char **argv)
1N/A{
1N/A unsigned long mnt_flags = 0;
1N/A int result = 0;
1N/A ntfs_volume *vol;
1N/A
1N/A ntfs_log_set_handler(ntfs_log_handler_outerr);
1N/A
1N/A if (!parse_options(argc, argv))
1N/A return 1;
1N/A
1N/A utils_set_locale();
1N/A
1N/A if (!opts.label)
1N/A opts.noaction++;
1N/A
1N/A vol = utils_mount_volume(opts.device,
1N/A (opts.noaction ? NTFS_MNT_RDONLY : 0) |
1N/A (opts.force ? NTFS_MNT_FORCE : 0));
1N/A if (!vol)
1N/A return 1;
1N/A
1N/A if (opts.label)
1N/A result = change_label(vol, mnt_flags, opts.label, opts.force);
1N/A else
1N/A result = print_label(vol, mnt_flags);
1N/A
1N/A ntfs_umount(vol, FALSE);
1N/A return result;
1N/A}
1N/A