3
0

mtab.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 source tree.
  8. */
  9. #include <mntent.h>
  10. #include "libbb.h"
  11. #if ENABLE_FEATURE_MTAB_SUPPORT
  12. void FAST_FUNC erase_mtab(const char *name)
  13. {
  14. struct mntent *entries;
  15. int i, count;
  16. FILE *mountTable;
  17. struct mntent *m;
  18. mountTable = setmntent(bb_path_mtab_file, "r");
  19. /* Bummer. Fall back on trying the /proc filesystem */
  20. if (!mountTable) mountTable = setmntent("/proc/mounts", "r");
  21. if (!mountTable) {
  22. bb_simple_perror_msg(bb_path_mtab_file);
  23. return;
  24. }
  25. entries = NULL;
  26. count = 0;
  27. while ((m = getmntent(mountTable)) != 0) {
  28. entries = xrealloc_vector(entries, 3, count);
  29. entries[count].mnt_fsname = xstrdup(m->mnt_fsname);
  30. entries[count].mnt_dir = xstrdup(m->mnt_dir);
  31. entries[count].mnt_type = xstrdup(m->mnt_type);
  32. entries[count].mnt_opts = xstrdup(m->mnt_opts);
  33. entries[count].mnt_freq = m->mnt_freq;
  34. entries[count].mnt_passno = m->mnt_passno;
  35. count++;
  36. }
  37. endmntent(mountTable);
  38. //TODO: make update atomic
  39. mountTable = setmntent(bb_path_mtab_file, "w");
  40. if (mountTable) {
  41. for (i = 0; i < count; i++) {
  42. if (strcmp(entries[i].mnt_fsname, name) != 0
  43. && strcmp(entries[i].mnt_dir, name) != 0)
  44. addmntent(mountTable, &entries[i]);
  45. }
  46. endmntent(mountTable);
  47. } else if (errno != EROFS)
  48. bb_simple_perror_msg(bb_path_mtab_file);
  49. }
  50. #endif