c30924ab79c1a12c16648dcb33bb8125d5b01f66
[tpg/acess2.git] / Usermode / Applications / automounter_src / main.c
1 /*
2  * Acess2 Automounter
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Program core
7  */
8 #include <stdlib.h>
9 #include <dirent.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <acess/sys.h>
13 #include <assert.h>
14
15 #define LVMBASE "/Devices/LVM"
16 #define AMOUNTROOT      "/Mount/auto"
17
18 void    TryMount(const char *Volume, const char *Part);
19
20 // === CODE ===
21 int main(int argc, char *argv[])
22 {
23         mkdir(AMOUNTROOT, 0755);
24         // TODO: Handle variants like 'by-label' and 'by-id'
25         
26         // Iterate '/Devices/LVM/*/*'
27         DIR     *dp = opendir(LVMBASE);
28         assert(dp);
29         struct dirent   *ent;
30         while( (ent = readdir(dp)) )
31         {
32                 if( ent->d_name[0] == '.' )
33                         continue ;
34                 if( strncmp(ent->d_name, "by-", 3) == 0 )
35                         continue ;
36                 
37                 char    path[sizeof(LVMBASE)+1+strlen(ent->d_name)+1];
38                 sprintf(path, LVMBASE"/%s", ent->d_name);
39                 DIR     *ddp = opendir(path);
40                 assert(ddp);
41
42                 // Attempt to mount each sub-volume
43                 struct dirent   *dent;
44                  int    nParts = 0;
45                 while( (dent = readdir(ddp)) )
46                 {
47                         if(dent->d_name[0] == '.')
48                                 continue ;
49                         if(strcmp(dent->d_name, "ROOT") == 0)
50                                 continue ;
51
52                         TryMount(ent->d_name, dent->d_name);
53
54                         nParts ++;
55                 }
56                 closedir(ddp);
57                 
58                 // If only ROOT exists, attempt to mount that as '/Mount/auto/devname'
59                 if( nParts == 0 )
60                 {
61                         TryMount(ent->d_name, NULL);
62                 }
63         }
64         
65         closedir(dp);
66         
67         return 0;
68 }
69
70 void TryMount(const char *Volume, const char *Part)
71 {
72         // Create device path
73         size_t  devpath_len = sizeof(LVMBASE)+1+strlen(Volume)+1 + (Part ? strlen(Part)+1 : 5);
74         char devpath[devpath_len];
75         sprintf(devpath, LVMBASE"/%s/%s", Volume, (Part ? Part : "ROOT"));
76
77         // Create mount path
78         size_t  mntpath_len = sizeof(AMOUNTROOT)+1+strlen(Volume)+1 + (Part ? strlen(Part)+1 : 0);
79         char mntpath[mntpath_len];
80         sprintf(mntpath, AMOUNTROOT"/%s", Volume);
81         if( Part ) {
82                 strcat(mntpath, "-");
83                 strcat(mntpath, Part);
84         }
85         mkdir(mntpath, 0755);
86         
87         // Attempt mount
88         if( _SysMount(devpath, mntpath, NULL, "") ) {
89                 fprintf(stderr, "Unable to mount '%s'\n", devpath);
90         }
91 }
92

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