summaryrefslogblamecommitdiffstats
path: root/misc-utils/findmnt.c
blob: 04aadb778aa8445ae933ceda06beba71b4aadb91 (plain) (tree)
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














































































































































































































































































































































































































































                                                                                        
/*
 * findmnt(8)
 *
 * Copyright (C) 2010 Red Hat, Inc. All rights reserved.
 * Written by Karel Zak <kzak@redhat.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 2 of the License, or
 * (at your option) any later version.
 *
 * 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 to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <err.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>

#include <mount.h>

#include "pathnames.h"
#include "nls.h"

/*
 * Column IDs
 */
enum {
	COL_SOURCE,
	COL_TARGET,
	COL_FSTYPE,
	COL_OPTIONS,
	COL_ISMOUNTED,
	COL_LABEL,
	COL_UUID,

	__NCOLUMNS
};

/*
 * Column names
 */
const char *colnames[] = {
	[COL_SOURCE] = "source",
	[COL_TARGET] = "target",
	[COL_FSTYPE] = "fstype",
	[COL_OPTIONS] = "options",
	[COL_ISMOUNTED] = "mounted",
	[COL_LABEL] = "label",
	[COL_UUID] = "uuid"
};

enum {
	FL_EVALUATE	= (1 << 1),
	FL_CANONICALIZE = (1 << 2),
	FL_FIRSTONLY	= (1 << 3),
	FL_INVERT	= (1 << 4)
};

struct match_data {
	const char *source;
	const char *target;
	const char *fstypes;
	const char *options;
};

int flags;
int columns[ __NCOLUMNS ];
int ncolumns;
mnt_cache *cache;

/*
 * converts @name to column ID
 */
static int get_column_id(const char *name, size_t namesz)
{
	int i;

	for (i = 0; i < __NCOLUMNS; i++) {
		const char *cn = colnames[i];

		if (!strncmp(name, cn, namesz) && !*(cn + namesz))
			return i;
	}

	errx(EXIT_FAILURE, _("unknown column: %*s"), (int) namesz, name);
	return -1;
}

/*
 * parses list of columns from @str and set column IDs to columns[]
 */
static int ctl_set_columns(const char *str)
{
	const char *begin = NULL, *end = NULL, *p;

	ncolumns = 0;

	if (!str || !*str)
		return -1;

	p = str;
	for (; p && *p; p++) {
		if (!begin)
			begin = p;		/* begin of the column name */
		if (*p == ',')
			end = p;		/* terminate the name */
		if (*(p + 1) == '\0')
			end = p + 1;		/* end of string */
		if (!begin || !end)
			continue;
		if (end <= begin)
			return -1;

		columns[ ncolumns++ ] =	get_column_id(begin, end - begin);
	}
	return 0;
}

static int print_column(mnt_fs *fs, int id)
{
	const char *str = NULL;

	if (!fs)
		return -1;

	switch(id) {
	case COL_SOURCE:
		/* dir or dev */
		str = mnt_fs_get_source(fs);

		if (str && ((flags & FL_EVALUATE) || (flags & FL_CANONICALIZE)))
			str = mnt_resolve_spec(str, cache);
		break;
	case COL_TARGET:
		str = mnt_fs_get_target(fs);
		break;
	case COL_FSTYPE:
		str = mnt_fs_get_fstype(fs);
		break;
	case COL_OPTIONS:
		str = mnt_fs_get_optstr(fs);
		break;
	default:
		return -1;
	}

	if (str)
		printf("%s", str);
	return 0;
}

static int print_fs(mnt_fs *fs)
{
	int i;

	for (i = 0; i < ncolumns; i++) {
		print_column(fs, i);
		printf("\t");
	}
	printf("\n");
	return 0;
}

