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.
23 #include "fs/request.h"
24 #include "ke/errors.h"
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
)
45 uint32_t fileindex
= 0xFFFFFFFF;
47 for (i
= 0; i
< devfsfile_count
; i
++)
49 if (!strcmp(devfsfiles
[i
]->path
, path
))
55 if (fileindex
== 0xFFFFFFFF) return 0;
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
));
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
);
74 static int fsDevFSRequest(struct FsFileSystem
*fs
, struct FsRequest
*request
)
76 switch (request
->type
)
78 // Open/close files at once
81 request
->file
= fsDevFSOpen(fs
, request
->buffer
);
84 request
->status
= KE_ERROR_UNKNOWN
;
85 request
->return_value
= -1;
89 uint32_t fileindex
= ((KeDevFSFile
*)request
->file
)->index
;
90 devfsfiles
[fileindex
]->query_request(devfsfiles
[fileindex
], request
);
92 fsFinishRequest(request
);
95 case FS_REQUEST_CLOSE
:
97 uint32_t fileindex
= ((KeDevFSFile
*)request
->file
)->index
;
98 devfsfiles
[fileindex
]->query_request(devfsfiles
[fileindex
], request
);
99 fsFinishRequest(request
);
102 // Pass other requests to the driver
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
)
123 int fsInitDevFS(void)
125 memset(&fs
, 0, sizeof(fs
));
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");
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;
145 kePrint("Created device: %s\n", file
->path
);
148 int fsDestroyDeviceFile(FsDeviceFile
*file
)
151 uint32_t fileindex
= 0xFFFFFFFF;
153 for (i
= 0; i
< devfsfile_count
; i
++)
155 if (devfsfiles
[i
] == file
)
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
*));
171 for (i
= fileindex
; i
< devfsfile_count
; i
++)
173 ((KeDevFSFile
*)files
[i
])->index
= i
;