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);
87 char tpath[tpath_len-1];
88 DiskTool_int_TranslatePath(tpath, Path);
91 VFS_MkDir(mountpoint);
92 // TODO: Detect filesystem?
93 return VFS_Mount(tpath, mountpoint, "", "");
96 int DiskTool_MkDir(const char *Directory)
101 int DiskTool_Copy(const char *Source, const char *Destination)
103 int src = DiskTool_int_TranslateOpen(Source, VFS_OPENFLAG_READ);
105 Log_Error("DiskTool", "Unable to open %s for reading", Source);
108 int dst = DiskTool_int_TranslateOpen(Destination, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_CREATE);
110 Log_Error("DiskTool", "Unable to open %s for writing", Destination);
116 size_t len, total = 0;
117 while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) {
118 VFS_Write(dst, len, buf);
121 VFS_Write(dst, len, buf), total += len;
123 Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination);
131 int DiskTool_ListDirectory(const char *Directory)
133 int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY);
135 // fprintf(stderr, "Can't open '%s'\n", Directory);
139 Log("Directory listing of '%s'", Directory);
142 while( VFS_ReadDir(fd, name) )
152 int DiskTool_Cat(const char *File)
154 int src = DiskTool_int_TranslateOpen(File, VFS_OPENFLAG_READ);
156 Log_Error("DiskTool", "Unable to open %s for reading", File);
161 size_t len, total = 0;
162 while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) {
163 _fwrite_stdout(len, buf);
166 _fwrite_stdout(len, buf);
169 Log_Notice("DiskTool", "%i bytes from %s", total, File);
175 int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest)
177 return VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512;
179 int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest)
181 return VFS_WriteAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512;
183 void DiskTool_LVM_Cleanup(void *Handle)
185 VFS_Close( (int)(tVAddr)Handle );
188 // --- Internal helpers ---
189 int DiskTool_int_TranslateOpen(const char *File, int Flags)
191 size_t tpath_len = DiskTool_int_TranslatePath(NULL, File);
194 char tpath[tpath_len-1];
195 DiskTool_int_TranslatePath(tpath, File);
197 return VFS_Open(tpath, Flags);