summaryrefslogtreecommitdiff
path: root/src/fatlabel.c
blob: ddb80203640e3406e64e0ff2768e65d45c26e467 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* fatlabel.c - User interface

   Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
   Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
   Copyright (C) 2007 Red Hat, Inc.
   Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
   Copyright (C) 2015-2017 Andreas Bombe <aeb@debian.org>
   Copyright (C) 2017-2018 Pali Rohár <pali.rohar@gmail.com>

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program. If not, see <http://www.gnu.org/licenses/>.

   The complete text of the GNU General Public License
   can be found in /usr/share/common-licenses/GPL-3 file.
*/

#include "version.h"

#include <stdbool.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <getopt.h>
#include <ctype.h>

#include "common.h"
#include "fsck.fat.h"
#include "io.h"
#include "boot.h"
#include "fat.h"
#include "file.h"
#include "check.h"
#include "charconv.h"

int rw = 0, list = 0, test = 0, verbose = 0, no_spaces_in_sfns = 0;
long fat_table = 0;
unsigned n_files = 0;
void *mem_queue = NULL;


static void handle_label(bool change, bool reset, const char *device, char *newlabel)
{
    DOS_FS fs = { 0 };
    off_t offset;
    DIR_ENT de;

    char label[12] = { 0 };
    size_t len;
    int ret;
    int i;

    if (change) {
	len = mbstowcs(NULL, newlabel, 0);
	if (len != (size_t)-1 && len > 11) {
	    fprintf(stderr,
		    "fatlabel: labels can be no longer than 11 characters\n");
	    exit(1);
	}

	if (!local_string_to_dos_string(label, newlabel, 12)) {
	    fprintf(stderr,
		    "fatlabel: error when processing label\n");
	    exit(1);
	}

	for (i = strlen(label); i < 11; ++i)
	    label[i] = ' ';
	label[11] = 0;

	ret = validate_volume_label(label);
	if (ret & 0x1) {
	    fprintf(stderr,
		    "fatlabel: warning - lowercase labels might not work properly on some systems\n");
	}
	if (ret & 0x2) {
	    fprintf(stderr,
		    "fatlabel: labels with characters below 0x20 are not allowed\n");
	    exit(1);
	}
	if (ret & 0x4) {
	    fprintf(stderr,
		    "fatlabel: labels with characters *?.,;:/\\|+=<>[]\" are not allowed\n");
	    exit(1);
	}
	if (ret & 0x08) {
	    fprintf(stderr,
		    "fatlabel: labels can't be empty or white space only\n");
	    exit(1);
	}
	if (ret & 0x10) {
	    fprintf(stderr,
		    "fatlabel: labels can't start with a space character\n");
	    exit(1);
	}
    }

    fs_open(device, rw);
    read_boot(&fs);

    if (!change && !reset) {
	if (fs.fat_bits == 32)
	    read_fat(&fs, 0);

	offset = find_volume_de(&fs, &de);
	if (offset != 0) {
	    if (de.name[0] == 0x05)
		de.name[0] = 0xe5;
	    printf("%s\n", pretty_label((char *)de.name));
	}

	if (fs.fat_bits == 32)
	    release_fat(&fs);

	exit(0);
    }

    if (fs.fat_bits == 32)
	read_fat(&fs, 1);

    if (!reset)
	write_label(&fs, label);
    else
	remove_label(&fs);

    if (fs.fat_bits == 32)
	release_fat(&fs);
}


