2014-06-08 10:51:01 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
*/
|
2014-06-08 10:13:29 +04:00
|
|
|
/* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2011-2014 Kevin Lange
|
|
|
|
* Copyright (C) 2014 Lioncash
|
|
|
|
* Copyright (C) 2012 Tianyi Wang
|
2011-12-11 03:34:10 +04:00
|
|
|
*
|
|
|
|
* Virtual File System
|
|
|
|
*
|
2011-12-09 01:37:11 +04:00
|
|
|
*/
|
2011-01-22 10:09:47 +03:00
|
|
|
#include <system.h>
|
|
|
|
#include <fs.h>
|
2014-04-06 03:36:17 +04:00
|
|
|
#include <printf.h>
|
2011-11-29 23:28:36 +04:00
|
|
|
#include <list.h>
|
2011-12-09 01:37:11 +04:00
|
|
|
#include <process.h>
|
2012-09-04 09:35:11 +04:00
|
|
|
#include <logging.h>
|
2011-01-22 10:09:47 +03:00
|
|
|
|
2013-03-15 11:20:55 +04:00
|
|
|
tree_t * fs_tree = NULL; /* File system mountpoint tree */
|
|
|
|
fs_node_t * fs_root = NULL; /* Pointer to the root mount fs_node (must be some form of filesystem, even ramdisk) */
|
2011-01-22 10:09:47 +03:00
|
|
|
|
2014-03-17 08:41:19 +04:00
|
|
|
|
|
|
|
static struct dirent * readdir_mapper(fs_node_t *node, uint32_t index) {
|
|
|
|
tree_node_t * d = (tree_node_t *)node->device;
|
|
|
|
|
|
|
|
if (index == 0) {
|
|
|
|
struct dirent * dir = malloc(sizeof(struct dirent));
|
|
|
|
strcpy(dir->name, ".");
|
|
|
|
dir->ino = 0;
|
|
|
|
return dir;
|
|
|
|
} else if (index == 1) {
|
|
|
|
struct dirent * dir = malloc(sizeof(struct dirent));
|
|
|
|
strcpy(dir->name, "..");
|
|
|
|
dir->ino = 1;
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
index -= 2;
|
|
|
|
unsigned int i = 0;
|
|
|
|
foreach(child, d->children) {
|
|
|
|
if (i == index) {
|
|
|
|
/* Recursively print the children */
|
|
|
|
tree_node_t * tchild = (tree_node_t *)child->value;
|
|
|
|
struct vfs_entry * n = (struct vfs_entry *)tchild->value;
|
|
|
|
struct dirent * dir = malloc(sizeof(struct dirent));
|
|
|
|
|
|
|
|
memcpy(&dir->name, n->name, min(256, strlen(n->name)+1));
|
|
|
|
dir->ino = i;
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fs_node_t * vfs_mapper(void) {
|
|
|
|
fs_node_t * fnode = malloc(sizeof(fs_node_t));
|
|
|
|
memset(fnode, 0x00, sizeof(fs_node_t));
|
|
|
|
fnode->mask = 0666;
|
|
|
|
fnode->flags = FS_DIRECTORY;
|
|
|
|
fnode->readdir = readdir_mapper;
|
|
|
|
return fnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/**
|
|
|
|
* read_fs: Read a file system node based on its underlying type.
|
|
|
|
*
|
|
|
|
* @param node Node to read
|
|
|
|
* @param offset Offset into the node data to read from
|
|
|
|
* @param size How much data to read (in bytes)
|
|
|
|
* @param buffer A buffer to copy of the read data into
|
|
|
|
* @returns Bytes read
|
|
|
|
*/
|
2011-01-22 10:09:47 +03:00
|
|
|
uint32_t read_fs(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
|
2014-05-28 08:38:30 +04:00
|
|
|
if (!node) return -1;
|
|
|
|
|
2012-01-26 05:25:59 +04:00
|
|
|
if (node->read) {
|
2011-12-11 05:15:12 +04:00
|
|
|
uint32_t ret = node->read(node, offset, size, buffer);
|
|
|
|
return ret;
|
2011-01-22 10:09:47 +03:00
|
|
|
} else {
|
2014-04-02 11:26:04 +04:00
|
|
|
return -1;
|
2011-01-22 10:09:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/**
|
|
|
|
* write_fs: Write a file system node based on its underlying type.
|
|
|
|
*
|
|
|
|
* @param node Node to write to
|
|
|
|
* @param offset Offset into the node data to write to
|
|
|
|
* @param size How much data to write (in bytes)
|
|
|
|
* @param buffer A buffer to copy from
|
|
|
|
* @returns Bytes written
|
|
|
|
*/
|
2011-01-22 10:09:47 +03:00
|
|
|
uint32_t write_fs(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
|
2014-05-28 08:38:30 +04:00
|
|
|
if (!node) return -1;
|
|
|
|
|
2012-01-26 05:25:59 +04:00
|
|
|
if (node->write) {
|
2011-12-11 05:15:12 +04:00
|
|
|
uint32_t ret = node->write(node, offset, size, buffer);
|
|
|
|
return ret;
|
2011-01-22 10:09:47 +03:00
|
|
|
} else {
|
2014-04-02 11:26:04 +04:00
|
|
|
return -1;
|
2011-01-22 10:09:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 10:20:02 +04:00
|
|
|
volatile uint8_t tmp_refcount_lock = 0;
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/**
|
|
|
|
* open_fs: Open a file system node.
|
|
|
|
*
|
|
|
|
* @param node Node to open
|
2013-04-24 11:19:08 +04:00
|
|
|
* @param flags Same as open, specifies read/write/append/truncate
|
2012-01-31 02:48:54 +04:00
|
|
|
*/
|
2013-04-24 11:19:08 +04:00
|
|
|
void open_fs(fs_node_t *node, unsigned int flags) {
|
2014-05-28 08:38:30 +04:00
|
|
|
|
|
|
|
if (!node) return;
|
|
|
|
|
|
|
|
if (node->refcount >= 0) {
|
2014-05-28 10:20:02 +04:00
|
|
|
spin_lock(&tmp_refcount_lock);
|
2014-05-28 08:38:30 +04:00
|
|
|
node->refcount++;
|
2014-05-28 10:20:02 +04:00
|
|
|
spin_unlock(&tmp_refcount_lock);
|
2014-05-28 08:38:30 +04:00
|
|
|
}
|
|
|
|
|
2012-01-26 05:25:59 +04:00
|
|
|
if (node->open) {
|
2013-04-24 11:19:08 +04:00
|
|
|
node->open(node, flags);
|
2011-01-22 10:09:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/**
|
|
|
|
* close_fs: Close a file system node
|
|
|
|
*
|
|
|
|
* @param node Node to close
|
|
|
|
*/
|
2011-01-22 10:09:47 +03:00
|
|
|
void close_fs(fs_node_t *node) {
|
2012-01-31 02:48:54 +04:00
|
|
|
assert(node != fs_root && "Attempted to close the filesystem root. kablooey");
|
2014-05-28 08:38:30 +04:00
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
debug_print(WARNING, "Double close? This isn't an fs_node.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-28 10:20:02 +04:00
|
|
|
spin_lock(&tmp_refcount_lock);
|
2014-05-28 08:38:30 +04:00
|
|
|
node->refcount--;
|
|
|
|
if (node->refcount == 0) {
|
|
|
|
debug_print(NOTICE, "Node refcount [%s] is now 0: %d", node->name, node->refcount);
|
|
|
|
|
|
|
|
if (node->close) {
|
|
|
|
node->close(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(node);
|
2011-01-22 10:09:47 +03:00
|
|
|
}
|
2014-05-28 10:20:02 +04:00
|
|
|
spin_unlock(&tmp_refcount_lock);
|
2011-01-22 10:09:47 +03:00
|
|
|
}
|
|
|
|
|
2013-04-23 12:14:33 +04:00
|
|
|
/**
|
|
|
|
* chmod_fs
|
|
|
|
*/
|
|
|
|
int chmod_fs(fs_node_t *node, int mode) {
|
|
|
|
if (node->chmod) {
|
|
|
|
return node->chmod(node, mode);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/**
|
|
|
|
* readdir_fs: Read a directory for the requested index
|
|
|
|
*
|
|
|
|
* @param node Directory to read
|
|
|
|
* @param index Offset to look for
|
|
|
|
* @returns A dirent object.
|
|
|
|
*/
|
2012-01-26 05:25:59 +04:00
|
|
|
struct dirent *readdir_fs(fs_node_t *node, uint32_t index) {
|
2014-05-28 08:38:30 +04:00
|
|
|
if (!node) return NULL;
|
|
|
|
|
2012-01-26 05:25:59 +04:00
|
|
|
if ((node->flags & FS_DIRECTORY) && node->readdir) {
|
|
|
|
struct dirent *ret = node->readdir(node, index);
|
2011-12-11 05:15:12 +04:00
|
|
|
return ret;
|
2011-01-22 10:09:47 +03:00
|
|
|
} else {
|
|
|
|
return (struct dirent *)NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/**
|
|
|
|
* finddir_fs: Find the requested file in the directory and return an fs_node for it
|
|
|
|
*
|
|
|
|
* @param node Directory to search
|
|
|
|
* @param name File to look for
|
|
|
|
* @returns An fs_node that the caller can free
|
|
|
|
*/
|
2011-01-22 10:09:47 +03:00
|
|
|
fs_node_t *finddir_fs(fs_node_t *node, char *name) {
|
2014-05-28 08:38:30 +04:00
|
|
|
if (!node) return NULL;
|
|
|
|
|
2012-01-26 05:25:59 +04:00
|
|
|
if ((node->flags & FS_DIRECTORY) && node->finddir) {
|
|
|
|
fs_node_t *ret = node->finddir(node, name);
|
2011-12-11 05:15:12 +04:00
|
|
|
return ret;
|
2011-01-22 10:09:47 +03:00
|
|
|
} else {
|
2012-09-29 11:39:01 +04:00
|
|
|
debug_print(WARNING, "Node passed to finddir_fs isn't a directory!");
|
2013-03-18 11:52:12 +04:00
|
|
|
debug_print(WARNING, "node = 0x%x, name = %s", node, name);
|
2011-01-22 10:09:47 +03:00
|
|
|
return (fs_node_t *)NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-19 10:57:40 +04:00
|
|
|
/**
|
|
|
|
* ioctl_fs: Control Device
|
|
|
|
*
|
|
|
|
* @param node Device node to control
|
|
|
|
* @param request Device-specific request code
|
|
|
|
* @param argp Depends on `request`
|
|
|
|
* @returns Depends on `request`
|
|
|
|
*/
|
|
|
|
int ioctl_fs(fs_node_t *node, int request, void * argp) {
|
2014-05-28 08:38:30 +04:00
|
|
|
if (!node) return -1;
|
|
|
|
|
2013-03-19 10:57:40 +04:00
|
|
|
if (node->ioctl) {
|
|
|
|
return node->ioctl(node, request, argp);
|
|
|
|
} else {
|
|
|
|
return -1; /* TODO Should actually be ENOTTY, but we're bad at error numbers */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/*
|
|
|
|
* XXX: The following two function should be replaced with
|
|
|
|
* one function to create children of directory nodes.
|
|
|
|
* There is no fundamental difference between a directory
|
|
|
|
* and a file, thus, the use of flag sets should suffice
|
|
|
|
*/
|
|
|
|
|
2012-02-06 02:04:41 +04:00
|
|
|
int create_file_fs(char *name, uint16_t permission) {
|
2013-04-22 10:17:26 +04:00
|
|
|
fs_node_t * parent;
|
|
|
|
char *cwd = (char *)(current_process->wd_name);
|
|
|
|
char *path = canonicalize_path(cwd, name);
|
|
|
|
|
|
|
|
char * parent_path = malloc(strlen(path) + 4);
|
|
|
|
sprintf(parent_path, "%s/..", path);
|
|
|
|
|
|
|
|
char * f_path = path + strlen(path) - 1;
|
|
|
|
while (f_path > path) {
|
|
|
|
if (*f_path == '/') {
|
|
|
|
f_path += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
f_path--;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_print(WARNING, "creating file %s within %s (hope these strings are good)", f_path, parent_path);
|
|
|
|
|
|
|
|
parent = kopen(parent_path, 0);
|
|
|
|
free(parent_path);
|
|
|
|
|
2013-05-12 11:38:56 +04:00
|
|
|
if (!parent) {
|
|
|
|
free(path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-22 10:17:26 +04:00
|
|
|
if (parent->create) {
|
|
|
|
parent->create(parent, f_path, permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
|
|
|
free(parent);
|
2013-04-23 12:14:33 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int unlink_fs(char * name) {
|
|
|
|
fs_node_t * parent;
|
|
|
|
char *cwd = (char *)(current_process->wd_name);
|
|
|
|
char *path = canonicalize_path(cwd, name);
|
|
|
|
|
|
|
|
char * parent_path = malloc(strlen(path) + 4);
|
|
|
|
sprintf(parent_path, "%s/..", path);
|
|
|
|
|
|
|
|
char * f_path = path + strlen(path) - 1;
|
|
|
|
while (f_path > path) {
|
|
|
|
if (*f_path == '/') {
|
|
|
|
f_path += 1;
|
2012-01-26 05:25:59 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-04-23 12:14:33 +04:00
|
|
|
f_path--;
|
2012-01-26 05:25:59 +04:00
|
|
|
}
|
|
|
|
|
2013-04-23 12:14:33 +04:00
|
|
|
debug_print(WARNING, "unlinking file %s within %s (hope these strings are good)", f_path, parent_path);
|
2012-01-26 05:25:59 +04:00
|
|
|
|
2013-04-23 12:14:33 +04:00
|
|
|
parent = kopen(parent_path, 0);
|
|
|
|
free(parent_path);
|
2012-01-26 05:25:59 +04:00
|
|
|
|
2013-05-12 11:38:56 +04:00
|
|
|
if (!parent) {
|
|
|
|
free(path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-23 12:14:33 +04:00
|
|
|
if (parent->unlink) {
|
|
|
|
parent->unlink(parent, f_path);
|
2012-01-26 05:25:59 +04:00
|
|
|
}
|
|
|
|
|
2013-04-23 12:14:33 +04:00
|
|
|
free(path);
|
|
|
|
free(parent);
|
|
|
|
|
2012-02-06 02:04:41 +04:00
|
|
|
return 0;
|
2012-01-26 05:25:59 +04:00
|
|
|
}
|
|
|
|
|
2012-02-06 02:04:41 +04:00
|
|
|
int mkdir_fs(char *name, uint16_t permission) {
|
2013-04-23 12:14:33 +04:00
|
|
|
fs_node_t * parent;
|
|
|
|
char *cwd = (char *)(current_process->wd_name);
|
|
|
|
char *path = canonicalize_path(cwd, name);
|
|
|
|
|
|
|
|
char * parent_path = malloc(strlen(path) + 4);
|
|
|
|
sprintf(parent_path, "%s/..", path);
|
|
|
|
|
|
|
|
char * f_path = path + strlen(path) - 1;
|
|
|
|
while (f_path > path) {
|
|
|
|
if (*f_path == '/') {
|
|
|
|
f_path += 1;
|
2012-01-26 05:25:59 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-04-23 12:14:33 +04:00
|
|
|
f_path--;
|
2012-01-26 05:25:59 +04:00
|
|
|
}
|
|
|
|
|
2013-04-23 12:14:33 +04:00
|
|
|
debug_print(WARNING, "creating directory %s within %s (hope these strings are good)", f_path, parent_path);
|
2012-01-26 05:25:59 +04:00
|
|
|
|
2013-04-23 12:14:33 +04:00
|
|
|
parent = kopen(parent_path, 0);
|
|
|
|
free(parent_path);
|
2012-01-26 05:25:59 +04:00
|
|
|
|
2013-05-12 11:38:56 +04:00
|
|
|
if (!parent) {
|
|
|
|
free(path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-23 12:14:33 +04:00
|
|
|
if (parent->mkdir) {
|
|
|
|
parent->mkdir(parent, f_path, permission);
|
2012-01-26 05:25:59 +04:00
|
|
|
}
|
|
|
|
|
2013-04-23 12:14:33 +04:00
|
|
|
free(path);
|
2014-06-06 06:08:07 +04:00
|
|
|
close_fs(parent);
|
2013-04-22 10:17:26 +04:00
|
|
|
|
2012-02-06 02:04:41 +04:00
|
|
|
return 0;
|
2012-01-26 05:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fs_node_t *clone_fs(fs_node_t *source) {
|
2014-05-28 08:38:30 +04:00
|
|
|
if (!source) return NULL;
|
|
|
|
|
|
|
|
if (source->refcount >= 0) {
|
2014-05-28 10:20:02 +04:00
|
|
|
spin_lock(&tmp_refcount_lock);
|
2014-05-28 08:38:30 +04:00
|
|
|
source->refcount++;
|
2014-05-28 10:20:02 +04:00
|
|
|
spin_unlock(&tmp_refcount_lock);
|
2011-12-09 01:25:48 +04:00
|
|
|
}
|
2014-05-28 08:38:30 +04:00
|
|
|
|
|
|
|
return source;
|
2011-12-09 01:25:48 +04:00
|
|
|
}
|
|
|
|
|
2012-01-26 05:25:59 +04:00
|
|
|
/**
|
2012-01-31 02:48:54 +04:00
|
|
|
* canonicalize_path: Canonicalize a path.
|
|
|
|
*
|
|
|
|
* @param cwd Current working directory
|
|
|
|
* @param input Path to append or canonicalize on
|
|
|
|
* @returns An absolute path string
|
2011-11-29 23:28:36 +04:00
|
|
|
*/
|
2012-01-26 05:25:59 +04:00
|
|
|
char *canonicalize_path(char *cwd, char *input) {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* This is a stack-based canonicalizer; we use a list as a stack */
|
2012-01-26 05:25:59 +04:00
|
|
|
list_t *out = list_create();
|
2011-11-29 23:28:36 +04:00
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/*
|
|
|
|
* If we have a relative path, we need to canonicalize
|
|
|
|
* the working directory and insert it into the stack.
|
|
|
|
*/
|
|
|
|
if (strlen(input) && input[0] != PATH_SEPARATOR) {
|
|
|
|
/* Make a copy of the working directory */
|
2012-01-26 05:25:59 +04:00
|
|
|
char *path = malloc((strlen(cwd) + 1) * sizeof(char));
|
2011-11-29 23:28:36 +04:00
|
|
|
memcpy(path, cwd, strlen(cwd) + 1);
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Setup tokenizer */
|
2012-01-26 05:25:59 +04:00
|
|
|
char *pch;
|
|
|
|
char *save;
|
2012-01-31 02:48:54 +04:00
|
|
|
pch = strtok_r(path,PATH_SEPARATOR_STRING,&save);
|
2011-11-29 23:28:36 +04:00
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Start tokenizing */
|
2011-11-29 23:28:36 +04:00
|
|
|
while (pch != NULL) {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Make copies of the path elements */
|
2012-01-26 05:25:59 +04:00
|
|
|
char *s = malloc(sizeof(char) * (strlen(pch) + 1));
|
2011-11-29 23:28:36 +04:00
|
|
|
memcpy(s, pch, strlen(pch) + 1);
|
2012-01-31 02:48:54 +04:00
|
|
|
/* And push them */
|
2011-11-29 23:28:36 +04:00
|
|
|
list_insert(out, s);
|
2012-01-31 02:48:54 +04:00
|
|
|
pch = strtok_r(NULL,PATH_SEPARATOR_STRING,&save);
|
2011-11-29 23:28:36 +04:00
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Similarly, we need to push the elements from the new path */
|
2012-01-26 05:25:59 +04:00
|
|
|
char *path = malloc((strlen(input) + 1) * sizeof(char));
|
2011-11-29 23:28:36 +04:00
|
|
|
memcpy(path, input, strlen(input) + 1);
|
2012-01-31 02:48:54 +04:00
|
|
|
|
|
|
|
/* Initialize the tokenizer... */
|
2012-01-26 05:25:59 +04:00
|
|
|
char *pch;
|
|
|
|
char *save;
|
2012-01-31 02:48:54 +04:00
|
|
|
pch = strtok_r(path,PATH_SEPARATOR_STRING,&save);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tokenize the path, this time, taking care to properly
|
|
|
|
* handle .. and . to represent up (stack pop) and current
|
|
|
|
* (do nothing)
|
|
|
|
*/
|
2011-11-29 23:28:36 +04:00
|
|
|
while (pch != NULL) {
|
2012-01-31 02:48:54 +04:00
|
|
|
if (!strcmp(pch,PATH_UP)) {
|
|
|
|
/*
|
|
|
|
* Path = ..
|
|
|
|
* Pop the stack to move up a directory
|
|
|
|
*/
|
2011-11-29 23:28:36 +04:00
|
|
|
node_t * n = list_pop(out);
|
2011-11-30 06:19:30 +04:00
|
|
|
if (n) {
|
|
|
|
free(n->value);
|
|
|
|
free(n);
|
|
|
|
}
|
2012-01-31 02:48:54 +04:00
|
|
|
} else if (!strcmp(pch,PATH_DOT)) {
|
|
|
|
/*
|
|
|
|
* Path = .
|
|
|
|
* Do nothing
|
|
|
|
*/
|
2011-11-29 23:28:36 +04:00
|
|
|
} else {
|
2012-01-31 02:48:54 +04:00
|
|
|
/*
|
|
|
|
* Regular path, push it
|
|
|
|
* XXX: Path elements should be checked for existence!
|
|
|
|
*/
|
2011-11-29 23:28:36 +04:00
|
|
|
char * s = malloc(sizeof(char) * (strlen(pch) + 1));
|
|
|
|
memcpy(s, pch, strlen(pch) + 1);
|
|
|
|
list_insert(out, s);
|
|
|
|
}
|
2012-01-31 02:48:54 +04:00
|
|
|
pch = strtok_r(NULL, PATH_SEPARATOR_STRING, &save);
|
2011-11-29 23:28:36 +04:00
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Calculate the size of the path string */
|
2011-11-29 23:28:36 +04:00
|
|
|
size_t size = 0;
|
|
|
|
foreach(item, out) {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Helpful use of our foreach macro. */
|
2011-11-29 23:28:36 +04:00
|
|
|
size += strlen(item->value) + 1;
|
|
|
|
}
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/* join() the list */
|
2012-01-26 05:25:59 +04:00
|
|
|
char *output = malloc(sizeof(char) * (size + 1));
|
|
|
|
char *output_offset = output;
|
2011-11-30 06:19:30 +04:00
|
|
|
if (size == 0) {
|
2012-01-31 02:48:54 +04:00
|
|
|
/*
|
|
|
|
* If the path is empty, we take this to mean the root
|
|
|
|
* thus we synthesize a path of "/" to return.
|
|
|
|
*/
|
2011-11-30 06:19:30 +04:00
|
|
|
output = realloc(output, sizeof(char) * 2);
|
2012-01-31 02:48:54 +04:00
|
|
|
output[0] = PATH_SEPARATOR;
|
2011-11-30 06:19:30 +04:00
|
|
|
output[1] = '\0';
|
|
|
|
} else {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Otherwise, append each element together */
|
2011-11-30 06:19:30 +04:00
|
|
|
foreach(item, out) {
|
2012-01-31 02:48:54 +04:00
|
|
|
output_offset[0] = PATH_SEPARATOR;
|
2011-11-30 06:19:30 +04:00
|
|
|
output_offset++;
|
|
|
|
memcpy(output_offset, item->value, strlen(item->value) + 1);
|
|
|
|
output_offset += strlen(item->value);
|
|
|
|
}
|
2011-11-29 23:28:36 +04:00
|
|
|
}
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Clean up the various things we used to get here */
|
2011-11-29 23:28:36 +04:00
|
|
|
list_destroy(out);
|
|
|
|
list_free(out);
|
|
|
|
free(out);
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/* And return a working, absolute path */
|
2011-11-29 23:28:36 +04:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2013-06-06 10:10:36 +04:00
|
|
|
void vfs_install(void) {
|
2013-03-15 11:20:55 +04:00
|
|
|
/* Initialize the mountpoint tree */
|
|
|
|
fs_tree = tree_create();
|
|
|
|
|
|
|
|
struct vfs_entry * root = malloc(sizeof(struct vfs_entry));
|
|
|
|
|
|
|
|
root->name = strdup("[root]");
|
|
|
|
root->file = NULL; /* Nothing mounted as root */
|
|
|
|
|
|
|
|
tree_set_root(fs_tree, root);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vfs_mount - Mount a file system to the specified path.
|
|
|
|
*
|
|
|
|
* For example, if we have an EXT2 filesystem with a root node
|
|
|
|
* of ext2_root and we want to mount it to /, we would run
|
|
|
|
* vfs_mount("/", ext2_root); - or, if we have a procfs node,
|
|
|
|
* we could mount that to /dev/procfs. Individual files can also
|
|
|
|
* be mounted.
|
|
|
|
*
|
|
|
|
* Paths here must be absolute.
|
|
|
|
*/
|
2014-03-17 08:41:19 +04:00
|
|
|
void * vfs_mount(char * path, fs_node_t * local_root) {
|
2013-03-15 11:20:55 +04:00
|
|
|
if (!fs_tree) {
|
|
|
|
debug_print(ERROR, "VFS hasn't been initialized, you can't mount things yet!");
|
2014-03-17 08:41:19 +04:00
|
|
|
return NULL;
|
2013-03-15 11:20:55 +04:00
|
|
|
}
|
|
|
|
if (!path || path[0] != '/') {
|
|
|
|
debug_print(ERROR, "Path must be absolute for mountpoint.");
|
2014-03-17 08:41:19 +04:00
|
|
|
return NULL;
|
2013-03-15 11:20:55 +04:00
|
|
|
}
|
|
|
|
|
2014-03-17 08:41:19 +04:00
|
|
|
tree_node_t * ret_val = NULL;
|
2013-03-15 11:20:55 +04:00
|
|
|
|
|
|
|
char * p = strdup(path);
|
|
|
|
char * i = p;
|
|
|
|
|
|
|
|
int path_len = strlen(p);
|
|
|
|
|
|
|
|
/* Chop the path up */
|
|
|
|
while (i < p + path_len) {
|
|
|
|
if (*i == PATH_SEPARATOR) {
|
|
|
|
*i = '\0';
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
/* Clean up */
|
|
|
|
p[path_len] = '\0';
|
|
|
|
i = p + 1;
|
|
|
|
|
|
|
|
/* Root */
|
|
|
|
tree_node_t * root_node = fs_tree->root;
|
|
|
|
|
|
|
|
if (*i == '\0') {
|
|
|
|
/* Special case, we're trying to set the root node */
|
|
|
|
struct vfs_entry * root = (struct vfs_entry *)root_node->value;
|
|
|
|
if (root->file) {
|
|
|
|
debug_print(WARNING, "Path %s already mounted, unmount before trying to mount something else.", path);
|
2014-03-17 08:41:19 +04:00
|
|
|
ret_val = root_node;
|
2013-03-15 11:20:55 +04:00
|
|
|
goto _vfs_cleanup;
|
|
|
|
}
|
|
|
|
root->file = local_root;
|
|
|
|
/* We also keep a legacy shortcut around for that */
|
|
|
|
fs_root = local_root;
|
|
|
|
} else {
|
|
|
|
tree_node_t * node = root_node;
|
|
|
|
char * at = i;
|
|
|
|
while (1) {
|
|
|
|
if (at >= p + path_len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int found = 0;
|
|
|
|
debug_print(INFO, "Searching for %s", at);
|
|
|
|
foreach(child, node->children) {
|
|
|
|
tree_node_t * tchild = (tree_node_t *)child->value;
|
|
|
|
struct vfs_entry * ent = (struct vfs_entry *)tchild->value;
|
|
|
|
if (!strcmp(ent->name, at)) {
|
|
|
|
found = 1;
|
|
|
|
node = tchild;
|
2014-03-17 08:41:19 +04:00
|
|
|
ret_val = node;
|
2013-03-15 11:20:55 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
debug_print(INFO, "Did not find %s, making it.", at);
|
|
|
|
struct vfs_entry * ent = malloc(sizeof(struct vfs_entry));
|
|
|
|
ent->name = strdup(at);
|
|
|
|
ent->file = NULL;
|
|
|
|
node = tree_node_insert_child(fs_tree, node, ent);
|
|
|
|
}
|
|
|
|
at = at + strlen(at) + 1;
|
|
|
|
}
|
|
|
|
struct vfs_entry * ent = (struct vfs_entry *)node->value;
|
|
|
|
if (ent->file) {
|
|
|
|
debug_print(WARNING, "Path %s already mounted, unmount before trying to mount something else.", path);
|
2014-03-17 08:41:19 +04:00
|
|
|
ret_val = node;
|
2013-03-15 11:20:55 +04:00
|
|
|
goto _vfs_cleanup;
|
|
|
|
}
|
|
|
|
ent->file = local_root;
|
|
|
|
}
|
|
|
|
|
|
|
|
_vfs_cleanup:
|
|
|
|
free(p);
|
|
|
|
return ret_val;
|
|
|
|
}
|
|
|
|
|
2014-03-17 08:41:19 +04:00
|
|
|
void map_vfs_directory(char * c) {
|
|
|
|
fs_node_t * f = vfs_mapper();
|
|
|
|
struct vfs_entry * e = vfs_mount(c, f);
|
|
|
|
strcpy(f->name, e->name);
|
|
|
|
f->device = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-15 11:20:55 +04:00
|
|
|
void debug_print_vfs_tree_node(tree_node_t * node, size_t height) {
|
|
|
|
/* End recursion on a blank entry */
|
|
|
|
if (!node) return;
|
2014-04-06 02:05:24 +04:00
|
|
|
char * tmp = malloc(512);
|
|
|
|
memset(tmp, 0, 512);
|
|
|
|
char * c = tmp;
|
2013-03-15 11:20:55 +04:00
|
|
|
/* Indent output */
|
2014-04-06 02:05:24 +04:00
|
|
|
for (uint32_t i = 0; i < height; ++i) {
|
|
|
|
c += sprintf(c, " ");
|
|
|
|
}
|
2013-03-15 11:20:55 +04:00
|
|
|
/* Get the current process */
|
|
|
|
struct vfs_entry * fnode = (struct vfs_entry *)node->value;
|
|
|
|
/* Print the process name */
|
|
|
|
if (fnode->file) {
|
2014-04-06 02:05:24 +04:00
|
|
|
c += sprintf(c, "%s → 0x%x (%s)", fnode->name, fnode->file, fnode->file->name);
|
2013-03-15 11:20:55 +04:00
|
|
|
} else {
|
2014-04-06 02:05:24 +04:00
|
|
|
c += sprintf(c, "%s → (empty)", fnode->name);
|
2013-03-15 11:20:55 +04:00
|
|
|
}
|
|
|
|
/* Linefeed */
|
2014-04-06 02:05:24 +04:00
|
|
|
debug_print(NOTICE, "%s", tmp);
|
|
|
|
free(tmp);
|
2013-03-15 11:20:55 +04:00
|
|
|
foreach(child, node->children) {
|
|
|
|
/* Recursively print the children */
|
|
|
|
debug_print_vfs_tree_node(child->value, height + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 10:10:36 +04:00
|
|
|
void debug_print_vfs_tree(void) {
|
2013-03-15 11:20:55 +04:00
|
|
|
debug_print_vfs_tree_node(fs_tree->root, 0);
|
|
|
|
}
|
|
|
|
|
2012-02-09 05:30:35 +04:00
|
|
|
/**
|
|
|
|
* get_mount_point
|
|
|
|
*
|
|
|
|
*/
|
2013-03-18 11:52:12 +04:00
|
|
|
fs_node_t *get_mount_point(char * path, unsigned int path_depth, char **outpath, unsigned int * outdepth) {
|
2012-02-09 05:30:35 +04:00
|
|
|
size_t depth;
|
|
|
|
|
2012-02-20 07:29:40 +04:00
|
|
|
for (depth = 0; depth <= path_depth; ++depth) {
|
|
|
|
path += strlen(path) + 1;
|
|
|
|
}
|
|
|
|
|
2013-03-15 11:20:55 +04:00
|
|
|
/* Last available node */
|
|
|
|
fs_node_t * last = fs_root;
|
|
|
|
tree_node_t * node = fs_tree->root;
|
|
|
|
|
|
|
|
char * at = *outpath;
|
2013-03-18 11:52:12 +04:00
|
|
|
int _depth = 1;
|
|
|
|
int _tree_depth = 0;
|
2012-02-09 05:30:35 +04:00
|
|
|
|
2013-03-15 11:20:55 +04:00
|
|
|
while (1) {
|
|
|
|
if (at >= path) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int found = 0;
|
|
|
|
debug_print(INFO, "Searching for %s", at);
|
|
|
|
foreach(child, node->children) {
|
|
|
|
tree_node_t * tchild = (tree_node_t *)child->value;
|
|
|
|
struct vfs_entry * ent = (struct vfs_entry *)tchild->value;
|
|
|
|
if (!strcmp(ent->name, at)) {
|
|
|
|
found = 1;
|
|
|
|
node = tchild;
|
|
|
|
at = at + strlen(at) + 1;
|
|
|
|
if (ent->file) {
|
2013-03-18 11:52:12 +04:00
|
|
|
_tree_depth = _depth;
|
2013-03-15 11:20:55 +04:00
|
|
|
last = ent->file;
|
|
|
|
*outpath = at;
|
|
|
|
}
|
|
|
|
break;
|
2012-02-09 05:30:35 +04:00
|
|
|
}
|
|
|
|
}
|
2013-03-15 11:20:55 +04:00
|
|
|
if (!found) {
|
|
|
|
break;
|
2012-02-09 05:30:35 +04:00
|
|
|
}
|
2013-03-18 11:52:12 +04:00
|
|
|
_depth++;
|
2012-02-09 05:30:35 +04:00
|
|
|
}
|
|
|
|
|
2013-03-18 11:52:12 +04:00
|
|
|
*outdepth = _tree_depth;
|
|
|
|
|
2013-04-22 10:17:26 +04:00
|
|
|
if (last) {
|
|
|
|
fs_node_t * last_clone = malloc(sizeof(fs_node_t));
|
|
|
|
memcpy(last_clone, last, sizeof(fs_node_t));
|
|
|
|
return last_clone;
|
|
|
|
}
|
2013-03-15 11:20:55 +04:00
|
|
|
return last;
|
2012-02-09 05:30:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-15 11:20:55 +04:00
|
|
|
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/**
|
|
|
|
* kopen: Open a file by name.
|
|
|
|
*
|
|
|
|
* Explore the file system tree to find the appropriate node for
|
|
|
|
* for a given path. The path can be relative to the working directory
|
|
|
|
* and will be canonicalized by the kernel.
|
|
|
|
*
|
|
|
|
* @param filename Filename to open
|
|
|
|
* @param flags Flag bits for read/write mode.
|
|
|
|
* @returns A file system node element that the caller can free.
|
2011-01-29 23:12:00 +03:00
|
|
|
*/
|
2012-01-26 05:25:59 +04:00
|
|
|
fs_node_t *kopen(char *filename, uint32_t flags) {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Simple sanity checks that we actually have a file system */
|
2011-11-30 06:19:30 +04:00
|
|
|
if (!fs_root || !filename) {
|
2011-04-12 23:37:10 +04:00
|
|
|
return NULL;
|
2011-01-29 23:12:00 +03:00
|
|
|
}
|
2012-01-31 02:48:54 +04:00
|
|
|
|
2012-09-29 11:39:01 +04:00
|
|
|
debug_print(INFO, "kopen(%s)", filename);
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Reference the current working directory */
|
2012-01-26 05:25:59 +04:00
|
|
|
char *cwd = (char *)(current_process->wd_name);
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Canonicalize the (potentially relative) path... */
|
|
|
|
char *path = canonicalize_path(cwd, filename);
|
|
|
|
/* And store the length once to save recalculations */
|
|
|
|
size_t path_len = strlen(path);
|
2012-01-26 05:25:59 +04:00
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/* If strlen(path) == 1, then path = "/"; return root */
|
2011-02-07 23:30:17 +03:00
|
|
|
if (path_len == 1) {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Clone the root file system node */
|
2012-01-26 05:25:59 +04:00
|
|
|
fs_node_t *root_clone = malloc(sizeof(fs_node_t));
|
2011-04-12 23:37:10 +04:00
|
|
|
memcpy(root_clone, fs_root, sizeof(fs_node_t));
|
2012-01-31 02:48:54 +04:00
|
|
|
|
|
|
|
/* Free the path */
|
|
|
|
free(path);
|
|
|
|
|
2014-05-28 08:38:30 +04:00
|
|
|
open_fs(root_clone, flags);
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/* And return the clone */
|
2011-04-12 23:37:10 +04:00
|
|
|
return root_clone;
|
2011-02-07 23:30:17 +03:00
|
|
|
}
|
2012-01-31 02:48:54 +04:00
|
|
|
|
|
|
|
/* Otherwise, we need to break the path up and start searching */
|
2012-01-26 05:25:59 +04:00
|
|
|
char *path_offset = path;
|
2011-01-29 23:12:00 +03:00
|
|
|
uint32_t path_depth = 0;
|
|
|
|
while (path_offset < path + path_len) {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Find each PATH_SEPARATOR */
|
|
|
|
if (*path_offset == PATH_SEPARATOR) {
|
2011-01-29 23:12:00 +03:00
|
|
|
*path_offset = '\0';
|
|
|
|
path_depth++;
|
|
|
|
}
|
|
|
|
path_offset++;
|
|
|
|
}
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Clean up */
|
2011-01-29 23:12:00 +03:00
|
|
|
path[path_len] = '\0';
|
|
|
|
path_offset = path + 1;
|
2012-01-31 02:48:54 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, the path is tokenized and path_offset points
|
|
|
|
* to the first token (directory) and path_depth is the number
|
|
|
|
* of directories in the path
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dig through the (real) tree to find the file
|
|
|
|
*/
|
2014-02-27 09:10:56 +04:00
|
|
|
unsigned int depth = 0;
|
2012-02-09 05:30:35 +04:00
|
|
|
/* Find the mountpoint for this file */
|
2014-05-28 09:52:37 +04:00
|
|
|
fs_node_t *node_ptr = get_mount_point(path, path_depth, &path_offset, &depth);
|
2013-03-15 11:20:55 +04:00
|
|
|
|
|
|
|
if (path_offset >= path+path_len) {
|
|
|
|
free(path);
|
2014-05-28 09:52:37 +04:00
|
|
|
open_fs(node_ptr, flags);
|
|
|
|
return node_ptr;
|
2013-03-15 11:20:55 +04:00
|
|
|
}
|
2012-01-26 05:25:59 +04:00
|
|
|
fs_node_t *node_next = NULL;
|
2013-03-18 11:52:12 +04:00
|
|
|
for (; depth < path_depth; ++depth) {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Search the active directory for the requested directory */
|
2012-09-29 11:39:01 +04:00
|
|
|
debug_print(INFO, "... Searching for %s", path_offset);
|
2011-04-12 23:37:10 +04:00
|
|
|
node_next = finddir_fs(node_ptr, path_offset);
|
|
|
|
free(node_ptr);
|
|
|
|
node_ptr = node_next;
|
2011-01-29 23:12:00 +03:00
|
|
|
if (!node_ptr) {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* We failed to find the requested directory */
|
2011-01-29 23:12:00 +03:00
|
|
|
free((void *)path);
|
|
|
|
return NULL;
|
|
|
|
} else if (depth == path_depth - 1) {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* We found the file and are done, open the node */
|
2013-04-24 11:19:08 +04:00
|
|
|
open_fs(node_ptr, flags);
|
2011-04-09 02:53:52 +04:00
|
|
|
free((void *)path);
|
2011-01-29 23:12:00 +03:00
|
|
|
return node_ptr;
|
|
|
|
}
|
2012-01-31 02:48:54 +04:00
|
|
|
/* We are still searching... */
|
2011-01-29 23:52:44 +03:00
|
|
|
path_offset += strlen(path_offset) + 1;
|
2011-01-29 23:12:00 +03:00
|
|
|
}
|
2013-04-23 09:36:47 +04:00
|
|
|
debug_print(INFO, "- Not found.");
|
2012-01-31 02:48:54 +04:00
|
|
|
/* We failed to find the requested file, but our loop terminated. */
|
2011-01-29 23:12:00 +03:00
|
|
|
free((void *)path);
|
|
|
|
return NULL;
|
2011-01-29 09:43:39 +03:00
|
|
|
}
|
|
|
|
|