]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/findmnt.c
build-sys: provide alternatives for err, errx, warn and warnx
[thirdparty/util-linux.git] / misc-utils / findmnt.c
1 /*
2 * findmnt(8)
3 *
4 * Copyright (C) 2010,2011 Red Hat, Inc. All rights reserved.
5 * Written by Karel Zak <kzak@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it would be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <string.h>
27 #include <termios.h>
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
30 #endif
31 #include <assert.h>
32
33 #define USE_UNSTABLE_LIBMOUNT_API
34 #include <libmount.h>
35
36 #include "pathnames.h"
37 #include "nls.h"
38 #include "c.h"
39 #include "tt.h"
40
41 /* flags */
42 enum {
43 FL_EVALUATE = (1 << 1),
44 FL_CANONICALIZE = (1 << 2),
45 FL_FIRSTONLY = (1 << 3),
46 FL_INVERT = (1 << 4),
47 FL_NOSWAPMATCH = (1 << 6),
48 FL_NOFSROOT = (1 << 7),
49 FL_SUBMOUNTS = (1 << 8),
50 };
51
52 /* column IDs */
53 enum {
54 COL_SOURCE,
55 COL_TARGET,
56 COL_FSTYPE,
57 COL_OPTIONS,
58 COL_VFS_OPTIONS,
59 COL_FS_OPTIONS,
60 COL_LABEL,
61 COL_UUID,
62 COL_MAJMIN,
63
64 __NCOLUMNS
65 };
66
67 /* column names */
68 struct colinfo {
69 const char *name; /* header */
70 double whint; /* width hint (N < 1 is in percent of termwidth) */
71 int truncate; /* boolean */
72 const char *match; /* pattern for match_func() */
73 };
74
75 /* columns descriptions */
76 struct colinfo infos[__NCOLUMNS] = {
77 [COL_SOURCE] = { "SOURCE", 0.25, FALSE },
78 [COL_TARGET] = { "TARGET", 0.30, FALSE },
79 [COL_FSTYPE] = { "FSTYPE", 0.10, TRUE },
80 [COL_OPTIONS] = { "OPTIONS", 0.10, TRUE },
81 [COL_VFS_OPTIONS] = { "VFS-OPTIONS", 0.20, TRUE },
82 [COL_FS_OPTIONS] = { "FS-OPTIONS", 0.10, TRUE },
83 [COL_LABEL] = { "LABEL", 0.10, FALSE },
84 [COL_UUID] = { "UUID", 36, FALSE },
85 [COL_MAJMIN] = { "MAJ:MIN", 6, FALSE },
86 };
87
88 /* global flags */
89 int flags;
90 int tt_flags = 0;
91
92 /* array with IDs of enabled columns */
93 int columns[__NCOLUMNS];
94 int ncolumns;
95
96 /* libmount cache */
97 struct libmnt_cache *cache;
98
99 static int get_column_id(int num)
100 {
101 assert(num < ncolumns);
102 assert(columns[num] < __NCOLUMNS);
103 return columns[num];
104 }
105
106 static struct colinfo *get_column_info(int num)
107 {
108 return &infos[ get_column_id(num) ];
109 }
110
111 static const char *column_id_to_name(int id)
112 {
113 assert(id < __NCOLUMNS);
114 return infos[id].name;
115 }
116
117 static const char *get_column_name(int num)
118 {
119 return get_column_info(num)->name;
120 }
121
122 static float get_column_whint(int num)
123 {
124 return get_column_info(num)->whint;
125 }
126
127 static int get_column_truncate(int num)
128 {
129 return get_column_info(num)->truncate;
130 }
131
132 static const char *get_match(int id)
133 {
134 assert(id < __NCOLUMNS);
135 return infos[id].match;
136 }
137
138 static void set_match(int id, const char *match)
139 {
140 assert(id < __NCOLUMNS);
141 infos[id].match = match;
142 }
143
144 /*
145 * "findmnt" without any filter
146 */
147 static int is_listall_mode(void)
148 {
149 return (!get_match(COL_SOURCE) &&
150 !get_match(COL_TARGET) &&
151 !get_match(COL_FSTYPE) &&
152 !get_match(COL_OPTIONS));
153 }
154
155 /*
156 * findmnt --first-only <devname|TAG=|mountpoint>
157 *
158 * ... it works like "mount <devname|TAG=|mountpoint>"
159 */
160 static int is_mount_compatible_mode(void)
161 {
162 if (!get_match(COL_SOURCE))
163 return 0; /* <devname|TAG=|mountpoint> is required */
164 if (get_match(COL_FSTYPE) || get_match(COL_OPTIONS))
165 return 0; /* cannot be restricted by -t or -O */
166 if (!(flags & FL_FIRSTONLY))
167 return 0; /* we have to return the first entry only */
168
169 return 1; /* ok */
170 }
171
172 static void set_all_columns_truncate(int set)
173 {
174 int i;
175
176 for (i = 0; i < __NCOLUMNS; i++)
177 infos[i].truncate = set;
178 }
179
180 /*
181 * converts @name to column ID
182 */
183 static int column_name_to_id(const char *name, size_t namesz)
184 {
185 int i;
186
187 for (i = 0; i < __NCOLUMNS; i++) {
188 const char *cn = column_id_to_name(i);
189
190 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
191 return i;
192 }
193 warnx(_("unknown column: %s"), name);
194 return -1;
195 }
196
197 /* Returns LABEL or UUID */
198 static const char *get_tag(struct libmnt_fs *fs, const char *tagname)
199 {
200 const char *t, *v, *res;
201
202 if (!mnt_fs_get_tag(fs, &t, &v) && !strcmp(t, tagname))
203 res = v;
204 else {
205 res = mnt_fs_get_source(fs);
206 if (res)
207 res = mnt_resolve_spec(res, cache);
208 if (res)
209 res = mnt_cache_find_tag_value(cache, res, tagname);
210 }
211
212 return res;
213 }
214
215 /* reads FS data from libmount
216 * TODO: add function that will deallocate data allocated by get_data()
217 */
218 static const char *get_data(struct libmnt_fs *fs, int num)
219 {
220 const char *str = NULL;
221
222 switch(get_column_id(num)) {
223 case COL_SOURCE:
224 {
225 const char *root = mnt_fs_get_root(fs);
226
227 str = mnt_fs_get_srcpath(fs);
228
229 if (str && (flags & FL_CANONICALIZE))
230 str = mnt_resolve_path(str, cache);
231 if (!str) {
232 str = mnt_fs_get_source(fs);
233
234 if (str && (flags & FL_EVALUATE))
235 str = mnt_resolve_spec(str, cache);
236 }
237 if (root && str && !(flags & FL_NOFSROOT) && strcmp(root, "/")) {
238 char *tmp;
239
240 if (asprintf(&tmp, "%s[%s]", str, root) > 0)
241 str = tmp;
242 }
243 break;
244 }
245 case COL_TARGET:
246 str = mnt_fs_get_target(fs);
247 break;
248 case COL_FSTYPE:
249 str = mnt_fs_get_fstype(fs);
250 break;
251 case COL_OPTIONS:
252 str = mnt_fs_strdup_options(fs);
253 break;
254 case COL_VFS_OPTIONS:
255 str = mnt_fs_get_vfs_options(fs);
256 break;
257 case COL_FS_OPTIONS:
258 str = mnt_fs_get_fs_options(fs);
259 break;
260 case COL_UUID:
261 str = get_tag(fs, "UUID");
262 break;
263 case COL_LABEL:
264 str = get_tag(fs, "LABEL");
265 break;
266 case COL_MAJMIN:
267 {
268 dev_t devno = mnt_fs_get_devno(fs);
269 if (devno) {
270 char *tmp;
271 int rc = 0;
272 if (tt_flags & TT_FL_RAW)
273 rc = asprintf(&tmp, "%u:%u",
274 major(devno), minor(devno));
275 else
276 rc = asprintf(&tmp, "%3u:%-3u",
277 major(devno), minor(devno));
278 if (rc)
279 str = tmp;
280 }
281 }
282 default:
283 break;
284 }
285 return str;
286 }
287
288 /* adds one line to the output @tab */
289 static struct tt_line *add_line(struct tt *tt, struct libmnt_fs *fs,
290 struct tt_line *parent)
291 {
292 int i;
293 struct tt_line *line = tt_add_line(tt, parent);
294
295 if (!line) {
296 warn(_("failed to add line to output"));
297 return NULL;
298 }
299 for (i = 0; i < ncolumns; i++)
300 tt_line_set_data(line, i, get_data(fs, i));
301
302 tt_line_set_userdata(line, fs);
303 return line;
304 }
305
306 static int has_line(struct tt *tt, struct libmnt_fs *fs)
307 {
308 struct list_head *p;
309
310 list_for_each(p, &tt->tb_lines) {
311 struct tt_line *ln = list_entry(p, struct tt_line, ln_lines);
312 if ((struct libmnt_fs *) ln->userdata == fs)
313 return 1;
314 }
315 return 0;
316 }
317
318 /* reads filesystems from @tb (libmount) and fillin @tt (output table) */
319 static int create_treenode(struct tt *tt, struct libmnt_table *tb,
320 struct libmnt_fs *fs, struct tt_line *parent_line)
321 {
322 struct libmnt_fs *chld = NULL;
323 struct libmnt_iter *itr = NULL;
324 struct tt_line *line;
325 int rc = -1;
326
327 if (!fs) {
328 /* first call, get root FS */
329 if (mnt_table_get_root_fs(tb, &fs))
330 goto leave;
331 parent_line = NULL;
332
333 } else if ((flags & FL_SUBMOUNTS) && has_line(tt, fs))
334 return 0;
335
336 itr = mnt_new_iter(MNT_ITER_FORWARD);
337 if (!itr)
338 goto leave;
339
340 line = add_line(tt, fs, parent_line);
341 if (!line)
342 goto leave;
343
344 /*
345 * add all children to the output table
346 */
347 while(mnt_table_next_child_fs(tb, itr, fs, &chld) == 0) {
348 if (create_treenode(tt, tb, chld, line))
349 goto leave;
350 }
351 rc = 0;
352 leave:
353 mnt_free_iter(itr);
354 return rc;
355 }
356
357 /* error callback */
358 static int parser_errcb(struct libmnt_table *tb, const char *filename, int line)
359 {
360 warn(_("%s: parse error at line %d"), filename, line);
361 return 0;
362 }
363
364 /* calls libmount fstab/mtab/mountinfo parser */
365 static struct libmnt_table *parse_tabfile(const char *path)
366 {
367 int rc;
368 struct libmnt_table *tb = mnt_new_table();
369
370 if (!tb) {
371 warn(_("failed to initialize libmount tab"));
372 return NULL;
373 }
374
375 mnt_table_set_parser_errcb(tb, parser_errcb);
376
377 if (!strcmp(path, _PATH_MNTTAB))
378 rc = mnt_table_parse_fstab(tb, NULL);
379 else if (!strcmp(path, _PATH_MOUNTED))
380 rc = mnt_table_parse_mtab(tb, NULL);
381 else
382 rc = mnt_table_parse_file(tb, path);
383
384 if (rc) {
385 mnt_free_table(tb);
386 warn(_("can't read: %s"), path);
387 return NULL;
388 }
389 return tb;
390 }
391
392 /* filter function for libmount (mnt_table_find_next_fs()) */
393 static int match_func(struct libmnt_fs *fs, void *data)
394 {
395 int rc = flags & FL_INVERT ? 1 : 0;
396 const char *m;
397
398 m = get_match(COL_TARGET);
399 if (m && !mnt_fs_match_target(fs, m, cache))
400 return rc;
401
402 m = get_match(COL_SOURCE);
403 if (m && !mnt_fs_match_source(fs, m, cache))
404 return rc;
405
406 m = get_match(COL_FSTYPE);
407 if (m && !mnt_fs_match_fstype(fs, m))
408 return rc;
409
410 m = get_match(COL_OPTIONS);
411 if (m && !mnt_fs_match_options(fs, m))
412 return rc;
413
414 return !rc;
415 }
416
417 /* iterate over filesystems in @tb */
418 static struct libmnt_fs *get_next_fs(struct libmnt_table *tb,
419 struct libmnt_iter *itr)
420 {
421 struct libmnt_fs *fs = NULL;
422
423 if (is_listall_mode()) {
424 /*
425 * Print whole file
426 */
427 mnt_table_next_fs(tb, itr, &fs);
428
429 } else if (is_mount_compatible_mode()) {
430 /*
431 * Look up for FS in the same way how mount(8) searchs in fstab
432 *
433 * findmnt -f <spec>
434 */
435 fs = mnt_table_find_source(tb, get_match(COL_SOURCE),
436 mnt_iter_get_direction(itr));
437
438 if (!fs && !(flags & FL_NOSWAPMATCH))
439 fs = mnt_table_find_target(tb, get_match(COL_SOURCE),
440 mnt_iter_get_direction(itr));
441 } else {
442 /*
443 * Look up for all matching entries
444 *
445 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
446 * findmnt [-l] <spec> [-O <options>] [-t <types>]
447 */
448 again:
449 mnt_table_find_next_fs(tb, itr, match_func, NULL, &fs);
450
451 if (!fs &&
452 !(flags & FL_NOSWAPMATCH) &&
453 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
454
455 /* swap 'spec' and target. */
456 set_match(COL_TARGET, get_match(COL_SOURCE));
457 set_match(COL_SOURCE, NULL);
458 mnt_reset_iter(itr, -1);
459
460 goto again;
461 }
462 }
463
464 return fs;
465 }
466
467 static int add_matching_lines(struct libmnt_table *tb,
468 struct tt *tt, int direction)
469 {
470 struct libmnt_iter *itr = NULL;
471 struct libmnt_fs *fs;
472 int nlines = 0, rc = -1;
473
474 itr = mnt_new_iter(direction);
475 if (!itr) {
476 warn(_("failed to initialize libmount iterator"));
477 goto done;
478 }
479
480 while((fs = get_next_fs(tb, itr))) {
481 if ((tt_flags & TT_FL_TREE) || (flags & FL_SUBMOUNTS))
482 rc = create_treenode(tt, tb, fs, NULL);
483 else
484 rc = !add_line(tt, fs, NULL);
485 if (rc)
486 goto done;
487 nlines++;
488 if (flags & FL_FIRSTONLY)
489 break;
490 flags |= FL_NOSWAPMATCH;
491 }
492
493 if (nlines)
494 rc = 0;
495 done:
496 mnt_free_iter(itr);
497 return rc;
498 }
499
500 static void __attribute__((__noreturn__)) usage(FILE *out)
501 {
502 int i;
503
504 fprintf(out, _(
505 "\nUsage:\n"
506 " %1$s [options]\n"
507 " %1$s [options] <device> | <mountpoint>\n"
508 " %1$s [options] <device> <mountpoint>\n"
509 " %1$s [options] [--source <device>] [--target <mountpoint>]\n"),
510 program_invocation_short_name);
511
512 fprintf(out, _(
513 "\nOptions:\n"
514 " -s, --fstab search in static table of filesystems\n"
515 " -m, --mtab search in table of mounted filesystems\n"
516 " -k, --kernel search in kernel table of mounted \n"
517 " filesystems (default)\n\n"
518
519 " -c, --canonicalize canonicalize printed paths\n"
520 " -d, --direction <word> search direction - 'forward' or 'backward'\n"
521 " -e, --evaluate print all TAGs (LABEL/UUID) evaluated\n"
522 " -f, --first-only print the first found filesystem only\n"
523 " -h, --help print this help\n"
524 " -i, --invert invert sense of matching\n"
525 " -l, --list use list format output\n"
526 " -n, --noheadings don't print headings\n"
527 " -u, --notruncate don't truncate text in columns\n"
528 " -O, --options <list> limit the set of filesystems by mount options\n"
529 " -o, --output <list> output columns\n"
530 " -r, --raw use raw format output\n"
531 " -a, --ascii use ascii chars for tree formatting\n"
532 " -t, --types <list> limit the set of filesystem by FS types\n"
533 " -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"
534 " -R, --submounts print all submount for the matching filesystems\n"
535 " -S, --source <string> device, LABEL= or UUID=device\n"
536 " -T, --target <string> mountpoint\n\n"));
537
538
539 fprintf(out, _("\nAvailable columns:\n"));
540
541 for (i = 0; i < __NCOLUMNS; i++) {
542
543 fprintf(out, " %-12s", infos[i].name);
544 if (i && !((i+1) % 3))
545 fputc('\n', out);
546 }
547 fputc('\n', out);
548
549 fprintf(out, _("\nFor more information see findmnt(1).\n"));
550
551 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
552 }
553
554 static void __attribute__((__noreturn__))
555 errx_mutually_exclusive(const char *opts)
556 {
557 errx(EXIT_FAILURE, "%s %s", opts, _("options are mutually exclusive"));
558 }
559
560 int main(int argc, char *argv[])
561 {
562 /* libmount */
563 struct libmnt_table *tb = NULL;
564 char *tabfile = NULL;
565 int direction = MNT_ITER_FORWARD;
566 int i, c, rc = -1;
567
568 /* table.h */
569 struct tt *tt = NULL;
570
571 struct option longopts[] = {
572 { "ascii", 0, 0, 'a' },
573 { "canonicalize", 0, 0, 'c' },
574 { "direction", 1, 0, 'd' },
575 { "evaluate", 0, 0, 'e' },
576 { "first-only", 0, 0, 'f' },
577 { "fstab", 0, 0, 's' },
578 { "help", 0, 0, 'h' },
579 { "invert", 0, 0, 'i' },
580 { "kernel", 0, 0, 'k' },
581 { "list", 0, 0, 'l' },
582 { "mtab", 0, 0, 'm' },
583 { "noheadings", 0, 0, 'n' },
584 { "notruncate", 0, 0, 'u' },
585 { "options", 1, 0, 'O' },
586 { "output", 1, 0, 'o' },
587 { "raw", 0, 0, 'r' },
588 { "types", 1, 0, 't' },
589 { "fsroot", 0, 0, 'v' },
590 { "submounts", 0, 0, 'R' },
591 { "source", 1, 0, 'S' },
592 { "target", 1, 0, 'T' },
593
594 { NULL, 0, 0, 0 }
595 };
596
597 assert(ARRAY_SIZE(columns) == __NCOLUMNS);
598
599 setlocale(LC_ALL, "");
600 bindtextdomain(PACKAGE, LOCALEDIR);
601 textdomain(PACKAGE);
602
603 /* default enabled columns */
604 columns[ncolumns++] = COL_TARGET;
605 columns[ncolumns++] = COL_SOURCE;
606 columns[ncolumns++] = COL_FSTYPE;
607 columns[ncolumns++] = COL_OPTIONS;
608
609 /* default output format */
610 tt_flags |= TT_FL_TREE;
611
612 while ((c = getopt_long(argc, argv,
613 "acd:ehifo:O:klmnrst:uvRS:T:", longopts, NULL)) != -1) {
614 switch(c) {
615 case 'a':
616 tt_flags |= TT_FL_ASCII;
617 break;
618 case 'c':
619 flags |= FL_CANONICALIZE;
620 break;
621 case 'd':
622 if (!strcmp(optarg, "forward"))
623 direction = MNT_ITER_FORWARD;
624 else if (!strcmp(optarg, "backward"))
625 direction = MNT_ITER_BACKWARD;
626 else
627 errx(EXIT_FAILURE,
628 _("unknown direction '%s'"), optarg);
629 break;
630 case 'e':
631 flags |= FL_EVALUATE;
632 break;
633 case 'h':
634 usage(stdout);
635 break;
636 case 'i':
637 flags |= FL_INVERT;
638 break;
639 case 'f':
640 flags |= FL_FIRSTONLY;
641 break;
642 case 'u':
643 set_all_columns_truncate(FALSE);
644 break;
645 case 'o':
646 if (tt_parse_columns_list(optarg, columns, &ncolumns,
647 column_name_to_id))
648 exit(EXIT_FAILURE);
649 break;
650 case 'O':
651 set_match(COL_OPTIONS, optarg);
652 break;
653 case 'm': /* mtab */
654 if (tabfile)
655 errx_mutually_exclusive("--{fstab,mtab,kernel}");
656 tabfile = _PATH_MOUNTED;
657 tt_flags &= ~TT_FL_TREE;
658 break;
659 case 's': /* fstab */
660 if (tabfile)
661 errx_mutually_exclusive("--{fstab,mtab,kernel}");
662 tabfile = _PATH_MNTTAB;
663 tt_flags &= ~TT_FL_TREE;
664 break;
665 case 'k': /* kernel (mountinfo) */
666 if (tabfile)
667 errx_mutually_exclusive("--{fstab,mtab,kernel}");
668 tabfile = _PATH_PROC_MOUNTINFO;
669 break;
670 case 't':
671 set_match(COL_FSTYPE, optarg);
672 break;
673 case 'r':
674 tt_flags &= ~TT_FL_TREE; /* disable the default */
675 tt_flags |= TT_FL_RAW; /* enable raw */
676 break;
677 case 'l':
678 if (tt_flags & TT_FL_RAW)
679 errx_mutually_exclusive("--{raw,list}");
680
681 tt_flags &= ~TT_FL_TREE; /* disable the default */
682 break;
683 case 'n':
684 tt_flags |= TT_FL_NOHEADINGS;
685 break;
686 case 'v':
687 flags |= FL_NOFSROOT;
688 break;
689 case 'R':
690 flags |= FL_SUBMOUNTS;
691 break;
692 case 'S':
693 set_match(COL_SOURCE, optarg);
694 flags |= FL_NOSWAPMATCH;
695 break;
696 case 'T':
697 set_match(COL_TARGET, optarg);
698 flags |= FL_NOSWAPMATCH;
699 break;
700 default:
701 usage(stderr);
702 break;
703 }
704 }
705
706 if (!tabfile) {
707 tabfile = _PATH_PROC_MOUNTINFO;
708
709 if (access(tabfile, R_OK)) { /* old kernel? */
710 tabfile = _PATH_PROC_MOUNTS;
711 tt_flags &= ~TT_FL_TREE;
712 }
713 }
714
715 if (optind < argc && (get_match(COL_SOURCE) || get_match(COL_TARGET)))
716 errx(EXIT_FAILURE, _(
717 "options --target and --source can't be used together "
718 "with command line element that is not an option"));
719
720 if (optind < argc)
721 set_match(COL_SOURCE, argv[optind++]); /* dev/tag/mountpoint */
722 if (optind < argc)
723 set_match(COL_TARGET, argv[optind++]); /* mountpoint */
724
725 if ((flags & FL_SUBMOUNTS) && is_listall_mode())
726 /* don't care about submounts if list all mounts */
727 flags &= ~FL_SUBMOUNTS;
728
729 if (!(flags & FL_SUBMOUNTS) &&
730 (!is_listall_mode() || (flags & FL_FIRSTONLY)))
731 tt_flags &= ~TT_FL_TREE;
732
733 if (!(flags & FL_NOSWAPMATCH) &&
734 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
735 /*
736 * Check if we can swap source and target, it's
737 * not possible if the source is LABEL=/UUID=
738 */
739 const char *x = get_match(COL_SOURCE);
740
741 if (!strncmp(x, "LABEL=", 6) || !strncmp(x, "UUID=", 5))
742 flags |= FL_NOSWAPMATCH;
743 }
744
745 /*
746 * initialize libmount
747 */
748 mnt_init_debug(0);
749
750 tb = parse_tabfile(tabfile);
751 if (!tb)
752 goto leave;
753
754 cache = mnt_new_cache();
755 if (!cache) {
756 warn(_("failed to initialize libmount cache"));
757 goto leave;
758 }
759 mnt_table_set_cache(tb, cache);
760
761 /*
762 * initialize output formatting (tt.h)
763 */
764 tt = tt_new_table(tt_flags);
765 if (!tt) {
766 warn(_("failed to initialize output table"));
767 goto leave;
768 }
769
770 for (i = 0; i < ncolumns; i++) {
771 int fl = get_column_truncate(i) ? TT_FL_TRUNC : 0;
772
773 if (get_column_id(i) == COL_TARGET && (tt_flags & TT_FL_TREE))
774 fl |= TT_FL_TREE;
775
776 if (!tt_define_column(tt, get_column_name(i),
777 get_column_whint(i), fl)) {
778 warn(_("failed to initialize output column"));
779 goto leave;
780 }
781 }
782
783 /*
784 * Fill in data to the output table
785 */
786 if ((tt_flags & TT_FL_TREE) && is_listall_mode())
787 /* whole tree */
788 rc = create_treenode(tt, tb, NULL, NULL);
789 else
790 /* whole lits of sub-tree */
791 rc = add_matching_lines(tb, tt, direction);
792
793 /*
794 * Print the output table
795 */
796 if (!rc)
797 tt_print_table(tt);
798 leave:
799 tt_free_table(tt);
800
801 mnt_free_table(tb);
802 mnt_free_cache(cache);
803
804 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
805 }