static mnt_tab *parse_tabfile(const char *path)
{
	mnt_tab *tb = mnt_new_tab(path);
	if (!tb)
		return NULL;

	if (mnt_tab_parse_file(tb) != 0)
		goto err;

	if (mnt_tab_get_nerrs(tb)) {
		char buf[BUFSIZ];
		mnt_tab_strerror(tb, buf, sizeof(buf));
		warnx(_("%s: parse error: %s"), path, buf);
	}
	return tb;
err:
	mnt_free_tab(tb);
	err(EXIT_FAILURE, _("can't read: %s"), path);

	return NULL;
}

static int match_func(mnt_fs *fs, void *data)
{
	struct match_data *m = (struct match_data *) data;
	int rc = flags & FL_INVERT ? 1 : 0;

	/*fprintf(stderr, "source: %s : %s\n", m->source, mnt_fs_get_source(fs));*/

	if (m->target && !mnt_fs_match_target(fs, m->target, cache))
		return rc;
	if (m->source && !mnt_fs_match_source(fs, m->source, cache))
		return rc;
	if (m->fstypes && !mnt_fs_match_fstype(fs, m->fstypes))
		return rc;
	if (m->options && !mnt_fs_match_options(fs, m->options))
		return rc;

	return !rc;
}

static inline int is_list_mode(struct match_data *m)
{
	return (!m->source && !m->target && !m->fstypes && !m->options);
}

static inline int is_mount_mode(struct match_data *m, int fl)
{
	if (!m->source)
	       return 0;		/* <devname|TAG=|mountpoint> is required */
	if (m->fstypes || m->options)
		return 0;		/* cannot be restricted by -t or -O */
	if (!(fl & FL_FIRSTONLY))
		return 0;		/* we have to return the first entry only */

	return 1;			/* ok */
}

static void __attribute__((__noreturn__)) usage(FILE *out)
{
	fprintf(out, _("Usage: %s [options] <spec> [<target>]\n\nOptions:\n"),
			program_invocation_short_name);

	fprintf(out, _(
	" -s, --fstab            search in static table of filesystems\n"
	" -m, --mtab             search in table of mounted filesystems (default)\n"
	" -k, --kernel           search in kernel (mountinfo) file\n\n"

	" -c, --canonicalize     canonicalize printed paths\n"
	" -d, --direction <word> search direction - 'forward' or 'backward'\n"
	" -e, --evaluate         print all TAGs (LABEL/UUID) evaluated\n"
	" -h, --help             print this help\n"
	" -i, --invert           invert sense of matching\n"
        " -l, --first-only       print the first found filesystem only\n"
	" -o, --output <list>    output columns\n"
	" -O, --options <list>   limit the set of filesystems by mount options\n"
	" -t, --types <list>     limit the set of filesystem by FS types\n"));

	fprintf(out, _("\nFor more information see findmnt(1).\n"));

	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}

