DiskTool - Autodetection of filesystems and better debugging
[tpg/acess2.git] / Tools / DiskTool / src / actions.c
1 /*
2  * Acess2 DiskTool
3  * - By John Hodge (thePowersGang)
4  *
5  * actions.c
6  * - High level actions that call the VFS
7  * # Kernel-space compiled
8  */
9 #include <acess.h>
10 #include <disktool_common.h>
11 #include <Storage/LVM/include/lvm.h>
12
13 // === IMPORTS ===
14 extern int      NativeFS_Install(char **Arguments);
15 extern int      LVM_Cleanup(void);
16
17 // === PROTOTYPES ===
18 void    DiskTool_Initialise(void)       __attribute__((constructor(101)));
19 void    DiskTool_Cleanup(void);
20  int    DiskTool_int_TranslateOpen(const char *File, int Mode);
21  int    DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest);
22  int    DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest);
23 void    DiskTool_LVM_Cleanup(void *Handle);
24
25 // === GLOBALS ===
26 tLVM_VolType    gDiskTool_VolumeType = {
27         .Name = "DiskTool",
28         .Read  = DiskTool_LVM_Read,
29         .Write = DiskTool_LVM_Write,
30         .Cleanup = DiskTool_LVM_Cleanup
31 };
32
33 // === CODE ===
34 void DiskTool_Initialise(void)
35 {
36         VFS_Init();
37         NativeFS_Install(NULL);
38         VFS_MkDir("/Native");
39         VFS_Mount("/", "/Native", "nativefs", "");
40 }
41
42 void DiskTool_Cleanup(void)
43 {
44          int    vfs_rv, lvm_rv;
45          int    nNochangeLoop = 0;
46         // Unmount all
47         do {
48                 lvm_rv = LVM_Cleanup();
49                 vfs_rv = VFS_UnmountAll();
50                 Log_Debug("DiskTool", "Unmounted %i volumes", vfs_rv);
51                 if( vfs_rv == 0 && lvm_rv == 0 ) {
52                         nNochangeLoop ++;
53                         if(nNochangeLoop == 2) {
54                                 Log_Error("DiskTool", "Possible handle leak");
55                                 break;
56                         }
57                 }
58                 else {
59                         nNochangeLoop = 0;
60                 }
61         }
62         while( vfs_rv >= 0 || lvm_rv != 0 );
63 }
64
65 int DiskTool_RegisterLVM(const char *Identifier, const char *Path)
66 {
67         int fd = DiskTool_int_TranslateOpen(Path, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE);
68         if(fd == -1) {
69                 Log_Notice("DiskTool", "Can't open '%s' for LVM %s", Path, Identifier);
70                 return -1;
71         }
72         VFS_Seek(fd, 0, SEEK_END);
73         LVM_AddVolume( &gDiskTool_VolumeType, Identifier, (void*)(tVAddr)fd, 512, VFS_Tell(fd)/512);
74         Log_Debug("DiskTool", "Registered '%s' for LVM %s", Path, Identifier);
75         return 0;
76 }
77
78 int DiskTool_MountImage(const char *Identifier, const char *Path)
79 {
80         // Validate Identifier and make mountpoint string
81         char mountpoint[sizeof("/Mount/") + strlen(Identifier) + 1];
82         strcpy(mountpoint, "/Mount/");
83         strcat(mountpoint, Identifier);
84         
85         // Translate path       
86         size_t tpath_len = DiskTool_int_TranslatePath(NULL, Path);
87         if(tpath_len == -1)
88                 return -1;
89         char tpath[tpath_len-1];
90         DiskTool_int_TranslatePath(tpath, Path);
91         
92         // Call mount
93         VFS_MkDir(mountpoint);
94         // TODO: Detect filesystem?
95         return VFS_Mount(tpath, mountpoint, "", "");
96 }
97
98 int DiskTool_Copy(const char *Source, const char *Destination)
99 {
100         int src = DiskTool_int_TranslateOpen(Source, VFS_OPENFLAG_READ);
101         if( src == -1 ) {
102                 Log_Error("DiskTool", "Unable to open %s for reading", Source);
103                 return -1;
104         }
105         int dst = DiskTool_int_TranslateOpen(Destination, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_CREATE);
106         if( dst == -1 ) {
107                 Log_Error("DiskTool", "Unable to open %s for writing", Destination);
108                 VFS_Close(src);
109                 return -1;
110         }
111
112         char    buf[1024];
113         size_t  len, total = 0;
114         while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) {
115                 VFS_Write(dst, len, buf);
116                 total += len;
117         }
118         VFS_Write(dst, len, buf), total += len;
119
120         Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination);
121
122         VFS_Close(dst);
123         VFS_Close(src);
124         
125         return 0;
126 }
127
128 int DiskTool_ListDirectory(const char *Directory)
129 {
130         int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY);
131         if(fd == -1) {
132 //              fprintf(stderr, "Can't open '%s'\n", Directory);
133                 return -1;
134         }
135
136         Log("Directory listing of '%s'", Directory);    
137
138         char    name[256];
139         while( VFS_ReadDir(fd, name) )
140         {
141                 Log("- %s", name);
142         }
143         
144         VFS_Close(fd);
145         
146         return 0;
147 }
148
149 int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest)
150 {
151         return VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512;
152 }
153 int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest)
154 {
155         return VFS_WriteAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512;
156 }
157 void DiskTool_LVM_Cleanup(void *Handle)
158 {
159         VFS_Close( (int)(tVAddr)Handle );
160 }
161
162 // --- Internal helpers ---
163 int DiskTool_int_TranslateOpen(const char *File, int Flags)
164 {
165         size_t tpath_len = DiskTool_int_TranslatePath(NULL, File);
166         if(tpath_len == -1)
167                 return -1;
168         char tpath[tpath_len-1];
169         DiskTool_int_TranslatePath(tpath, File);
170
171         return VFS_Open(tpath, Flags);
172 }
173

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