231 lines
5.3 KiB
C
231 lines
5.3 KiB
C
/*
|
|
* Copyright 2010 Stefan Lankes, Chair for Operating Systems,
|
|
* RWTH Aachen University
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
* This file is part of MetalSVM.
|
|
*/
|
|
|
|
#include <metalsvm/stdlib.h>
|
|
#include <metalsvm/stdio.h>
|
|
#include <metalsvm/string.h>
|
|
#include <metalsvm/fs.h>
|
|
#include <metalsvm/errno.h>
|
|
#include <metalsvm/spinlock.h>
|
|
|
|
vfs_node_t* fs_root = NULL; // The root of the filesystem.
|
|
|
|
ssize_t read_fs(fildes_t* file, uint8_t* buffer, size_t size)
|
|
{
|
|
vfs_node_t* node = file->node;
|
|
ssize_t ret = -EINVAL;
|
|
|
|
if (BUILTIN_EXPECT(!node || !buffer, 0))
|
|
return ret;
|
|
|
|
if (node->type != FS_SOCKET) {
|
|
spinlock_lock(&node->lock);
|
|
// Has the node got a read callback?
|
|
if (node->read != 0)
|
|
ret = node->read(file, buffer, size);
|
|
spinlock_unlock(&node->lock);
|
|
} else if (node->read != 0)
|
|
ret = node->read(file, buffer, size);
|
|
|
|
return ret;
|
|
}
|
|
|
|
ssize_t write_fs(fildes_t* file, uint8_t* buffer, size_t size)
|
|
{
|
|
vfs_node_t* node = file->node;
|
|
ssize_t ret = -EINVAL;
|
|
|
|
if (BUILTIN_EXPECT(!node || !buffer, 0))
|
|
return ret;
|
|
|
|
if (node->type != FS_SOCKET) {
|
|
spinlock_lock(&node->lock);
|
|
// Has the node got a write callback?
|
|
if (node->write != 0)
|
|
ret = node->write(file, buffer, size);
|
|
spinlock_unlock(&node->lock);
|
|
} else if (node->write != 0)
|
|
ret = node->write(file, buffer, size);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int open_fs(fildes_t* file, const char* name)
|
|
{
|
|
uint32_t ret = 0, i, j = 1;
|
|
vfs_node_t* file_node = NULL; /* file node */
|
|
vfs_node_t* dir_node = NULL;
|
|
char fname[MAX_FNAME];
|
|
|
|
if (BUILTIN_EXPECT(!name, 0))
|
|
return ret;
|
|
|
|
if (name[0] == '/')
|
|
file_node = fs_root;
|
|
|
|
while((name[j] != '\0') || ((file_node != NULL) && (file_node->type == FS_DIRECTORY))) {
|
|
i = 0;
|
|
while((name[j] != '/') && (name[j] != '\0')) {
|
|
fname[i] = name[j];
|
|
i++; j++;
|
|
}
|
|
fname[i] = '\0';
|
|
dir_node = file_node; /* file must be a directory */
|
|
file_node = finddir_fs(dir_node, fname);
|
|
if (name[j] == '/')
|
|
j++;
|
|
}
|
|
|
|
//kprintf("dir_node = %p, file_node = %p, name = %s \n", dir_node, file_node, fname);
|
|
|
|
/* file exists */
|
|
if(file_node) {
|
|
spinlock_lock(&file_node->lock);
|
|
file->node = file_node;
|
|
// Has the file_node got an open callback?
|
|
if (file_node->open != 0)
|
|
ret = file->node->open(file, NULL);
|
|
spinlock_unlock(&file_node->lock);
|
|
} else if (dir_node) { /* file doesn't exist or opendir was called */
|
|
spinlock_lock(&dir_node->lock);
|
|
file->node = dir_node;
|
|
// Has the dir_node got an open callback?
|
|
if (dir_node->open != 0)
|
|
ret = dir_node->open(file, fname);
|
|
spinlock_unlock(&dir_node->lock);
|
|
} else {
|
|
ret = -ENOENT;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int close_fs(fildes_t* file)
|
|
{
|
|
int ret = -EINVAL;
|
|
|
|
if (BUILTIN_EXPECT(!(file->node), 0))
|
|
return ret;
|
|
|
|
spinlock_lock(&file->node->lock);
|
|
// Has the node got a close callback?
|
|
if (file->node->close != 0)
|
|
ret = file->node->close(file);
|
|
spinlock_unlock(&file->node->lock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void list_fs(vfs_node_t* node, uint32_t depth)
|
|
{
|
|
int i = 0;
|
|
dirent_t* dirent = NULL;
|
|
fildes_t* file = kmalloc(sizeof(fildes_t));
|
|
file->offset = 0;
|
|
file->flags = 0;
|
|
|
|
while ((dirent = readdir_fs(node, i)) != 0) {
|
|
kprintf("%*c|- %s\n", 2*depth, ' ', dirent->name);
|
|
|
|
if (strcmp(dirent->name, ".") && strcmp(dirent->name, "..")) {
|
|
vfs_node_t *new_node = finddir_fs(node, dirent->name);
|
|
if (new_node) {
|
|
kprintf("%*c\\\n", 2*depth, ' ');
|
|
list_fs(new_node, depth + 1);
|
|
}
|
|
}
|
|
|
|
i++;
|
|
}
|
|
kfree(file);
|
|
}
|
|
|
|
struct dirent* readdir_fs(vfs_node_t * node, uint32_t index)
|
|
{
|
|
struct dirent* ret = NULL;
|
|
|
|
if (BUILTIN_EXPECT(!node, 0))
|
|
return ret;
|
|
|
|
spinlock_lock(&node->lock);
|
|
// Is the node a directory, and does it have a callback?
|
|
if ((node->type == FS_DIRECTORY) && node->readdir != 0)
|
|
ret = node->readdir(node, index);
|
|
spinlock_unlock(&node->lock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
vfs_node_t* finddir_fs(vfs_node_t* node, const char *name)
|
|
{
|
|
vfs_node_t* ret = NULL;
|
|
|
|
if (BUILTIN_EXPECT(!node, 0))
|
|
return ret;
|
|
|
|
spinlock_lock(&node->lock);
|
|
// Is the node a directory, and does it have a callback?
|
|
if ((node->type == FS_DIRECTORY) && node->finddir != 0)
|
|
ret = node->finddir(node, name);
|
|
spinlock_unlock(&node->lock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
vfs_node_t* mkdir_fs(vfs_node_t* node, const char *name)
|
|
{
|
|
vfs_node_t* ret = NULL;
|
|
|
|
if (BUILTIN_EXPECT(!node, 0))
|
|
return ret;
|
|
|
|
spinlock_lock(&node->lock);
|
|
if (node->mkdir != 0)
|
|
ret = node->mkdir(node, name);
|
|
spinlock_unlock(&node->lock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
vfs_node_t* findnode_fs(const char* name)
|
|
{
|
|
uint32_t i, j = 1;
|
|
vfs_node_t* ret = NULL;
|
|
char fname[MAX_FNAME];
|
|
|
|
if (BUILTIN_EXPECT(!name, 0))
|
|
return ret;
|
|
|
|
if (name[0] == '/')
|
|
ret = fs_root;
|
|
|
|
while((name[j] != '\0') && ret) {
|
|
i = 0;
|
|
while((name[j] != '/') && (name[j] != '\0')) {
|
|
fname[i] = name[j];
|
|
i++; j++;
|
|
}
|
|
fname[i] = '\0';
|
|
ret = finddir_fs(ret, fname);
|
|
if (name[j] == '/')
|
|
j++;
|
|
}
|
|
|
|
return ret;
|
|
}
|