blob: c521b1e057eed1ce8b2302dc3d91be0d47730c0c [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00002/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.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
Eric Andersenbc341901999-11-06 04:56:00 +000022#include <stdlib.h>
23#include <unistd.h>
24#include <errno.h>
25#include <string.h>
26#include <stdio.h>
27#include <mntent.h>
Eric Andersenc4cef5a2001-04-01 16:01:11 +000028#include "libbb.h"
Eric Andersenbc341901999-11-06 04:56:00 +000029
Erik Andersene49d5ec2000-02-08 19:58:47 +000030extern const char mtab_file[]; /* Defined in utility.c */
Eric Andersen741f2c92001-03-14 01:48:10 +000031static const int MS_RDONLY = 1; /* Mount read-only. */
Eric Andersenbc341901999-11-06 04:56:00 +000032
Erik Andersene49d5ec2000-02-08 19:58:47 +000033void erase_mtab(const char *name)
Eric Andersenbc341901999-11-06 04:56:00 +000034{
Erik Andersene49d5ec2000-02-08 19:58:47 +000035 struct mntent entries[20];
36 int count = 0;
Eric Andersenbc341901999-11-06 04:56:00 +000037 FILE *mountTable = setmntent(mtab_file, "r");
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 struct mntent *m;
Eric Andersenbc341901999-11-06 04:56:00 +000039
Eric Andersen0ecb54a1999-12-05 23:24:55 +000040 /* Check if reading the mtab file failed */
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 if (mountTable == 0
Eric Andersenaf4ac772001-02-01 22:43:49 +000042 /* Bummer. fall back on trying the /proc filesystem */
43 && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +000044 perror_msg("%s", mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000045 return;
46 }
47
Erik Andersene49d5ec2000-02-08 19:58:47 +000048 while ((m = getmntent(mountTable)) != 0) {
Erik Andersenfac10d72000-02-07 05:29:42 +000049 entries[count].mnt_fsname = strdup(m->mnt_fsname);
50 entries[count].mnt_dir = strdup(m->mnt_dir);
51 entries[count].mnt_type = strdup(m->mnt_type);
52 entries[count].mnt_opts = strdup(m->mnt_opts);
Eric Andersenbc341901999-11-06 04:56:00 +000053 entries[count].mnt_freq = m->mnt_freq;
54 entries[count].mnt_passno = m->mnt_passno;
55 count++;
56 }
57 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 if ((mountTable = setmntent(mtab_file, "w"))) {
59 int i;
Eric Andersenbc341901999-11-06 04:56:00 +000060
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 for (i = 0; i < count; i++) {
62 int result = (strcmp(entries[i].mnt_fsname, name) == 0
63 || strcmp(entries[i].mnt_dir, name) == 0);
64
65 if (result)
Eric Andersenbc341901999-11-06 04:56:00 +000066 continue;
67 else
68 addmntent(mountTable, &entries[i]);
69 }
70 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 } else if (errno != EROFS)
Matt Kraaia9819b22000-12-22 01:48:07 +000072 perror_msg("%s", mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000073}
74
Erik Andersene49d5ec2000-02-08 19:58:47 +000075void write_mtab(char *blockDevice, char *directory,
76 char *filesystemType, long flags, char *string_flags)
Eric Andersenbc341901999-11-06 04:56:00 +000077{
78 FILE *mountTable = setmntent(mtab_file, "a+");
79 struct mntent m;
80
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 if (mountTable == 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +000082 perror_msg("%s", mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000083 return;
84 }
85 if (mountTable) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 int length = strlen(directory);
Eric Andersenbc341901999-11-06 04:56:00 +000087
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 if (length > 1 && directory[length - 1] == '/')
89 directory[length - 1] = '\0';
Eric Andersenbc341901999-11-06 04:56:00 +000090
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 if (filesystemType == 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000092 struct mntent *p = find_mount_point(blockDevice, "/proc/mounts");
Eric Andersenbc341901999-11-06 04:56:00 +000093
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 if (p && p->mnt_type)
95 filesystemType = p->mnt_type;
96 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 m.mnt_fsname = blockDevice;
98 m.mnt_dir = directory;
99 m.mnt_type = filesystemType ? filesystemType : "default";
Eric Andersenbc341901999-11-06 04:56:00 +0000100
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 if (*string_flags) {
102 m.mnt_opts = string_flags;
103 } else {
104 if ((flags | MS_RDONLY) == flags)
105 m.mnt_opts = "ro";
106 else
107 m.mnt_opts = "rw";
108 }
109
110 m.mnt_freq = 0;
111 m.mnt_passno = 0;
112 addmntent(mountTable, &m);
113 endmntent(mountTable);
114 }
115}