- Fixed module relocation.
[planlOS.git] / system / kernel / fs / fs.c
blob491ddd5f22cc10e7e5d27d46b1b8186e5fec1c2b
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "fs/fs.h"
23 #include "ke/errors.h"
24 #include "ke/cpu.h"
25 #include "fs/request.h"
26 #include <stdlib.h>
27 #include <string.h>
29 typedef struct FsDriverEntry
31 FsFileSystemDriver *driver;
32 char *name;
33 } FsDriverEntry;
35 static FsDriverEntry *drivers = 0;
36 static uint32_t driver_count = 0;
38 static FsFileSystem **file_systems = 0;
39 static uint32_t fs_count = 0;
41 int fsInit(void)
43 return KE_ERROR_NOTIMPLEMENTED;
46 int fsRegisterDriver(FsFileSystemDriver *driver, const char *name)
48 // Add driver to driver list
49 drivers = realloc(drivers, (driver_count + 1) * sizeof(FsDriverEntry));
50 drivers[driver_count].driver = driver;
51 drivers[driver_count].name = strdup(name);
52 driver_count++;
53 return 0;
55 int fsUnregisterDriver(FsFileSystemDriver *driver)
57 return KE_ERROR_NOTIMPLEMENTED;
59 FsFileSystemDriver *fsGetDriver(const char *name)
61 uint32_t i;
62 for (i = 0; i < driver_count; i++)
64 if (!strcmp(drivers[i].name, name)) return drivers[i].driver;
66 return 0;
68 int fsMount(FsFileSystemDriver *driver, const char *path, const char *device,
69 uint32_t flags)
71 // Call driver
72 FsFileSystem *fs = driver->mount(driver, path, device, flags);
73 if (!fs) return KE_ERROR_UNKNOWN;
74 // Add file system to list
75 file_systems = realloc(file_systems, (fs_count + 1) * sizeof(FsFileSystem*));
76 file_systems[fs_count] = fs;
77 fs_count++;
78 return 0;
80 int fsUnmount(const char *path)
82 return KE_ERROR_NOTIMPLEMENTED;
85 FsFileHandle *fsOpen(const char *filename, uint32_t mode)
87 // Get file system from path
88 uint32_t i = 0;
89 FsFileSystem *fs = 0;
90 uint32_t matched = 0;
91 for (i = 0; i < fs_count; i++)
93 if (strlen(file_systems[i]->path) <= matched) continue;
94 if (!strncmp(file_systems[i]->path, filename, strlen(file_systems[i]->path)))
96 matched = strlen(file_systems[i]->path);
97 fs = file_systems[i];
100 if (!fs) return 0;
101 filename += matched;
102 // Send open request
103 FsRequest request;
104 memset(&request, 0, sizeof(request));
105 request.type = FS_REQUEST_OPEN;
106 request.fs = fs;
107 request.bufferlength = strlen(filename) + 1;
108 request.buffer = (char*)filename;
109 int status = fsCreateRequest(&request, 1);
110 if (status) return 0;
111 if (request.status || request.return_value) return 0;
112 // Create file handle
113 FsFileHandle *fh = malloc(sizeof(FsFileHandle));
114 fh->file = request.file;
115 fh->position = 0;
116 fh->flags = 0;
117 return fh;
119 int fsClose(FsFileHandle *file)
121 if (!file) return KE_ERROR_UNKNOWN;
122 // Send close request
123 FsRequest request;
124 memset(&request, 0, sizeof(request));
125 request.type = FS_REQUEST_CLOSE;
126 request.fs = file->file->fs;
127 request.file = file->file;
128 int status = fsCreateRequest(&request, 1);
129 if (status) return KE_ERROR_UNKNOWN;
130 if (request.status || request.return_value) return KE_ERROR_UNKNOWN;
131 free(file);
132 return 0;
135 int fsWrite(FsFileHandle *file, void *buffer, int size)
137 if (!file) return -1;
138 // Send close request
139 FsRequest request;
140 memset(&request, 0, sizeof(request));
141 request.type = FS_REQUEST_WRITE;
142 request.fs = file->file->fs;
143 request.file = file->file;
144 request.bufferlength = size;
145 request.buffer = buffer;
146 request.offset = file->position;
147 int status = fsCreateRequest(&request, 1);
148 if (status) return -1;
149 if (request.status) return -1;
150 return request.return_value;
152 int fsRead(FsFileHandle *file, void *buffer, int size, int blocking)
154 if (!file) return -1;
155 // Send close request
156 FsRequest request;
157 memset(&request, 0, sizeof(request));
158 request.type = FS_REQUEST_READ;
159 request.fs = file->file->fs;
160 request.file = file->file;
161 request.bufferlength = size;
162 request.buffer = buffer;
163 request.offset = file->position;
164 int status = fsCreateRequest(&request, 1);
165 if (status) return -1;
166 if (request.status) return -1;
167 return request.return_value;
169 uint64_t fsSeek(FsFileHandle *file, uint64_t offset, int whence)
171 return 0;
173 int fsIOCTL(FsFileHandle *file, int request, ...)
175 return -1;