]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add mountpoint(1) implementation to samples/
authorKarel Zak <kzak@redhat.com>
Fri, 1 Apr 2011 12:17:51 +0000 (14:17 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 1 Apr 2011 12:17:51 +0000 (14:17 +0200)
shlibs/mount/samples/.gitignore
shlibs/mount/samples/Makefile.am
shlibs/mount/samples/mountpoint.c [new file with mode: 0644]

index fde64773937cff389e9973a7910b416798bfd5b4..6008ee3212b9d7938f495fb60a9dcf8524d61aaa 100644 (file)
@@ -1 +1,2 @@
 mount
+mountpoint
index b0c655f1ac191d22174c41ebaf5f4b9f66f49d97..ee13d8b777bf901b3e4fd388b8134f3a87301e13 100644 (file)
@@ -3,5 +3,5 @@ include $(top_srcdir)/config/include-Makefile.am
 AM_CPPFLAGS += -I$(ul_libmount_incdir)
 AM_LDFLAGS += $(ul_libmount_la)
 
-noinst_PROGRAMS = mount
+noinst_PROGRAMS = mount mountpoint
 
diff --git a/shlibs/mount/samples/mountpoint.c b/shlibs/mount/samples/mountpoint.c
new file mode 100644 (file)
index 0000000..92d2830
--- /dev/null
@@ -0,0 +1,174 @@
+/*
+ * mountpoint(1) - see if a directory is a mountpoint
+ *
+ * This is libmount based reimplementation of the mountpoit(1)
+ * from sysvinit project.
+ *
+ *
+ * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
+ * Written by Karel Zak <kzak@redhat.com>
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it would 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 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
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+
+#include <libmount.h>
+
+#include "nls.h"
+#include "xalloc.h"
+#include "c.h"
+
+static int quiet;
+
+static char *dir_to_device(const char *spec)
+{
+       struct libmnt_table *tb = mnt_new_table_from_file("/proc/self/mountinfo");
+       struct libmnt_fs *fs;
+       char *res = NULL;
+
+       if (!tb)
+               return NULL;
+
+       fs = mnt_table_find_target(tb, spec, MNT_ITER_BACKWARD);
+       if (fs && mnt_fs_get_target(fs))
+               res = xstrdup(mnt_fs_get_source(fs));
+
+       mnt_free_table(tb);
+       return res;
+}
+
+static int print_devno(const char *devname, struct stat *st)
+{
+       struct stat stbuf;
+
+       if (!st && stat(devname, &stbuf) == 0)
+               st = &stbuf;
+       if (!st)
+               return -1;
+       if (!S_ISBLK(st->st_mode)) {
+               if (!quiet)
+                       warnx(_("%s: not a block device"), devname);
+               return -1;
+       }
+       printf("%u:%u\n", major(st->st_rdev), minor(st->st_rdev));
+       return 0;
+}
+
+static void __attribute__((__noreturn__)) usage(FILE *out)
+{
+       fprintf(out, _("Usage:\n"
+               " %1$s [-qd] /path/to/directory\n"
+               " %1$s -x /dev/device\n"),
+               program_invocation_short_name);
+
+       fprintf(out, _(
+       "\nOptions:\n"
+       " -q, --quiet        quiet mode - don't print anything\n"
+       " -d, --fs-devno     print maj:min device number of the filesystem\n"
+       " -x, --devno        print maj:min device number of the block device\n"
+       " -h, --help         this help\n"
+       ));
+
+       fprintf(out, _("\nFor more information see mountpoint(1).\n"));
+
+       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+int main(int argc, char **argv)
+{
+       int c, fs_devno = 0, dev_devno = 0, rc = 0;
+       char *spec;
+       struct stat st;
+
+       static const struct option longopts[] = {
+               { "quiet", 0, 0, 'q' },
+               { "fs-devno", 0, 0, 'd' },
+               { "devno", 0, 0, 'x' },
+               { "help", 0, 0, 'h' },
+               { NULL, 0, 0, 0 }
+       };
+
+       setlocale(LC_ALL, "");
+       bindtextdomain(PACKAGE, LOCALEDIR);
+       textdomain(PACKAGE);
+
+       mnt_init_debug(0);
+
+       while ((c = getopt_long(argc, argv, "qdxh", longopts, NULL)) != -1) {
+
+               switch(c) {
+               case 'q':
+                       quiet = 1;
+                       break;
+               case 'd':
+                       fs_devno = 1;
+                       break;
+               case 'x':
+                       dev_devno = 1;
+                       break;
+               case 'h':
+                       usage(stdout);
+                       break;
+               default:
+                       usage(stderr);
+                       break;
+               }
+       }
+
+       if (optind + 1 != argc)
+               usage(stderr);
+
+       spec = argv[optind++];
+
+       if (stat(spec, &st)) {
+               if (!quiet)
+                       err(EXIT_FAILURE, "%s", spec);
+               return EXIT_FAILURE;
+       }
+       if (dev_devno)
+               rc = print_devno(spec, &st);
+       else {
+               char *src;
+
+               if (!S_ISDIR(st.st_mode)) {
+                       if (!quiet)
+                               errx(EXIT_FAILURE, _("%s: not a directory"), spec);
+                       return EXIT_FAILURE;
+               }
+               src = dir_to_device(spec);
+               if (!src) {
+                       if (!quiet)
+                               printf(_("%s is not a mountpoint\n"), spec);
+                       return EXIT_FAILURE;
+               }
+               if (fs_devno)
+                       rc = print_devno(src, NULL);
+               else if (!quiet)
+                       printf(_("%s is a mountpoint\n"), spec);
+               free(src);
+       }
+
+       return rc ? EXIT_FAILURE : EXIT_SUCCESS;
+}