mountpoint.c 1.8 KB

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