DiskTool - Added 'cat' command
[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_MkDir(const char *Directory)
99 {
100         return -1;
101 }
102
103 int DiskTool_Copy(const char *Source, const char *Destination)
104 {
105         int src = DiskTool_int_TranslateOpen(Source, VFS_OPENFLAG_READ);
106         if( src == -1 ) {
107                 Log_Error("DiskTool", "Unable to open %s for reading", Source);
108                 return -1;
109         }
110         int dst = DiskTool_int_TranslateOpen(Destination, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_CREATE);
111         if( dst == -1 ) {
112                 Log_Error("DiskTool", "Unable to open %s for writing", Destination);
113                 VFS_Close(src);
114                 return -1;
115         }
116
117         char    buf[1024];
118         size_t  len, total = 0;
119         while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) {
120                 VFS_Write(dst, len, buf);
121                 total += len;
122         }
123         VFS_Write(dst, len, buf), total += len;
124
125         Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination);
126
127         VFS_Close(dst);
128         VFS_Close(src);
129         
130         return 0;
131 }
132
133 int DiskTool_ListDirectory(const char *Directory)
134 {
135         int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY);
136         if(fd == -1) {
137 //              fprintf(stderr, "Can't open '%s'\n", Directory);
138                 return -1;
139         }
140
141         Log("Directory listing of '%s'", Directory);    
142
143         char    name[256];
144         while( VFS_ReadDir(fd, name) )
145         {
146                 Log("- %s", name);
147         }
148         
149         VFS_Close(fd);
150         
151         return 0;
152 }
153
154 int DiskTool_Cat(const char *File)
155 {
156         int src = DiskTool_int_TranslateOpen(File, VFS_OPENFLAG_READ);
157         if( src == -1 ) {
158                 Log_Error("DiskTool", "Unable to open %s for reading", File);
159                 return -1;
160         }
161         
162         char    buf[1024];
163         size_t  len, total = 0;
164         while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) {
165                 _fwrite_stdout(len, buf);
166                 total += len;
167         }
168         _fwrite_stdout(len, buf);
169         total += len;
170
171         Log_Notice("DiskTool", "%i bytes from %s", total, File);
172
173         VFS_Close(src);
174         return 0;
175 }
176
177 int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest)
178 {
179         return VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512;
180 }
181 int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest)
182 {
183         return VFS_WriteAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512;
184 }
185 void DiskTool_LVM_Cleanup(void *Handle)
186 {
187         VFS_Close( (int)(tVAddr)Handle );
188 }
189
190 // --- Internal helpers ---
191 int DiskTool_int_TranslateOpen(const char *File, int Flags)
192 {
193         size_t tpath_len = DiskTool_int_TranslatePath(NULL, File);
194         if(tpath_len == -1)
195                 return -1;
196         char tpath[tpath_len-1];
197         DiskTool_int_TranslatePath(tpath, File);
198
199         return VFS_Open(tpath, Flags);
200 }
201

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