3 * - By John Hodge (thePowersGang)
6 * - High level actions that call the VFS
7 * # Kernel-space compiled
10 #include <disktool_common.h>
11 #include <Storage/LVM/include/lvm.h>
14 extern int NativeFS_Install(char **Arguments);
15 extern int LVM_Cleanup(void);
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);
26 tLVM_VolType gDiskTool_VolumeType = {
28 .Read = DiskTool_LVM_Read,
29 .Write = DiskTool_LVM_Write,
30 .Cleanup = DiskTool_LVM_Cleanup
34 void DiskTool_Initialise(void)
37 NativeFS_Install(NULL);
39 VFS_Mount("/", "/Native", "nativefs", "");
42 void DiskTool_Cleanup(void)
45 int nNochangeLoop = 0;
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 ) {
53 if(nNochangeLoop == 2) {
54 Log_Error("DiskTool", "Possible handle leak");
62 while( vfs_rv >= 0 || lvm_rv != 0 );
65 int DiskTool_RegisterLVM(const char *Identifier, const char *Path)
67 int fd = DiskTool_int_TranslateOpen(Path, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE);
69 Log_Notice("DiskTool", "Can't open '%s' for LVM %s", Path, Identifier);
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);
78 int DiskTool_MountImage(const char *Identifier, const char *Path)
80 // Validate Identifier and make mountpoint string
81 char mountpoint[sizeof("/Mount/") + strlen(Identifier) + 1];
82 strcpy(mountpoint, "/Mount/");
83 strcat(mountpoint, Identifier);
86 size_t tpath_len = DiskTool_int_TranslatePath(NULL, Path);
89 char tpath[tpath_len-1];
90 DiskTool_int_TranslatePath(tpath, Path);
93 VFS_MkDir(mountpoint);
94 // TODO: Detect filesystem?
95 return VFS_Mount(tpath, mountpoint, "", "");
98 int DiskTool_MkDir(const char *Directory)
103 int DiskTool_Copy(const char *Source, const char *Destination)
105 int src = DiskTool_int_TranslateOpen(Source, VFS_OPENFLAG_READ);
107 Log_Error("DiskTool", "Unable to open %s for reading", Source);
110 int dst = DiskTool_int_TranslateOpen(Destination, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_CREATE);
112 Log_Error("DiskTool", "Unable to open %s for writing", Destination);
118 size_t len, total = 0;
119 while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) {
120 VFS_Write(dst, len, buf);
123 VFS_Write(dst, len, buf), total += len;
125 Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination);
133 int DiskTool_ListDirectory(const char *Directory)
135 int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY);
137 // fprintf(stderr, "Can't open '%s'\n", Directory);
141 Log("Directory listing of '%s'", Directory);
144 while( VFS_ReadDir(fd, name) )
154 int DiskTool_Cat(const char *File)
156 int src = DiskTool_int_TranslateOpen(File, VFS_OPENFLAG_READ);
158 Log_Error("DiskTool", "Unable to open %s for reading", File);
163 size_t len, total = 0;
164 while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) {
165 _fwrite_stdout(len, buf);
168 _fwrite_stdout(len, buf);
171 Log_Notice("DiskTool", "%i bytes from %s", total, File);
177 int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest)
179 return VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512;
181 int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest)
183 return VFS_WriteAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512;
185 void DiskTool_LVM_Cleanup(void *Handle)
187 VFS_Close( (int)(tVAddr)Handle );
190 // --- Internal helpers ---
191 int DiskTool_int_TranslateOpen(const char *File, int Flags)
193 size_t tpath_len = DiskTool_int_TranslatePath(NULL, File);
196 char tpath[tpath_len-1];
197 DiskTool_int_TranslatePath(tpath, File);
199 return VFS_Open(tpath, Flags);