Back to index

avfs  1.0.1
mount.c
Go to the documentation of this file.
00001 /*
00002     AVFS: A Virtual File System Library
00003     Copyright (C) 1998-2001  Miklos Szeredi <miklos@szeredi.hu>
00004 
00005     This program can be distributed under the terms of the GNU GPL.
00006     See the file COPYING.
00007 */
00008 
00009 #include <stdio.h>
00010 #include <string.h>
00011 #include <errno.h>
00012 #include <fcntl.h>
00013 #include <sys/mount.h>
00014 #include <sys/types.h>
00015 #include <mntent.h>
00016 
00017 #include CODAINCLUDE
00018 
00019 
00020 int mount_coda(const char *dev, const char *dir, int devfd, int quiet)
00021 {
00022     int res;
00023     const char *type;
00024     FILE *fd;
00025     struct mntent ent;
00026     void *mntdata = NULL;
00027 #ifdef CODA_MOUNT_VERSION
00028     struct coda_mount_data data;
00029     
00030     data.version = CODA_MOUNT_VERSION;
00031     data.fd = devfd;
00032     mntdata = &data;
00033 #endif
00034     type = "coda";
00035     res = mount("coda", dir, type, MS_MGC_VAL | MS_NOSUID | MS_NODEV, mntdata);
00036     
00037     if(res == -1) {
00038        if(!quiet) 
00039            fprintf(stderr, "mount failed: %s\n", strerror(errno));
00040 
00041        return -1;
00042     }
00043     
00044     fd = setmntent("/etc/mtab", "a");
00045     if(fd == NULL) {
00046        fprintf(stderr, "setmntent(\"/etc/mtab\") failed: %s\n",
00047               strerror(errno));
00048        return -1;
00049     }
00050     
00051     ent.mnt_fsname = (char *) dev;
00052     ent.mnt_dir = (char *) dir;
00053     ent.mnt_type = (char *) type;
00054     ent.mnt_opts = "rw,nosuid,nodev";
00055     ent.mnt_freq = 0;
00056     ent.mnt_passno = 0;
00057     res = addmntent(fd, & ent);
00058     if(res != 0) {
00059        fprintf(stderr, "addmntent() failed: %s\n", strerror(errno));
00060     }
00061     
00062     endmntent(fd);
00063     
00064     return 0;
00065 }
00066 
00067 int unmount_coda(const char *dir, int quiet)
00068 {
00069     int res;
00070     FILE *fdold, *fdnew;
00071     struct mntent *entp;
00072     
00073     res = umount(dir);
00074     
00075     if(res == -1) {
00076        if(!quiet) 
00077            fprintf(stderr, "umount failed: %s\n", strerror(errno));
00078 
00079        return -1;
00080     }
00081     
00082     fdold = setmntent("/etc/mtab", "r");
00083     if(fdold == NULL) {
00084        fprintf(stderr, "setmntent(\"/etc/mtab\") failed: %s\n",
00085               strerror(errno));
00086        return -1;
00087     }
00088 
00089     fdnew = setmntent("/etc/mtab~", "w");
00090     if(fdnew == NULL) {
00091        fprintf(stderr, "setmntent(\"/etc/mtab~\") failed: %s\n",
00092               strerror(errno));
00093        return -1;
00094     }
00095 
00096     do {
00097        entp = getmntent(fdold);
00098        if(entp != NULL) {
00099            if(strcmp(entp->mnt_dir, dir) != 0) {
00100               res = addmntent(fdnew, entp);
00101               if(res != 0) {
00102                   fprintf(stderr, "addmntent() failed: %s\n",
00103                          strerror(errno));
00104               }
00105            }
00106        }
00107     } while(entp != NULL);
00108 
00109     endmntent(fdold);
00110     endmntent(fdnew);
00111 
00112     res = rename("/etc/mtab~", "/etc/mtab");
00113     if(res == -1) {
00114        fprintf(stderr, "rename(\"/etc/mtab~\", \"/etc/mtab\") failed: %s\n", 
00115               strerror(errno));
00116        return -1;
00117     }
00118     
00119     return 0;
00120 }