2011-12-11 03:42:41 +04:00
|
|
|
/* vim: shiftwidth=4 tabstop=4 noexpandtab
|
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>
|
2011-11-29 23:28:36 +04:00
|
|
|
#include <list.h>
|
2011-12-09 01:37:11 +04:00
|
|
|
#include <process.h>
|
2011-01-22 10:09:47 +03:00
|
|
|
|
|
|
|
fs_node_t *fs_root = 0;
|
|
|
|
|
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) {
|
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 {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
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 {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 02:48:54 +04:00
|
|
|
/**
|
|
|
|
* open_fs: Open a file system node.
|
|
|
|
*
|
|
|
|
* @param node Node to open
|
|
|
|
* @param read Read permission? (1 = yes)
|
|
|
|
* @param write Write permission? (1 = overwrite, 2 = append)
|
|
|
|
*/
|
2011-01-22 10:09:47 +03:00
|
|
|
void open_fs(fs_node_t *node, uint8_t read, uint8_t write) {
|
2012-01-26 05:25:59 +04:00
|
|
|
if (node->open) {
|
2011-01-22 10:09:47 +03:00
|
|
|
node->open(node, read, write);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
2012-01-26 05:25:59 +04:00
|
|
|
if (node->close) {
|
2011-01-22 10:09:47 +03:00
|
|
|
node->close(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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) {
|
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 {
|
|
|
|
return (fs_node_t *)NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2012-01-26 05:25:59 +04:00
|
|
|
int32_t i = strlen(name);
|
|
|
|
char *dir_name = malloc(i + 1);
|
|
|
|
memcpy(dir_name, name, i);
|
|
|
|
dir_name[i] = '\0';
|
|
|
|
if (dir_name[i - 1] == '/')
|
|
|
|
dir_name[i - 1] = '\0';
|
|
|
|
if (strlen(dir_name) == 0) {
|
2012-02-06 02:04:41 +04:00
|
|
|
return 1;
|
2012-01-26 05:25:59 +04:00
|
|
|
}
|
|
|
|
for (i = strlen(dir_name) - 1; i >= 0; i--) {
|
|
|
|
if (dir_name[i] == '/') {
|
|
|
|
dir_name[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the parent dir node.
|
|
|
|
fs_node_t *node;
|
|
|
|
if (i >= 0) {
|
|
|
|
node = kopen(dir_name, 0);
|
|
|
|
} else {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* XXX This is wrong */
|
2012-01-26 05:25:59 +04:00
|
|
|
node = kopen(".", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node == NULL) {
|
|
|
|
free(dir_name);
|
2012-02-06 02:04:41 +04:00
|
|
|
return 2;
|
2012-01-26 05:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
if ((node->flags & FS_DIRECTORY) && node->mkdir) {
|
|
|
|
node->create(node, dir_name + i, permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(node);
|
|
|
|
free(dir_name);
|
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) {
|
2012-01-26 05:25:59 +04:00
|
|
|
int32_t i = strlen(name);
|
|
|
|
char *dir_name = malloc(i + 1);
|
|
|
|
memcpy(dir_name, name, i);
|
|
|
|
dir_name[i] = '\0';
|
|
|
|
if (dir_name[i - 1] == '/')
|
|
|
|
dir_name[i - 1] = '\0';
|
|
|
|
if (strlen(dir_name) == 0) {
|
2012-02-06 02:04:41 +04:00
|
|
|
return 1;
|
2012-01-26 05:25:59 +04:00
|
|
|
}
|
|
|
|
for (i = strlen(dir_name) - 1; i >= 0; i--) {
|
|
|
|
if (dir_name[i] == '/') {
|
|
|
|
dir_name[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the parent dir node.
|
|
|
|
fs_node_t *node;
|
|
|
|
if (i >= 0) {
|
|
|
|
node = kopen(dir_name, 0);
|
|
|
|
} else {
|
|
|
|
node = kopen(".", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node == NULL) {
|
|
|
|
kprintf("mkdir: Directory does not exist\n");
|
|
|
|
free(dir_name);
|
2012-02-06 02:04:41 +04:00
|
|
|
return 1;
|
2012-01-26 05:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
if ((node->flags & FS_DIRECTORY) && node->mkdir) {
|
|
|
|
node->mkdir(node, dir_name + i, permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(node);
|
|
|
|
free(dir_name);
|
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) {
|
2011-12-09 01:25:48 +04:00
|
|
|
if (!source) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-01-26 05:25:59 +04:00
|
|
|
fs_node_t *n = malloc(sizeof(fs_node_t));
|
2011-12-09 01:25:48 +04:00
|
|
|
memcpy(n, source, sizeof(fs_node_t));
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-02-09 05:30:35 +04:00
|
|
|
/**
|
|
|
|
* get_mount_point
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fs_node_t *get_mount_point(char * path, size_t path_depth) {
|
|
|
|
size_t depth;
|
|
|
|
|
2012-02-20 07:29:40 +04:00
|
|
|
kprintf("[root]");
|
|
|
|
for (depth = 0; depth <= path_depth; ++depth) {
|
|
|
|
kprintf("%s%c", path, (depth == path_depth) ? '\n' : '/');
|
|
|
|
path += strlen(path) + 1;
|
|
|
|
}
|
|
|
|
|
2012-02-09 05:30:35 +04:00
|
|
|
#if 0
|
|
|
|
tree_node_t * tnode = from;
|
|
|
|
foreach(node, tnode->children) {
|
|
|
|
tree_node_t * _node = (tree_node_t *)node->value;
|
|
|
|
shm_node_t * snode = (shm_node_t *)_node->value;
|
|
|
|
|
|
|
|
if (!strcmp(snode->name, pch)) {
|
|
|
|
if (*save == '\0') {
|
|
|
|
return snode;
|
|
|
|
}
|
|
|
|
return _get_node(save, create, _node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
for (depth = 0; depth < path_depth; ++depth) {
|
|
|
|
/* Search the active directory for the requested directory */
|
|
|
|
node_next = finddir_fs(node_ptr, path_offset);
|
|
|
|
free(node_ptr);
|
|
|
|
node_ptr = node_next;
|
|
|
|
if (!node_ptr) {
|
|
|
|
/* We failed to find the requested directory */
|
|
|
|
/* XXX: This is where we should be checking other file system mappings */
|
|
|
|
free((void *)path);
|
|
|
|
return NULL;
|
|
|
|
} else if (depth == path_depth - 1) {
|
|
|
|
/* We found the file and are done, open the node */
|
|
|
|
open_fs(node_ptr, 1, 0);
|
|
|
|
free((void *)path);
|
|
|
|
return node_ptr;
|
|
|
|
}
|
|
|
|
/* We are still searching... */
|
|
|
|
path_offset += strlen(path_offset) + 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return fs_root;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
2011-01-29 23:12:00 +03:00
|
|
|
uint32_t depth;
|
2012-01-26 05:25:59 +04:00
|
|
|
fs_node_t *node_ptr = malloc(sizeof(fs_node_t));
|
2012-02-09 05:30:35 +04:00
|
|
|
/* Find the mountpoint for this file */
|
|
|
|
fs_node_t *mount_point = get_mount_point(path, path_depth);
|
|
|
|
/* Set the active directory to the mountpoint */
|
|
|
|
memcpy(node_ptr, mount_point, sizeof(fs_node_t));
|
2012-01-26 05:25:59 +04:00
|
|
|
fs_node_t *node_next = NULL;
|
2011-01-29 23:12:00 +03:00
|
|
|
for (depth = 0; depth < path_depth; ++depth) {
|
2012-01-31 02:48:54 +04:00
|
|
|
/* Search the active directory for the requested directory */
|
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 */
|
2011-01-29 23:52:44 +03:00
|
|
|
open_fs(node_ptr, 1, 0);
|
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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|