- Implemented execp*.
[planlOS.git] / system / kernel / fs / devfs.c
blobca887642eeb95648fea599ea92237d697f9b5517
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/devfs.h"
23 #include "fs/request.h"
24 #include "ke/errors.h"
25 #include "ke/debug.h"
26 #include <string.h>
27 #include <stdlib.h>
29 typedef struct
31 FsFile file;
32 uint32_t index;
33 } KeDevFSFile;
35 static FsFileSystemDriver devfs;
36 static FsFileSystem fs;
38 static FsDeviceFile **devfsfiles = 0;
39 static FsFile **files = 0;
40 static uint32_t devfsfile_count = 0;
42 static FsFile *fsDevFSOpen(FsFileSystem *fs, const char *path)
44 // Search for file
45 uint32_t fileindex = 0xFFFFFFFF;
46 uint32_t i;
47 for (i = 0; i < devfsfile_count; i++)
49 if (!strcmp(devfsfiles[i]->path, path))
51 fileindex = i;
52 break;
55 if (fileindex == 0xFFFFFFFF) return 0;
56 // Open existing file
57 if (files[fileindex]) return files[fileindex];
58 // Create file structure
59 KeDevFSFile *file = malloc(sizeof(KeDevFSFile));
60 files[fileindex] = &file->file;
61 memset(file, 0, sizeof(FsFile));
62 file->file.fs = fs;
63 file->index = fileindex;
64 return files[fileindex];
67 static FsFileSystem *fsDevFSMount(struct FsFileSystemDriver *driver,
68 const char *path, const char *device, uint32_t flags)
70 if (fs.path) return 0;
71 fs.path = strdup(path);
72 return &fs;
74 static int fsDevFSRequest(struct FsFileSystem *fs, struct FsRequest *request)
76 switch (request->type)
78 // Open/close files at once
79 case FS_REQUEST_OPEN:
81 request->file = fsDevFSOpen(fs, request->buffer);
82 if (!request->file)
84 request->status = KE_ERROR_UNKNOWN;
85 request->return_value = -1;
87 else
89 uint32_t fileindex = ((KeDevFSFile*)request->file)->index;
90 devfsfiles[fileindex]->query_request(devfsfiles[fileindex], request);
92 fsFinishRequest(request);
94 return 0;
95 case FS_REQUEST_CLOSE:
97 uint32_t fileindex = ((KeDevFSFile*)request->file)->index;
98 devfsfiles[fileindex]->query_request(devfsfiles[fileindex], request);
99 fsFinishRequest(request);
101 return 0;
102 // Pass other requests to the driver
103 default:
105 uint32_t fileindex = ((KeDevFSFile*)request->file)->index;
106 if (!devfsfiles[fileindex]->query_request)
108 kePrint("devfs: No request handler.\n");
109 return KE_ERROR_UNKNOWN;
111 return devfsfiles[fileindex]->query_request(devfsfiles[fileindex], request);
116 static int fsDevFSUnmount(FsFileSystem *fs)
118 free(fs->path);
119 fs->path = 0;
120 return 0;
123 int fsInitDevFS(void)
125 memset(&fs, 0, sizeof(fs));
126 fs.driver = &devfs;
127 fs.unmount = fsDevFSUnmount;
128 fs.query_request = fsDevFSRequest;
130 // Register file system driver
131 devfs.flags = FS_DRIVER_SINGLE | FS_DRIVER_NOMOUNT | FS_DRIVER_NODATA;
132 devfs.mount = fsDevFSMount;
133 fsRegisterDriver(&devfs, "devfs");
134 return 0;
137 int fsCreateDeviceFile(FsDeviceFile *file)
139 // Add file to file list
140 devfsfiles = realloc(devfsfiles, (devfsfile_count + 1) * sizeof(FsDeviceFile*));
141 files = realloc(files, (devfsfile_count + 1) * sizeof(FsFile*));
142 devfsfiles[devfsfile_count] = file;
143 files[devfsfile_count] = 0;
144 devfsfile_count++;
145 kePrint("Created device: %s\n", file->path);
146 return 0;
148 int fsDestroyDeviceFile(FsDeviceFile *file)
150 // Search for file
151 uint32_t fileindex = 0xFFFFFFFF;
152 uint32_t i;
153 for (i = 0; i < devfsfile_count; i++)
155 if (devfsfiles[i] == file)
157 fileindex = i;
158 break;
161 if (fileindex == 0xFFFFFFFF) return KE_ERROR_UNKNOWN;
162 // Close open file handles
163 // Remove from file list
164 memmove(devfsfiles + fileindex, devfsfiles + fileindex + 1,
165 (devfsfile_count - fileindex - 1) * sizeof(FsDeviceFile*));
166 devfsfiles = realloc(devfsfiles, (devfsfile_count - 1) * sizeof(FsDeviceFile*));
167 memmove(files + fileindex, files + i + 1,
168 (devfsfile_count - fileindex - 1) * sizeof(FsFile*));
169 files = realloc(files, (devfsfile_count - 1) * sizeof(FsFile*));
170 devfsfile_count--;
171 for (i = fileindex; i < devfsfile_count; i++)
173 ((KeDevFSFile*)files[i])->index = i;
175 return 0;