Changed "common.h" to "acess.h" to reduce possible conflicts
[tpg/acess2.git] / Kernel / vfs / mount.c
1 /* 
2  * Acess Micro - VFS Server version 1
3  */
4 #include <acess.h>
5 #include <vfs.h>
6 #include <vfs_int.h>
7 #include <fs_sysfs.h>
8
9 // === IMPORTS ===
10 extern int      giVFS_MountFileID;
11 extern char     *gsVFS_MountFile;
12
13 // === PROTOTYPES ===
14  int    VFS_Mount(char *Device, char *MountPoint, char *Filesystem, char *ArgString);
15 void    VFS_UpdateMountFile();
16
17 // === GLOBALS ===
18  int    glVFS_MountList = 0;
19 tVFS_Mount      *gVFS_Mounts;
20 tVFS_Mount      *gVFS_RootMount = NULL;
21
22 // === CODE ===
23 /**
24  * \fn int VFS_Mount(char *Device, char *MountPoint, char *Filesystem, char *Options)
25  * \brief Mount a device
26  * \param Device        Device string to mount
27  * \param MountPoint    Destination for the mount
28  * \param Filesystem    Filesystem to use for the mount
29  * \param Options               Options to be passed to the filesystem
30  * \return -1 on Invalid FS, -2 on No Mem, 0 on success
31  */
32 int VFS_Mount(char *Device, char *MountPoint, char *Filesystem, char *Options)
33 {
34         tVFS_Mount      *mnt;
35         tVFS_Driver     *fs;
36          int    deviceLen = strlen(Device);
37          int    mountLen = strlen(MountPoint);
38          int    argLen = strlen(Options);
39         
40         // Get the filesystem
41         fs = VFS_GetFSByName(Filesystem);
42         if(!fs) {
43                 Warning("VFS_Mount - Unknown FS Type '%s'", Filesystem);
44                 return -1;
45         }
46         
47         // Create mount information
48         mnt = malloc( sizeof(tVFS_Mount)+deviceLen+1+mountLen+1+argLen+1 );
49         if(!mnt) {
50                 return -2;
51         }
52         
53         // HACK: Forces VFS_ParsePath to fall back on root  
54         if(mountLen == 1 && MountPoint[0] == '/')
55                 mnt->MountPointLen = 0;
56         else
57                 mnt->MountPointLen = mountLen;
58         
59         // Fill Structure
60         mnt->Filesystem = fs;
61         
62         mnt->Device = &mnt->StrData[0];
63         memcpy( mnt->Device, Device, deviceLen+1 );
64         
65         mnt->MountPoint = &mnt->StrData[deviceLen+1];
66         memcpy( mnt->MountPoint, MountPoint, mountLen+1 );
67         
68         mnt->Options = &mnt->StrData[deviceLen+1+mountLen+1];
69         memcpy( mnt->Options, Options, argLen+1 );
70         
71         // Initialise Volume
72         mnt->RootNode = fs->InitDevice(Device, NULL);   //&ArgString);
73         if(!mnt->RootNode) {
74                 free(mnt);
75                 return -2;
76         }
77         
78         // Set root
79         if(!gVFS_RootMount)     gVFS_RootMount = mnt;
80         
81         // Add to mount list
82         LOCK( &glVFS_MountList );
83         {
84                 tVFS_Mount      *tmp;
85                 mnt->Next = NULL;
86                 if(gVFS_Mounts) {
87                         for( tmp = gVFS_Mounts; tmp->Next; tmp = tmp->Next );
88                         tmp->Next = mnt;
89                 }
90                 else {
91                         gVFS_Mounts = mnt;
92                 }
93         }
94         RELEASE( &glVFS_MountList );
95         
96         Log("VFS_Mount: Mounted '%s' to '%s' ('%s')", Device, MountPoint, Filesystem);
97         
98         VFS_UpdateMountFile();
99         
100         return 0;
101 }
102
103 /**
104  * \fn void VFS_UpdateMountFile()
105  * \brief Updates the mount file buffer
106  */
107 void VFS_UpdateMountFile()
108 {
109          int    len = 0;
110         char    *buf;
111         tVFS_Mount      *mnt;
112         
113         // Format:
114         // <device>\t<location>\t<type>\t<options>\n
115         
116         for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
117         {
118                 len += 4 + strlen(mnt->Device) + strlen(mnt->MountPoint)
119                         + strlen(mnt->Filesystem->Name) + strlen(mnt->Options);
120         }
121         
122         buf = malloc( len + 1 );
123         len = 0;
124         for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
125         {
126                 strcpy( &buf[len], mnt->Device );
127                 len += strlen(mnt->Device);
128                 buf[len++] = '\t';
129                 
130                 strcpy( &buf[len], mnt->MountPoint );
131                 len += strlen(mnt->MountPoint);
132                 buf[len++] = '\t';
133                 
134                 strcpy( &buf[len], mnt->Filesystem->Name );
135                 len += strlen(mnt->Filesystem->Name);
136                 buf[len++] = '\t';
137                 
138                 strcpy( &buf[len], mnt->Options );
139                 len += strlen(mnt->Options);
140                 buf[len++] = '\n';
141         }
142         buf[len] = 0;
143         
144         SysFS_UpdateFile( giVFS_MountFileID, buf, len );
145         if( gsVFS_MountFile )   free( gsVFS_MountFile );
146         gsVFS_MountFile = buf;
147 }

UCC git Repository :: git.ucc.asn.au