Added some very pedantic warning flags
[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 #if 0
15  int    VFS_Mount(const char *Device, const char *MountPoint, const char *Filesystem, const char *Options);
16 #endif
17 void    VFS_UpdateMountFile(void);
18
19 // === GLOBALS ===
20 tMutex  glVFS_MountList;
21 tVFS_Mount      *gVFS_Mounts;
22 tVFS_Mount      *gVFS_RootMount = NULL;
23
24 // === CODE ===
25 /**
26  * \brief Mount a device
27  * \param Device        Device string to mount
28  * \param MountPoint    Destination for the mount
29  * \param Filesystem    Filesystem to use for the mount
30  * \param Options               Options to be passed to the filesystem
31  * \return -1 on Invalid FS, -2 on No Mem, 0 on success
32  * 
33  * Mounts the filesystem on \a Device at \a MountPoint using the driver
34  * \a Filesystem. The options in the string \a Options is passed to the
35  * driver's mount.
36  */
37 int VFS_Mount(const char *Device, const char *MountPoint, const char *Filesystem, const char *Options)
38 {
39         tVFS_Mount      *mnt;
40         tVFS_Driver     *fs;
41          int    deviceLen = strlen(Device);
42          int    mountLen = strlen(MountPoint);
43          int    argLen = strlen(Options);
44         
45         // Get the filesystem
46         fs = VFS_GetFSByName(Filesystem);
47         if(!fs) {
48                 Log_Warning("VFS", "VFS_Mount - Unknown FS Type '%s'", Filesystem);
49                 return -1;
50         }
51         
52         // Create mount information
53         mnt = malloc( sizeof(tVFS_Mount)+deviceLen+1+mountLen+1+argLen+1 );
54         if(!mnt) {
55                 return -2;
56         }
57         
58         // HACK: Forces VFS_ParsePath to fall back on root  
59         if(mountLen == 1 && MountPoint[0] == '/')
60                 mnt->MountPointLen = 0;
61         else
62                 mnt->MountPointLen = mountLen;
63         
64         // Fill Structure
65         mnt->Filesystem = fs;
66         
67         mnt->Device = &mnt->StrData[0];
68         memcpy( mnt->Device, Device, deviceLen+1 );
69         
70         mnt->MountPoint = &mnt->StrData[deviceLen+1];
71         memcpy( mnt->MountPoint, MountPoint, mountLen+1 );
72         
73         mnt->Options = &mnt->StrData[deviceLen+1+mountLen+1];
74         memcpy( mnt->Options, Options, argLen+1 );
75         
76         // Initialise Volume
77         mnt->RootNode = fs->InitDevice(Device, NULL);   //&ArgString);
78         if(!mnt->RootNode) {
79                 free(mnt);
80                 return -2;
81         }
82         
83         // Set root
84         if(!gVFS_RootMount)     gVFS_RootMount = mnt;
85         
86         // Add to mount list
87         Mutex_Acquire( &glVFS_MountList );
88         {
89                 tVFS_Mount      *tmp;
90                 mnt->Next = NULL;
91                 if(gVFS_Mounts) {
92                         for( tmp = gVFS_Mounts; tmp->Next; tmp = tmp->Next );
93                         tmp->Next = mnt;
94                 }
95                 else {
96                         gVFS_Mounts = mnt;
97                 }
98         }
99         Mutex_Release( &glVFS_MountList );
100         
101         Log_Log("VFS", "Mounted '%s' to '%s' ('%s')", Device, MountPoint, Filesystem);
102         
103         VFS_UpdateMountFile();
104         
105         return 0;
106 }
107
108 /**
109  * \brief Updates the mount file buffer
110  * 
111  * Updates the ProcFS mounts file buffer to match the current mounts list.
112  */
113 void VFS_UpdateMountFile(void)
114 {
115          int    len = 0;
116         char    *buf;
117         tVFS_Mount      *mnt;
118         
119         // Format:
120         // <device>\t<location>\t<type>\t<options>\n
121         
122         for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
123         {
124                 len += 4 + strlen(mnt->Device) + strlen(mnt->MountPoint)
125                         + strlen(mnt->Filesystem->Name) + strlen(mnt->Options);
126         }
127         
128         buf = malloc( len + 1 );
129         len = 0;
130         for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
131         {
132                 strcpy( &buf[len], mnt->Device );
133                 len += strlen(mnt->Device);
134                 buf[len++] = '\t';
135                 
136                 strcpy( &buf[len], mnt->MountPoint );
137                 len += strlen(mnt->MountPoint);
138                 buf[len++] = '\t';
139                 
140                 strcpy( &buf[len], mnt->Filesystem->Name );
141                 len += strlen(mnt->Filesystem->Name);
142                 buf[len++] = '\t';
143                 
144                 strcpy( &buf[len], mnt->Options );
145                 len += strlen(mnt->Options);
146                 buf[len++] = '\n';
147         }
148         buf[len] = 0;
149         
150         SysFS_UpdateFile( giVFS_MountFileID, buf, len );
151         if( gsVFS_MountFile )   free( gsVFS_MountFile );
152         gsVFS_MountFile = buf;
153 }

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