DiskTool - Cleanup, copy command
[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
12 // === IMPORTS ===
13 extern int      NativeFS_Install(char **Arguments);
14
15 // === PROTOTYPES ===
16 void    DiskTool_Initialise(void)       __attribute__((constructor(101)));
17  int    DiskTool_int_TranslateOpen(const char *File, int Mode);
18
19 // === CODE ===
20 void DiskTool_Initialise(void)
21 {
22         VFS_Init();
23         NativeFS_Install(NULL);
24         VFS_MkDir("/Native");
25         VFS_Mount("/", "/Native", "nativefs", "");
26 }
27
28 int DiskTool_MountImage(const char *Identifier, const char *Path)
29 {
30         // Validate Identifier and make mountpoint string
31         char mountpoint[sizeof("/Mount/") + strlen(Identifier) + 1];
32         strcpy(mountpoint, "/Mount/");
33         strcat(mountpoint, Identifier);
34         
35         // Translate path       
36         size_t tpath_len = DiskTool_int_TranslatePath(NULL, Path);
37         if(tpath_len == -1)
38                 return -1;
39         char tpath[tpath_len-1];
40         DiskTool_int_TranslatePath(tpath, Path);
41         
42         // Call mount
43         // TODO: Detect filesystem?
44         return VFS_Mount(tpath, mountpoint, "fat", "");
45 }
46
47 int DiskTool_Copy(const char *Source, const char *Destination)
48 {
49         int src = DiskTool_int_TranslateOpen(Source, VFS_OPENFLAG_READ);
50         if( src == -1 ) {
51                 Log_Error("DiskTool", "Unable to open %s for reading", Source);
52                 return -1;
53         }
54         int dst = DiskTool_int_TranslateOpen(Destination, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_CREATE);
55         if( dst == -1 ) {
56                 Log_Error("DiskTool", "Unable to open %s for writing", Destination);
57                 VFS_Close(src);
58                 return -1;
59         }
60
61         char    buf[1024];
62         size_t  len, total = 0;
63         while( (len = VFS_Read(src, sizeof(buf), buf)) == sizeof(buf) )
64                 VFS_Write(dst, len, buf), total += len;
65         VFS_Write(dst, len, buf), total += len;
66
67         Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination);
68
69         VFS_Close(dst);
70         VFS_Close(src);
71         
72         return 0;
73 }
74
75 int DiskTool_ListDirectory(const char *Directory)
76 {
77         int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY);
78         if(fd == -1) {
79 //              fprintf(stderr, "Can't open '%s'\n", Directory);
80                 return -1;
81         }
82
83         Log("Directory listing of '%s'", Directory);    
84
85         char    name[256];
86         while( VFS_ReadDir(fd, name) )
87         {
88                 Log("- %s", name);
89         }
90         
91         VFS_Close(fd);
92         
93         return 0;
94 }
95
96 // --- Internal helpers ---
97 int DiskTool_int_TranslateOpen(const char *File, int Flags)
98 {
99         size_t tpath_len = DiskTool_int_TranslatePath(NULL, File);
100         if(tpath_len == -1)
101                 return -1;
102         char tpath[tpath_len-1];
103         DiskTool_int_TranslatePath(tpath, File);
104
105         return VFS_Open(tpath, Flags);
106 }
107

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