6cbd7b852629ba306ddc54680ec6510b3a085ac0
[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);
65                 total += len;
66         }
67         VFS_Write(dst, len, buf), total += len;
68
69         Log_Notice("DiskTool", "Copied %i from %s to %s", total, Source, Destination);
70
71         VFS_Close(dst);
72         VFS_Close(src);
73         
74         return 0;
75 }
76
77 int DiskTool_ListDirectory(const char *Directory)
78 {
79         int fd = DiskTool_int_TranslateOpen(Directory, VFS_OPENFLAG_READ|VFS_OPENFLAG_DIRECTORY);
80         if(fd == -1) {
81 //              fprintf(stderr, "Can't open '%s'\n", Directory);
82                 return -1;
83         }
84
85         Log("Directory listing of '%s'", Directory);    
86
87         char    name[256];
88         while( VFS_ReadDir(fd, name) )
89         {
90                 Log("- %s", name);
91         }
92         
93         VFS_Close(fd);
94         
95         return 0;
96 }
97
98 // --- Internal helpers ---
99 int DiskTool_int_TranslateOpen(const char *File, int Flags)
100 {
101         size_t tpath_len = DiskTool_int_TranslatePath(NULL, File);
102         if(tpath_len == -1)
103                 return -1;
104         char tpath[tpath_len-1];
105         DiskTool_int_TranslatePath(tpath, File);
106
107         return VFS_Open(tpath, Flags);
108 }
109

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