aboutsummaryrefslogtreecommitdiffstats
path: root/src/kernel/vfs.c
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2018-11-29 20:43:06 -0500
committerClyne Sullivan <tullivan99@gmail.com>2018-11-29 20:43:06 -0500
commit563c92e6d08c305cb9f7693818ecbe2a2dec527b (patch)
tree5b8a6dcdad1daa64dc3a73e3dc9385b85b2c9200 /src/kernel/vfs.c
parent22615096dee294f63c6940c17f2a448da51d9197 (diff)
hello world from initrd
Diffstat (limited to 'src/kernel/vfs.c')
-rw-r--r--src/kernel/vfs.c72
1 files changed, 62 insertions, 10 deletions
diff --git a/src/kernel/vfs.c b/src/kernel/vfs.c
index 6236691..b9be9bc 100644
--- a/src/kernel/vfs.c
+++ b/src/kernel/vfs.c
@@ -1,3 +1,23 @@
+/**
+ * @file vfs.c
+ * An implementation of filesystem abstraction
+ *
+ * Copyright (C) 2018 Clyne Sullivan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
#include "vfs.h"
#include <kernel/task.h>
@@ -10,6 +30,11 @@
static vfs_volume vfs_volumes[VFS_MAX_VOLS];
static vfs_file vfs_files[VFS_MAX_FILES];
+#define VFS_PID_CHECK(fpid) { \
+ uint32_t pid = task_getpid(); \
+ if (pid != fpid && fpid != 0) \
+ return 0; }
+
void vfs_svc(uint32_t *args)
{
switch (args[0]) {
@@ -114,7 +139,8 @@ int vfs_open(const char *path, uint32_t flags)
vfs_files[file].flags = VFS_FILE_OPEN | flags;
vfs_files[file].vol = drive;
vfs_files[file].pid = task_getpid();
- vfs_files[file].fsinfo = fsinfo;
+ vfs_files[file].info.pos = 0;
+ vfs_files[file].info.fsinfo = fsinfo;
return file;
}
@@ -125,14 +151,13 @@ int vfs_close(int fd)
return -1;
if (vfs_volumes[vfs_files[fd].vol].funcs->close == 0)
return -1;
- if (vfs_files[fd].pid != task_getpid())
- return -1;
+ VFS_PID_CHECK(vfs_files[fd].pid);
if (!(vfs_files[fd].flags & VFS_FILE_OPEN))
return 0;
// TODO care
/*int ret =*/ vfs_volumes[vfs_files[fd].vol].funcs->close(
- vfs_files[fd].fsinfo);
+ &vfs_files[fd].info);
vfs_files[fd].flags = 0;
vfs_files[fd].pid = 0;
@@ -143,16 +168,17 @@ uint32_t vfs_read(int fd, uint32_t count, uint8_t *buffer)
{
if (fd < 0 || fd > VFS_MAX_FILES || count == 0 || buffer == 0)
return 0;
+ if (!(vfs_files[fd].flags & VFS_FILE_OPEN))
+ return -1;
if (vfs_volumes[vfs_files[fd].vol].funcs->read == 0)
return -1;
- if (vfs_files[fd].pid != task_getpid())
- return 0;
+ VFS_PID_CHECK(vfs_files[fd].pid);
if ((!(vfs_files[fd].flags & VFS_FILE_READ)) || (vfs_files[fd].flags &
VFS_EOF))
return 0;
uint32_t ret = vfs_volumes[vfs_files[fd].vol].funcs->read(
- vfs_files[fd].fsinfo, count, buffer);
+ &vfs_files[fd].info, count, buffer);
if (ret < count)
vfs_files[fd].flags |= VFS_EOF;
@@ -164,17 +190,18 @@ uint32_t vfs_write(int fd, uint32_t count, const uint8_t *buffer)
{
if (fd < 0 || fd > VFS_MAX_FILES || count == 0 || buffer == 0)
return 0;
+ if (!(vfs_files[fd].flags & VFS_FILE_OPEN))
+ return -1;
if (vfs_volumes[vfs_files[fd].vol].funcs->write == 0)
return -1;
- if (vfs_files[fd].pid != task_getpid())
- return 0;
+ VFS_PID_CHECK(vfs_files[fd].pid);
// TODO append?
if ((!(vfs_files[fd].flags & VFS_FILE_WRITE)) || (vfs_files[fd].flags &
VFS_EOF))
return 0;
uint32_t ret = vfs_volumes[vfs_files[fd].vol].funcs->write(
- vfs_files[fd].fsinfo, count, buffer);
+ &vfs_files[fd].info, count, buffer);
if (ret < count)
vfs_files[fd].flags |= VFS_EOF;
@@ -182,3 +209,28 @@ uint32_t vfs_write(int fd, uint32_t count, const uint8_t *buffer)
return ret;
}
+int vfs_seek(int fd, int32_t offset, int whence)
+{
+ if (fd < 0 || fd > VFS_MAX_FILES)
+ return -1;
+ if (!(vfs_files[fd].flags & VFS_FILE_OPEN))
+ return -1;
+ if (vfs_volumes[vfs_files[fd].vol].funcs->seek == 0)
+ return -1;
+ VFS_PID_CHECK(vfs_files[fd].pid);
+
+ return vfs_volumes[vfs_files[fd].vol].funcs->seek(&vfs_files[fd].info,
+ offset, whence);
+}
+
+int32_t vfs_tell(int fd)
+{
+ if (fd < 0 || fd > VFS_MAX_FILES)
+ return -1;
+ if (!(vfs_files[fd].flags & VFS_FILE_OPEN))
+ return -1;
+ VFS_PID_CHECK(vfs_files[fd].pid);
+
+ return (int32_t)vfs_files[fd].info.pos;
+}
+