find_mount_point.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. #include <mntent.h>
  11. /*
  12. * Given a block device, find the mount table entry if that block device
  13. * is mounted.
  14. *
  15. * Given any other file (or directory), find the mount table entry for its
  16. * filesystem.
  17. */
  18. struct mntent* FAST_FUNC find_mount_point(const char *name, int subdir_too)
  19. {
  20. struct stat s;
  21. FILE *mtab_fp;
  22. struct mntent *mountEntry;
  23. dev_t devno_of_name;
  24. bool block_dev;
  25. if (stat(name, &s) != 0)
  26. return NULL;
  27. devno_of_name = s.st_dev;
  28. block_dev = 0;
  29. if (S_ISBLK(s.st_mode)) {
  30. devno_of_name = s.st_rdev;
  31. block_dev = 1;
  32. }
  33. mtab_fp = setmntent(bb_path_mtab_file, "r");
  34. if (!mtab_fp)
  35. return NULL;
  36. while ((mountEntry = getmntent(mtab_fp)) != NULL) {
  37. /* rootfs mount in Linux 2.6 exists always,
  38. * and it makes sense to always ignore it.
  39. * Otherwise people can't reference their "real" root! */
  40. if (strcmp(mountEntry->mnt_fsname, "rootfs") == 0)
  41. continue;
  42. if (strcmp(name, mountEntry->mnt_dir) == 0
  43. || strcmp(name, mountEntry->mnt_fsname) == 0
  44. ) { /* String match. */
  45. break;
  46. }
  47. if (!(subdir_too || block_dev))
  48. continue;
  49. /* Is device's dev_t == name's dev_t? */
  50. if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == devno_of_name)
  51. break;
  52. /* Match the directory's mount point. */
  53. if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == devno_of_name)
  54. break;
  55. }
  56. endmntent(mtab_fp);
  57. return mountEntry;
  58. }