3
0

find_mount_point.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 *find_mount_point(const char *name, const char *table)
  19. {
  20. struct stat s;
  21. dev_t mountDevice;
  22. FILE *mountTable;
  23. struct mntent *mountEntry;
  24. if (stat(name, &s) != 0)
  25. return 0;
  26. if ((s.st_mode & S_IFMT) == S_IFBLK)
  27. mountDevice = s.st_rdev;
  28. else
  29. mountDevice = s.st_dev;
  30. mountTable = setmntent(table ? table : bb_path_mtab_file, "r");
  31. if (!mountTable)
  32. return 0;
  33. while ((mountEntry = getmntent(mountTable)) != 0) {
  34. if (strcmp(name, mountEntry->mnt_dir) == 0
  35. || strcmp(name, mountEntry->mnt_fsname) == 0
  36. ) { /* String match. */
  37. break;
  38. }
  39. if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
  40. break;
  41. if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
  42. break;
  43. }
  44. endmntent(mountTable);
  45. return mountEntry;
  46. }