Various Changes
[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    "/Acess/Conf/Mounted"
10
11 // === PROTOTYPES ===
12 void    ShowUsage();
13  int    GetMountDefs(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         if(argc < 3) {
31                 ShowUsage();
32                 return EXIT_FAILURE;
33         }
34         
35         // Parse Arguments
36         for( i = 1; i < argc; i++ )
37         {
38                 arg = argv[i];
39                 
40                 if(arg[0] == '-')
41                 {
42                         switch(arg[1])
43                         {
44                         case 't':       sType = argv[++i];      break;
45                         case '-':
46                                 //TODO: Long Arguments
47                         default:
48                                 ShowUsage(argv[0]);
49                                 return EXIT_FAILURE;
50                         }
51                         continue;
52                 }
53                 
54                 if(sDevice == NULL) {
55                         sDevice = arg;
56                         continue;
57                 }
58                 
59                 if(sDir == NULL) {
60                         sDir = arg;
61                         continue;
62                 }
63                 
64                 ShowUsage(argv[0]);
65                 return EXIT_FAILURE;
66         }
67
68         // Check if we even got a device/mountpoint
69         if(sDevice == NULL) {
70                 ShowUsage(argv[0]);
71                 return EXIT_FAILURE;
72         }
73
74         // If no directory was passed (we want to use the mount list)
75         // or we are not root (we need to use the mount list)
76         // Check the mount list
77         if(sDir == NULL || getuid() != 0)
78         {
79                 // Check if it is defined in the mounts file
80                 if(GetMountDefs(&sDevice, &sDir, &sType, &sOptions) == 0)
81                 {
82                         if(sDir == NULL)
83                                 fprintf(stderr, "Unable to find '%s' in '%s'\n",
84                                         sDevice, MOUNTABLE_FILE
85                                         );
86                         else
87                                 fprintf(stderr, "You must be root to mount devices or directories not in '%s'\n",
88                                         MOUNTABLE_FILE
89                                         );
90                         return EXIT_FAILURE;
91                 }
92         
93                 // We need to be root to mount a filesystem, so, let us be elevated!
94                 setuid(0);      // I hope I have the setuid bit implemented.
95         }
96         else
97         {
98                 // Check that we were passed a filesystem type
99                 if(sType == NULL) {
100                         fprintf(stderr, "Please pass a filesystem type\n");
101                         return EXIT_FAILURE;
102                 }
103         }
104         
105         // Check Device
106         fd = open(sDevice, OPENFLAG_READ);
107         if(fd == -1) {
108                 printf("Device '%s' cannot be opened for reading\n", sDevice);
109                 return EXIT_FAILURE;
110         }
111         close(fd);
112         
113         // Check Mount Point
114         fd = open(sDir, OPENFLAG_EXEC);
115         if(fd == -1) {
116                 printf("Directory '%s' does not exist\n", sDir);
117                 return EXIT_FAILURE;
118         }
119         close(fd);
120
121         // Replace sOptions with an empty string if it is still NULL
122         if(sOptions == NULL)    sOptions = "";
123
124         // Let's Mount!
125         _SysMount(sDevice, sDir, sType, sOptions);
126
127         return 0;
128 }
129
130 void ShowUsage(char *ProgName)
131 {
132         fprintf(stderr, "Usage:\n", ProgName);
133         fprintf(stderr, "    %s [-t <type>] <device> <directory>\n");
134         fprintf(stderr, "or  %s <device>\n");
135         fprintf(stderr, "or  %s <directory>\n");
136 }
137
138 /**
139  * \fn int GetMountDefs(char **spDevice, char **spDir, char **spType, char **spOptions)
140  * \brief Reads the mountable definitions file and returns the corresponding entry
141  * \param spDevice      Pointer to a string (pointer) determining the device (also is the input for this function)
142  * \note STUB
143  */
144 int GetMountDefs(char **spDevice, char **spDir, char **spType, char **spOptions)
145 {
146         // TODO: Read the mounts file (after deciding what it will be)
147         return 0;
148 }

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