DiskTool - Autodetection of filesystems and better debugging
[tpg/acess2.git] / Tools / DiskTool / src / actions.c
index 6cbd7b8..d1fb703 100644 (file)
@@ -8,13 +8,27 @@
  */
 #include <acess.h>
 #include <disktool_common.h>
+#include <Storage/LVM/include/lvm.h>
 
 // === IMPORTS ===
 extern int     NativeFS_Install(char **Arguments);
+extern int     LVM_Cleanup(void);
 
 // === PROTOTYPES ===
 void   DiskTool_Initialise(void)       __attribute__((constructor(101)));
+void   DiskTool_Cleanup(void);
  int   DiskTool_int_TranslateOpen(const char *File, int Mode);
+ int   DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest);
+ int   DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest);
+void   DiskTool_LVM_Cleanup(void *Handle);
+
+// === GLOBALS ===
+tLVM_VolType   gDiskTool_VolumeType = {
+       .Name = "DiskTool",
+       .Read  = DiskTool_LVM_Read,
+       .Write = DiskTool_LVM_Write,
+       .Cleanup = DiskTool_LVM_Cleanup
+};
 
 // === CODE ===
 void DiskTool_Initialise(void)
@@ -25,6 +39,42 @@ void DiskTool_Initialise(void)
        VFS_Mount("/", "/Native", "nativefs", "");
 }
 
+void DiskTool_Cleanup(void)
+{
+        int    vfs_rv, lvm_rv;
+        int    nNochangeLoop = 0;
+       // Unmount all
+       do {
+               lvm_rv = LVM_Cleanup();
+               vfs_rv = VFS_UnmountAll();
+               Log_Debug("DiskTool", "Unmounted %i volumes", vfs_rv);
+               if( vfs_rv == 0 && lvm_rv == 0 ) {
+                       nNochangeLoop ++;
+                       if(nNochangeLoop == 2) {
+                               Log_Error("DiskTool", "Possible handle leak");
+                               break;
+                       }
+               }
+               else {
+                       nNochangeLoop = 0;
+               }
+       }
+       while( vfs_rv >= 0 || lvm_rv != 0 );
+}
+
+int DiskTool_RegisterLVM(const char *Identifier, const char *Path)
+{
+       int fd = DiskTool_int_TranslateOpen(Path, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE);
+       if(fd == -1) {
+               Log_Notice("DiskTool", "Can't open '%s' for LVM %s", Path, Identifier);
+               return -1;
+       }
+       VFS_Seek(fd, 0, SEEK_END);
+       LVM_AddVolume( &gDiskTool_VolumeType, Identifier, (void*)(tVAddr)fd, 512, VFS_Tell(fd)/512);
+       Log_Debug("DiskTool", "Registered '%s' for LVM %s", Path, Identifier);
+       return 0;
+}
+
 int DiskTool_MountImage(const char *Identifier, const char *Path)
 {
        // Validate Identifier and make mountpoint string
@@ -40,8 +90,9 @@ int DiskTool_MountImage(const char *Identifier, const char *Path)
        DiskTool_int_TranslatePath(tpath, Path);
        
        // Call mount
+       VFS_MkDir(mountpoint);
        // TODO: Detect filesystem?
-       return VFS_Mount(tpath, mountpoint, "fat", "");
+       return VFS_Mount(tpath, mountpoint, "", "");
 }
 
 int DiskTool_Copy(const char *Source, const char *Destination)
@@ -95,6 +146,19 @@ int DiskTool_ListDirectory(const char *Directory)
        return 0;
 }
 
+int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest)
+{
+       return VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512;
+}
+int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest)
+{
+       return VFS_WriteAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest) / 512;
+}
+void DiskTool_LVM_Cleanup(void *Handle)
+{
+       VFS_Close( (int)(tVAddr)Handle );
+}
+
 // --- Internal helpers ---
 int DiskTool_int_TranslateOpen(const char *File, int Flags)
 {

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