Rev 2693 | Rev 2699 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2693 | Rev 2694 | ||
---|---|---|---|
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 <unistd.h> |
37 | #include <unistd.h> |
- | 38 | #include <dirent.h> |
|
37 | #include <fcntl.h> |
39 | #include <fcntl.h> |
38 | #include <ipc/ipc.h> |
40 | #include <ipc/ipc.h> |
39 | #include <ipc/services.h> |
41 | #include <ipc/services.h> |
40 | #include <async.h> |
42 | #include <async.h> |
41 | #include <atomic.h> |
43 | #include <atomic.h> |
42 | #include <futex.h> |
44 | #include <futex.h> |
43 | #include <errno.h> |
45 | #include <errno.h> |
44 | #include <string.h> |
46 | #include <string.h> |
45 | #include "../../srv/vfs/vfs.h" |
47 | #include "../../srv/vfs/vfs.h" |
46 | 48 | ||
47 | int vfs_phone = -1; |
49 | int vfs_phone = -1; |
48 | atomic_t vfs_phone_futex = FUTEX_INITIALIZER; |
50 | atomic_t vfs_phone_futex = FUTEX_INITIALIZER; |
49 | 51 | ||
50 | static int vfs_connect(void) |
52 | static int vfs_connect(void) |
51 | { |
53 | { |
52 | if (vfs_phone < 0) |
54 | if (vfs_phone < 0) |
53 | 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); |
54 | return vfs_phone; |
56 | return vfs_phone; |
55 | } |
57 | } |
56 | 58 | ||
57 | 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) |
58 | { |
60 | { |
59 | int res; |
61 | int res; |
60 | ipcarg_t rc; |
62 | ipcarg_t rc; |
61 | aid_t req; |
63 | aid_t req; |
62 | 64 | ||
63 | int dev_handle = 0; /* TODO */ |
65 | int dev_handle = 0; /* TODO */ |
64 | 66 | ||
65 | futex_down(&vfs_phone_futex); |
67 | futex_down(&vfs_phone_futex); |
66 | async_serialize_start(); |
68 | async_serialize_start(); |
67 | if (vfs_phone < 0) { |
69 | if (vfs_phone < 0) { |
68 | res = vfs_connect(); |
70 | res = vfs_connect(); |
69 | if (res < 0) { |
71 | if (res < 0) { |
70 | async_serialize_end(); |
72 | async_serialize_end(); |
71 | futex_up(&vfs_phone_futex); |
73 | futex_up(&vfs_phone_futex); |
72 | return res; |
74 | return res; |
73 | } |
75 | } |
74 | } |
76 | } |
75 | req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL); |
77 | req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL); |
76 | 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)); |
77 | if (rc != EOK) { |
79 | if (rc != EOK) { |
78 | async_wait_for(req, NULL); |
80 | async_wait_for(req, NULL); |
79 | async_serialize_end(); |
81 | async_serialize_end(); |
80 | futex_up(&vfs_phone_futex); |
82 | futex_up(&vfs_phone_futex); |
81 | return (int) rc; |
83 | return (int) rc; |
82 | } |
84 | } |
83 | rc = ipc_data_write_start(vfs_phone, (void *)mp, strlen(mp)); |
85 | rc = ipc_data_write_start(vfs_phone, (void *)mp, strlen(mp)); |
84 | if (rc != EOK) { |
86 | if (rc != EOK) { |
85 | async_wait_for(req, NULL); |
87 | async_wait_for(req, NULL); |
86 | async_serialize_end(); |
88 | async_serialize_end(); |
87 | futex_up(&vfs_phone_futex); |
89 | futex_up(&vfs_phone_futex); |
88 | return (int) rc; |
90 | return (int) rc; |
89 | } |
91 | } |
90 | async_wait_for(req, &rc); |
92 | async_wait_for(req, &rc); |
91 | async_serialize_end(); |
93 | async_serialize_end(); |
92 | futex_up(&vfs_phone_futex); |
94 | futex_up(&vfs_phone_futex); |
93 | return (int) rc; |
95 | return (int) rc; |
94 | } |
96 | } |
95 | 97 | ||
96 | 98 | ||
97 | int open(const char *path, int oflag, ...) |
99 | int open(const char *path, int oflag, ...) |
98 | { |
100 | { |
99 | int res; |
101 | int res; |
100 | ipcarg_t rc; |
102 | ipcarg_t rc; |
101 | ipc_call_t answer; |
103 | ipc_call_t answer; |
102 | aid_t req; |
104 | aid_t req; |
103 | 105 | ||
104 | futex_down(&vfs_phone_futex); |
106 | futex_down(&vfs_phone_futex); |
105 | async_serialize_start(); |
107 | async_serialize_start(); |
106 | if (vfs_phone < 0) { |
108 | if (vfs_phone < 0) { |
107 | res = vfs_connect(); |
109 | res = vfs_connect(); |
108 | if (res < 0) { |
110 | if (res < 0) { |
109 | async_serialize_end(); |
111 | async_serialize_end(); |
110 | futex_up(&vfs_phone_futex); |
112 | futex_up(&vfs_phone_futex); |
111 | return res; |
113 | return res; |
112 | } |
114 | } |
113 | } |
115 | } |
114 | req = async_send_2(vfs_phone, VFS_OPEN, oflag, 0, &answer); |
116 | req = async_send_2(vfs_phone, VFS_OPEN, oflag, 0, &answer); |
115 | rc = ipc_data_write_start(vfs_phone, path, strlen(path)); |
117 | rc = ipc_data_write_start(vfs_phone, path, strlen(path)); |
116 | if (rc != EOK) { |
118 | if (rc != EOK) { |
117 | async_wait_for(req, NULL); |
119 | async_wait_for(req, NULL); |
118 | async_serialize_end(); |
120 | async_serialize_end(); |
119 | futex_up(&vfs_phone_futex); |
121 | futex_up(&vfs_phone_futex); |
120 | return (int) rc; |
122 | return (int) rc; |
121 | } |
123 | } |
122 | async_wait_for(req, &rc); |
124 | async_wait_for(req, &rc); |
123 | async_serialize_end(); |
125 | async_serialize_end(); |
124 | futex_up(&vfs_phone_futex); |
126 | futex_up(&vfs_phone_futex); |
125 | return (int) IPC_GET_ARG1(answer); |
127 | return (int) IPC_GET_ARG1(answer); |
126 | } |
128 | } |
127 | 129 | ||
128 | ssize_t read(int fildes, void *buf, size_t nbyte) |
130 | ssize_t read(int fildes, void *buf, size_t nbyte) |
129 | { |
131 | { |
130 | int res; |
132 | int res; |
131 | ipcarg_t rc; |
133 | ipcarg_t rc; |
132 | ipc_call_t answer; |
134 | ipc_call_t answer; |
133 | aid_t req; |
135 | aid_t req; |
134 | 136 | ||
135 | futex_down(&vfs_phone_futex); |
137 | futex_down(&vfs_phone_futex); |
136 | async_serialize_start(); |
138 | async_serialize_start(); |
137 | if (vfs_phone < 0) { |
139 | if (vfs_phone < 0) { |
138 | res = vfs_connect(); |
140 | res = vfs_connect(); |
139 | if (res < 0) { |
141 | if (res < 0) { |
140 | async_serialize_end(); |
142 | async_serialize_end(); |
141 | futex_up(&vfs_phone_futex); |
143 | futex_up(&vfs_phone_futex); |
142 | return res; |
144 | return res; |
143 | } |
145 | } |
144 | } |
146 | } |
145 | req = async_send_1(vfs_phone, VFS_READ, fildes, &answer); |
147 | req = async_send_1(vfs_phone, VFS_READ, fildes, &answer); |
146 | if (ipc_data_read_start(vfs_phone, (void *)buf, nbyte) != EOK) { |
148 | if (ipc_data_read_start(vfs_phone, (void *)buf, nbyte) != EOK) { |
147 | async_wait_for(req, NULL); |
149 | async_wait_for(req, NULL); |
148 | async_serialize_end(); |
150 | async_serialize_end(); |
149 | futex_up(&vfs_phone_futex); |
151 | futex_up(&vfs_phone_futex); |
150 | return (ssize_t) rc; |
152 | return (ssize_t) rc; |
151 | } |
153 | } |
152 | async_wait_for(req, &rc); |
154 | async_wait_for(req, &rc); |
153 | async_serialize_end(); |
155 | async_serialize_end(); |
154 | futex_up(&vfs_phone_futex); |
156 | futex_up(&vfs_phone_futex); |
155 | return (ssize_t) IPC_GET_ARG1(answer); |
157 | return (ssize_t) IPC_GET_ARG1(answer); |
156 | } |
158 | } |
157 | 159 | ||
158 | ssize_t write(int fildes, const void *buf, size_t nbyte) |
160 | ssize_t write(int fildes, const void *buf, size_t nbyte) |
159 | { |
161 | { |
160 | int res; |
162 | int res; |
161 | ipcarg_t rc; |
163 | ipcarg_t rc; |
162 | ipc_call_t answer; |
164 | ipc_call_t answer; |
163 | aid_t req; |
165 | aid_t req; |
164 | 166 | ||
165 | futex_down(&vfs_phone_futex); |
167 | futex_down(&vfs_phone_futex); |
166 | async_serialize_start(); |
168 | async_serialize_start(); |
167 | if (vfs_phone < 0) { |
169 | if (vfs_phone < 0) { |
168 | res = vfs_connect(); |
170 | res = vfs_connect(); |
169 | if (res < 0) { |
171 | if (res < 0) { |
170 | async_serialize_end(); |
172 | async_serialize_end(); |
171 | futex_up(&vfs_phone_futex); |
173 | futex_up(&vfs_phone_futex); |
172 | return res; |
174 | return res; |
173 | } |
175 | } |
174 | } |
176 | } |
175 | req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer); |
177 | req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer); |
176 | if (ipc_data_write_start(vfs_phone, (void *)buf, nbyte) != EOK) { |
178 | if (ipc_data_write_start(vfs_phone, (void *)buf, nbyte) != EOK) { |
177 | async_wait_for(req, NULL); |
179 | async_wait_for(req, NULL); |
178 | async_serialize_end(); |
180 | async_serialize_end(); |
179 | futex_up(&vfs_phone_futex); |
181 | futex_up(&vfs_phone_futex); |
180 | return (ssize_t) rc; |
182 | return (ssize_t) rc; |
181 | } |
183 | } |
182 | async_wait_for(req, &rc); |
184 | async_wait_for(req, &rc); |
183 | async_serialize_end(); |
185 | async_serialize_end(); |
184 | futex_up(&vfs_phone_futex); |
186 | futex_up(&vfs_phone_futex); |
185 | return (ssize_t) IPC_GET_ARG1(answer); |
187 | return (ssize_t) IPC_GET_ARG1(answer); |
186 | } |
188 | } |
187 | 189 | ||
188 | off_t lseek(int fildes, off_t offset, int whence) |
190 | off_t lseek(int fildes, off_t offset, int whence) |
189 | { |
191 | { |
190 | int res; |
192 | int res; |
191 | ipcarg_t rc; |
193 | ipcarg_t rc; |
192 | 194 | ||
193 | futex_down(&vfs_phone_futex); |
195 | futex_down(&vfs_phone_futex); |
194 | async_serialize_start(); |
196 | async_serialize_start(); |
195 | if (vfs_phone < 0) { |
197 | if (vfs_phone < 0) { |
196 | res = vfs_connect(); |
198 | res = vfs_connect(); |
197 | if (res < 0) { |
199 | if (res < 0) { |
198 | async_serialize_end(); |
200 | async_serialize_end(); |
199 | futex_up(&vfs_phone_futex); |
201 | futex_up(&vfs_phone_futex); |
200 | return res; |
202 | return res; |
201 | } |
203 | } |
202 | } |
204 | } |
203 | 205 | ||
204 | off_t newoffs; |
206 | off_t newoffs; |
205 | rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence, |
207 | rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence, |
206 | &newoffs); |
208 | &newoffs); |
207 | 209 | ||
208 | async_serialize_end(); |
210 | async_serialize_end(); |
209 | futex_up(&vfs_phone_futex); |
211 | futex_up(&vfs_phone_futex); |
210 | 212 | ||
211 | if (rc != EOK) |
213 | if (rc != EOK) |
212 | return (off_t) -1; |
214 | return (off_t) -1; |
213 | 215 | ||
214 | return newoffs; |
216 | return newoffs; |
215 | } |
217 | } |
216 | 218 | ||
217 | int ftruncate(int fildes, off_t length) |
219 | int ftruncate(int fildes, off_t length) |
218 | { |
220 | { |
219 | int res; |
221 | int res; |
220 | ipcarg_t rc; |
222 | ipcarg_t rc; |
221 | 223 | ||
222 | futex_down(&vfs_phone_futex); |
224 | futex_down(&vfs_phone_futex); |
223 | async_serialize_start(); |
225 | async_serialize_start(); |
224 | if (vfs_phone < 0) { |
226 | if (vfs_phone < 0) { |
225 | res = vfs_connect(); |
227 | res = vfs_connect(); |
226 | if (res < 0) { |
228 | if (res < 0) { |
227 | async_serialize_end(); |
229 | async_serialize_end(); |
228 | futex_up(&vfs_phone_futex); |
230 | futex_up(&vfs_phone_futex); |
229 | return res; |
231 | return res; |
230 | } |
232 | } |
231 | } |
233 | } |
232 | rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length); |
234 | rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length); |
233 | async_serialize_end(); |
235 | async_serialize_end(); |
234 | futex_up(&vfs_phone_futex); |
236 | futex_up(&vfs_phone_futex); |
235 | return (int) rc; |
237 | return (int) rc; |
236 | } |
238 | } |
- | 239 | ||
- | 240 | DIR *opendir(const char *dirname) |
|
- | 241 | { |
|
- | 242 | DIR *dirp = malloc(sizeof(DIR)); |
|
- | 243 | if (!dirp) |
|
- | 244 | return NULL; |
|
- | 245 | dirp->fd = open(dirname, 0); /* TODO: must be a directory */ |
|
- | 246 | if (!dirp->fd) { |
|
- | 247 | free(dirp); |
|
- | 248 | return NULL; |
|
- | 249 | } |
|
- | 250 | dirp->pos = 0; |
|
- | 251 | return dirp; |
|
- | 252 | } |
|
- | 253 | ||
- | 254 | struct dirent *readdir(DIR *dirp) |
|
- | 255 | { |
|
- | 256 | return NULL; /* TODO */ |
|
- | 257 | } |
|
- | 258 | ||
- | 259 | void rewinddir(DIR *dirp) |
|
- | 260 | { |
|
- | 261 | dirp->pos = 0; |
|
- | 262 | } |
|
- | 263 | ||
- | 264 | int closedir(DIR *dirp) |
|
- | 265 | { |
|
- | 266 | (void) close(dirp->fd); |
|
- | 267 | free(dirp); |
|
- | 268 | return 0; |
|
- | 269 | } |
|
- | 270 | ||
- | 271 | int close(int fildes) |
|
- | 272 | { |
|
- | 273 | return 0; /* TODO */ |
|
- | 274 | } |
|
237 | 275 | ||
238 | /** @} |
276 | /** @} |
239 | */ |
277 | */ |
240 | 278 |