File: fstab.c

package info (click to toggle)
xfsdump 3.1.9%2B0
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 3,932 kB
  • sloc: ansic: 45,863; sh: 3,227; makefile: 545
file content (461 lines) | stat: -rw-r--r-- 11,217 bytes parent folder | download | duplicates (3)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * Copyright (c) 2002 Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * 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.
 *
 * This program is distributed in the hope that it would 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, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <xfs/xfs.h>
#include <xfs/jdm.h>

#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <ncurses.h>
#include <sys/mman.h>
#include <string.h>
#include <uuid/uuid.h>

#include "types.h"
#include "mlog.h"
#include "inv_priv.h"
#include "getopt.h"

#include "invutil.h"
#include "cmenu.h"
#include "list.h"
#include "invidx.h"
#include "fstab.h"

fstab_fileinfo_t *fstab_file;
int fstab_numfiles;

menu_ops_t fstab_ops = {
    NULL,
    NULL,
    NULL, /*fstab_saveall, */
    fstab_select,
    NULL,
    NULL,
    NULL,
    NULL,
    fstab_highlight,
    menu_unhighlight,
    fstab_commit,
    fstab_prune,
};

/*ARGSUSED*/
int
fstab_commit(WINDOW *win, node_t *current, node_t *list)
{
    int i;
    int fidx;
    node_t *n;
    data_t *d;
    invt_fstab_t *fstabentry;
    int fstabentry_idx;

    n = current;
    if(n == NULL || n->data == NULL)
	return 0;

    d = ((data_t *)(n->data));
    fidx = d->file_idx;
    fstabentry = fstab_file[fidx].data[d->data_idx];

    if(d->deleted == BOOL_TRUE && d->imported == BOOL_FALSE) {
	for(i = 0; i < d->nbr_children; i++) {
	    invidx_commit(win, d->children[i], list);
	}
	mark_all_children_commited(current);

	fstabentry_idx = (int)(((long)fstabentry - (long)fstab_file[fidx].mapaddr - sizeof(invt_counter_t)) / sizeof(invt_fstab_t));

	if (fstab_file[fidx].counter->ic_curnum > 1) {
	    memmove(fstabentry,
		    fstabentry + 1,
		    (sizeof(invt_fstab_t) * (fstab_file[fidx].counter->ic_curnum - fstabentry_idx - 1)));

	    fstab_file[fidx].data[d->data_idx] = NULL;

	    for(i = d->data_idx + 1; i < fstab_file[fidx].numrecords; i++) {
		if(fstab_file[fidx].data[i] != NULL)
		    fstab_file[fidx].data[i]--;
	    }
	}
	fstab_file[fidx].counter->ic_curnum--;
    }
    else if(d->imported == BOOL_TRUE) {
	invt_fstab_t *dest;

	for(i = 0; i < d->nbr_children; i++) {
	    invidx_commit(win, d->children[i], list);
	}
	mark_all_children_commited(current);

	if(find_matching_fstab(0, fstabentry) >= 0) {
	    return 0;
	}

	if(fstab_file[0].counter->ic_curnum < fstab_file[0].nEntries) {
	    dest = fstab_file[0].data[fstab_file[0].counter->ic_curnum - 1] + 1;
	}
	else {
	    /* expand mmap by one */
	    dest = remmap_fstab(0, 1);
	}

	/* copy new data to mmap */
	memmove(dest, fstabentry, sizeof(*fstabentry));

	/* increment counter */
	fstab_file[0].counter->ic_curnum++;
    }

    return 0;
}

/*ARGSUSED*/
int
fstab_select(WINDOW *win, node_t *current, node_t *list)
{
    data_t *d;

    if(current == NULL || current->data == NULL) {
	return 0;
    }
    d = (data_t *)(current->data);

    if(d->expanded == TRUE) {
	return menu_collapse(win, current, list);
    }

    return menu_expand(win, current, list);
}

/*ARGSUSED*/
int
fstab_highlight(WINDOW *win, node_t *current, node_t *list)
{
    static char txt[256];
    char uuidstr[UUID_STR_LEN + 1];
    data_t *d;
    invt_fstab_t *fstabentry;

    if(current == NULL || current->data == NULL) {
	return 0;
    }
    d = (data_t *)(current->data);
    fstabentry = fstab_file[d->file_idx].data[d->data_idx];

    wclear(infowin);

    put_info_header("fstab entry");

    snprintf(txt, sizeof(txt), "device: %s", fstabentry->ft_devpath);
    put_info_line(1, txt);

    uuid_unparse(fstabentry->ft_uuid, uuidstr);
    snprintf(txt, sizeof(txt), "uuid:   %s", uuidstr);
    put_info_line(2, txt);

    return FALSE;
}

