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);
70 VFS_Seek(fd, 0, SEEK_END);
71 LVM_AddVolume( &gDiskTool_VolumeType, Identifier, (void*)(tVAddr)fd, 512, VFS_Tell(fd)/512);
75 int DiskTool_MountImage(const char *Identifier, const char *Path)
77 // Validate Identifier and make mountpoint string
78 char mountpoint[sizeof("/Mount/") + strlen(Identifier) + 1];
79 strcpy(mountpoint, "/Mount/");
80 strcat(mountpoint, Identifier);
83 size_t tpath_len = DiskTool_int_TranslatePath(NULL, Path);
86 char tpath[tpath_len-1];
87 DiskTool_int_TranslatePath(tpath, Path);
90 VFS_MkDir(mountpoint);
91 // TODO: Detect filesystem?
92 return VFS_Mount(tpath, mountpoint, "fat", "");
95 int DiskTool_Copy(const char *Source, const char *Destination)
97 int src = DiskTool_int_TranslateOpen(Source, VFS_OPENFLAG_READ);
99 Log_Error("DiskTool", "Unable to open %s for reading", Source);
102 int dst = DiskTool_int_TranslateOpen(Destination, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_CREATE);
104 Log_Error("DiskTool", "Unable to open %s for writing", Destination);
110 size_t len, total = 0;
111 while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) {
112 VFS_Write(dst, len, buf);
115 VFS_Write(dst, len, buf), total += len;
117 Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination);
125 int DiskTool_ListDirectory(const char *Directory)
127 int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY);
129 // fprintf(stderr, "Can't open '%s'\n", Directory);
133 Log("Directory listing of '%s'", Directory);
136 while( VFS_ReadDir(fd, name) )
146 int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest)
148 VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest);
151 int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest)
153 VFS_WriteAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest);
156 void DiskTool_LVM_Cleanup(void *Handle)
158 VFS_Close( (int)(tVAddr)Handle );
161 // --- Internal helpers ---
162 int DiskTool_int_TranslateOpen(const char *File, int Flags)
164 size_t tpath_len = DiskTool_int_TranslatePath(NULL, File);
167 char tpath[tpath_len-1];
168 DiskTool_int_TranslatePath(tpath, File);
170 return VFS_Open(tpath, Flags);