]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/swapon-common.c
Merge branch 'non-canonical-completion' of https://github.com/kevinoid/util-linux
[thirdparty/util-linux.git] / sys-utils / swapon-common.c
1
2 #include "c.h"
3 #include "nls.h"
4 #include "xalloc.h"
5
6 #include "swapon-common.h"
7
8 /*
9 * content of /proc/swaps and /etc/fstab
10 */
11 static struct libmnt_table *swaps, *fstab;
12
13 struct libmnt_cache *mntcache;
14
15 static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
16 const char *filename, int line)
17 {
18 if (filename)
19 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
20 return 1;
21 }
22
23 struct libmnt_table *get_fstab(void)
24 {
25 if (!fstab) {
26 fstab = mnt_new_table();
27 if (!fstab)
28 return NULL;
29 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
30 mnt_table_set_cache(fstab, mntcache);
31 if (mnt_table_parse_fstab(fstab, NULL) != 0)
32 return NULL;
33 }
34
35 return fstab;
36 }
37
38 struct libmnt_table *get_swaps(void)
39 {
40 if (!swaps) {
41 swaps = mnt_new_table();
42 if (!swaps)
43 return NULL;
44 mnt_table_set_cache(swaps, mntcache);
45 mnt_table_set_parser_errcb(swaps, table_parser_errcb);
46 if (mnt_table_parse_swaps(swaps, NULL) != 0)
47 return NULL;
48 }
49
50 return swaps;
51 }
52
53 void free_tables(void)
54 {
55 mnt_unref_table(swaps);
56 mnt_unref_table(fstab);
57 }
58
59 int match_swap(struct libmnt_fs *fs, void *data __attribute__((unused)))
60 {
61 return fs && mnt_fs_is_swaparea(fs);
62 }
63
64 int is_active_swap(const char *filename)
65 {
66 struct libmnt_table *st = get_swaps();
67 return st && mnt_table_find_source(st, filename, MNT_ITER_BACKWARD);
68 }
69
70
71 int cannot_find(const char *special)
72 {
73 warnx(_("cannot find the device for %s"), special);
74 return -1;
75 }
76
77 /*
78 * Lists with -L and -U option
79 */
80 static const char **llist;
81 static size_t llct;
82 static const char **ulist;
83 static size_t ulct;
84
85
86 void add_label(const char *label)
87 {
88 llist = xrealloc(llist, (++llct) * sizeof(char *));
89 llist[llct - 1] = label;
90 }
91
92 const char *get_label(size_t i)
93 {
94 return i < llct ? llist[i] : NULL;
95 }
96
97 size_t numof_labels(void)
98 {
99 return llct;
100 }
101
102 void add_uuid(const char *uuid)
103 {
104 ulist = xrealloc(ulist, (++ulct) * sizeof(char *));
105 ulist[ulct - 1] = uuid;
106 }
107
108 const char *get_uuid(size_t i)
109 {
110 return i < ulct ? ulist[i] : NULL;
111 }
112
113 size_t numof_uuids(void)
114 {
115 return ulct;
116 }
117