/*ARGSUSED*/
int
fstab_prune(char *mountpt, uuid_t *uuidp, time32_t prunetime, node_t *node, node_t *list)
{
    data_t		*d;
    invt_fstab_t	*fstabentry;

    if(node == NULL || node->data == NULL) {
	return BOOL_FALSE;
    }

    d = (data_t *)(node->data);
    fstabentry = fstab_file[d->file_idx].data[d->data_idx];

    if(mountpt != NULL && mntpnt_equal(mountpt, fstabentry->ft_mountpt)) {
	return BOOL_TRUE;
    }
    if((!uuid_is_null(*uuidp))
       && (uuid_compare(*uuidp, fstabentry->ft_uuid) == 0)) {
	return BOOL_TRUE;
    }

    return BOOL_FALSE;
}

int
add_fstab_data(int fidx, invt_fstab_t *data)
{
    if(fstab_file[fidx].numrecords == 0) {
	fstab_file[fidx].data = malloc(sizeof(*fstab_file[fidx].data));
	if(fstab_file[fidx].data == NULL) {
	    fprintf(stderr, "%s: internal memory error: fstab data allocation\n", g_programName);
	    exit(1);
	}
    }
    else {
	fstab_file[fidx].data = realloc(fstab_file[fidx].data,
					sizeof(*fstab_file[fidx].data)
					* (fstab_file[fidx].numrecords + 1));
	if(fstab_file[fidx].data == NULL) {
	    fprintf(stderr, "%s: internal memory error: fstab data reallocation\n", g_programName);
	    exit(1);
	}
    }
    fstab_file[fidx].data[fstab_file[fidx].numrecords] = data;
    fstab_file[fidx].numrecords++;

    return fstab_file[fidx].numrecords - 1;
}

node_t *
generate_fstab_menu(char * inv_path, node_t *startnode, int level, char *fstabname)
{
    int		i;
    char	*invname;
    char	*txt;
    int		idx;
    int		len;
    node_t	*list = NULL;
    node_t	*n;
    invt_fstab_t *fstab_entry;

    if((idx = open_fstab(fstabname)) < 0) {
	return NULL;
    }
    fstab_entry = (invt_fstab_t *)(fstab_file[idx].mapaddr + sizeof(invt_counter_t));

    n = startnode;
    for(i=0; i < fstab_file[idx].counter->ic_curnum; i++) {
	len = strlen(fstab_entry[i].ft_mountpt) + strlen(fstab_entry[i].ft_devpath) + 40;
	txt = malloc(len);
	if(txt == NULL) {
	    fprintf(stderr, "%s: internal memory error: fstab_text\n", g_programName);
	    exit(1);
	}
	snprintf(txt, len, "    fs: %s", fstab_entry[i].ft_mountpt);

	n = list_add(n, node_create(BOOL_FALSE,	/* hidden */
				    BOOL_FALSE,	/* expanded */
				    level,	/* level */
				    BOOL_FALSE,	/* deleted */
				    idx,	/* file_idx */
				    txt,	/* text */
				    &(fstab_ops), /* menu ops */
				    NULL,	/* parent */
				    NULL,	/* children */
				    0,		/* nbr_children */
				    i));	/* data_idx */
	if(i == 0) {
	    list = n;
	}
	if(n == NULL) {
	    fprintf(stderr, "%s: internal memory error: list_add: node_create: fstab\n", g_programName);
	    exit(1);
	}

	add_fstab_data(idx, &(fstab_entry[i]));
	invname = GetNameOfInvIndex(inv_path, fstab_entry[i].ft_uuid);
	n = generate_invidx_menu(inv_path, n, level + 1, invname);
	free(invname);
    }

    return list;
}

int
add_fstab(char *name, int fd, int nEntries, char *mapaddr, invt_counter_t *counter)
{
    static int highwatermark = 10;

    if(fstab_numfiles == 0) {
	fstab_file = malloc(sizeof(*fstab_file) * highwatermark);
	if(fstab_file == NULL) {
	    fprintf(stderr, "%s: internal memory error: malloc fstab_file\n", g_programName);
	    exit(1);
	}
    }
    else {
	if(fstab_numfiles >= highwatermark - 1) {
	    highwatermark += highwatermark;

	    fstab_file = realloc(fstab_file, sizeof(*fstab_file) * highwatermark);
	    if(fstab_file == NULL) {
		fprintf(stderr, "%s: internal memory error: realloc fstab\n", g_programName);
		exit(1);
	    }
	}
    }

    fstab_file[fstab_numfiles].fd	= fd;
    fstab_file[fstab_numfiles].name	= name;
    fstab_file[fstab_numfiles].nEntries = nEntries;
    fstab_file[fstab_numfiles].mapaddr	= mapaddr;
    fstab_file[fstab_numfiles].counter	= counter;
    fstab_file[fstab_numfiles].data	= NULL;
    fstab_file[fstab_numfiles].numrecords = 0;

    fstab_numfiles++;

    return fstab_numfiles - 1;
}

