aboutsummaryrefslogtreecommitdiff
path: root/toys/other/mountpoint.c
blob: a97c600a84360b924547317d9035163768fd24ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* mountpoint.c - Check if a directory is a mountpoint.
 *
 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>

USE_MOUNTPOINT(NEWTOY(mountpoint, "<1qdx[-dx]", TOYFLAG_BIN))

config MOUNTPOINT
  bool "mountpoint"
  default y
  help
    usage: mountpoint [-qd] DIR
           mountpoint [-qx] DEVICE

    Check whether the directory or device is a mountpoint.

    -q	Be quiet, return zero if directory is a mountpoint
    -d	Print major/minor device number of the directory
    -x	Print major/minor device number of the block device
*/

#define FOR_mountpoint
#include "toys.h"

static void die(char *gripe)
{
  if (!(toys.optflags & FLAG_q)) printf("%s: not a %s\n", *toys.optargs, gripe);

  toys.exitval++;
  xexit();
}

void mountpoint_main(void)
{
  struct stat st1, st2;
  char *arg = *toys.optargs;
  int quiet = toys.optflags & FLAG_q;

  if (lstat(arg, &st1)) perror_exit_raw(arg);

  if (toys.optflags & FLAG_x) {
    if (S_ISBLK(st1.st_mode)) {
      if (!quiet)
        printf("%u:%u\n", dev_major(st1.st_rdev), dev_minor(st1.st_rdev));

      return;
    }
    die("block device");
  }

  // TODO: Ignore the fact a file can be a mountpoint for --bind mounts.
  if (!S_ISDIR(st1.st_mode)) die("directory");

  arg = xmprintf("%s/..", arg);
  xstat(arg, &st2);
  if (CFG_TOYBOX_FREE) free(arg);

  // If the device is different, it's a mount point. If the device _and_
  // inode are the same, it's probably "/". This misses --bind mounts from
  // elsewhere in the same filesystem, but so does the other one and in the
  // absence of a spec I guess that's the expected behavior?
  toys.exitval = !(st1.st_dev != st2.st_dev || st1.st_ino == st2.st_ino);
  if (toys.optflags & FLAG_d)
    printf("%u:%u\n", dev_major(st1.st_dev), dev_minor(st1.st_dev));
  else if (!quiet)
    printf("%s is %sa mountpoint\n", *toys.optargs, toys.exitval ? "not " : "");
}