toybox/toys/other/mountpoint.c
<<
>>
Prefs
   1/* mountpoint.c - Check if a directory is a mountpoint.
   2 *
   3 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
   4
   5USE_MOUNTPOINT(NEWTOY(mountpoint, "<1qdx[-dx]", TOYFLAG_BIN))
   6
   7config MOUNTPOINT
   8  bool "mountpoint"
   9  default y
  10  help
  11    usage: mountpoint [-qd] DIR
  12           mountpoint [-qx] DEVICE
  13
  14    Check whether the directory or device is a mountpoint.
  15
  16    -q  Be quiet, return zero if directory is a mountpoint
  17    -d  Print major/minor device number of the directory
  18    -x  Print major/minor device number of the block device
  19*/
  20
  21#define FOR_mountpoint
  22#include "toys.h"
  23
  24static void die(char *gripe)
  25{
  26  if (!FLAG(q)) printf("%s: not a %s\n", *toys.optargs, gripe);
  27
  28  toys.exitval++;
  29  xexit();
  30}
  31
  32void mountpoint_main(void)
  33{
  34  struct stat st1, st2;
  35  char *arg = *toys.optargs;
  36
  37  if (lstat(arg, &st1)) perror_exit_raw(arg);
  38
  39  if (FLAG(x)) {
  40    if (!S_ISBLK(st1.st_mode)) die("block device");
  41    if (!FLAG(q))
  42      printf("%u:%u\n", dev_major(st1.st_rdev), dev_minor(st1.st_rdev));
  43
  44    return;
  45  }
  46
  47  // TODO: a file can be a mountpoint for --bind mounts.
  48  if (!S_ISDIR(st1.st_mode)) die("directory");
  49
  50  arg = xmprintf("%s/..", arg);
  51  xstat(arg, &st2);
  52  free(arg);
  53
  54  // If the device is different, it's a mount point. If the device _and_
  55  // inode are the same, it's probably "/". This misses --bind mounts from
  56  // elsewhere in the same filesystem, but so does the other one and in the
  57  // absence of a spec I guess that's the expected behavior?
  58  toys.exitval = !same_file(&st1, &st2);
  59  if (FLAG(d)) printf("%u:%u\n", dev_major(st1.st_dev), dev_minor(st1.st_dev));
  60  else if (!FLAG(q))
  61    printf("%s is %sa mountpoint\n", *toys.optargs, toys.exitval ? "not " : "");
  62}
  63