DiskTool - Adding cleanup
[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                 return -1;
70         VFS_Seek(fd, 0, SEEK_END);
71         LVM_AddVolume( &gDiskTool_VolumeType, Identifier, (void*)(tVAddr)fd, 512, VFS_Tell(fd)/512);
72         return 0;
73 }
74
75 int DiskTool_MountImage(const char *Identifier, const char *Path)
76 {
77         // Validate Identifier and make mountpoint string
78         char mountpoint[sizeof("/Mount/") + strlen(Identifier) + 1];
79         strcpy(mountpoint, "/Mount/");
80         strcat(mountpoint, Identifier);
81         
82         // Translate path       
83         size_t tpath_len = DiskTool_int_TranslatePath(NULL, Path);
84         if(tpath_len == -1)
85                 return -1;
86         char tpath[tpath_len-1];
87         DiskTool_int_TranslatePath(tpath, Path);
88         
89         // Call mount
90         VFS_MkDir(mountpoint);
91         // TODO: Detect filesystem?
92         return VFS_Mount(tpath, mountpoint, "fat", "");
93 }
94
95 int DiskTool_Copy(const char *Source, const char *Destination)
96 {
97         int src = DiskTool_int_TranslateOpen(Source, VFS_OPENFLAG_READ);
98         if( src == -1 ) {
99                 Log_Error("DiskTool", "Unable to open %s for reading", Source);
100                 return -1;
101         }
102         int dst = DiskTool_int_TranslateOpen(Destination, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_CREATE);
103         if( dst == -1 ) {
104                 Log_Error("DiskTool", "Unable to open %s for writing", Destination);
105                 VFS_Close(src);
106                 return -1;
107         }
108
109         char    buf[1024];
110         size_t  len, total = 0;
111         while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) {
112                 VFS_Write(dst, len, buf);
113                 total += len;
114         }
115         VFS_Write(dst, len, buf), total += len;
116
117         Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination);
118
119         VFS_Close(dst);
120         VFS_Close(src);
121         
122         return 0;
123 }
124
125 int DiskTool_ListDirectory(const char *Directory)
126 {
127         int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY);
128         if(fd == -1) {
129 //              fprintf(stderr, "Can't open '%s'\n", Directory);
130                 return -1;
131         }
132
133         Log("Directory listing of '%s'", Directory);    
134
135         char    name[256];
136         while( VFS_ReadDir(fd, name) )
137         {
138                 Log("- %s", name);
139         }
140         
141         VFS_Close(fd);
142         
143         return 0;
144 }
145
146 int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest)
147 {
148         VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest);
149         return 0;
150 }
151 int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest)
152 {
153         VFS_WriteAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest);
154         return 0;
155 }
156 void DiskTool_LVM_Cleanup(void *Handle)
157 {
158         VFS_Close( (int)(tVAddr)Handle );
159 }
160
161 // --- Internal helpers ---
162 int DiskTool_int_TranslateOpen(const char *File, int Flags)
163 {
164         size_t tpath_len = DiskTool_int_TranslatePath(NULL, File);
165         if(tpath_len == -1)
166                 return -1;
167         char tpath[tpath_len-1];
168         DiskTool_int_TranslatePath(tpath, File);
169
170         return VFS_Open(tpath, Flags);
171 }
172

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