int main(int argc, char *argv[])
{
	char *tabfile = NULL;
	int direction = MNT_ITER_FORWARD, ct = 0;
	mnt_tab *tb;
	mnt_iter *itr;
	mnt_fs *fs = NULL;
	int c;
	struct match_data mdata_buf, *mdata = &mdata_buf;

	struct option longopts[] = {
	    { "fstab",        0, 0, 's' },
	    { "mtab",         0, 0, 'm' },
	    { "kernel",       0, 0, 'k' },
	    { "canonicalize", 0, 0, 'c' },
	    { "direction",    1, 0, 'd' },
	    { "evaluate",     0, 0, 'e' },
	    { "help",         0, 0, 'h' },
	    { "invert",       0, 0, 'i' },
	    { "first-only",   0, 0, 'l' },
	    { "output",       0, 0, 'o' },
	    { "options",      1, 0, 'O' },
	    { "types",        1, 0, 't' },
	    { NULL,           0, 0, 0 }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	/* default */
	columns[ncolumns++] = COL_SOURCE;
	columns[ncolumns++] = COL_TARGET;
	columns[ncolumns++] = COL_FSTYPE;
	columns[ncolumns++] = COL_OPTIONS;

	memset(mdata, 0, sizeof(*mdata));

	while ((c = getopt_long(argc, argv, "cd:ehiloO:kmst:", longopts, NULL)) != -1) {
		switch(c) {
		case 'c':
			flags |= FL_CANONICALIZE;
			break;
		case 'd':
			if (!strcmp(optarg, _("forward")))
				direction = MNT_ITER_FORWARD;
			else if (!strcmp(optarg, _("backward")))
				direction = MNT_ITER_BACKWARD;
			else
				errx(EXIT_FAILURE,
					_("uknown direction '%s')"), optarg);
			break;
		case 'e':
			flags |= FL_EVALUATE;
			break;
		case 'h':
			usage(stdout);
			break;
		case 'i':
			flags |= FL_INVERT;
			break;
		case 'l':
			flags |= FL_FIRSTONLY;
			break;
		case 'o':
			ctl_set_columns(optarg);
			break;
		case 'O':
			mdata->options = optarg;
			break;
		case 'm':
			if (tabfile)
				errx(EXIT_FAILURE, _("--{fstab,mtab,kernel} "
					"options are mutually exclusive"));
			tabfile = _PATH_MOUNTED;
			break;
		case 's':
			if (tabfile)
				errx(EXIT_FAILURE, _("--{fstab,mtab,kernel} "
					"options are mutually exclusive"));
			tabfile = _PATH_MNTTAB;
			break;
		case 'k':
			if (tabfile)
				errx(EXIT_FAILURE, _("--{fstab,mtab,kernel} "
					"options are mutually exclusive"));
			tabfile = _PATH_PROC_MOUNTINFO;
			break;
		case 't':
			mdata->fstypes = optarg;
			break;
		default:
			usage(stderr);
			break;
		}
	}

	if (!tabfile)
		tabfile = _PATH_MOUNTED;
	if (optind < argc)
		/* dev, tag or mountpoint */
		mdata->source = argv[optind++];
	if (optind < argc)
		/* mountpoint */
		mdata->target = argv[optind++];

	tb = parse_tabfile(tabfile);
	if (!tb)
		return EXIT_FAILURE;

	itr = mnt_new_iter(direction);
	if (!itr)
		err(EXIT_FAILURE, _("failed to initialize libmount iterator"));

	cache = mnt_new_cache();
	if (!cache)
		err(EXIT_FAILURE, _("failed to initialize libmount cache"));

	mnt_tab_set_cache(tb, cache);

	if (is_list_mode(mdata)) {
		/*
		 * Print whole file
		 */
		while(mnt_tab_next_fs(tb, itr, &fs) == 0) {
			print_fs(fs);
			ct++;
			if (flags & FL_FIRSTONLY)
				break;
		}
	} else if (is_mount_mode(mdata, flags)) {

		/*
		 * Look up for FS in the same way how mount(8) searchs in fstab
		 *
		 *   findmnt -l <spec>
		 */
		fs = mnt_tab_find_source(tb, mdata->source, direction);
		if (!fs)
			fs = mnt_tab_find_target(tb, mdata->source, direction);
		if (fs) {
			print_fs(fs);
			ct++;
		}
	} else {
		/*
		 * Look up for all matching entries
		 *
		 *    findmnt [-l] <source> <target> [-O <options>] [-t <types>]
		 *    findmnt [-l] <spec> [-O <options>] [-t <types>]
		 */
again:
		while (!mnt_tab_find_next_fs(tb, itr,
					match_func, (void *) mdata, &fs)) {
			print_fs(fs);
			ct++;
			if (flags & FL_FIRSTONLY)
				break;
		}
		if (!ct && !mdata->target && mdata->source) {
			/* swap 'spec' and target. */
			mdata->target = mdata->source;
			mdata->source = NULL;
			mnt_reset_iter(itr, direction);
			goto again;
		}
	}

	mnt_free_tab(tb);
	mnt_free_cache(cache);
	mnt_free_iter(itr);

	return ct ? EXIT_SUCCESS : 2;
}