]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/fstype.c
150afe65e2baa9a61bcf2eb096c324421f51efe4
[bacula/bacula] / bacula / src / tools / fstype.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2004-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  * Program for determining file system type
22  *
23  *   Written by Preben 'Peppe' Guldberg, December MMIV
24  */
25
26 #include "bacula.h"
27 #include "findlib/find.h"
28
29 static void usage()
30 {
31    fprintf(stderr, _(
32 "\n"
33 "Usage: fstype [-v] path ...\n"
34 "\n"
35 "       Print the file system type for each file/directory argument given.\n"
36 "       The following options are supported:\n"
37 "\n"
38 "       -l     print all file system types in mtab.\n"
39 "       -m     print full entries in mtab.\n"
40 "       -v     print both path and file system type of each argument.\n"
41 "       -?     print this message.\n"
42 "\n"));
43
44    exit(1);
45 }
46
47 struct mtab_item {
48    rblink link;
49    uint64_t dev;
50    char fstype[1];
51 };
52
53 /* Compare two device types */
54 static int compare_mtab_items(void *item1, void *item2)
55 {
56    mtab_item *mtab1, *mtab2;
57    mtab1 = (mtab_item *)item1;
58    mtab2 = (mtab_item *)item2;
59    if (mtab1->dev < mtab2->dev) return -1;
60    if (mtab1->dev > mtab2->dev) return 1;
61    return 0;
62 }
63
64 void print_mtab_item(void *user_ctx, struct stat *st, const char *fstype,
65                       const char *mountpoint, const char *mntopts,
66                       const char *fsname)
67 {
68    fprintf(stderr, "dev=%p fstype=%s mountpoint=%s mntopts=%s\n",
69       ((void *)st->st_dev), fstype, mountpoint, mntopts);
70 }
71
72 void add_mtab_item(void *user_ctx, struct stat *st, const char *fstype,
73                       const char *mountpoint, const char *mntopts,
74                       const char *fsname)
75 {
76    rblist *mtab_list = (rblist *)user_ctx;
77    mtab_item *item, *ritem;
78    int len = strlen(fstype) + 1;
79    
80    item = (mtab_item *)malloc(sizeof(mtab_item) + len);
81    item->dev = (uint64_t)st->st_dev;
82    bstrncpy(item->fstype, fstype, len);
83    //fprintf(stderr, "Add dev=%lx fstype=%s\n", item->dev, item->fstype);
84    ritem = (mtab_item *)mtab_list->insert((void *)item, compare_mtab_items);
85    if (ritem != item) {
86       fprintf(stderr, "Problem!! Returned item not equal added item\n");
87    }
88    //fprintf(stderr, "dev=%p fstype=%s mountpoint=%s mntopts=%s\n",
89    //   ((void *)st->st_dev), fstype, mountpoint, mntopts);
90 }
91
92
93 int main (int argc, char *const *argv)
94 {
95    char fs[1000];
96    bool verbose = false;
97    bool list = false;
98    bool mtab = false;
99    int status = 0;
100    int ch, i;
101
102    setlocale(LC_ALL, "");
103    bindtextdomain("bacula", LOCALEDIR);
104    textdomain("bacula");
105
106    while ((ch = getopt(argc, argv, "lmv?")) != -1) {
107       switch (ch) {
108          case 'l':
109             list = true;
110             break;
111          case 'm':
112             mtab = true; /* list mtab */
113             break;
114          case 'v':
115             verbose = true;
116             break;
117          case '?':
118          default:
119             usage();
120
121       }
122    }
123    argc -= optind;
124    argv += optind;
125
126
127    OSDependentInit();
128
129    if (mtab) {
130       read_mtab(print_mtab_item, NULL);
131       status = 1;
132       goto get_out;
133    }
134    if (list) {
135       rblist *mtab_list;
136       mtab_item *item;
137       mtab_list = New(rblist());
138       read_mtab(add_mtab_item, mtab_list);
139       fprintf(stderr, "Size of mtab=%d\n", mtab_list->size());
140       foreach_rblist(item, mtab_list) {
141          fprintf(stderr, "Found dev=%lx fstype=%s\n", item->dev, item->fstype);
142       }
143       delete mtab_list;
144       goto get_out;
145    }
146
147    if (argc < 1) {
148       usage();
149    }
150    for (i = 0; i < argc; --argc, ++argv) {
151       FF_PKT ff_pkt;
152       memset(&ff_pkt, 0, sizeof(ff_pkt));
153       ff_pkt.fname = ff_pkt.link = *argv;
154       if (lstat(ff_pkt.fname, &ff_pkt.statp) != 0) {
155          fprintf(stderr, "lstat of %s failed.\n", ff_pkt.fname);
156          status = 1;
157          break;
158       }
159       if (fstype(&ff_pkt, fs, sizeof(fs))) {
160          if (verbose) {
161             printf("%s: %s\n", *argv, fs);
162          } else {
163             puts(fs);
164          }
165       } else {
166          fprintf(stderr, _("%s: unknown file system type\n"), *argv);
167          status = 1;
168       }
169    }
170
171 get_out:
172    exit(status);
173 }