Initial Commit
[Projects.git] / pkgbuilds / wiifuse / src / wiifuse-0.2.0 / server / source / fs_wrapper.c
blob4307b1061cfa949e0dbe8b515a3af81767e1cd72
1 /*
2 * Copyright (C) 2008 dhewg, #wiidev efnet
4 * this file is part of wiifuse
5 * http://wiibrew.org/index.php?title=Wiifuse
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <malloc.h>
27 #include <gctypes.h>
28 #include <gccore.h>
29 #include <ogcsys.h>
31 #include "../../shared.h"
32 #include "global.h"
33 #include "fs_nand.h"
35 bool wrapper_init (u8 **data, u16 *data_size) {
36 return nand_init (data, data_size);
39 bool wrapper_deinit () {
40 return nand_deinit ();
43 enum remote_result wrapper_access (struct client_request *request) {
44 u8 mode;
46 if (!request->data || request->data_size != 1)
47 return RES_UNKNOWN;
49 mode = request->data[0];
51 LOG (2, "access %s 0x%02x", request->filename, mode);
53 return nand_access (request->filename, mode);
56 enum remote_result wrapper_getattr (struct client_request *request, u8 **data,
57 u16 *data_size) {
58 enum remote_result res = RES_UNKNOWN;
59 struct remote_attr attributes;
61 LOG (2, "getattr %s", request->filename);
63 memset (&attributes, 0, sizeof (struct remote_attr));
65 res = nand_getattr (request->filename, &attributes);
67 if (res == RES_OK) {
68 *data_size = 17;
69 *data = malloc (*data_size);
70 memset (*data, 0, *data_size);
71 *data[0] = attributes.is_dir;
72 put_be32 (*data + 1, attributes.nlink);
73 put_be32 (*data + 3, attributes.size);
74 put_be32 (*data + 7, attributes.owner);
75 put_be32 (*data + 11, attributes.group);
76 put_be16 (*data + 15, attributes.perms);
79 return res;
82 enum remote_result wrapper_open (struct client_request *request) {
83 LOG (2, "open %s", request->filename);
85 return nand_open (request->filename);
88 enum remote_result wrapper_read (struct client_request *request, u8 **data,
89 u16 *data_size) {
90 enum remote_result res;
91 u16 size;
92 u64 offset;
94 if (!request->data || request->data_size != 10)
95 return RES_UNKNOWN;
97 size = get_be16 (request->data);
98 offset = get_be64 (&request->data[2]);
100 LOG (2, "read %s off=0x%016llx size=0x%04x", request->filename, offset,
101 size);
103 *data = memalign (32, size);
104 memset (*data, 0, size);
106 res = nand_read (request->filename, offset, &size, *data);
108 if (res == RES_OK)
109 *data_size = size;
110 else {
111 free (*data);
112 *data = NULL;
115 return res;
118 enum remote_result wrapper_opendir (struct client_request *request) {
119 LOG (2, "opendir %s", request->filename);
121 return nand_opendir (request->filename);
124 enum remote_result wrapper_readdir (struct client_request *request, u8 **data,
125 u16 *data_size) {
126 LOG (2, "readdir %s", request->filename);
128 return nand_readdir (request->filename, data, data_size);
131 enum remote_result wrapper_mkdir (struct client_request *request) {
132 u16 perms;
134 if (!request->data || request->data_size != 2)
135 return RES_UNKNOWN;
137 perms = get_be16 (request->data);
139 LOG (2, "mkdir %s 0%o", request->filename, perms);
141 return nand_mkdir (request->filename, perms);
144 enum remote_result wrapper_rmdir (struct client_request *request) {
145 LOG (2, "rmdir %s", request->filename);
147 return nand_rmdir (request->filename);
150 enum remote_result wrapper_create (struct client_request *request) {
151 u16 perms;
153 if (!request->data || request->data_size != 2)
154 return RES_UNKNOWN;
156 perms = get_be16 (request->data);
158 LOG (2, "create %s 0%o", request->filename, perms);
160 return nand_create (request->filename, perms);
163 enum remote_result wrapper_write (struct client_request *request, u8 **data,
164 u16 *data_size) {
165 enum remote_result res;
166 u16 size;
167 u64 offset;
169 if (!request->data || request->data_size < 10)
170 return RES_UNKNOWN;
172 size = get_be16 (request->data);
173 offset = get_be64 (&request->data[2]);
175 LOG (2, "write %s off=0x%016llx size=0x%04x", request->filename,
176 offset, size);
178 res = nand_write (request->filename, offset, &size, request->data + 10);
180 if (res == RES_OK) {
181 *data = malloc (8);
182 put_be64 (*data, size);
183 *data_size = 8;
186 return res;
189 enum remote_result wrapper_chown (struct client_request *request) {
190 u32 uid, gid;
192 if (!request->data || request->data_size != 8)
193 return RES_UNKNOWN;
195 uid = get_be32 (request->data);
196 gid = get_be32 (&(request->data[4]));
198 LOG (2, "chown %s uid %u gid %u", request->filename, uid, gid);
200 return nand_chown (request->filename, uid, gid);
203 enum remote_result wrapper_chmod (struct client_request *request) {
204 u16 perms;
206 if (!request->data || request->data_size != 2)
207 return RES_UNKNOWN;
209 perms = get_be16 (request->data);
211 LOG (2, "chmod %s 0%o", request->filename, perms);
213 return nand_chmod (request->filename, perms);
216 enum remote_result wrapper_rename (struct client_request *request) {
217 char *src, *dst;
218 enum remote_result res;
220 if (!request->data)
221 return RES_UNKNOWN;
223 src = memalign (32, strlen (request->filename) + 1);
224 strcpy (src, request->filename);
226 dst = memalign (32, request->data_size + 1);
227 memcpy (dst, request->data, request->data_size);
228 dst[request->data_size] = 0;
230 LOG (2, "rename %s -> %s", src, dst);
232 res = nand_rename (src, dst);
234 free (src);
235 free (dst);
237 return res;
240 enum remote_result wrapper_unlink (struct client_request *request) {
241 LOG (2, "unlink %s", request->filename);
243 return nand_unlink (request->filename);
246 enum remote_result wrapper_statfs (struct client_request *request, u8 **data,
247 u16 *data_size) {
248 u64 bsize = 0;
249 u64 blocks = 0;
250 u64 bfree = 0;
251 u64 files = 0;
252 u64 ffree = 0;
254 s32 res;
255 u8 *p;
257 LOG (2, "statfs %s", request->filename);
259 res = nand_statfs (request->filename, &bsize, &blocks, &bfree, &files,
260 &ffree);
262 if (res == RES_OK) {
263 *data_size = 5 * 8;
264 *data = malloc (*data_size);
265 p = *data;
266 put_be64 (p, bsize);
267 p += 8;
268 put_be64 (p, blocks);
269 p += 8;
270 put_be64 (p, bfree);
271 p += 8;
272 put_be64 (p, files);
273 p += 8;
274 put_be64 (p, ffree);
277 return RES_OK;