Cleaning up some debug, fixing cosmetic bugs in usermode apps
[tpg/acess2.git] / Usermode / Applications / mount_src / main.c
1 /*
2  * Acess2 mount command
3  */
4 #include <acess/sys.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 #define MOUNTABLE_FILE  "/Acess/Conf/Mountable"
9 #define MOUNTED_FILE    "/Devices/System/VFS/Mounts"
10
11 // === PROTOTYPES ===
12 void    ShowUsage(char *ProgName);
13  int    GetMountDefs(char *Ident, char **spDevice, char **spDir, char **spType, char **spOptions);
14
15 // === CODE ===
16 /**
17  * \fn int main(int argc, char *argv[])
18  * \brief Entrypoint
19  */
20 int main(int argc, char *argv[])
21 {
22          int    fd;
23          int    i;
24         char    *arg;
25         char    *sType = NULL;
26         char    *sDevice = NULL;
27         char    *sDir = NULL;
28         char    *sOptions = NULL;
29
30         // List mounted filesystems
31         // - This is cheating, isn't it?
32         if(argc == 1) {
33                 // Dump the contents of /Devices/system/VFS/Mounts
34                 FILE    *fp = fopen("/Devices/system/VFS/Mounts", "r");
35                 char    buf[1024];
36                  int    len;
37                 while( (len = fread(buf, 1024, 1, fp)) )
38                         fwrite(buf, len, 1, stdout);
39                 printf("\n");
40                 return 0;
41         }
42
43         if(argc < 3) {
44                 ShowUsage(argv[0]);
45                 return EXIT_FAILURE;
46         }
47         
48         // Parse Arguments
49         for( i = 1; i < argc; i++ )
50         {
51                 arg = argv[i];
52                 
53                 if(arg[0] == '-')
54                 {
55                         switch(arg[1])
56                         {
57                         // -t <driver> :: Filesystem driver to use
58                         case 't':       sType = argv[++i];      break;
59                         case '-':
60                                 //TODO: Long Arguments
61                         default:
62                                 ShowUsage(argv[0]);
63                                 return EXIT_FAILURE;
64                         }
65                         continue;
66                 }
67                 
68                 // Device?
69                 if(sDevice == NULL) {
70                         sDevice = arg;
71                         continue;
72                 }
73                 
74                 // Directory?
75                 if(sDir == NULL) {
76                         sDir = arg;
77                         continue;
78                 }
79                 
80                 ShowUsage(argv[0]);
81                 return EXIT_FAILURE;
82         }
83
84         // Check if we even got a device/mountpoint
85         if(sDevice == NULL) {
86                 ShowUsage(argv[0]);
87                 return EXIT_FAILURE;
88         }
89
90         // If no directory was passed (we want to use the mount list)
91         // or we are not root (we need to use the mount list)
92         // Check the mount list
93         if(sDir == NULL || getuid() != 0)
94         {
95                 // Check if it is defined in the mounts file
96                 // - At this point sDevice could be a device name or a mount point
97                 if(GetMountDefs(sDevice, &sDevice, &sDir, &sType, &sOptions) == 0)
98                 {
99                         if(sDir == NULL)
100                                 fprintf(stderr, "Unable to find '%s' in '%s'\n",
101                                         sDevice, MOUNTABLE_FILE
102                                         );
103                         else
104                                 fprintf(stderr, "You must be root to mount devices or directories not in '%s'\n",
105                                         MOUNTABLE_FILE
106                                         );
107                         return EXIT_FAILURE;
108                 }
109         
110                 // We need to be root to mount a filesystem, so, let us be elevated!
111                 setuid(0);      // I hope I have the setuid bit implemented.
112         }
113         else
114         {
115                 // Check that we were passed a filesystem type
116                 if(sType == NULL) {
117                         fprintf(stderr, "Please pass a filesystem type\n");
118                         return EXIT_FAILURE;
119                 }
120         }
121         
122         // Check Device
123         fd = open(sDevice, OPENFLAG_READ);
124         if(fd == -1) {
125                 printf("Device '%s' cannot be opened for reading\n", sDevice);
126                 return EXIT_FAILURE;
127         }
128         close(fd);
129         
130         // Check Mount Point
131         fd = open(sDir, OPENFLAG_EXEC);
132         if(fd == -1) {
133                 printf("Directory '%s' does not exist\n", sDir);
134                 return EXIT_FAILURE;
135         }
136         close(fd);
137
138         // Replace sOptions with an empty string if it is still NULL
139         if(sOptions == NULL)    sOptions = "";
140
141         // Let's Mount!
142         _SysMount(sDevice, sDir, sType, sOptions);
143
144         return 0;
145 }
146
147 void ShowUsage(char *ProgName)
148 {
149         fprintf(stderr, "Usage:\n");
150         fprintf(stderr, "    %s [-t <type>] <device> <directory>\n", ProgName);
151         fprintf(stderr, "or  %s <device>\n", ProgName);
152         fprintf(stderr, "or  %s <directory>\n", ProgName);
153         fprintf(stderr, "or  %s\n", ProgName);
154 }
155
156 /**
157  * \fn int GetMountDefs(char *Ident, char **spDevice, char **spDir, char **spType, char **spOptions)
158  * \brief Reads the mountable definitions file and returns the corresponding entry
159  * \param spDevice      Pointer to a string (pointer) determining the device (also is the input for this function)
160  * \note STUB
161  */
162 int GetMountDefs(char *Ident, char **spDevice, char **spDir, char **spType, char **spOptions)
163 {
164         // TODO: Read the mounts file (after deciding what it will be)
165         return 0;
166 }

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