blob: 729f96cbf5e20239f5e6c2dea64eae5d0764cf42 [file] [log] [blame]
/*
* Copyright (c) 2009 Travis Geiselbrecht
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <debug.h>
#include <string.h>
#include <lib/console.h>
#include <lib/fs.h>
#include <stdio.h>
#include <stdlib.h>
#include <platform.h>
#include <err.h>
static void test_normalize(const char *in)
{
char path[1024];
strlcpy(path, in, sizeof(path));
fs_normalize_path(path);
printf("'%s' -> '%s'\n", in, path);
}
#if 0
test_normalize("/");
test_normalize("/test");
test_normalize("/test/");
test_normalize("test/");
test_normalize("test");
test_normalize("/test//");
test_normalize("/test/foo");
test_normalize("/test/foo/");
test_normalize("/test/foo/bar");
test_normalize("/test/foo/bar//");
test_normalize("/test//foo/bar//");
test_normalize("/test//./foo/bar//");
test_normalize("/test//./.foo/bar//");
test_normalize("/test//./..foo/bar//");
test_normalize("/test//./../foo/bar//");
test_normalize("/test/../foo");
test_normalize("/test/bar/../foo");
test_normalize("../foo");
test_normalize("../foo/");
test_normalize("/../foo");
test_normalize("/../foo/");
test_normalize("/../../foo");
test_normalize("/bleh/../../foo");
test_normalize("/bleh/bar/../../foo");
test_normalize("/bleh/bar/../../foo/..");
test_normalize("/bleh/bar/../../foo/../meh");
#endif
#if defined(WITH_LIB_CONSOLE)
#if LK_DEBUGLEVEL > 1
static int cmd_fs(int argc, const cmd_args *argv);
STATIC_COMMAND_START
STATIC_COMMAND("fs", "fs debug commands", &cmd_fs)
STATIC_COMMAND_END(fs);
extern int fs_mount_type(const char *path, const char *device, const char *name);
static int cmd_fs_ioctl(int argc, const cmd_args *argv)
{
if (argc < 3) {
printf("not enough arguments\n");
return ERR_INVALID_ARGS;
}
int request = argv[2].u;
switch (request) {
case FS_IOCTL_GET_FILE_ADDR: {
if (argc < 4) {
printf("%s %s %lu <path>\n", argv[0].str, argv[1].str,
argv[2].u);
return ERR_INVALID_ARGS;
}
int err;
filehandle *handle;
err = fs_open_file(argv[3].str, &handle);
if (err != NO_ERROR) {
printf("error %d opening file\n", err);
return err;
}
void *file_addr;
err = fs_file_ioctl(handle, request, &file_addr);
if (err != NO_ERROR) {
fs_close_file(handle);
return err;
}
printf("%s is mapped at %p\n", argv[3].str, file_addr);
return fs_close_file(handle);
break;
}
default: {
printf("error, unsupported ioctl: %d\n", request);
}
}
return ERR_NOT_SUPPORTED;
}
static int cmd_fs(int argc, const cmd_args *argv)
{
int rc = 0;
if (argc < 2) {
notenoughargs:
printf("not enough arguments:\n");
usage:
printf("%s mount <path> <type> [device]\n", argv[0].str);
printf("%s unmount <path>\n", argv[0].str);
printf("%s write <path> <string> [<offset>]\n", argv[0].str);
printf("%s format <type> [device]\n", argv[0].str);
printf("%s stat <path>\n", argv[0].str);
printf("%s ioctl <request> [args...]\n", argv[0].str);
return -1;
}
if (!strcmp(argv[1].str, "mount")) {
int err;
if (argc < 4)
goto notenoughargs;
err = fs_mount(argv[2].str, argv[3].str,
(argc >= 5) ? argv[4].str : NULL);
if (err < 0) {
printf("error %d mounting device\n", err);
return err;
}
} else if (!strcmp(argv[1].str, "unmount")) {
int err;
if (argc < 3)
goto notenoughargs;
err = fs_unmount(argv[2].str);
if (err < 0) {
printf("error %d unmounting device\n", err);
return err;
}
} else if (!strcmp(argv[1].str, "format")) {
int err;
if (argc < 3) {
goto notenoughargs;
}
err = fs_format_device(
argv[2].str,
(argc >= 4) ? argv[3].str : NULL,
NULL
);
if (err != NO_ERROR) {
printf("error %d formatting device\n", err);
return err;
}
} else if (!strcmp(argv[1].str, "stat")) {
int err;
if (argc < 3) {
goto notenoughargs;
}
struct fs_stat stat;
err = fs_stat_fs(argv[2].str, &stat);
if (err != NO_ERROR) {
printf("error %d statting filesystem\n", err);
return err;
}
printf("\ttotal bytes: %llu\n", stat.total_space);
printf("\tfree bytes: %llu\n", stat.free_space);
printf("\n");
printf("\ttotal inodes: %d\n", stat.total_inodes);
printf("\tfree inodes: %d\n", stat.free_inodes);
} else if (!strcmp(argv[1].str, "ioctl")) {
return cmd_fs_ioctl(argc, argv);
} else if (!strcmp(argv[1].str, "write")) {
int err;
off_t off;
filehandle *handle;
struct file_stat stat;
if (argc < 3)
goto notenoughargs;
err = fs_open_file(argv[2].str, &handle);
if (err < 0) {
printf("error %d opening file\n", err);
return err;
}
err = fs_stat_file(handle, &stat);
if (err < 0) {
printf("error %d stat'ing file\n", err);
fs_close_file(handle);
return err;
}
if (argc < 5)
off = stat.size;
else
off = argv[4].u;
err = fs_write_file(handle, argv[3].str, off, strlen(argv[3].str));
if (err < 0) {
printf("error %d writing file\n", err);
fs_close_file(handle);
return err;
}
fs_close_file(handle);
} else {
printf("unrecognized subcommand\n");
goto usage;
}
return rc;
}
#endif
#endif