int
fstab_equal(invt_fstab_t *f1, invt_fstab_t *f2)
{
    if(strcmp(f1->ft_mountpt, f2->ft_mountpt) != 0)
	return BOOL_FALSE;

    if(strcmp(f1->ft_devpath, f2->ft_devpath) != 0)
	return BOOL_FALSE;

    if(uuid_compare(f1->ft_uuid, f2->ft_uuid) != 0)
	return BOOL_FALSE;

    return BOOL_TRUE;
}

int
find_matching_fstab(int fidx, invt_fstab_t *fstabentry)
{
    int i;

    for(i = 0; i < fstab_file[fidx].numrecords; i++) {
	if(fstab_equal(fstab_file[fidx].data[i], fstabentry) == BOOL_TRUE) {
	    return i;
	}
    }

    return -1;
}

void *
remmap_fstab(int fidx, int num)
{
    int		i;
    int		nEntries;
    char	*mapaddr;
    invt_fstab_t *fstab_entry;

    nEntries = fstab_file[fidx].counter->ic_curnum;

    munmap(fstab_file[fidx].mapaddr,
	    (nEntries * sizeof(invt_fstab_t)) + sizeof(invt_counter_t));

    /* need to lseek on the file to grow it to the right size - no autogrow on linux */
    lseek(fstab_file[fidx].fd, (num * sizeof(invt_fstab_t)) - 1, SEEK_END);
    write_n_bytes(fstab_file[fidx].fd, "", 1, fstab_file[fidx].name);

    mapaddr = mmap_n_bytes(fstab_file[fidx].fd,
			   (nEntries + 1) * (num * sizeof(invt_fstab_t))
			   + sizeof(invt_counter_t),
			   BOOL_FALSE,
			   fstab_file[fidx].name);

    fstab_file[fidx].nEntries += num;
    fstab_file[fidx].mapaddr = mapaddr;
    fstab_file[fidx].counter = (invt_counter_t *)mapaddr;

    /* regenerate the data index for the file given the new mapping */
    fstab_entry = (invt_fstab_t *)(fstab_file[fidx].mapaddr + sizeof(invt_counter_t));
    for(i=0; i < nEntries; i++) {
    	if(i >= fstab_file[fidx].numrecords) {
	    add_fstab_data(fidx, &(fstab_entry[i]));
	}
	else {
	    fstab_file[fidx].data[i] = &(fstab_entry[i]);
	}
    }

    return &(fstab_entry[i]);
}

int
open_fstab(char *fstabname)
{
    int			fd;
    int			nEntries;
    char		*mapaddr;
    char		*name;
    invt_counter_t	cnt;

    fd = open_and_lock(fstabname, FILE_WRITE, wait_for_locks);
    if (fd < 0) {
	fprintf(stderr, "%s: abnormal termination\n", g_programName);
	exit(1);
    }

    name = strdup(fstabname);
    if(name == NULL) {
	fprintf(stderr, "%s: internal memory error:  strdup fstabname\n", g_programName);
	exit(1);
    }

    read_n_bytes(fd, &cnt, sizeof(invt_counter_t), fstabname);

    nEntries = cnt.ic_curnum;

    mapaddr = mmap_n_bytes(fd,
			   nEntries * sizeof(invt_fstab_t) + sizeof(invt_counter_t),
			   BOOL_FALSE,
			   fstabname);

    return add_fstab(name, fd, nEntries, mapaddr, (invt_counter_t *)mapaddr);
}

int
close_fstab(int fidx)
{
    int fstabentries;

    fstabentries = fstab_file[fidx].counter->ic_curnum;

    munmap(fstab_file[fidx].mapaddr,
	    (fstab_file[fidx].counter->ic_curnum * sizeof(invt_fstab_t)) + sizeof(invt_counter_t));

    if ((fstabentries != 0)  && (fstabentries < fstab_file[fidx].nEntries)) {
	ftruncate(fstab_file[fidx].fd,
		  sizeof(invt_counter_t) + (sizeof(invt_fstab_t) * fstabentries));
    }

    close(fstab_file[fidx].fd);

    if (fstabentries == 0) {
	unlink(fstab_file[fidx].name);
    }

    free(fstab_file[fidx].name);
    free(fstab_file[fidx].data);

    return 0;
}

int
close_all_fstab()
{
    int i;

    for(i = 0; i < fstab_numfiles; i++) {
	close_fstab(i);
    }

    free(fstab_file);
    fstab_file = NULL;
    return 0;
}