blob: 6d9b43396e5758cc502dbc63f31123e2c6d68ffd [file] [log] [blame]
Travis Geiselbrechtf86c43c2010-05-06 14:17:34 -07001/*
2 * Copyright (c) 2009 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <debug.h>
24#include <string.h>
25#include <lib/console.h>
26#include <lib/fs.h>
27#include <stdlib.h>
28#include <platform.h>
29
30#if defined(WITH_LIB_CONSOLE)
31
32#if DEBUGLEVEL > 1
33static int cmd_fs(int argc, const cmd_args *argv);
34
35STATIC_COMMAND_START
36STATIC_COMMAND("fs", "fs debug commands", &cmd_fs)
37STATIC_COMMAND_END(fs);
38
39extern int fs_mount_type(const char *path, const char *device, const char *name);
40extern int fs_create_file(const char *path, filecookie *fcookie);
41extern int fs_make_dir(const char *path);
42extern int fs_write_file(filecookie fcookie, const void *buf, off_t offset, size_t len);
43
44static int cmd_fs(int argc, const cmd_args *argv)
45{
46 int rc = 0;
47
48 if (argc < 2) {
49notenoughargs:
50 printf("not enough arguments:\n");
51usage:
52 printf("%s mount <path> <device> [<type>]\n", argv[0].str);
53 printf("%s unmount <path>\n", argv[0].str);
54 printf("%s create <path>\n", argv[0].str);
55 printf("%s mkdir <path>\n", argv[0].str);
56 printf("%s read <path> [<offset>] [<len>]\n", argv[0].str);
57 printf("%s write <path> <string> [<offset>]\n", argv[0].str);
58 printf("%s stat <file>\n", argv[0].str);
59 return -1;
60 }
61
62 if (!strcmp(argv[1].str, "mount")) {
63 int err;
64
65 if (argc < 4)
66 goto notenoughargs;
67
68 if (argc < 5)
69 err = fs_mount(argv[2].str, argv[3].str);
70
71 else
72 err = fs_mount_type(argv[2].str, argv[3].str, argv[4].str);
73
74 if (err < 0) {
75 printf("error %d mounting device\n", err);
76 return err;
77 }
78 } else if (!strcmp(argv[1].str, "unmount")) {
79 int err;
80
81 if (argc < 3)
82 goto notenoughargs;
83
84 err = fs_unmount(argv[2].str);
85 if (err < 0) {
86 printf("error %d unmounting device\n", err);
87 return err;
88 }
89 } else if (!strcmp(argv[1].str, "create")) {
90 int err;
91 filecookie cookie;
92
93 if (argc < 3)
94 goto notenoughargs;
95
96 err = fs_create_file(argv[2].str, &cookie);
97 if (err < 0) {
98 printf("error %d creating file\n", err);
99 return err;
100 }
101
102 fs_close_file(cookie);
103 } else if (!strcmp(argv[1].str, "mkdir")) {
104 int err;
105
106 if (argc < 3)
107 goto notenoughargs;
108
109 err = fs_make_dir(argv[2].str);
110 if (err < 0) {
111 printf("error %d making directory\n", err);
112 return err;
113 }
114 } else if (!strcmp(argv[1].str, "read")) {
115 int err;
116 char *buf;
117 off_t off;
118 size_t len;
119 filecookie cookie;
120 struct file_stat stat;
121
122 if (argc < 3)
123 goto notenoughargs;
124
125 err = fs_open_file(argv[2].str, &cookie);
126 if (err < 0) {
127 printf("error %d opening file\n", err);
128 return err;
129 }
130
131 err = fs_stat_file(cookie, &stat);
132 if (err < 0) {
133 printf("error %d stat'ing file\n", err);
134 fs_close_file(cookie);
135 return err;
136 }
137
138 if (argc < 4)
139 off = 0;
140
141 else
142 off = argv[3].u;
143
144 if (argc < 5)
145 len = stat.size - off;
146
147 else
148 len = argv[4].u;
149
150 buf = malloc(len + 1);
151
152 err = fs_read_file(cookie, buf, off, len);
153 if (err < 0) {
154 printf("error %d reading file\n", err);
155 free(buf);
156 fs_close_file(cookie);
157 return err;
158 }
159
160 buf[len] = '\0';
161 printf("%s\n", buf);
162 free(buf);
163 fs_close_file(cookie);
164 } else if (!strcmp(argv[1].str, "write")) {
165 int err;
166 off_t off;
167 filecookie cookie;
168 struct file_stat stat;
169
170 if (argc < 3)
171 goto notenoughargs;
172
173 err = fs_open_file(argv[2].str, &cookie);
174 if (err < 0) {
175 printf("error %d opening file\n", err);
176 return err;
177 }
178
179 err = fs_stat_file(cookie, &stat);
180 if (err < 0) {
181 printf("error %d stat'ing file\n", err);
182 fs_close_file(cookie);
183 return err;
184 }
185
186 if (argc < 5)
187 off = stat.size;
188
189 else
190 off = argv[4].u;
191
192 err = fs_write_file(cookie, argv[3].str, off, strlen(argv[3].str));
193 if (err < 0) {
194 printf("error %d writing file\n", err);
195 fs_close_file(cookie);
196 return err;
197 }
198
199 fs_close_file(cookie);
200 } else if (!strcmp(argv[1].str, "stat")) {
201 int err;
202 struct file_stat stat;
203 filecookie cookie;
204
205 if (argc < 3)
206 goto notenoughargs;
207
208 err = fs_open_file(argv[2].str, &cookie);
209 if (err < 0) {
210 printf("error %d opening file\n", err);
211 return err;
212 }
213
214 err = fs_stat_file(cookie, &stat);
215 if (err < 0) {
216 printf("error %d statting file\n", err);
217 fs_close_file(cookie);
218 return err;
219 }
220
221 printf("stat successful:\n");
222 printf("\tis_dir: %d\n", stat.is_dir ? 1 : 0);
223 printf("\tsize: %lld\n", stat.size);
224
225 fs_close_file(cookie);
226 } else {
227 printf("unrecognized subcommand\n");
228 goto usage;
229 }
230
231 return rc;
232}
233
234#endif
235
236#endif
237