]>
Commit | Line | Data |
---|---|---|
04fd7a9f KZ |
1 | /* |
2 | * findmnt(8) | |
3 | * | |
85c9ca5a | 4 | * Copyright (C) 2010-2014 Red Hat, Inc. All rights reserved. |
04fd7a9f KZ |
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 | * | |
7cebf0bb SK |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
04fd7a9f | 20 | */ |
04fd7a9f KZ |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <errno.h> | |
04fd7a9f KZ |
24 | #include <unistd.h> |
25 | #include <getopt.h> | |
26 | #include <string.h> | |
9d67679b | 27 | #include <termios.h> |
9d67679b | 28 | #ifdef HAVE_SYS_IOCTL_H |
e346233e | 29 | # include <sys/ioctl.h> |
9d67679b | 30 | #endif |
9d67679b | 31 | #include <assert.h> |
32e5466a | 32 | #include <poll.h> |
a85ef33b DR |
33 | #include <sys/statvfs.h> |
34 | #include <sys/types.h> | |
e346233e KZ |
35 | #ifdef HAVE_LIBUDEV |
36 | # include <libudev.h> | |
37 | #endif | |
2a1f429a | 38 | #include <libmount.h> |
218a3a94 | 39 | #include <libsmartcols.h> |
fdedb45e | 40 | |
04fd7a9f KZ |
41 | #include "pathnames.h" |
42 | #include "nls.h" | |
c05a80ca | 43 | #include "closestream.h" |
9d67679b | 44 | #include "c.h" |
ad38fb9f | 45 | #include "strutils.h" |
6f312c89 | 46 | #include "xalloc.h" |
fb9a0042 | 47 | #include "optutils.h" |
e346233e | 48 | #include "mangle.h" |
04fd7a9f | 49 | |
9d67679b KZ |
50 | /* flags */ |
51 | enum { | |
52 | FL_EVALUATE = (1 << 1), | |
53 | FL_CANONICALIZE = (1 << 2), | |
54 | FL_FIRSTONLY = (1 << 3), | |
55 | FL_INVERT = (1 << 4), | |
9d67679b | 56 | FL_NOSWAPMATCH = (1 << 6), |
b2214e1f | 57 | FL_NOFSROOT = (1 << 7), |
049caefd | 58 | FL_SUBMOUNTS = (1 << 8), |
cd4ed46f KZ |
59 | FL_POLL = (1 << 9), |
60 | FL_DF = (1 << 10), | |
bebdda30 | 61 | FL_ALL = (1 << 11), |
2b6759df | 62 | FL_UNIQ = (1 << 12), |
85c9ca5a | 63 | FL_BYTES = (1 << 13), |
097e7f2f | 64 | FL_NOCACHE = (1 << 14), |
85c9ca5a KZ |
65 | |
66 | /* basic table settings */ | |
67 | FL_ASCII = (1 << 20), | |
68 | FL_RAW = (1 << 21), | |
69 | FL_NOHEADINGS = (1 << 22), | |
70 | FL_EXPORT = (1 << 23), | |
71 | FL_TREE = (1 << 24) | |
9d67679b KZ |
72 | }; |
73 | ||
74 | /* column IDs */ | |
04fd7a9f KZ |
75 | enum { |
76 | COL_SOURCE, | |
77 | COL_TARGET, | |
78 | COL_FSTYPE, | |
79 | COL_OPTIONS, | |
631280e0 KZ |
80 | COL_VFS_OPTIONS, |
81 | COL_FS_OPTIONS, | |
04fd7a9f KZ |
82 | COL_LABEL, |
83 | COL_UUID, | |
090b5e84 KZ |
84 | COL_PARTLABEL, |
85 | COL_PARTUUID, | |
46236388 | 86 | COL_MAJMIN, |
32e5466a KZ |
87 | COL_ACTION, |
88 | COL_OLD_TARGET, | |
89 | COL_OLD_OPTIONS, | |
a85ef33b DR |
90 | COL_SIZE, |
91 | COL_AVAIL, | |
92 | COL_USED, | |
93 | COL_USEPERC, | |
8b8cd87b | 94 | COL_FSROOT, |
7d8f4e0c | 95 | COL_TID, |
9238e0fa | 96 | COL_ID, |
327ea85a | 97 | COL_OPT_FIELDS, |
624f2b47 | 98 | COL_PROPAGATION, |
426f0cea KZ |
99 | COL_FREQ, |
100 | COL_PASSNO, | |
04fd7a9f | 101 | |
af0dc7d3 | 102 | FINDMNT_NCOLUMNS |
04fd7a9f KZ |
103 | }; |
104 | ||
8c5eba3e KZ |
105 | enum { |
106 | TABTYPE_FSTAB = 1, | |
107 | TABTYPE_MTAB, | |
108 | TABTYPE_KERNEL | |
109 | }; | |
110 | ||
9d67679b KZ |
111 | /* column names */ |
112 | struct colinfo { | |
113 | const char *name; /* header */ | |
114 | double whint; /* width hint (N < 1 is in percent of termwidth) */ | |
218a3a94 | 115 | int flags; /* libsmartcols flags */ |
b152e359 | 116 | const char *help; /* column description */ |
9d67679b | 117 | const char *match; /* pattern for match_func() */ |
cd492186 | 118 | void *match_data; /* match specific data */ |
04fd7a9f KZ |
119 | }; |
120 | ||
af0dc7d3 KZ |
121 | /* columns descriptions (don't use const, this is writable) */ |
122 | static struct colinfo infos[FINDMNT_NCOLUMNS] = { | |
218a3a94 OO |
123 | [COL_SOURCE] = { "SOURCE", 0.25, SCOLS_FL_NOEXTREMES, N_("source device") }, |
124 | [COL_TARGET] = { "TARGET", 0.30, SCOLS_FL_TREE| SCOLS_FL_NOEXTREMES, N_("mountpoint") }, | |
125 | [COL_FSTYPE] = { "FSTYPE", 0.10, SCOLS_FL_TRUNC, N_("filesystem type") }, | |
126 | [COL_OPTIONS] = { "OPTIONS", 0.10, SCOLS_FL_TRUNC, N_("all mount options") }, | |
127 | [COL_VFS_OPTIONS] = { "VFS-OPTIONS", 0.20, SCOLS_FL_TRUNC, N_("VFS specific mount options") }, | |
128 | [COL_FS_OPTIONS] = { "FS-OPTIONS", 0.10, SCOLS_FL_TRUNC, N_("FS specific mount options") }, | |
b152e359 KZ |
129 | [COL_LABEL] = { "LABEL", 0.10, 0, N_("filesystem label") }, |
130 | [COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") }, | |
090b5e84 KZ |
131 | [COL_PARTLABEL] = { "PARTLABEL", 0.10, 0, N_("partition label") }, |
132 | [COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") }, | |
b152e359 | 133 | [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number") }, |
218a3a94 OO |
134 | [COL_ACTION] = { "ACTION", 10, SCOLS_FL_STRICTWIDTH, N_("action detected by --poll") }, |
135 | [COL_OLD_OPTIONS] = { "OLD-OPTIONS", 0.10, SCOLS_FL_TRUNC, N_("old mount options saved by --poll") }, | |
b152e359 | 136 | [COL_OLD_TARGET] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") }, |
218a3a94 OO |
137 | [COL_SIZE] = { "SIZE", 5, SCOLS_FL_RIGHT, N_("filesystem size") }, |
138 | [COL_AVAIL] = { "AVAIL", 5, SCOLS_FL_RIGHT, N_("filesystem size available") }, | |
139 | [COL_USED] = { "USED", 5, SCOLS_FL_RIGHT, N_("filesystem size used") }, | |
140 | [COL_USEPERC] = { "USE%", 3, SCOLS_FL_RIGHT, N_("filesystem use percentage") }, | |
141 | [COL_FSROOT] = { "FSROOT", 0.25, SCOLS_FL_NOEXTREMES, N_("filesystem root") }, | |
142 | [COL_TID] = { "TID", 4, SCOLS_FL_RIGHT, N_("task ID") }, | |
143 | [COL_ID] = { "ID", 2, SCOLS_FL_RIGHT, N_("mount ID") }, | |
144 | [COL_OPT_FIELDS] = { "OPT-FIELDS", 0.10, SCOLS_FL_TRUNC, N_("optional mount fields") }, | |
426f0cea | 145 | [COL_PROPAGATION] = { "PROPAGATION", 0.10, 0, N_("VFS propagation flags") }, |
218a3a94 OO |
146 | [COL_FREQ] = { "FREQ", 1, SCOLS_FL_RIGHT, N_("dump(8) period in days [fstab only]") }, |
147 | [COL_PASSNO] = { "PASSNO", 1, SCOLS_FL_RIGHT, N_("pass number on parallel fsck(8) [fstab only]") } | |
04fd7a9f KZ |
148 | }; |
149 | ||
9d67679b | 150 | /* global flags */ |
af0dc7d3 | 151 | static int flags; |
9d67679b | 152 | |
fdedb45e | 153 | /* array with IDs of enabled columns */ |
af0dc7d3 KZ |
154 | static int columns[FINDMNT_NCOLUMNS]; |
155 | static int ncolumns; | |
9d67679b | 156 | |
582a5006 | 157 | /* poll actions (parsed --poll=<list> */ |
af0dc7d3 KZ |
158 | #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */ |
159 | static int actions[FINDMNT_NACTIONS]; | |
160 | static int nactions; | |
582a5006 | 161 | |
9d67679b | 162 | /* libmount cache */ |
af0dc7d3 | 163 | static struct libmnt_cache *cache; |
04fd7a9f | 164 | |
e346233e KZ |
165 | #ifdef HAVE_LIBUDEV |
166 | struct udev *udev; | |
167 | #endif | |
168 | ||
31f67453 KZ |
169 | static int match_func(struct libmnt_fs *fs, void *data __attribute__ ((__unused__))); |
170 | ||
171 | ||
00b4bcdf | 172 | static int get_column_id(int num) |
9d67679b | 173 | { |
9d67679b | 174 | assert(num < ncolumns); |
af0dc7d3 | 175 | assert(columns[num] < FINDMNT_NCOLUMNS); |
fdedb45e | 176 | return columns[num]; |
9d67679b KZ |
177 | } |
178 | ||
00b4bcdf | 179 | static struct colinfo *get_column_info(int num) |
9d67679b KZ |
180 | { |
181 | return &infos[ get_column_id(num) ]; | |
182 | } | |
183 | ||
00b4bcdf | 184 | static const char *column_id_to_name(int id) |
9d67679b | 185 | { |
af0dc7d3 | 186 | assert(id < FINDMNT_NCOLUMNS); |
9d67679b KZ |
187 | return infos[id].name; |
188 | } | |
189 | ||
00b4bcdf | 190 | static const char *get_column_name(int num) |
9d67679b | 191 | { |
fdedb45e | 192 | return get_column_info(num)->name; |
9d67679b KZ |
193 | } |
194 | ||
00b4bcdf | 195 | static float get_column_whint(int num) |
9d67679b | 196 | { |
fdedb45e | 197 | return get_column_info(num)->whint; |
9d67679b KZ |
198 | } |
199 | ||
32e5466a | 200 | static int get_column_flags(int num) |
9d67679b | 201 | { |
32e5466a | 202 | return get_column_info(num)->flags; |
9d67679b KZ |
203 | } |
204 | ||
00b4bcdf | 205 | static const char *get_match(int id) |
9d67679b | 206 | { |
af0dc7d3 | 207 | assert(id < FINDMNT_NCOLUMNS); |
9d67679b KZ |
208 | return infos[id].match; |
209 | } | |
210 | ||
cd492186 KZ |
211 | static void *get_match_data(int id) |
212 | { | |
213 | assert(id < FINDMNT_NCOLUMNS); | |
214 | return infos[id].match_data; | |
215 | } | |
216 | ||
00b4bcdf | 217 | static void set_match(int id, const char *match) |
9d67679b | 218 | { |
af0dc7d3 | 219 | assert(id < FINDMNT_NCOLUMNS); |
9d67679b KZ |
220 | infos[id].match = match; |
221 | } | |
222 | ||
cd492186 KZ |
223 | static void set_match_data(int id, void *data) |
224 | { | |
225 | assert(id < FINDMNT_NCOLUMNS); | |
226 | infos[id].match_data = data; | |
227 | } | |
228 | ||
4bfd4bff KZ |
229 | /* |
230 | * source match means COL_SOURCE *or* COL_MAJMIN, depends on | |
231 | * data format. | |
232 | */ | |
cd492186 KZ |
233 | static void set_source_match(const char *data) |
234 | { | |
235 | int maj, min; | |
236 | ||
237 | if (sscanf(data, "%d:%d", &maj, &min) == 2) { | |
238 | dev_t *devno = xmalloc(sizeof(dev_t)); | |
239 | ||
240 | *devno = makedev(maj, min); | |
241 | set_match(COL_MAJMIN, data); | |
242 | set_match_data(COL_MAJMIN, (void *) devno); | |
4bfd4bff | 243 | flags |= FL_NOSWAPMATCH; |
cd492186 KZ |
244 | } else |
245 | set_match(COL_SOURCE, data); | |
246 | } | |
247 | ||
4bfd4bff KZ |
248 | static void enable_extra_target_match(void) |
249 | { | |
097e7f2f | 250 | const char *cn = NULL, *mnt = NULL; |
4bfd4bff KZ |
251 | |
252 | /* | |
253 | * Check if match pattern is mountpoint, if not use the | |
254 | * real mountpoint. | |
255 | */ | |
097e7f2f KZ |
256 | if (flags & FL_NOCACHE) |
257 | cn = get_match(COL_TARGET); | |
258 | else { | |
259 | cn = mnt_resolve_path(get_match(COL_TARGET), cache); | |
260 | if (!cn) | |
261 | return; | |
262 | } | |
4bfd4bff KZ |
263 | |
264 | mnt = mnt_get_mountpoint(cn); | |
265 | if (!mnt || strcmp(mnt, cn) == 0) | |
266 | return; | |
267 | ||
268 | /* replace the current setting with the real mountpoint */ | |
269 | set_match(COL_TARGET, mnt); | |
270 | } | |
271 | ||
cd492186 | 272 | |
32e5466a KZ |
273 | static int is_tabdiff_column(int id) |
274 | { | |
af0dc7d3 | 275 | assert(id < FINDMNT_NCOLUMNS); |
32e5466a KZ |
276 | |
277 | switch(id) { | |
278 | case COL_ACTION: | |
279 | case COL_OLD_TARGET: | |
280 | case COL_OLD_OPTIONS: | |
281 | return 1; | |
282 | default: | |
283 | break; | |
284 | } | |
285 | return 0; | |
286 | } | |
287 | ||
9d67679b KZ |
288 | /* |
289 | * "findmnt" without any filter | |
290 | */ | |
00b4bcdf | 291 | static int is_listall_mode(void) |
9d67679b | 292 | { |
cd4ed46f KZ |
293 | if ((flags & FL_DF) && !(flags & FL_ALL)) |
294 | return 0; | |
295 | ||
9d67679b KZ |
296 | return (!get_match(COL_SOURCE) && |
297 | !get_match(COL_TARGET) && | |
298 | !get_match(COL_FSTYPE) && | |
cd492186 KZ |
299 | !get_match(COL_OPTIONS) && |
300 | !get_match(COL_MAJMIN)); | |
9d67679b KZ |
301 | } |
302 | ||
582a5006 KZ |
303 | /* |
304 | * Returns 1 if the @act is in the --poll=<list> | |
305 | */ | |
306 | static int has_poll_action(int act) | |
307 | { | |
308 | int i; | |
309 | ||
310 | if (!nactions) | |
311 | return 1; /* all actions enabled */ | |
312 | for (i = 0; i < nactions; i++) | |
313 | if (actions[i] == act) | |
314 | return 1; | |
315 | return 0; | |
316 | } | |
317 | ||
318 | static int poll_action_name_to_id(const char *name, size_t namesz) | |
319 | { | |
320 | int id = -1; | |
321 | ||
322 | if (strncasecmp(name, "move", namesz) == 0 && namesz == 4) | |
323 | id = MNT_TABDIFF_MOVE; | |
324 | else if (strncasecmp(name, "mount", namesz) == 0 && namesz == 5) | |
325 | id = MNT_TABDIFF_MOUNT; | |
326 | else if (strncasecmp(name, "umount", namesz) == 0 && namesz == 6) | |
327 | id = MNT_TABDIFF_UMOUNT; | |
328 | else if (strncasecmp(name, "remount", namesz) == 0 && namesz == 7) | |
329 | id = MNT_TABDIFF_REMOUNT; | |
330 | else | |
331 | warnx(_("unknown action: %s"), name); | |
332 | ||
333 | return id; | |
334 | } | |
335 | ||
9d67679b KZ |
336 | /* |
337 | * findmnt --first-only <devname|TAG=|mountpoint> | |
338 | * | |
339 | * ... it works like "mount <devname|TAG=|mountpoint>" | |
340 | */ | |
00b4bcdf | 341 | static int is_mount_compatible_mode(void) |
9d67679b KZ |
342 | { |
343 | if (!get_match(COL_SOURCE)) | |
344 | return 0; /* <devname|TAG=|mountpoint> is required */ | |
345 | if (get_match(COL_FSTYPE) || get_match(COL_OPTIONS)) | |
346 | return 0; /* cannot be restricted by -t or -O */ | |
347 | if (!(flags & FL_FIRSTONLY)) | |
348 | return 0; /* we have to return the first entry only */ | |
349 | ||
350 | return 1; /* ok */ | |
351 | } | |
352 | ||
32e5466a | 353 | static void disable_columns_truncate(void) |
9d67679b KZ |
354 | { |
355 | int i; | |
356 | ||
af0dc7d3 | 357 | for (i = 0; i < FINDMNT_NCOLUMNS; i++) |
218a3a94 | 358 | infos[i].flags &= ~SCOLS_FL_TRUNC; |
9d67679b KZ |
359 | } |
360 | ||
04fd7a9f KZ |
361 | /* |
362 | * converts @name to column ID | |
363 | */ | |
9d67679b | 364 | static int column_name_to_id(const char *name, size_t namesz) |
04fd7a9f KZ |
365 | { |
366 | int i; | |
367 | ||
af0dc7d3 | 368 | for (i = 0; i < FINDMNT_NCOLUMNS; i++) { |
9d67679b | 369 | const char *cn = column_id_to_name(i); |
04fd7a9f | 370 | |
9d67679b | 371 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) |
04fd7a9f KZ |
372 | return i; |
373 | } | |
fdedb45e | 374 | warnx(_("unknown column: %s"), name); |
04fd7a9f KZ |
375 | return -1; |
376 | } | |
377 | ||
e346233e KZ |
378 | |
379 | #ifdef HAVE_LIBUDEV | |
380 | static char *get_tag_from_udev(const char *devname, int col) | |
381 | { | |
382 | struct udev_device *dev; | |
383 | const char *data = NULL; | |
4ba4893b | 384 | char *res = NULL, *path; |
e346233e KZ |
385 | |
386 | if (!udev) | |
387 | udev = udev_new(); | |
388 | if (!udev) | |
389 | return NULL; | |
390 | ||
4ba4893b KZ |
391 | /* libudev don't like /dev/mapper/ symlinks */ |
392 | path = realpath(devname, NULL); | |
393 | if (path) | |
394 | devname = path; | |
395 | ||
e346233e KZ |
396 | if (strncmp(devname, "/dev/", 5) == 0) |
397 | devname += 5; | |
398 | ||
399 | dev = udev_device_new_from_subsystem_sysname(udev, "block", devname); | |
4ba4893b KZ |
400 | free(path); |
401 | ||
e346233e KZ |
402 | if (!dev) |
403 | return NULL; | |
404 | ||
405 | switch(col) { | |
406 | case COL_LABEL: | |
407 | data = udev_device_get_property_value(dev, "ID_FS_LABEL_ENC"); | |
408 | break; | |
409 | case COL_UUID: | |
410 | data = udev_device_get_property_value(dev, "ID_FS_UUID_ENC"); | |
411 | break; | |
412 | case COL_PARTUUID: | |
413 | data = udev_device_get_property_value(dev, "ID_PART_ENTRY_UUID"); | |
414 | break; | |
415 | case COL_PARTLABEL: | |
416 | data = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME"); | |
417 | break; | |
418 | default: | |
419 | break; | |
420 | } | |
421 | ||
422 | if (data) { | |
423 | res = xstrdup(data); | |
424 | unhexmangle_string(res); | |
425 | } | |
426 | ||
427 | udev_device_unref(dev); | |
428 | return res; | |
429 | } | |
430 | #endif /* HAVE_LIBUDEV */ | |
431 | ||
fdedb45e | 432 | /* Returns LABEL or UUID */ |
b3386c83 | 433 | static char *get_tag(struct libmnt_fs *fs, const char *tagname, int col |
81499ab2 KZ |
434 | #ifndef HAVE_LIBUDEV |
435 | __attribute__((__unused__)) | |
436 | #endif | |
437 | ) | |
9d67679b | 438 | { |
b3386c83 KZ |
439 | const char *t, *v; |
440 | char *res = NULL; | |
9d67679b KZ |
441 | |
442 | if (!mnt_fs_get_tag(fs, &t, &v) && !strcmp(t, tagname)) | |
b3386c83 | 443 | res = xstrdup(v); |
9d67679b | 444 | else { |
e346233e KZ |
445 | const char *dev = mnt_fs_get_source(fs); |
446 | ||
097e7f2f | 447 | if (dev && !(flags & FL_NOCACHE)) |
e346233e KZ |
448 | dev = mnt_resolve_spec(dev, cache); |
449 | #ifdef HAVE_LIBUDEV | |
450 | if (dev) | |
451 | res = get_tag_from_udev(dev, col); | |
452 | #endif | |
b3386c83 | 453 | if (!res) { |
e346233e | 454 | res = mnt_cache_find_tag_value(cache, dev, tagname); |
b3386c83 KZ |
455 | if (res && cache) |
456 | /* don't return pointer to cache */ | |
457 | res = xstrdup(res); | |
458 | } | |
9d67679b KZ |
459 | } |
460 | ||
461 | return res; | |
462 | } | |
463 | ||
b3386c83 | 464 | static char *get_vfs_attr(struct libmnt_fs *fs, int sizetype) |
a85ef33b DR |
465 | { |
466 | struct statvfs buf; | |
6901f22c | 467 | uint64_t vfs_attr = 0; |
a85ef33b DR |
468 | char *sizestr; |
469 | ||
470 | if (statvfs(mnt_fs_get_target(fs), &buf) != 0) | |
471 | return NULL; | |
472 | ||
473 | switch(sizetype) { | |
474 | case COL_SIZE: | |
475 | vfs_attr = buf.f_frsize * buf.f_blocks; | |
476 | break; | |
477 | case COL_AVAIL: | |
ed6c8c1b | 478 | vfs_attr = buf.f_frsize * buf.f_bavail; |
a85ef33b DR |
479 | break; |
480 | case COL_USED: | |
481 | vfs_attr = buf.f_frsize * (buf.f_blocks - buf.f_bfree); | |
482 | break; | |
483 | case COL_USEPERC: | |
484 | if (buf.f_blocks == 0) | |
203c945a | 485 | return xstrdup("-"); |
a85ef33b | 486 | |
d7bcb205 KZ |
487 | xasprintf(&sizestr, "%.0f%%", |
488 | (double)(buf.f_blocks - buf.f_bfree) / | |
489 | buf.f_blocks * 100); | |
a85ef33b DR |
490 | return sizestr; |
491 | } | |
492 | ||
2b6759df KZ |
493 | if (!vfs_attr) |
494 | sizestr = xstrdup("0"); | |
495 | else if (flags & FL_BYTES) | |
496 | xasprintf(&sizestr, "%ju", vfs_attr); | |
497 | else | |
498 | sizestr = size_to_human_string(SIZE_SUFFIX_1LETTER, vfs_attr); | |
499 | ||
500 | return sizestr; | |
a85ef33b DR |
501 | } |
502 | ||
46236388 | 503 | /* reads FS data from libmount |
46236388 | 504 | */ |
b3386c83 | 505 | static char *get_data(struct libmnt_fs *fs, int num) |
9d67679b | 506 | { |
b3386c83 | 507 | char *str = NULL; |
a85ef33b | 508 | int col_id = get_column_id(num); |
9d67679b | 509 | |
a85ef33b | 510 | switch (col_id) { |
04fd7a9f | 511 | case COL_SOURCE: |
b2214e1f KZ |
512 | { |
513 | const char *root = mnt_fs_get_root(fs); | |
b3386c83 | 514 | const char *spec = mnt_fs_get_srcpath(fs); |
b2214e1f | 515 | |
b3386c83 KZ |
516 | if (spec && (flags & FL_CANONICALIZE)) |
517 | spec = mnt_resolve_path(spec, cache); | |
518 | if (!spec) { | |
519 | spec = mnt_fs_get_source(fs); | |
9d67679b | 520 | |
b3386c83 KZ |
521 | if (spec && (flags & FL_EVALUATE)) |
522 | spec = mnt_resolve_spec(spec, cache); | |
b2214e1f | 523 | } |
b3386c83 KZ |
524 | if (root && spec && !(flags & FL_NOFSROOT) && strcmp(root, "/")) |
525 | xasprintf(&str, "%s[%s]", spec, root); | |
526 | else if (spec) | |
527 | str = xstrdup(spec); | |
04fd7a9f | 528 | break; |
b2214e1f | 529 | } |
04fd7a9f | 530 | case COL_TARGET: |
b3386c83 | 531 | str = xstrdup(mnt_fs_get_target(fs)); |
04fd7a9f KZ |
532 | break; |
533 | case COL_FSTYPE: | |
b3386c83 | 534 | str = xstrdup(mnt_fs_get_fstype(fs)); |
04fd7a9f KZ |
535 | break; |
536 | case COL_OPTIONS: | |
b3386c83 | 537 | str = xstrdup(mnt_fs_get_options(fs)); |
04fd7a9f | 538 | break; |
631280e0 | 539 | case COL_VFS_OPTIONS: |
b3386c83 | 540 | str = xstrdup(mnt_fs_get_vfs_options(fs)); |
631280e0 KZ |
541 | break; |
542 | case COL_FS_OPTIONS: | |
b3386c83 | 543 | str = xstrdup(mnt_fs_get_fs_options(fs)); |
631280e0 | 544 | break; |
327ea85a | 545 | case COL_OPT_FIELDS: |
b3386c83 | 546 | str = xstrdup(mnt_fs_get_optional_fields(fs)); |
327ea85a | 547 | break; |
9d67679b | 548 | case COL_UUID: |
e346233e | 549 | str = get_tag(fs, "UUID", col_id); |
9d67679b | 550 | break; |
090b5e84 | 551 | case COL_PARTUUID: |
e346233e | 552 | str = get_tag(fs, "PARTUUID", col_id); |
090b5e84 | 553 | break; |
9d67679b | 554 | case COL_LABEL: |
e346233e | 555 | str = get_tag(fs, "LABEL", col_id); |
9d67679b | 556 | break; |
090b5e84 | 557 | case COL_PARTLABEL: |
e346233e | 558 | str = get_tag(fs, "PARTLABEL", col_id); |
090b5e84 KZ |
559 | break; |
560 | ||
46236388 KZ |
561 | case COL_MAJMIN: |
562 | { | |
563 | dev_t devno = mnt_fs_get_devno(fs); | |
60b17c6f KZ |
564 | if (!devno) |
565 | break; | |
566 | ||
85c9ca5a | 567 | if ((flags & FL_RAW) || (flags & FL_EXPORT)) |
b3386c83 | 568 | xasprintf(&str, "%u:%u", major(devno), minor(devno)); |
60b17c6f | 569 | else |
b3386c83 | 570 | xasprintf(&str, "%3u:%-3u", major(devno), minor(devno)); |
a85ef33b | 571 | break; |
46236388 | 572 | } |
a85ef33b DR |
573 | case COL_SIZE: |
574 | case COL_AVAIL: | |
575 | case COL_USED: | |
576 | case COL_USEPERC: | |
577 | str = get_vfs_attr(fs, col_id); | |
578 | break; | |
8b8cd87b | 579 | case COL_FSROOT: |
b3386c83 | 580 | str = xstrdup(mnt_fs_get_root(fs)); |
8b8cd87b | 581 | break; |
7d8f4e0c | 582 | case COL_TID: |
b3386c83 KZ |
583 | if (mnt_fs_get_tid(fs)) |
584 | xasprintf(&str, "%d", mnt_fs_get_tid(fs)); | |
7d8f4e0c | 585 | break; |
9238e0fa | 586 | case COL_ID: |
b3386c83 KZ |
587 | if (mnt_fs_get_id(fs)) |
588 | xasprintf(&str, "%d", mnt_fs_get_id(fs)); | |
9238e0fa | 589 | break; |
624f2b47 KZ |
590 | case COL_PROPAGATION: |
591 | if (mnt_fs_is_kernel(fs)) { | |
592 | unsigned long fl = 0; | |
593 | char *n = NULL; | |
594 | ||
595 | if (mnt_fs_get_propagation(fs, &fl) != 0) | |
596 | break; | |
597 | ||
598 | n = xstrdup((fl & MS_SHARED) ? "shared" : "private"); | |
599 | ||
600 | if (fl & MS_SLAVE) { | |
b3386c83 | 601 | xasprintf(&str, "%s,slave", n); |
624f2b47 | 602 | free(n); |
b3386c83 | 603 | n = str; |
624f2b47 KZ |
604 | } |
605 | if (fl & MS_UNBINDABLE) { | |
b3386c83 | 606 | xasprintf(&str, "%s,unbindable", n); |
624f2b47 | 607 | free(n); |
b3386c83 | 608 | n = str; |
624f2b47 KZ |
609 | } |
610 | str = n; | |
611 | } | |
612 | break; | |
426f0cea | 613 | case COL_FREQ: |
b3386c83 KZ |
614 | if (!mnt_fs_is_kernel(fs)) |
615 | xasprintf(&str, "%d", mnt_fs_get_freq(fs)); | |
426f0cea KZ |
616 | break; |
617 | case COL_PASSNO: | |
b3386c83 KZ |
618 | if (!mnt_fs_is_kernel(fs)) |
619 | xasprintf(&str, "%d", mnt_fs_get_passno(fs)); | |
426f0cea | 620 | break; |
04fd7a9f | 621 | default: |
9d67679b | 622 | break; |
04fd7a9f | 623 | } |
fdedb45e | 624 | return str; |
9d67679b KZ |
625 | } |
626 | ||
b3386c83 | 627 | static char *get_tabdiff_data(struct libmnt_fs *old_fs, |
32e5466a KZ |
628 | struct libmnt_fs *new_fs, |
629 | int change, | |
630 | int num) | |
631 | { | |
b3386c83 | 632 | char *str = NULL; |
32e5466a KZ |
633 | |
634 | switch (get_column_id(num)) { | |
635 | case COL_ACTION: | |
636 | switch (change) { | |
637 | case MNT_TABDIFF_MOUNT: | |
638 | str = _("mount"); | |
639 | break; | |
640 | case MNT_TABDIFF_UMOUNT: | |
641 | str = _("umount"); | |
642 | break; | |
643 | case MNT_TABDIFF_REMOUNT: | |
644 | str = _("remount"); | |
645 | break; | |
646 | case MNT_TABDIFF_MOVE: | |
647 | str = _("move"); | |
648 | break; | |
649 | default: | |
650 | str = _("unknown"); | |
651 | break; | |
652 | } | |
b3386c83 | 653 | str = xstrdup(str); |
32e5466a KZ |
654 | break; |
655 | case COL_OLD_OPTIONS: | |
77a1c5f7 KZ |
656 | if (old_fs && (change == MNT_TABDIFF_REMOUNT || |
657 | change == MNT_TABDIFF_UMOUNT)) | |
b3386c83 | 658 | str = xstrdup(mnt_fs_get_options(old_fs)); |
32e5466a KZ |
659 | break; |
660 | case COL_OLD_TARGET: | |
77a1c5f7 KZ |
661 | if (old_fs && (change == MNT_TABDIFF_MOVE || |
662 | change == MNT_TABDIFF_UMOUNT)) | |
b3386c83 | 663 | str = xstrdup(mnt_fs_get_target(old_fs)); |
32e5466a KZ |
664 | break; |
665 | default: | |
666 | if (new_fs) | |
667 | str = get_data(new_fs, num); | |
668 | else | |
669 | str = get_data(old_fs, num); | |
670 | break; | |
671 | } | |
672 | return str; | |
673 | } | |
674 | ||
fdedb45e | 675 | /* adds one line to the output @tab */ |
218a3a94 OO |
676 | static struct libscols_line *add_line(struct libscols_table *table, struct libmnt_fs *fs, |
677 | struct libscols_line *parent) | |
9d67679b | 678 | { |
fdedb45e | 679 | int i; |
218a3a94 | 680 | struct libscols_line *line = scols_table_new_line(table, parent); |
9d67679b | 681 | |
fdedb45e KZ |
682 | if (!line) { |
683 | warn(_("failed to add line to output")); | |
684 | return NULL; | |
9d67679b | 685 | } |
fdedb45e | 686 | for (i = 0; i < ncolumns; i++) |
85c9ca5a | 687 | scols_line_refer_data(line, i, get_data(fs, i)); |
9d67679b | 688 | |
218a3a94 | 689 | scols_line_set_userdata(line, fs); |
fdedb45e | 690 | return line; |
9d67679b KZ |
691 | } |
692 | ||
218a3a94 | 693 | static struct libscols_line *add_tabdiff_line(struct libscols_table *table, struct libmnt_fs *new_fs, |
32e5466a KZ |
694 | struct libmnt_fs *old_fs, int change) |
695 | { | |
696 | int i; | |
218a3a94 | 697 | struct libscols_line *line = scols_table_new_line(table, NULL); |
32e5466a KZ |
698 | |
699 | if (!line) { | |
700 | warn(_("failed to add line to output")); | |
701 | return NULL; | |
702 | } | |
703 | for (i = 0; i < ncolumns; i++) | |
85c9ca5a | 704 | scols_line_refer_data(line, i, |
32e5466a KZ |
705 | get_tabdiff_data(old_fs, new_fs, change, i)); |
706 | ||
707 | return line; | |
708 | } | |
709 | ||
218a3a94 | 710 | static int has_line(struct libscols_table *table, struct libmnt_fs *fs) |
049caefd | 711 | { |
218a3a94 OO |
712 | struct libscols_line *ln; |
713 | struct libscols_iter *itr; | |
714 | ||
715 | itr = scols_new_iter(SCOLS_ITER_FORWARD); | |
716 | if (!itr) | |
717 | return 0; | |
049caefd | 718 | |
218a3a94 OO |
719 | while(scols_table_next_line(table, itr, &ln) == 0) { |
720 | if ((struct libmnt_fs *) scols_line_get_userdata(ln) == fs) | |
049caefd KZ |
721 | return 1; |
722 | } | |
723 | return 0; | |
724 | } | |
725 | ||
218a3a94 OO |
726 | /* reads filesystems from @tb (libmount) and fillin @table (output table) */ |
727 | static int create_treenode(struct libscols_table *table, struct libmnt_table *tb, | |
728 | struct libmnt_fs *fs, struct libscols_line *parent_line) | |
04fd7a9f | 729 | { |
68164f6c KZ |
730 | struct libmnt_fs *chld = NULL; |
731 | struct libmnt_iter *itr = NULL; | |
218a3a94 | 732 | struct libscols_line *line; |
fdedb45e | 733 | int rc = -1; |
9d67679b | 734 | |
fdedb45e KZ |
735 | if (!fs) { |
736 | /* first call, get root FS */ | |
68164f6c | 737 | if (mnt_table_get_root_fs(tb, &fs)) |
fdedb45e KZ |
738 | goto leave; |
739 | parent_line = NULL; | |
049caefd | 740 | |
218a3a94 | 741 | } else if ((flags & FL_SUBMOUNTS) && has_line(table, fs)) |
049caefd | 742 | return 0; |
9d67679b | 743 | |
fdedb45e KZ |
744 | itr = mnt_new_iter(MNT_ITER_FORWARD); |
745 | if (!itr) | |
746 | goto leave; | |
9d67679b | 747 | |
31f67453 | 748 | if ((flags & FL_SUBMOUNTS) || match_func(fs, NULL)) { |
218a3a94 | 749 | line = add_line(table, fs, parent_line); |
31f67453 KZ |
750 | if (!line) |
751 | goto leave; | |
752 | } else | |
753 | line = parent_line; | |
9d67679b | 754 | |
fdedb45e KZ |
755 | /* |
756 | * add all children to the output table | |
757 | */ | |
68164f6c | 758 | while(mnt_table_next_child_fs(tb, itr, fs, &chld) == 0) { |
218a3a94 | 759 | if (create_treenode(table, tb, chld, line)) |
fdedb45e | 760 | goto leave; |
9d67679b | 761 | } |
fdedb45e KZ |
762 | rc = 0; |
763 | leave: | |
764 | mnt_free_iter(itr); | |
765 | return rc; | |
04fd7a9f KZ |
766 | } |
767 | ||
20055151 | 768 | /* error callback */ |
6e5c0fc2 KZ |
769 | static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)), |
770 | const char *filename, int line) | |
20055151 | 771 | { |
b1b9f1c1 | 772 | warnx(_("%s: parse error at line %d"), filename, line); |
20055151 KZ |
773 | return 0; |
774 | } | |
775 | ||
8c5eba3e KZ |
776 | static char **append_tabfile(char **files, int *nfiles, char *filename) |
777 | { | |
99d618c0 | 778 | files = xrealloc(files, sizeof(char *) * (*nfiles + 1)); |
8c5eba3e KZ |
779 | files[(*nfiles)++] = filename; |
780 | return files; | |
781 | } | |
782 | ||
c7fcc830 KZ |
783 | static char **append_pid_tabfile(char **files, int *nfiles, pid_t pid) |
784 | { | |
785 | char *path = NULL; | |
786 | ||
787 | xasprintf(&path, "/proc/%d/mountinfo", (int) pid); | |
788 | return append_tabfile(files, nfiles, path); | |
789 | } | |
790 | ||
fdedb45e | 791 | /* calls libmount fstab/mtab/mountinfo parser */ |
8c5eba3e KZ |
792 | static struct libmnt_table *parse_tabfiles(char **files, |
793 | int nfiles, | |
794 | int tabtype) | |
04fd7a9f | 795 | { |
8c5eba3e | 796 | struct libmnt_table *tb; |
762ef0e1 | 797 | int rc = 0; |
20055151 | 798 | |
8c5eba3e | 799 | tb = mnt_new_table(); |
fdedb45e | 800 | if (!tb) { |
32e5466a | 801 | warn(_("failed to initialize libmount table")); |
04fd7a9f | 802 | return NULL; |
fdedb45e | 803 | } |
68164f6c | 804 | mnt_table_set_parser_errcb(tb, parser_errcb); |
20055151 | 805 | |
8c5eba3e KZ |
806 | do { |
807 | /* NULL means that libmount will use default paths */ | |
808 | const char *path = nfiles ? *files++ : NULL; | |
809 | ||
810 | switch (tabtype) { | |
811 | case TABTYPE_FSTAB: | |
812 | rc = mnt_table_parse_fstab(tb, path); | |
813 | break; | |
814 | case TABTYPE_MTAB: | |
815 | rc = mnt_table_parse_mtab(tb, path); | |
816 | break; | |
817 | case TABTYPE_KERNEL: | |
818 | if (!path) | |
819 | path = access(_PATH_PROC_MOUNTINFO, R_OK) == 0 ? | |
820 | _PATH_PROC_MOUNTINFO : | |
821 | _PATH_PROC_MOUNTS; | |
822 | ||
823 | rc = mnt_table_parse_file(tb, path); | |
824 | break; | |
825 | } | |
826 | if (rc) { | |
50fccba1 | 827 | mnt_unref_table(tb); |
8c5eba3e KZ |
828 | warn(_("can't read %s"), path); |
829 | return NULL; | |
830 | } | |
831 | } while (--nfiles > 0); | |
20055151 | 832 | |
04fd7a9f | 833 | return tb; |
04fd7a9f KZ |
834 | } |
835 | ||
2f1ac44b KZ |
836 | /* checks if @tb contains parent->child relations */ |
837 | static int tab_is_tree(struct libmnt_table *tb) | |
838 | { | |
839 | struct libmnt_fs *fs = NULL; | |
840 | struct libmnt_iter *itr = NULL; | |
841 | int rc = 0; | |
842 | ||
843 | itr = mnt_new_iter(MNT_ITER_BACKWARD); | |
844 | if (!itr) | |
845 | return 0; | |
846 | ||
6c373810 KZ |
847 | rc = (mnt_table_next_fs(tb, itr, &fs) == 0 && |
848 | mnt_fs_is_kernel(fs) && | |
849 | mnt_fs_get_root(fs)); | |
2f1ac44b KZ |
850 | |
851 | mnt_free_iter(itr); | |
852 | return rc; | |
853 | } | |
854 | ||
b215d8e9 | 855 | |
68164f6c | 856 | /* filter function for libmount (mnt_table_find_next_fs()) */ |
6e5c0fc2 KZ |
857 | static int match_func(struct libmnt_fs *fs, |
858 | void *data __attribute__ ((__unused__))) | |
04fd7a9f | 859 | { |
04fd7a9f | 860 | int rc = flags & FL_INVERT ? 1 : 0; |
9d67679b | 861 | const char *m; |
cd492186 | 862 | void *md; |
04fd7a9f | 863 | |
9d67679b KZ |
864 | m = get_match(COL_FSTYPE); |
865 | if (m && !mnt_fs_match_fstype(fs, m)) | |
04fd7a9f | 866 | return rc; |
9d67679b KZ |
867 | |
868 | m = get_match(COL_OPTIONS); | |
869 | if (m && !mnt_fs_match_options(fs, m)) | |
04fd7a9f KZ |
870 | return rc; |
871 | ||
cd492186 KZ |
872 | md = get_match_data(COL_MAJMIN); |
873 | if (md && mnt_fs_get_devno(fs) != *((dev_t *) md)) | |
874 | return rc; | |
875 | ||
4096946f KZ |
876 | m = get_match(COL_TARGET); |
877 | if (m && !mnt_fs_match_target(fs, m, cache)) | |
878 | return rc; | |
879 | ||
880 | m = get_match(COL_SOURCE); | |
881 | if (m && !mnt_fs_match_source(fs, m, cache)) | |
882 | return rc; | |
883 | ||
cd4ed46f KZ |
884 | if ((flags & FL_DF) && !(flags & FL_ALL)) { |
885 | const char *type = mnt_fs_get_fstype(fs); | |
886 | ||
887 | if (type && strstr(type, "tmpfs")) /* tmpfs is wanted */ | |
888 | return !rc; | |
889 | ||
890 | if (mnt_fs_is_pseudofs(fs)) | |
891 | return rc; | |
892 | } | |
893 | ||
04fd7a9f KZ |
894 | return !rc; |
895 | } | |
896 | ||
fdedb45e | 897 | /* iterate over filesystems in @tb */ |
68164f6c KZ |
898 | static struct libmnt_fs *get_next_fs(struct libmnt_table *tb, |
899 | struct libmnt_iter *itr) | |
04fd7a9f | 900 | { |
68164f6c | 901 | struct libmnt_fs *fs = NULL; |
9d67679b KZ |
902 | |
903 | if (is_listall_mode()) { | |
904 | /* | |
905 | * Print whole file | |
906 | */ | |
e3963f60 KZ |
907 | if (mnt_table_next_fs(tb, itr, &fs) != 0) |
908 | return NULL; | |
9d67679b KZ |
909 | |
910 | } else if (is_mount_compatible_mode()) { | |
911 | /* | |
912 | * Look up for FS in the same way how mount(8) searchs in fstab | |
913 | * | |
914 | * findmnt -f <spec> | |
915 | */ | |
68164f6c | 916 | fs = mnt_table_find_source(tb, get_match(COL_SOURCE), |
9d67679b | 917 | mnt_iter_get_direction(itr)); |
9a30c6ef KZ |
918 | |
919 | if (!fs && !(flags & FL_NOSWAPMATCH)) | |
68164f6c | 920 | fs = mnt_table_find_target(tb, get_match(COL_SOURCE), |
9d67679b KZ |
921 | mnt_iter_get_direction(itr)); |
922 | } else { | |
923 | /* | |
924 | * Look up for all matching entries | |
925 | * | |
926 | * findmnt [-l] <source> <target> [-O <options>] [-t <types>] | |
927 | * findmnt [-l] <spec> [-O <options>] [-t <types>] | |
928 | */ | |
929 | again: | |
68164f6c | 930 | mnt_table_find_next_fs(tb, itr, match_func, NULL, &fs); |
9d67679b KZ |
931 | |
932 | if (!fs && | |
933 | !(flags & FL_NOSWAPMATCH) && | |
934 | !get_match(COL_TARGET) && get_match(COL_SOURCE)) { | |
935 | ||
936 | /* swap 'spec' and target. */ | |
937 | set_match(COL_TARGET, get_match(COL_SOURCE)); | |
938 | set_match(COL_SOURCE, NULL); | |
939 | mnt_reset_iter(itr, -1); | |
940 | ||
941 | goto again; | |
942 | } | |
943 | } | |
9d67679b | 944 | |
fdedb45e | 945 | return fs; |
04fd7a9f KZ |
946 | } |
947 | ||
31f67453 KZ |
948 | /* |
949 | * Filter out unwanted lines for --list output or top level lines for | |
950 | * --submounts tree output. | |
951 | */ | |
68164f6c | 952 | static int add_matching_lines(struct libmnt_table *tb, |
218a3a94 | 953 | struct libscols_table *table, int direction) |
049caefd | 954 | { |
68164f6c KZ |
955 | struct libmnt_iter *itr = NULL; |
956 | struct libmnt_fs *fs; | |
049caefd KZ |
957 | int nlines = 0, rc = -1; |
958 | ||
959 | itr = mnt_new_iter(direction); | |
960 | if (!itr) { | |
961 | warn(_("failed to initialize libmount iterator")); | |
962 | goto done; | |
963 | } | |
964 | ||
965 | while((fs = get_next_fs(tb, itr))) { | |
85c9ca5a | 966 | if ((flags & FL_TREE) || (flags & FL_SUBMOUNTS)) |
218a3a94 | 967 | rc = create_treenode(table, tb, fs, NULL); |
049caefd | 968 | else |
218a3a94 | 969 | rc = !add_line(table, fs, NULL); |
049caefd KZ |
970 | if (rc) |
971 | goto done; | |
972 | nlines++; | |
973 | if (flags & FL_FIRSTONLY) | |
974 | break; | |
975 | flags |= FL_NOSWAPMATCH; | |
976 | } | |
977 | ||
978 | if (nlines) | |
979 | rc = 0; | |
980 | done: | |
981 | mnt_free_iter(itr); | |
982 | return rc; | |
983 | } | |
984 | ||
582a5006 KZ |
985 | static int poll_match(struct libmnt_fs *fs) |
986 | { | |
987 | int rc = match_func(fs, NULL); | |
988 | ||
989 | if (rc == 0 && !(flags & FL_NOSWAPMATCH) && | |
990 | get_match(COL_SOURCE) && !get_match(COL_TARGET)) { | |
991 | /* | |
992 | * findmnt --poll /foo | |
cd492186 | 993 | * The '/foo' maybe source as well as target. |
582a5006 KZ |
994 | */ |
995 | const char *str = get_match(COL_SOURCE); | |
996 | ||
997 | set_match(COL_TARGET, str); /* swap */ | |
998 | set_match(COL_SOURCE, NULL); | |
999 | ||
1000 | rc = match_func(fs, NULL); | |
1001 | ||
1002 | set_match(COL_TARGET, NULL); /* restore */ | |
1003 | set_match(COL_SOURCE, str); | |
1004 | ||
1005 | } | |
1006 | return rc; | |
1007 | } | |
1008 | ||
32e5466a | 1009 | static int poll_table(struct libmnt_table *tb, const char *tabfile, |
218a3a94 | 1010 | int timeout, struct libscols_table *table, int direction) |
32e5466a | 1011 | { |
d1cabd5c | 1012 | FILE *f = NULL; |
32e5466a KZ |
1013 | int rc = -1; |
1014 | struct libmnt_iter *itr = NULL; | |
1015 | struct libmnt_table *tb_new = NULL; | |
1016 | struct libmnt_tabdiff *diff = NULL; | |
1017 | struct pollfd fds[1]; | |
1018 | ||
1019 | tb_new = mnt_new_table(); | |
1020 | if (!tb_new) { | |
1021 | warn(_("failed to initialize libmount table")); | |
1022 | goto done; | |
1023 | } | |
1024 | ||
1025 | itr = mnt_new_iter(direction); | |
1026 | if (!itr) { | |
1027 | warn(_("failed to initialize libmount iterator")); | |
1028 | goto done; | |
1029 | } | |
1030 | ||
1031 | diff = mnt_new_tabdiff(); | |
1032 | if (!diff) { | |
1033 | warn(_("failed to initialize libmount tabdiff")); | |
1034 | goto done; | |
1035 | } | |
1036 | ||
1037 | /* cache is unnecessary to detect changes */ | |
1038 | mnt_table_set_cache(tb, NULL); | |
1039 | mnt_table_set_cache(tb_new, NULL); | |
1040 | ||
1041 | f = fopen(tabfile, "r"); | |
1042 | if (!f) { | |
289dcc90 | 1043 | warn(_("cannot open %s"), tabfile); |
32e5466a KZ |
1044 | goto done; |
1045 | } | |
1046 | ||
1047 | mnt_table_set_parser_errcb(tb_new, parser_errcb); | |
1048 | ||
1049 | fds[0].fd = fileno(f); | |
1050 | fds[0].events = POLLPRI; | |
1051 | ||
1052 | while (1) { | |
1053 | struct libmnt_table *tmp; | |
1054 | struct libmnt_fs *old, *new; | |
582a5006 | 1055 | int change, count; |
32e5466a | 1056 | |
582a5006 KZ |
1057 | count = poll(fds, 1, timeout); |
1058 | if (count == 0) | |
ad38fb9f | 1059 | break; /* timeout */ |
582a5006 | 1060 | if (count < 0) { |
32e5466a KZ |
1061 | warn(_("poll() failed")); |
1062 | goto done; | |
1063 | } | |
1064 | ||
1065 | rewind(f); | |
1066 | rc = mnt_table_parse_stream(tb_new, f, tabfile); | |
1067 | if (!rc) | |
1068 | rc = mnt_diff_tables(diff, tb, tb_new); | |
1069 | if (rc < 0) | |
1070 | goto done; | |
1071 | ||
582a5006 | 1072 | count = 0; |
32e5466a KZ |
1073 | mnt_reset_iter(itr, direction); |
1074 | while(mnt_tabdiff_next_change( | |
1075 | diff, itr, &old, &new, &change) == 0) { | |
1076 | ||
582a5006 KZ |
1077 | if (!has_poll_action(change)) |
1078 | continue; | |
1079 | if (!poll_match(new ? new : old)) | |
1080 | continue; | |
1081 | count++; | |
218a3a94 | 1082 | rc = !add_tabdiff_line(table, new, old, change); |
32e5466a KZ |
1083 | if (rc) |
1084 | goto done; | |
1085 | if (flags & FL_FIRSTONLY) | |
1086 | break; | |
1087 | } | |
1088 | ||
582a5006 | 1089 | if (count) { |
218a3a94 | 1090 | rc = scols_print_table(table); |
582a5006 KZ |
1091 | if (rc) |
1092 | goto done; | |
1093 | } | |
32e5466a KZ |
1094 | |
1095 | /* swap tables */ | |
1096 | tmp = tb; | |
1097 | tb = tb_new; | |
1098 | tb_new = tmp; | |
1099 | ||
218a3a94 | 1100 | scols_table_remove_lines(table); |
32e5466a | 1101 | mnt_reset_table(tb_new); |
582a5006 KZ |
1102 | |
1103 | if (count && (flags & FL_FIRSTONLY)) | |
1104 | break; | |
32e5466a KZ |
1105 | } |
1106 | ||
1107 | rc = 0; | |
1108 | done: | |
50fccba1 | 1109 | mnt_unref_table(tb_new); |
32e5466a KZ |
1110 | mnt_free_tabdiff(diff); |
1111 | mnt_free_iter(itr); | |
d1cabd5c KZ |
1112 | if (f) |
1113 | fclose(f); | |
32e5466a KZ |
1114 | return rc; |
1115 | } | |
1116 | ||
bebdda30 KZ |
1117 | static int uniq_fs_target_cmp( |
1118 | struct libmnt_table *tb __attribute__((__unused__)), | |
1119 | struct libmnt_fs *a, | |
1120 | struct libmnt_fs *b) | |
1121 | { | |
1122 | return !mnt_fs_match_target(a, mnt_fs_get_target(b), cache); | |
1123 | } | |
1124 | ||
abafd686 | 1125 | static void __attribute__((__noreturn__)) usage(FILE *out) |
04fd7a9f | 1126 | { |
9ead0006 KZ |
1127 | int i; |
1128 | ||
5cc12c40 | 1129 | fputs(USAGE_HEADER, out); |
9d67679b | 1130 | fprintf(out, _( |
9d67679b KZ |
1131 | " %1$s [options]\n" |
1132 | " %1$s [options] <device> | <mountpoint>\n" | |
1133 | " %1$s [options] <device> <mountpoint>\n" | |
1134 | " %1$s [options] [--source <device>] [--target <mountpoint>]\n"), | |
1135 | program_invocation_short_name); | |
04fd7a9f | 1136 | |
b95c6e88 KZ |
1137 | fputs(USAGE_OPTIONS, out); |
1138 | fputs(_(" -s, --fstab search in static table of filesystems\n"), out); | |
1139 | fputs(_(" -m, --mtab search in table of mounted filesystems\n"), out); | |
1140 | fputs(_(" -k, --kernel search in kernel table of mounted\n" | |
ab5b4f83 | 1141 | " filesystems (default)\n"), out); |
b95c6e88 KZ |
1142 | fputc('\n', out); |
1143 | fputs(_(" -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"), out); | |
1144 | fputs(_(" -w, --timeout <num> upper limit in milliseconds that --poll will block\n"), out); | |
1145 | fputc('\n', out); | |
1146 | ||
1147 | fputs(_(" -A, --all disable all built-in filters, print all filesystems\n"), out); | |
1148 | fputs(_(" -a, --ascii use ASCII chars for tree formatting\n"), out); | |
2b6759df | 1149 | fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out); |
ab5b4f83 | 1150 | fputs(_(" -C, --nocanonicalize don't canonicalize when comparing paths\n"), out); |
b95c6e88 KZ |
1151 | fputs(_(" -c, --canonicalize canonicalize printed paths\n"), out); |
1152 | fputs(_(" -D, --df imitate the output of df(1)\n"), out); | |
1153 | fputs(_(" -d, --direction <word> direction of search, 'forward' or 'backward'\n"), out); | |
1154 | fputs(_(" -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n" | |
1155 | " to device names\n"), out); | |
1156 | fputs(_(" -F, --tab-file <path> alternative file for -s, -m or -k options\n"), out); | |
1157 | fputs(_(" -f, --first-only print the first found filesystem only\n"), out); | |
1158 | fputs(_(" -i, --invert invert the sense of matching\n"), out); | |
1159 | fputs(_(" -l, --list use list format output\n"), out); | |
1160 | fputs(_(" -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"), out); | |
1161 | fputs(_(" -n, --noheadings don't print column headings\n"), out); | |
b95c6e88 KZ |
1162 | fputs(_(" -O, --options <list> limit the set of filesystems by mount options\n"), out); |
1163 | fputs(_(" -o, --output <list> the output columns to be shown\n"), out); | |
1164 | fputs(_(" -P, --pairs use key=\"value\" output format\n"), out); | |
b95c6e88 | 1165 | fputs(_(" -R, --submounts print all submounts for the matching filesystems\n"), out); |
ab5b4f83 | 1166 | fputs(_(" -r, --raw use raw output format\n"), out); |
b95c6e88 KZ |
1167 | fputs(_(" -S, --source <string> the device to mount (by name, maj:min, \n" |
1168 | " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"), out); | |
1169 | fputs(_(" -T, --target <string> the mountpoint to use\n"), out); | |
ab5b4f83 BS |
1170 | fputs(_(" -t, --types <list> limit the set of filesystems by FS types\n"), out); |
1171 | fputs(_(" -U, --uniq ignore filesystems with duplicate target\n"), out); | |
1172 | fputs(_(" -u, --notruncate don't truncate text in columns\n"), out); | |
1173 | fputs(_(" -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"), out); | |
04fd7a9f | 1174 | |
5cc12c40 SK |
1175 | fputs(USAGE_SEPARATOR, out); |
1176 | fputs(USAGE_HELP, out); | |
1177 | fputs(USAGE_VERSION, out); | |
9ead0006 KZ |
1178 | |
1179 | fprintf(out, _("\nAvailable columns:\n")); | |
1180 | ||
b152e359 KZ |
1181 | for (i = 0; i < FINDMNT_NCOLUMNS; i++) |
1182 | fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help)); | |
9ead0006 | 1183 | |
beda9dd3 | 1184 | fprintf(out, USAGE_MAN_TAIL("findmnt(8)")); |
04fd7a9f KZ |
1185 | |
1186 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
1187 | } | |
1188 | ||
1189 | int main(int argc, char *argv[]) | |
1190 | { | |
68164f6c | 1191 | struct libmnt_table *tb = NULL; |
8c5eba3e | 1192 | char **tabfiles = NULL; |
9d67679b | 1193 | int direction = MNT_ITER_FORWARD; |
cd4ed46f | 1194 | int i, c, rc = -1, timeout = -1; |
8c5eba3e | 1195 | int ntabfiles = 0, tabtype = 0; |
c57dca68 | 1196 | char *outarg = NULL; |
fdedb45e | 1197 | |
218a3a94 | 1198 | struct libscols_table *table = NULL; |
fdedb45e | 1199 | |
6c7d5ae9 | 1200 | static const struct option longopts[] = { |
cd4ed46f | 1201 | { "all", 0, 0, 'A' }, |
9d67679b | 1202 | { "ascii", 0, 0, 'a' }, |
2b6759df | 1203 | { "bytes", 0, 0, 'b' }, |
04fd7a9f KZ |
1204 | { "canonicalize", 0, 0, 'c' }, |
1205 | { "direction", 1, 0, 'd' }, | |
eda399b9 | 1206 | { "df", 0, 0, 'D' }, |
04fd7a9f | 1207 | { "evaluate", 0, 0, 'e' }, |
9d67679b | 1208 | { "first-only", 0, 0, 'f' }, |
2f1ac44b | 1209 | { "fstab", 0, 0, 's' }, |
04fd7a9f KZ |
1210 | { "help", 0, 0, 'h' }, |
1211 | { "invert", 0, 0, 'i' }, | |
9d67679b KZ |
1212 | { "kernel", 0, 0, 'k' }, |
1213 | { "list", 0, 0, 'l' }, | |
1214 | { "mtab", 0, 0, 'm' }, | |
1215 | { "noheadings", 0, 0, 'n' }, | |
1216 | { "notruncate", 0, 0, 'u' }, | |
04fd7a9f | 1217 | { "options", 1, 0, 'O' }, |
9d67679b | 1218 | { "output", 1, 0, 'o' }, |
582a5006 | 1219 | { "poll", 2, 0, 'p' }, |
49e9fd3a | 1220 | { "pairs", 0, 0, 'P' }, |
9d67679b | 1221 | { "raw", 0, 0, 'r' }, |
04fd7a9f | 1222 | { "types", 1, 0, 't' }, |
097e7f2f | 1223 | { "nocanonicalize", 0, 0, 'C' }, |
395eb05d | 1224 | { "nofsroot", 0, 0, 'v' }, |
049caefd | 1225 | { "submounts", 0, 0, 'R' }, |
9d67679b | 1226 | { "source", 1, 0, 'S' }, |
2f1ac44b | 1227 | { "tab-file", 1, 0, 'F' }, |
c7fcc830 | 1228 | { "task", 1, 0, 'N' }, |
9d67679b | 1229 | { "target", 1, 0, 'T' }, |
ad38fb9f | 1230 | { "timeout", 1, 0, 'w' }, |
bebdda30 | 1231 | { "uniq", 0, 0, 'U' }, |
5cc12c40 | 1232 | { "version", 0, 0, 'V' }, |
9d67679b | 1233 | |
04fd7a9f KZ |
1234 | { NULL, 0, 0, 0 } |
1235 | }; | |
1236 | ||
f1622b57 | 1237 | static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */ |
097e7f2f KZ |
1238 | { 'C', 'c'}, /* [no]canonicalize */ |
1239 | { 'C', 'e' }, /* nocanonicalize, evaluate */ | |
f1622b57 KZ |
1240 | { 'N','k','m','s' }, /* task,kernel,mtab,fstab */ |
1241 | { 'P','l','r' }, /* pairs,list,raw */ | |
1242 | { 'm','p','s' }, /* mtab,poll,fstab */ | |
1243 | { 0 } | |
1244 | }; | |
1245 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
1246 | ||
af0dc7d3 | 1247 | assert(ARRAY_SIZE(columns) == FINDMNT_NCOLUMNS); |
9d67679b | 1248 | |
04fd7a9f KZ |
1249 | setlocale(LC_ALL, ""); |
1250 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1251 | textdomain(PACKAGE); | |
c05a80ca | 1252 | atexit(close_stdout); |
04fd7a9f | 1253 | |
9d67679b | 1254 | /* default output format */ |
85c9ca5a | 1255 | flags |= FL_TREE; |
04fd7a9f | 1256 | |
9d67679b | 1257 | while ((c = getopt_long(argc, argv, |
097e7f2f | 1258 | "AabCcDd:ehifF:o:O:p::PklmnN:rst:uvRS:T:Uw:V", |
d466c6a1 | 1259 | longopts, NULL)) != -1) { |
f1622b57 KZ |
1260 | |
1261 | err_exclusive_options(c, longopts, excl, excl_st); | |
1262 | ||
04fd7a9f | 1263 | switch(c) { |
cd4ed46f KZ |
1264 | case 'A': |
1265 | flags |= FL_ALL; | |
1266 | break; | |
9d67679b | 1267 | case 'a': |
85c9ca5a | 1268 | flags |= FL_ASCII; |
9d67679b | 1269 | break; |
2b6759df KZ |
1270 | case 'b': |
1271 | flags |= FL_BYTES; | |
1272 | break; | |
097e7f2f KZ |
1273 | case 'C': |
1274 | flags |= FL_NOCACHE; | |
1275 | break; | |
04fd7a9f KZ |
1276 | case 'c': |
1277 | flags |= FL_CANONICALIZE; | |
1278 | break; | |
eda399b9 | 1279 | case 'D': |
85c9ca5a | 1280 | flags &= ~FL_TREE; |
cd4ed46f | 1281 | flags |= FL_DF; |
eda399b9 | 1282 | break; |
04fd7a9f | 1283 | case 'd': |
4e6bd74c | 1284 | if (!strcmp(optarg, "forward")) |
04fd7a9f | 1285 | direction = MNT_ITER_FORWARD; |
4e6bd74c | 1286 | else if (!strcmp(optarg, "backward")) |
04fd7a9f KZ |
1287 | direction = MNT_ITER_BACKWARD; |
1288 | else | |
1289 | errx(EXIT_FAILURE, | |
8e350e48 | 1290 | _("unknown direction '%s'"), optarg); |
04fd7a9f KZ |
1291 | break; |
1292 | case 'e': | |
1293 | flags |= FL_EVALUATE; | |
1294 | break; | |
1295 | case 'h': | |
1296 | usage(stdout); | |
1297 | break; | |
1298 | case 'i': | |
1299 | flags |= FL_INVERT; | |
1300 | break; | |
9d67679b | 1301 | case 'f': |
04fd7a9f KZ |
1302 | flags |= FL_FIRSTONLY; |
1303 | break; | |
2f1ac44b | 1304 | case 'F': |
8c5eba3e | 1305 | tabfiles = append_tabfile(tabfiles, &ntabfiles, optarg); |
2f1ac44b | 1306 | break; |
9d67679b | 1307 | case 'u': |
32e5466a | 1308 | disable_columns_truncate(); |
9d67679b | 1309 | break; |
04fd7a9f | 1310 | case 'o': |
c57dca68 | 1311 | outarg = optarg; |
04fd7a9f KZ |
1312 | break; |
1313 | case 'O': | |
9d67679b | 1314 | set_match(COL_OPTIONS, optarg); |
04fd7a9f | 1315 | break; |
32e5466a | 1316 | case 'p': |
bdc3ed66 | 1317 | if (optarg) { |
c87638ad | 1318 | nactions = string_to_idarray(optarg, |
bdc3ed66 KZ |
1319 | actions, ARRAY_SIZE(actions), |
1320 | poll_action_name_to_id); | |
1321 | if (nactions < 0) | |
1322 | exit(EXIT_FAILURE); | |
1323 | } | |
32e5466a | 1324 | flags |= FL_POLL; |
85c9ca5a | 1325 | flags &= ~FL_TREE; |
32e5466a | 1326 | break; |
49e9fd3a | 1327 | case 'P': |
85c9ca5a KZ |
1328 | flags |= FL_EXPORT; |
1329 | flags &= ~FL_TREE; | |
49e9fd3a | 1330 | break; |
fdedb45e | 1331 | case 'm': /* mtab */ |
8c5eba3e | 1332 | tabtype = TABTYPE_MTAB; |
85c9ca5a | 1333 | flags &= ~FL_TREE; |
04fd7a9f | 1334 | break; |
fdedb45e | 1335 | case 's': /* fstab */ |
8c5eba3e | 1336 | tabtype = TABTYPE_FSTAB; |
85c9ca5a | 1337 | flags &= ~FL_TREE; |
04fd7a9f | 1338 | break; |
fdedb45e | 1339 | case 'k': /* kernel (mountinfo) */ |
8c5eba3e | 1340 | tabtype = TABTYPE_KERNEL; |
04fd7a9f KZ |
1341 | break; |
1342 | case 't': | |
9d67679b KZ |
1343 | set_match(COL_FSTYPE, optarg); |
1344 | break; | |
1345 | case 'r': | |
85c9ca5a KZ |
1346 | flags &= ~FL_TREE; /* disable the default */ |
1347 | flags |= FL_RAW; /* enable raw */ | |
9d67679b KZ |
1348 | break; |
1349 | case 'l': | |
85c9ca5a | 1350 | flags &= ~FL_TREE; /* disable the default */ |
9d67679b KZ |
1351 | break; |
1352 | case 'n': | |
85c9ca5a | 1353 | flags |= FL_NOHEADINGS; |
9d67679b | 1354 | break; |
c7fcc830 | 1355 | case 'N': |
c7fcc830 KZ |
1356 | tabtype = TABTYPE_KERNEL; |
1357 | tabfiles = append_pid_tabfile(tabfiles, &ntabfiles, | |
1358 | strtou32_or_err(optarg, | |
1359 | _("invalid TID argument"))); | |
1360 | break; | |
b2214e1f KZ |
1361 | case 'v': |
1362 | flags |= FL_NOFSROOT; | |
1363 | break; | |
049caefd KZ |
1364 | case 'R': |
1365 | flags |= FL_SUBMOUNTS; | |
1366 | break; | |
9d67679b | 1367 | case 'S': |
cd492186 | 1368 | set_source_match(optarg); |
9d67679b KZ |
1369 | flags |= FL_NOSWAPMATCH; |
1370 | break; | |
1371 | case 'T': | |
1372 | set_match(COL_TARGET, optarg); | |
1373 | flags |= FL_NOSWAPMATCH; | |
04fd7a9f | 1374 | break; |
bebdda30 KZ |
1375 | case 'U': |
1376 | flags |= FL_UNIQ; | |
1377 | break; | |
ad38fb9f | 1378 | case 'w': |
db41a429 | 1379 | timeout = strtos32_or_err(optarg, _("invalid timeout argument")); |
ad38fb9f | 1380 | break; |
5cc12c40 SK |
1381 | case 'V': |
1382 | printf(UTIL_LINUX_VERSION); | |
1383 | return EXIT_SUCCESS; | |
04fd7a9f KZ |
1384 | default: |
1385 | usage(stderr); | |
1386 | break; | |
1387 | } | |
1388 | } | |
1389 | ||
cd4ed46f | 1390 | if (!ncolumns && (flags & FL_DF)) { |
eda399b9 DR |
1391 | columns[ncolumns++] = COL_SOURCE; |
1392 | columns[ncolumns++] = COL_FSTYPE; | |
1393 | columns[ncolumns++] = COL_SIZE; | |
1394 | columns[ncolumns++] = COL_USED; | |
1395 | columns[ncolumns++] = COL_AVAIL; | |
1396 | columns[ncolumns++] = COL_USEPERC; | |
1397 | columns[ncolumns++] = COL_TARGET; | |
1398 | } | |
1399 | ||
32e5466a KZ |
1400 | /* default columns */ |
1401 | if (!ncolumns) { | |
1402 | if (flags & FL_POLL) | |
1403 | columns[ncolumns++] = COL_ACTION; | |
1404 | ||
1405 | columns[ncolumns++] = COL_TARGET; | |
1406 | columns[ncolumns++] = COL_SOURCE; | |
1407 | columns[ncolumns++] = COL_FSTYPE; | |
1408 | columns[ncolumns++] = COL_OPTIONS; | |
1409 | } | |
1410 | ||
c57dca68 MB |
1411 | if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns), |
1412 | &ncolumns, column_name_to_id) < 0) | |
1413 | return EXIT_FAILURE; | |
1414 | ||
8c5eba3e KZ |
1415 | if (!tabtype) |
1416 | tabtype = TABTYPE_KERNEL; | |
2f1ac44b | 1417 | |
f1622b57 KZ |
1418 | if ((flags & FL_POLL) && ntabfiles > 1) |
1419 | errx(EXIT_FAILURE, _("--poll accepts only one file, but more specified by --tab-file")); | |
b2214e1f | 1420 | |
9d67679b KZ |
1421 | if (optind < argc && (get_match(COL_SOURCE) || get_match(COL_TARGET))) |
1422 | errx(EXIT_FAILURE, _( | |
1423 | "options --target and --source can't be used together " | |
1424 | "with command line element that is not an option")); | |
1425 | ||
04fd7a9f | 1426 | if (optind < argc) |
cd492186 | 1427 | set_source_match(argv[optind++]); /* dev/tag/mountpoint/maj:min */ |
04fd7a9f | 1428 | if (optind < argc) |
9d67679b | 1429 | set_match(COL_TARGET, argv[optind++]); /* mountpoint */ |
04fd7a9f | 1430 | |
049caefd KZ |
1431 | if ((flags & FL_SUBMOUNTS) && is_listall_mode()) |
1432 | /* don't care about submounts if list all mounts */ | |
1433 | flags &= ~FL_SUBMOUNTS; | |
1434 | ||
31f67453 KZ |
1435 | if (!(flags & FL_SUBMOUNTS) && ((flags & FL_FIRSTONLY) |
1436 | || get_match(COL_TARGET) | |
1437 | || get_match(COL_SOURCE) | |
1438 | || get_match(COL_MAJMIN))) | |
85c9ca5a | 1439 | flags &= ~FL_TREE; |
fdedb45e | 1440 | |
ac808156 KZ |
1441 | if (!(flags & FL_NOSWAPMATCH) && |
1442 | !get_match(COL_TARGET) && get_match(COL_SOURCE)) { | |
1443 | /* | |
1444 | * Check if we can swap source and target, it's | |
1445 | * not possible if the source is LABEL=/UUID= | |
1446 | */ | |
1447 | const char *x = get_match(COL_SOURCE); | |
1448 | ||
090b5e84 KZ |
1449 | if (!strncmp(x, "LABEL=", 6) || !strncmp(x, "UUID=", 5) || |
1450 | !strncmp(x, "PARTLABEL=", 10) || !strncmp(x, "PARTUUID=", 9)) | |
ac808156 KZ |
1451 | flags |= FL_NOSWAPMATCH; |
1452 | } | |
1453 | ||
fdedb45e KZ |
1454 | /* |
1455 | * initialize libmount | |
1456 | */ | |
ac808156 | 1457 | mnt_init_debug(0); |
710ed55d | 1458 | scols_init_debug(0); |
ac808156 | 1459 | |
8c5eba3e | 1460 | tb = parse_tabfiles(tabfiles, ntabfiles, tabtype); |
04fd7a9f | 1461 | if (!tb) |
fdedb45e | 1462 | goto leave; |
04fd7a9f | 1463 | |
85c9ca5a KZ |
1464 | if ((flags & FL_TREE) && (ntabfiles > 1 || !tab_is_tree(tb))) |
1465 | flags &= ~FL_TREE; | |
2f1ac44b | 1466 | |
097e7f2f KZ |
1467 | if (!(flags & FL_NOCACHE)) { |
1468 | cache = mnt_new_cache(); | |
1469 | if (!cache) { | |
1470 | warn(_("failed to initialize libmount cache")); | |
1471 | goto leave; | |
1472 | } | |
1473 | mnt_table_set_cache(tb, cache); | |
fdedb45e | 1474 | } |
04fd7a9f | 1475 | |
bebdda30 KZ |
1476 | if (flags & FL_UNIQ) |
1477 | mnt_table_uniq_fs(tb, MNT_UNIQ_KEEPTREE, uniq_fs_target_cmp); | |
4bfd4bff | 1478 | |
fdedb45e | 1479 | /* |
218a3a94 | 1480 | * initialize output formatting (libsmartcols.h) |
fdedb45e | 1481 | */ |
0925a9dd | 1482 | table = scols_new_table(); |
218a3a94 | 1483 | if (!table) { |
fdedb45e KZ |
1484 | warn(_("failed to initialize output table")); |
1485 | goto leave; | |
1486 | } | |
0925a9dd KZ |
1487 | scols_table_enable_raw(table, !!(flags & FL_RAW)); |
1488 | scols_table_enable_export(table, !!(flags & FL_EXPORT)); | |
1489 | scols_table_enable_ascii(table, !!(flags & FL_ASCII)); | |
1490 | scols_table_enable_noheadings(table, !!(flags & FL_NOHEADINGS)); | |
ac808156 | 1491 | |
fdedb45e | 1492 | for (i = 0; i < ncolumns; i++) { |
32e5466a KZ |
1493 | int fl = get_column_flags(i); |
1494 | int id = get_column_id(i); | |
1495 | ||
85c9ca5a | 1496 | if (!(flags & FL_TREE)) |
218a3a94 | 1497 | fl &= ~SCOLS_FL_TREE; |
04fd7a9f | 1498 | |
32e5466a | 1499 | if (!(flags & FL_POLL) && is_tabdiff_column(id)) { |
77a1c5f7 | 1500 | warnx(_("%s column is requested, but --poll " |
32e5466a KZ |
1501 | "is not enabled"), get_column_name(i)); |
1502 | goto leave; | |
1503 | } | |
218a3a94 | 1504 | if (!scols_table_new_column(table, get_column_name(i), |
fdedb45e KZ |
1505 | get_column_whint(i), fl)) { |
1506 | warn(_("failed to initialize output column")); | |
1507 | goto leave; | |
9d67679b | 1508 | } |
fdedb45e | 1509 | } |
9d67679b | 1510 | |
fdedb45e KZ |
1511 | /* |
1512 | * Fill in data to the output table | |
1513 | */ | |
275c9a48 | 1514 | if (flags & FL_POLL) { |
8c5eba3e | 1515 | /* poll mode (accept the first tabfile only) */ |
218a3a94 | 1516 | rc = poll_table(tb, tabfiles ? *tabfiles : _PATH_PROC_MOUNTINFO, timeout, table, direction); |
32e5466a | 1517 | |
85c9ca5a | 1518 | } else if ((flags & FL_TREE) && !(flags & FL_SUBMOUNTS)) { |
049caefd | 1519 | /* whole tree */ |
218a3a94 | 1520 | rc = create_treenode(table, tb, NULL, NULL); |
31f67453 | 1521 | } else { |
049caefd | 1522 | /* whole lits of sub-tree */ |
218a3a94 | 1523 | rc = add_matching_lines(tb, table, direction); |
fdedb45e | 1524 | |
3e373967 KZ |
1525 | if (rc != 0 |
1526 | && tabtype == TABTYPE_KERNEL | |
1527 | && (flags & FL_NOSWAPMATCH) | |
1528 | && get_match(COL_TARGET)) { | |
1529 | /* | |
1530 | * Found nothing, maybe the --target is regular file, | |
1531 | * try it again with extra functionality for target | |
1532 | * match | |
1533 | */ | |
1534 | enable_extra_target_match(); | |
218a3a94 | 1535 | rc = add_matching_lines(tb, table, direction); |
3e373967 KZ |
1536 | } |
1537 | } | |
1538 | ||
fdedb45e | 1539 | /* |
32e5466a | 1540 | * Print the output table for non-poll modes |
fdedb45e | 1541 | */ |
32e5466a | 1542 | if (!rc && !(flags & FL_POLL)) |
218a3a94 | 1543 | scols_print_table(table); |
fdedb45e | 1544 | leave: |
218a3a94 | 1545 | scols_unref_table(table); |
fdedb45e | 1546 | |
50fccba1 | 1547 | mnt_unref_table(tb); |
6195f9e6 | 1548 | mnt_unref_cache(cache); |
50fccba1 | 1549 | |
8c5eba3e | 1550 | free(tabfiles); |
e346233e KZ |
1551 | #ifdef HAVE_LIBUDEV |
1552 | udev_unref(udev); | |
1553 | #endif | |
049caefd | 1554 | return rc ? EXIT_FAILURE : EXIT_SUCCESS; |
04fd7a9f | 1555 | } |