mtab.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <mntent.h>
  27. #include "libbb.h"
  28. #define MTAB_MAX_ENTRIES 40
  29. static const int MS_RDONLY = 1; /* Mount read-only. */
  30. void erase_mtab(const char *name)
  31. {
  32. struct mntent entries[MTAB_MAX_ENTRIES];
  33. int count = 0;
  34. FILE *mountTable = setmntent(bb_path_mtab_file, "r");
  35. struct mntent *m;
  36. /* Check if reading the mtab file failed */
  37. if (mountTable == 0
  38. /* Bummer. fall back on trying the /proc filesystem */
  39. && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
  40. bb_perror_msg(bb_path_mtab_file);
  41. return;
  42. }
  43. while (((m = getmntent(mountTable)) != 0) && (count < MTAB_MAX_ENTRIES))
  44. {
  45. entries[count].mnt_fsname = strdup(m->mnt_fsname);
  46. entries[count].mnt_dir = strdup(m->mnt_dir);
  47. entries[count].mnt_type = strdup(m->mnt_type);
  48. entries[count].mnt_opts = strdup(m->mnt_opts);
  49. entries[count].mnt_freq = m->mnt_freq;
  50. entries[count].mnt_passno = m->mnt_passno;
  51. count++;
  52. }
  53. endmntent(mountTable);
  54. if ((mountTable = setmntent(bb_path_mtab_file, "w"))) {
  55. int i;
  56. for (i = 0; i < count; i++) {
  57. int result = (strcmp(entries[i].mnt_fsname, name) == 0
  58. || strcmp(entries[i].mnt_dir, name) == 0);
  59. if (result)
  60. continue;
  61. else
  62. addmntent(mountTable, &entries[i]);
  63. }
  64. endmntent(mountTable);
  65. } else if (errno != EROFS)
  66. bb_perror_msg(bb_path_mtab_file);
  67. }
  68. void write_mtab(char *blockDevice, char *directory,
  69. char *filesystemType, long flags, char *string_flags)
  70. {
  71. FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
  72. struct mntent m;
  73. if (mountTable == 0) {
  74. bb_perror_msg(bb_path_mtab_file);
  75. return;
  76. }
  77. if (mountTable) {
  78. int length = strlen(directory);
  79. if (length > 1 && directory[length - 1] == '/')
  80. directory[length - 1] = '\0';
  81. if (filesystemType == 0) {
  82. struct mntent *p = find_mount_point(blockDevice, "/proc/mounts");
  83. if (p && p->mnt_type)
  84. filesystemType = p->mnt_type;
  85. }
  86. m.mnt_fsname = blockDevice;
  87. m.mnt_dir = directory;
  88. m.mnt_type = filesystemType ? filesystemType : "default";
  89. if (*string_flags) {
  90. m.mnt_opts = string_flags;
  91. } else {
  92. if ((flags | MS_RDONLY) == flags)
  93. m.mnt_opts = "ro";
  94. else
  95. m.mnt_opts = "rw";
  96. }
  97. m.mnt_freq = 0;
  98. m.mnt_passno = 0;
  99. addmntent(mountTable, &m);
  100. endmntent(mountTable);
  101. }
  102. }