DiskTool - Added LVM support
[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
16 // === PROTOTYPES ===
17 void    DiskTool_Initialise(void)       __attribute__((constructor(101)));
18  int    DiskTool_int_TranslateOpen(const char *File, int Mode);
19  int    DiskTook_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest);
20  int    DiskTook_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest);
21
22 // === GLOBALS ===
23 tLVM_VolType    gDiskTool_VolumeType = {
24         .Name = "DiskTool",
25         .Read  = DiskTook_LVM_Read,
26         .Write = DiskTook_LVM_Write
27 };
28
29 // === CODE ===
30 void DiskTool_Initialise(void)
31 {
32         VFS_Init();
33         NativeFS_Install(NULL);
34         VFS_MkDir("/Native");
35         VFS_Mount("/", "/Native", "nativefs", "");
36 }
37
38 int DiskTool_RegisterLVM(const char *Identifier, const char *Path)
39 {
40         int fd = DiskTool_int_TranslateOpen(Path, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE);
41         if(fd == -1)
42                 return -1;
43         VFS_Seek(fd, 0, SEEK_END);
44         LVM_AddVolume( &gDiskTool_VolumeType, Identifier, (void*)(tVAddr)fd, 512, VFS_Tell(fd)/512);
45         return 0;
46 }
47
48 int DiskTool_MountImage(const char *Identifier, const char *Path)
49 {
50         // Validate Identifier and make mountpoint string
51         char mountpoint[sizeof("/Mount/") + strlen(Identifier) + 1];
52         strcpy(mountpoint, "/Mount/");
53         strcat(mountpoint, Identifier);
54         
55         // Translate path       
56         size_t tpath_len = DiskTool_int_TranslatePath(NULL, Path);
57         if(tpath_len == -1)
58                 return -1;
59         char tpath[tpath_len-1];
60         DiskTool_int_TranslatePath(tpath, Path);
61         
62         // Call mount
63         // TODO: Detect filesystem?
64         return VFS_Mount(tpath, mountpoint, "fat", "");
65 }
66
67 int DiskTool_Copy(const char *Source, const char *Destination)
68 {
69         int src = DiskTool_int_TranslateOpen(Source, VFS_OPENFLAG_READ);
70         if( src == -1 ) {
71                 Log_Error("DiskTool", "Unable to open %s for reading", Source);
72                 return -1;
73         }
74         int dst = DiskTool_int_TranslateOpen(Destination, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_CREATE);
75         if( dst == -1 ) {
76                 Log_Error("DiskTool", "Unable to open %s for writing", Destination);
77                 VFS_Close(src);
78                 return -1;
79         }
80
81         char    buf[1024];
82         size_t  len, total = 0;
83         while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) ) {
84                 VFS_Write(dst, len, buf);
85                 total += len;
86         }
87         VFS_Write(dst, len, buf), total += len;
88
89         Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination);
90
91         VFS_Close(dst);
92         VFS_Close(src);
93         
94         return 0;
95 }
96
97 int DiskTool_ListDirectory(const char *Directory)
98 {
99         int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY);
100         if(fd == -1) {
101 //              fprintf(stderr, "Can't open '%s'\n", Directory);
102                 return -1;
103         }
104
105         Log("Directory listing of '%s'", Directory);    
106
107         char    name[256];
108         while( VFS_ReadDir(fd, name) )
109         {
110                 Log("- %s", name);
111         }
112         
113         VFS_Close(fd);
114         
115         return 0;
116 }
117
118 int DiskTook_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest)
119 {
120         VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest);
121         return 0;
122 }
123 int DiskTook_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest)
124 {
125         VFS_WriteAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest);
126         return 0;
127 }
128
129 // --- Internal helpers ---
130 int DiskTool_int_TranslateOpen(const char *File, int Flags)
131 {
132         size_t tpath_len = DiskTool_int_TranslatePath(NULL, File);
133         if(tpath_len == -1)
134                 return -1;
135         char tpath[tpath_len-1];
136         DiskTool_int_TranslatePath(tpath, File);
137
138         return VFS_Open(tpath, Flags);
139 }
140

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