]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/tools/fstype.c
Change copyright as per agreement with FSFE + update copyright year
[bacula/bacula] / bacula / src / tools / fstype.c
index 32abe6fa40b644b8bcf41fee3002e5e33723803b..d61e0f28b828c1a57d3b96949240ad0027f59b44 100644 (file)
@@ -1,30 +1,25 @@
 /*
- * Program for determining file system type
- *
- *   Written by Preben 'Peppe' Guldberg, December MMIV
- *
- *   Version $Id$
- *
- */
+   Bacula(R) - The Network Backup Solution
 
-/*
-   Copyright (C) 2004 Kern Sibbald
+   Copyright (C) 2000-2016 Kern Sibbald
 
-   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.
+   The original author of Bacula is Kern Sibbald, with contributions
+   from many others, a complete list can be found in the file AUTHORS.
 
-   This program is distributed in the hope that it will 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 may use this file and others of this release according to the
+   license defined in the LICENSE file, which includes the Affero General
+   Public License, v3.0 ("AGPLv3") and some additional permissions and
+   terms pursuant to its AGPLv3 Section 7.
 
-   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.
+   This notice must be preserved when any source code is 
+   conveyed and/or propagated.
 
+   Bacula(R) is a registered trademark of Kern Sibbald.
+*/
+/*
+ * Program for determining file system type
+ *
+ *   Written by Preben 'Peppe' Guldberg, December MMIV
  */
 
 #include "bacula.h"
@@ -34,57 +29,144 @@ static void usage()
 {
    fprintf(stderr, _(
 "\n"
-"Usage: fstype [-d debug_level] path ...\n"
+"Usage: fstype [-v] path ...\n"
 "\n"
-"       Print the file system type a given file/directory is on.\n"
+"       Print the file system type for each file/directory argument given.\n"
 "       The following options are supported:\n"
 "\n"
-"       -v     print both path and file system type.\n"
+"       -l     print all file system types in mtab.\n"
+"       -m     print full entries in mtab.\n"
+"       -v     print both path and file system type of each argument.\n"
 "       -?     print this message.\n"
 "\n"));
 
    exit(1);
 }
 
+struct mtab_item {
+   rblink link;
+   uint64_t dev;
+   char fstype[1];
+};
+
+/* Compare two device types */
+static int compare_mtab_items(void *item1, void *item2)
+{
+   mtab_item *mtab1, *mtab2;
+   mtab1 = (mtab_item *)item1;
+   mtab2 = (mtab_item *)item2;
+   if (mtab1->dev < mtab2->dev) return -1;
+   if (mtab1->dev > mtab2->dev) return 1;
+   return 0;
+}
+
+void print_mtab_item(void *user_ctx, struct stat *st, const char *fstype,
+                      const char *mountpoint, const char *mntopts,
+                      const char *fsname)
+{
+   fprintf(stderr, "dev=%p fstype=%s mountpoint=%s mntopts=%s\n",
+      ((void *)st->st_dev), fstype, mountpoint, mntopts);
+}
 
-int
-main (int argc, char *const *argv)
+void add_mtab_item(void *user_ctx, struct stat *st, const char *fstype,
+                      const char *mountpoint, const char *mntopts,
+                      const char *fsname)
 {
-   int verbose = 0;
+   rblist *mtab_list = (rblist *)user_ctx;
+   mtab_item *item, *ritem;
+   int len = strlen(fstype) + 1;
+   
+   item = (mtab_item *)malloc(sizeof(mtab_item) + len);
+   item->dev = (uint64_t)st->st_dev;
+   bstrncpy(item->fstype, fstype, len);
+   //fprintf(stderr, "Add dev=%lx fstype=%s\n", item->dev, item->fstype);
+   ritem = (mtab_item *)mtab_list->insert((void *)item, compare_mtab_items);
+   if (ritem != item) {
+      fprintf(stderr, "Problem!! Returned item not equal added item\n");
+   }
+   //fprintf(stderr, "dev=%p fstype=%s mountpoint=%s mntopts=%s\n",
+   //   ((void *)st->st_dev), fstype, mountpoint, mntopts);
+}
+
+
+int main (int argc, char *const *argv)
+{
+   char fs[1000];
+   bool verbose = false;
+   bool list = false;
+   bool mtab = false;
    int status = 0;
    int ch, i;
-   char fs[1000];
 
-   while ((ch = getopt(argc, argv, "v?")) != -1) {
+   setlocale(LC_ALL, "");
+   bindtextdomain("bacula", LOCALEDIR);
+   textdomain("bacula");
+
+   while ((ch = getopt(argc, argv, "lmv?")) != -1) {
       switch (ch) {
+         case 'l':
+            list = true;
+            break;
+         case 'm':
+            mtab = true; /* list mtab */
+            break;
          case 'v':
-           verbose = 1;
-           break;
+            verbose = true;
+            break;
          case '?':
-        default:
-           usage();
+         default:
+            usage();
 
       }
    }
    argc -= optind;
    argv += optind;
 
+
+   OSDependentInit();
+
+   if (mtab) {
+      read_mtab(print_mtab_item, NULL);
+      status = 1;
+      goto get_out;
+   }
+   if (list) {
+      rblist *mtab_list;
+      mtab_item *item;
+      mtab_list = New(rblist());
+      read_mtab(add_mtab_item, mtab_list);
+      fprintf(stderr, "Size of mtab=%d\n", mtab_list->size());
+      foreach_rblist(item, mtab_list) {
+         fprintf(stderr, "Found dev=%lx fstype=%s\n", item->dev, item->fstype);
+      }
+      delete mtab_list;
+      goto get_out;
+   }
+
    if (argc < 1) {
       usage();
    }
-
    for (i = 0; i < argc; --argc, ++argv) {
-      if (fstype(*argv, fs, sizeof(fs))) {
-        if (verbose) {
+      FF_PKT ff_pkt;
+      memset(&ff_pkt, 0, sizeof(ff_pkt));
+      ff_pkt.fname = ff_pkt.link = *argv;
+      if (lstat(ff_pkt.fname, &ff_pkt.statp) != 0) {
+         fprintf(stderr, "lstat of %s failed.\n", ff_pkt.fname);
+         status = 1;
+         break;
+      }
+      if (fstype(&ff_pkt, fs, sizeof(fs))) {
+         if (verbose) {
             printf("%s: %s\n", *argv, fs);
-        } else {
-           puts(fs);
-        }
+         } else {
+            puts(fs);
+         }
       } else {
-         fprintf(stderr, "%s: unknown\n", *argv);
-        status = 1;
+         fprintf(stderr, _("%s: unknown file system type\n"), *argv);
+         status = 1;
       }
    }
 
+get_out:
    exit(status);
 }