Rev 2699 | Rev 2707 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2699 | Rev 2700 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup libc |
29 | /** @addtogroup libc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #include <vfs.h> |
35 | #include <vfs.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
37 | #include <unistd.h> |
37 | #include <unistd.h> |
38 | #include <dirent.h> |
38 | #include <dirent.h> |
39 | #include <fcntl.h> |
39 | #include <fcntl.h> |
40 | #include <ipc/ipc.h> |
40 | #include <ipc/ipc.h> |
41 | #include <ipc/services.h> |
41 | #include <ipc/services.h> |
42 | #include <async.h> |
42 | #include <async.h> |
43 | #include <atomic.h> |
43 | #include <atomic.h> |
44 | #include <futex.h> |
44 | #include <futex.h> |
45 | #include <errno.h> |
45 | #include <errno.h> |
46 | #include <string.h> |
46 | #include <string.h> |
47 | #include "../../srv/vfs/vfs.h" |
47 | #include "../../srv/vfs/vfs.h" |
48 | 48 | ||
49 | int vfs_phone = -1; |
49 | int vfs_phone = -1; |
50 | atomic_t vfs_phone_futex = FUTEX_INITIALIZER; |
50 | atomic_t vfs_phone_futex = FUTEX_INITIALIZER; |
51 | 51 | ||
52 | static int vfs_connect(void) |
52 | static int vfs_connect(void) |
53 | { |
53 | { |
54 | if (vfs_phone < 0) |
54 | if (vfs_phone < 0) |
55 | vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0); |
55 | vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0); |
56 | return vfs_phone; |
56 | return vfs_phone; |
57 | } |
57 | } |
58 | 58 | ||
59 | int mount(const char *fs_name, const char *mp, const char *dev) |
59 | int mount(const char *fs_name, const char *mp, const char *dev) |
60 | { |
60 | { |
61 | int res; |
61 | int res; |
62 | ipcarg_t rc; |
62 | ipcarg_t rc; |
63 | aid_t req; |
63 | aid_t req; |
64 | 64 | ||
65 | int dev_handle = 0; /* TODO */ |
65 | int dev_handle = 0; /* TODO */ |
66 | 66 | ||
67 | futex_down(&vfs_phone_futex); |
67 | futex_down(&vfs_phone_futex); |
68 | async_serialize_start(); |
68 | async_serialize_start(); |
69 | if (vfs_phone < 0) { |
69 | if (vfs_phone < 0) { |
70 | res = vfs_connect(); |
70 | res = vfs_connect(); |
71 | if (res < 0) { |
71 | if (res < 0) { |
72 | async_serialize_end(); |
72 | async_serialize_end(); |
73 | futex_up(&vfs_phone_futex); |
73 | futex_up(&vfs_phone_futex); |
74 | return res; |
74 | return res; |
75 | } |
75 | } |
76 | } |
76 | } |
77 | req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL); |
77 | req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL); |
78 | rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name)); |
78 | rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name)); |
79 | if (rc != EOK) { |
79 | if (rc != EOK) { |
80 | async_wait_for(req, NULL); |
80 | async_wait_for(req, NULL); |
81 | async_serialize_end(); |
81 | async_serialize_end(); |
82 | futex_up(&vfs_phone_futex); |
82 | futex_up(&vfs_phone_futex); |
83 | return (int) rc; |
83 | return (int) rc; |
84 | } |
84 | } |
85 | rc = ipc_data_write_start(vfs_phone, (void *)mp, strlen(mp)); |
85 | rc = ipc_data_write_start(vfs_phone, (void *)mp, strlen(mp)); |
86 | if (rc != EOK) { |
86 | if (rc != EOK) { |
87 | async_wait_for(req, NULL); |
87 | async_wait_for(req, NULL); |
88 | async_serialize_end(); |
88 | async_serialize_end(); |
89 | futex_up(&vfs_phone_futex); |
89 | futex_up(&vfs_phone_futex); |
90 | return (int) rc; |
90 | return (int) rc; |
91 | } |
91 | } |
92 | async_wait_for(req, &rc); |
92 | async_wait_for(req, &rc); |
93 | async_serialize_end(); |
93 | async_serialize_end(); |
94 | futex_up(&vfs_phone_futex); |
94 | futex_up(&vfs_phone_futex); |
95 | return (int) rc; |
95 | return (int) rc; |
96 | } |
96 | } |
97 | 97 | ||
98 | - | ||
99 | int open(const char *path, int oflag, ...) |
98 | static int _open(const char *path, int lflag, int oflag, ...) |
100 | { |
99 | { |
101 | int res; |
100 | int res; |
102 | ipcarg_t rc; |
101 | ipcarg_t rc; |
103 | ipc_call_t answer; |
102 | ipc_call_t answer; |
104 | aid_t req; |
103 | aid_t req; |
105 | 104 | ||
106 | futex_down(&vfs_phone_futex); |
105 | futex_down(&vfs_phone_futex); |
107 | async_serialize_start(); |
106 | async_serialize_start(); |
108 | if (vfs_phone < 0) { |
107 | if (vfs_phone < 0) { |
109 | res = vfs_connect(); |
108 | res = vfs_connect(); |
110 | if (res < 0) { |
109 | if (res < 0) { |
111 | async_serialize_end(); |
110 | async_serialize_end(); |
112 | futex_up(&vfs_phone_futex); |
111 | futex_up(&vfs_phone_futex); |
113 | return res; |
112 | return res; |
114 | } |
113 | } |
115 | } |
114 | } |
116 | req = async_send_2(vfs_phone, VFS_OPEN, oflag, 0, &answer); |
115 | req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer); |
117 | rc = ipc_data_write_start(vfs_phone, path, strlen(path)); |
116 | rc = ipc_data_write_start(vfs_phone, path, strlen(path)); |
118 | if (rc != EOK) { |
117 | if (rc != EOK) { |
119 | async_wait_for(req, NULL); |
118 | async_wait_for(req, NULL); |
120 | async_serialize_end(); |
119 | async_serialize_end(); |
121 | futex_up(&vfs_phone_futex); |
120 | futex_up(&vfs_phone_futex); |
122 | return (int) rc; |
121 | return (int) rc; |
123 | } |
122 | } |
124 | async_wait_for(req, &rc); |
123 | async_wait_for(req, &rc); |
125 | async_serialize_end(); |
124 | async_serialize_end(); |
126 | futex_up(&vfs_phone_futex); |
125 | futex_up(&vfs_phone_futex); |
127 | return (int) IPC_GET_ARG1(answer); |
126 | return (int) IPC_GET_ARG1(answer); |
128 | } |
127 | } |
129 | 128 | ||
- | 129 | int open(const char *path, int oflag, ...) |
|
- | 130 | { |
|
- | 131 | return _open(path, L_FILE, oflag); |
|
- | 132 | } |
|
- | 133 | ||
130 | ssize_t read(int fildes, void *buf, size_t nbyte) |
134 | ssize_t read(int fildes, void *buf, size_t nbyte) |
131 | { |
135 | { |
132 | int res; |
136 | int res; |
133 | ipcarg_t rc; |
137 | ipcarg_t rc; |
134 | ipc_call_t answer; |
138 | ipc_call_t answer; |
135 | aid_t req; |
139 | aid_t req; |
136 | 140 | ||
137 | futex_down(&vfs_phone_futex); |
141 | futex_down(&vfs_phone_futex); |
138 | async_serialize_start(); |
142 | async_serialize_start(); |
139 | if (vfs_phone < 0) { |
143 | if (vfs_phone < 0) { |
140 | res = vfs_connect(); |
144 | res = vfs_connect(); |
141 | if (res < 0) { |
145 | if (res < 0) { |
142 | async_serialize_end(); |
146 | async_serialize_end(); |
143 | futex_up(&vfs_phone_futex); |
147 | futex_up(&vfs_phone_futex); |
144 | return res; |
148 | return res; |
145 | } |
149 | } |
146 | } |
150 | } |
147 | req = async_send_1(vfs_phone, VFS_READ, fildes, &answer); |
151 | req = async_send_1(vfs_phone, VFS_READ, fildes, &answer); |
148 | if (ipc_data_read_start(vfs_phone, (void *)buf, nbyte) != EOK) { |
152 | if (ipc_data_read_start(vfs_phone, (void *)buf, nbyte) != EOK) { |
149 | async_wait_for(req, NULL); |
153 | async_wait_for(req, NULL); |
150 | async_serialize_end(); |
154 | async_serialize_end(); |
151 | futex_up(&vfs_phone_futex); |
155 | futex_up(&vfs_phone_futex); |
152 | return (ssize_t) rc; |
156 | return (ssize_t) rc; |
153 | } |
157 | } |
154 | async_wait_for(req, &rc); |
158 | async_wait_for(req, &rc); |
155 | async_serialize_end(); |
159 | async_serialize_end(); |
156 | futex_up(&vfs_phone_futex); |
160 | futex_up(&vfs_phone_futex); |
157 | return (ssize_t) IPC_GET_ARG1(answer); |
161 | return (ssize_t) IPC_GET_ARG1(answer); |
158 | } |
162 | } |
159 | 163 | ||
160 | ssize_t write(int fildes, const void *buf, size_t nbyte) |
164 | ssize_t write(int fildes, const void *buf, size_t nbyte) |
161 | { |
165 | { |
162 | int res; |
166 | int res; |
163 | ipcarg_t rc; |
167 | ipcarg_t rc; |
164 | ipc_call_t answer; |
168 | ipc_call_t answer; |
165 | aid_t req; |
169 | aid_t req; |
166 | 170 | ||
167 | futex_down(&vfs_phone_futex); |
171 | futex_down(&vfs_phone_futex); |
168 | async_serialize_start(); |
172 | async_serialize_start(); |
169 | if (vfs_phone < 0) { |
173 | if (vfs_phone < 0) { |
170 | res = vfs_connect(); |
174 | res = vfs_connect(); |
171 | if (res < 0) { |
175 | if (res < 0) { |
172 | async_serialize_end(); |
176 | async_serialize_end(); |
173 | futex_up(&vfs_phone_futex); |
177 | futex_up(&vfs_phone_futex); |
174 | return res; |
178 | return res; |
175 | } |
179 | } |
176 | } |
180 | } |
177 | req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer); |
181 | req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer); |
178 | if (ipc_data_write_start(vfs_phone, (void *)buf, nbyte) != EOK) { |
182 | if (ipc_data_write_start(vfs_phone, (void *)buf, nbyte) != EOK) { |
179 | async_wait_for(req, NULL); |
183 | async_wait_for(req, NULL); |
180 | async_serialize_end(); |
184 | async_serialize_end(); |
181 | futex_up(&vfs_phone_futex); |
185 | futex_up(&vfs_phone_futex); |
182 | return (ssize_t) rc; |
186 | return (ssize_t) rc; |
183 | } |
187 | } |
184 | async_wait_for(req, &rc); |
188 | async_wait_for(req, &rc); |
185 | async_serialize_end(); |
189 | async_serialize_end(); |
186 | futex_up(&vfs_phone_futex); |
190 | futex_up(&vfs_phone_futex); |
187 | return (ssize_t) IPC_GET_ARG1(answer); |
191 | return (ssize_t) IPC_GET_ARG1(answer); |
188 | } |
192 | } |
189 | 193 | ||
190 | off_t lseek(int fildes, off_t offset, int whence) |
194 | off_t lseek(int fildes, off_t offset, int whence) |
191 | { |
195 | { |
192 | int res; |
196 | int res; |
193 | ipcarg_t rc; |
197 | ipcarg_t rc; |
194 | 198 | ||
195 | futex_down(&vfs_phone_futex); |
199 | futex_down(&vfs_phone_futex); |
196 | async_serialize_start(); |
200 | async_serialize_start(); |
197 | if (vfs_phone < 0) { |
201 | if (vfs_phone < 0) { |
198 | res = vfs_connect(); |
202 | res = vfs_connect(); |
199 | if (res < 0) { |
203 | if (res < 0) { |
200 | async_serialize_end(); |
204 | async_serialize_end(); |
201 | futex_up(&vfs_phone_futex); |
205 | futex_up(&vfs_phone_futex); |
202 | return res; |
206 | return res; |
203 | } |
207 | } |
204 | } |
208 | } |
205 | 209 | ||
206 | off_t newoffs; |
210 | off_t newoffs; |
207 | rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence, |
211 | rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence, |
208 | &newoffs); |
212 | &newoffs); |
209 | 213 | ||
210 | async_serialize_end(); |
214 | async_serialize_end(); |
211 | futex_up(&vfs_phone_futex); |
215 | futex_up(&vfs_phone_futex); |
212 | 216 | ||
213 | if (rc != EOK) |
217 | if (rc != EOK) |
214 | return (off_t) -1; |
218 | return (off_t) -1; |
215 | 219 | ||
216 | return newoffs; |
220 | return newoffs; |
217 | } |
221 | } |
218 | 222 | ||
219 | int ftruncate(int fildes, off_t length) |
223 | int ftruncate(int fildes, off_t length) |
220 | { |
224 | { |
221 | int res; |
225 | int res; |
222 | ipcarg_t rc; |
226 | ipcarg_t rc; |
223 | 227 | ||
224 | futex_down(&vfs_phone_futex); |
228 | futex_down(&vfs_phone_futex); |
225 | async_serialize_start(); |
229 | async_serialize_start(); |
226 | if (vfs_phone < 0) { |
230 | if (vfs_phone < 0) { |
227 | res = vfs_connect(); |
231 | res = vfs_connect(); |
228 | if (res < 0) { |
232 | if (res < 0) { |
229 | async_serialize_end(); |
233 | async_serialize_end(); |
230 | futex_up(&vfs_phone_futex); |
234 | futex_up(&vfs_phone_futex); |
231 | return res; |
235 | return res; |
232 | } |
236 | } |
233 | } |
237 | } |
234 | rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length); |
238 | rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length); |
235 | async_serialize_end(); |
239 | async_serialize_end(); |
236 | futex_up(&vfs_phone_futex); |
240 | futex_up(&vfs_phone_futex); |
237 | return (int) rc; |
241 | return (int) rc; |
238 | } |
242 | } |
239 | 243 | ||
240 | DIR *opendir(const char *dirname) |
244 | DIR *opendir(const char *dirname) |
241 | { |
245 | { |
242 | DIR *dirp = malloc(sizeof(DIR)); |
246 | DIR *dirp = malloc(sizeof(DIR)); |
243 | if (!dirp) |
247 | if (!dirp) |
244 | return NULL; |
248 | return NULL; |
245 | dirp->fd = open(dirname, 0); /* TODO: must be a directory */ |
249 | dirp->fd = _open(dirname, L_DIRECTORY, 0); |
246 | if (dirp->fd < 0) { |
250 | if (dirp->fd < 0) { |
247 | free(dirp); |
251 | free(dirp); |
248 | return NULL; |
252 | return NULL; |
249 | } |
253 | } |
250 | return dirp; |
254 | return dirp; |
251 | } |
255 | } |
252 | 256 | ||
253 | struct dirent *readdir(DIR *dirp) |
257 | struct dirent *readdir(DIR *dirp) |
254 | { |
258 | { |
255 | ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1); |
259 | ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1); |
256 | if (len <= 0) |
260 | if (len <= 0) |
257 | return NULL; |
261 | return NULL; |
258 | return &dirp->res; |
262 | return &dirp->res; |
259 | } |
263 | } |
260 | 264 | ||
261 | void rewinddir(DIR *dirp) |
265 | void rewinddir(DIR *dirp) |
262 | { |
266 | { |
263 | (void) lseek(dirp->fd, 0, SEEK_SET); |
267 | (void) lseek(dirp->fd, 0, SEEK_SET); |
264 | } |
268 | } |
265 | 269 | ||
266 | int closedir(DIR *dirp) |
270 | int closedir(DIR *dirp) |
267 | { |
271 | { |
268 | (void) close(dirp->fd); |
272 | (void) close(dirp->fd); |
269 | free(dirp); |
273 | free(dirp); |
270 | return 0; |
274 | return 0; |
271 | } |
275 | } |
272 | 276 | ||
273 | int close(int fildes) |
277 | int close(int fildes) |
274 | { |
278 | { |
275 | return 0; /* TODO */ |
279 | return 0; /* TODO */ |
276 | } |
280 | } |
277 | 281 | ||
278 | /** @} |
282 | /** @} |
279 | */ |
283 | */ |
280 | 284 |