static void handle_volid(bool change, bool reset, const char *device, const char *newserial)
{
    DOS_FS fs = { 0 };
    char *tmp;
    long long conversion;
    uint32_t serial = 0;

    if (change) {
	errno = 0;
	conversion = strtoll(newserial, &tmp, 16);

	if (!*newserial || isspace(*newserial) || *tmp || conversion < 0) {
	    fprintf(stderr, "fatlabel: volume ID must be a hexadecimal number\n");
	    exit(1);
	}
	if (conversion > UINT32_MAX) {
	    fprintf(stderr, "fatlabel: given volume ID does not fit in 32 bit\n");
	    exit(1);
	}
	if (errno) {
	    fprintf(stderr, "fatlabel: parsing volume ID failed (%s)\n", strerror(errno));
	    exit(1);
	}

	serial = conversion;
    }

    if (reset)
	serial = generate_volume_id();

    fs_open(device, rw);
    read_boot(&fs);
    if (!change && !reset) {
	printf("%08x\n", fs.serial);
	exit(0);
    }

    write_serial(&fs, serial);
}


static void usage(int error, int usage_only)
{
    FILE *f = error ? stderr : stdout;
    int status = error ? 1 : 0;

    fprintf(f, "Usage: fatlabel [OPTIONS] DEVICE [NEW]\n");
    if (usage_only)
	exit(status);

    fprintf(f, "Change the FAT filesystem label or serial on DEVICE to NEW or display the\n");
    fprintf(f, "existing label or serial if NEW is not given.\n");
    fprintf(f, "\n");
    fprintf(f, "Options:\n");
    fprintf(f, "  -i, --volume-id     Work on serial number instead of label\n");
    fprintf(f, "  -r, --reset         Remove label or generate new serial number\n");
    fprintf(f, "  -c N, --codepage=N  use DOS codepage N to encode/decode label (default: %d)\n", DEFAULT_DOS_CODEPAGE);
    fprintf(f, "  -V, --version       Show version number and terminate\n");
    fprintf(f, "  -h, --help          Print this message and terminate\n");
    exit(status);
}


int main(int argc, char *argv[])
{
    const struct option long_options[] = {
	{"volume-id", no_argument, NULL, 'i'},
	{"reset",     no_argument, NULL, 'r'},
	{"codepage",  required_argument, NULL, 'c'},
	{"version",   no_argument, NULL, 'V'},
	{"help",      no_argument, NULL, 'h'},
	{0,}
    };
    bool change;
    bool reset = false;
    bool volid_mode = false;
    char *device = NULL;
    char *new = NULL;
    char *tmp;
    long codepage;
    int c;

    check_atari();

    while ((c = getopt_long(argc, argv, "irc:Vh", long_options, NULL)) != -1) {
	switch (c) {
	    case 'i':
		volid_mode = 1;
		break;

	    case 'r':
		reset = true;
		break;

	    case 'c':
		errno = 0;
		codepage = strtol(optarg, &tmp, 10);
		if (!*optarg || isspace(*optarg) || *tmp || errno || codepage < 0 || codepage > INT_MAX) {
		    fprintf(stderr, "Invalid codepage : %s\n", optarg);
		    usage(1, 0);
		}
		if (!set_dos_codepage(codepage))
		    usage(1, 0);
		break;

	    case 'V':
		printf("fatlabel " VERSION " (" VERSION_DATE ")\n");
		exit(0);
		break;

	    case 'h':
		usage(0, 0);
		break;

	    case '?':
		usage(1, 0);
		exit(1);

	    default:
		fprintf(stderr,
			"Internal error: getopt_long() returned unexpected value %d\n", c);
		exit(2);
	}
    }

    if (!set_dos_codepage(-1))	/* set default codepage if none was given in command line */
        exit(1);

    if (optind == argc - 2) {
	change = true;
    } else if (optind == argc - 1) {
	change = false;
    } else {
	usage(1, 1);
    }

    if (change || reset)
	rw = 1;

    if (change && reset) {
	fprintf(stderr, "fatlabel: giving new value with --reset not allowed\n");
	exit(1);
    }

    device = argv[optind++];
    if (change)
	new = argv[optind];

    if (!volid_mode)
	handle_label(change, reset, device, new);
    else
	handle_volid(change, reset, device, new);

    fs_close(rw);
    return 0;
}