Subversion Repositories HelenOS

Rev

Rev 3246 | Rev 3292 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3246 Rev 3255
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/vfs.h>
35
#include <vfs/vfs.h>
36
#include <vfs/canonify.h>
36
#include <vfs/canonify.h>
37
#include <stdlib.h>
37
#include <stdlib.h>
38
#include <unistd.h>
38
#include <unistd.h>
39
#include <dirent.h>
39
#include <dirent.h>
40
#include <fcntl.h>
40
#include <fcntl.h>
41
#include <sys/stat.h>
41
#include <sys/stat.h>
42
#include <stdio.h>
42
#include <stdio.h>
43
#include <sys/types.h>
43
#include <sys/types.h>
44
#include <ipc/ipc.h>
44
#include <ipc/ipc.h>
45
#include <ipc/services.h>
45
#include <ipc/services.h>
46
#include <async.h>
46
#include <async.h>
47
#include <atomic.h>
47
#include <atomic.h>
48
#include <futex.h>
48
#include <futex.h>
49
#include <errno.h>
49
#include <errno.h>
50
#include <string.h>
50
#include <string.h>
51
#include <ipc/devmap.h>
51
#include <ipc/devmap.h>
52
#include "../../srv/vfs/vfs.h"
52
#include "../../srv/vfs/vfs.h"
53
 
53
 
54
int vfs_phone = -1;
54
int vfs_phone = -1;
55
futex_t vfs_phone_futex = FUTEX_INITIALIZER;
55
futex_t vfs_phone_futex = FUTEX_INITIALIZER;
56
 
56
 
57
futex_t cwd_futex = FUTEX_INITIALIZER;
57
futex_t cwd_futex = FUTEX_INITIALIZER;
58
DIR *cwd_dir = NULL;
58
DIR *cwd_dir = NULL;
59
char *cwd_path = NULL;
59
char *cwd_path = NULL;
60
size_t cwd_len = 0;
60
size_t cwd_len = 0;
61
 
61
 
62
static char *absolutize(const char *path, size_t *retlen)
62
static char *absolutize(const char *path, size_t *retlen)
63
{
63
{
64
    char *ncwd_path;
64
    char *ncwd_path;
65
 
65
 
66
    futex_down(&cwd_futex);
66
    futex_down(&cwd_futex);
67
    size_t len = strlen(path);
67
    size_t len = strlen(path);
68
    if (*path != '/') {
68
    if (*path != '/') {
69
        if (!cwd_path) {
69
        if (!cwd_path) {
70
            futex_up(&cwd_futex);
70
            futex_up(&cwd_futex);
71
            return NULL;
71
            return NULL;
72
        }
72
        }
73
        ncwd_path = malloc(len + cwd_len + 1);
73
        ncwd_path = malloc(len + cwd_len + 1);
74
        if (!ncwd_path) {
74
        if (!ncwd_path) {
75
            futex_up(&cwd_futex);
75
            futex_up(&cwd_futex);
76
            return NULL;
76
            return NULL;
77
        }
77
        }
78
        strcpy(ncwd_path, cwd_path);
78
        strcpy(ncwd_path, cwd_path);
79
        ncwd_path[cwd_len] = '/';
79
        ncwd_path[cwd_len] = '/';
80
        ncwd_path[cwd_len + 1] = '\0';
80
        ncwd_path[cwd_len + 1] = '\0';
81
    } else {
81
    } else {
82
        ncwd_path = malloc(len + 1);
82
        ncwd_path = malloc(len + 1);
83
        if (!ncwd_path) {
83
        if (!ncwd_path) {
84
            futex_up(&cwd_futex);
84
            futex_up(&cwd_futex);
85
            return NULL;
85
            return NULL;
86
        }
86
        }
87
        ncwd_path[0] = '\0';
87
        ncwd_path[0] = '\0';
88
    }
88
    }
89
    strcat(ncwd_path, path);
89
    strcat(ncwd_path, path);
90
    if (!canonify(ncwd_path, retlen)) {
90
    if (!canonify(ncwd_path, retlen)) {
91
        futex_up(&cwd_futex);
91
        futex_up(&cwd_futex);
92
        free(ncwd_path);
92
        free(ncwd_path);
93
        return NULL;
93
        return NULL;
94
    }
94
    }
95
    futex_up(&cwd_futex);
95
    futex_up(&cwd_futex);
96
    return ncwd_path;
96
    return ncwd_path;
97
}
97
}
98
 
98
 
99
static int vfs_connect(void)
99
static int vfs_connect(void)
100
{
100
{
101
    if (vfs_phone < 0)
101
    if (vfs_phone < 0)
102
        vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0);
102
        vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0);
103
    return vfs_phone;
103
    return vfs_phone;
104
}
104
}
105
 
105
 
106
static int device_get_handle(char *name, dev_handle_t *handle)
106
static int device_get_handle(char *name, dev_handle_t *handle)
107
{
107
{
108
    int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT,
108
    int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT,
109
        0);
109
        0);
110
    if (phone < 0)
110
    if (phone < 0)
111
        return phone;
111
        return phone;
112
   
112
   
113
    ipc_call_t answer;
113
    ipc_call_t answer;
114
    aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, 0, 0,
114
    aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, 0, 0,
115
        &answer);
115
        &answer);
116
   
116
   
117
    ipcarg_t retval = ipc_data_write_start(phone, name, strlen(name) + 1);
117
    ipcarg_t retval = ipc_data_write_start(phone, name, strlen(name) + 1);
118
 
118
 
119
    if (retval != EOK) {
119
    if (retval != EOK) {
120
        async_wait_for(req, NULL);
120
        async_wait_for(req, NULL);
121
        ipc_hangup(phone);
121
        ipc_hangup(phone);
122
        return retval;
122
        return retval;
123
    }
123
    }
124
 
124
 
125
    async_wait_for(req, &retval);
125
    async_wait_for(req, &retval);
126
 
126
 
127
    if (handle != NULL)
127
    if (handle != NULL)
128
        *handle = -1;
128
        *handle = -1;
129
   
129
   
130
    if (retval == EOK) {
130
    if (retval == EOK) {
131
        if (handle != NULL)
131
        if (handle != NULL)
132
            *handle = (dev_handle_t) IPC_GET_ARG1(answer);
132
            *handle = (dev_handle_t) IPC_GET_ARG1(answer);
133
    }
133
    }
134
   
134
   
135
    ipc_hangup(phone);
135
    ipc_hangup(phone);
136
    return retval;
136
    return retval;
137
}
137
}
138
 
138
 
139
int mount(const char *fs_name, const char *mp, const char *dev)
139
int mount(const char *fs_name, const char *mp, const char *dev)
140
{
140
{
141
    int res;
141
    int res;
142
    ipcarg_t rc;
142
    ipcarg_t rc;
143
    aid_t req;
143
    aid_t req;
144
    dev_handle_t dev_handle;
144
    dev_handle_t dev_handle;
145
   
145
   
146
    res = device_get_handle(dev, &dev_handle);
146
    res = device_get_handle(dev, &dev_handle);
147
    if (res != EOK)
147
    if (res != EOK)
148
        return res;
148
        return res;
149
   
149
   
150
    size_t mpa_len;
150
    size_t mpa_len;
151
    char *mpa = absolutize(mp, &mpa_len);
151
    char *mpa = absolutize(mp, &mpa_len);
152
    if (!mpa)
152
    if (!mpa)
153
        return ENOMEM;
153
        return ENOMEM;
154
 
154
 
155
    futex_down(&vfs_phone_futex);
155
    futex_down(&vfs_phone_futex);
156
    async_serialize_start();
156
    async_serialize_start();
157
    if (vfs_phone < 0) {
157
    if (vfs_phone < 0) {
158
        res = vfs_connect();
158
        res = vfs_connect();
159
        if (res < 0) {
159
        if (res < 0) {
160
            async_serialize_end();
160
            async_serialize_end();
161
            futex_up(&vfs_phone_futex);
161
            futex_up(&vfs_phone_futex);
162
            free(mpa);
162
            free(mpa);
163
            return res;
163
            return res;
164
        }
164
        }
165
    }
165
    }
166
    req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL);
166
    req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL);
167
    rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name));
167
    rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name));
168
    if (rc != EOK) {
168
    if (rc != EOK) {
169
        async_wait_for(req, NULL);
169
        async_wait_for(req, NULL);
170
        async_serialize_end();
170
        async_serialize_end();
171
        futex_up(&vfs_phone_futex);
171
        futex_up(&vfs_phone_futex);
172
        free(mpa);
172
        free(mpa);
173
        return (int) rc;
173
        return (int) rc;
174
    }
174
    }
-
 
175
    /* Ask VFS whether it likes fs_name. */
-
 
176
    rc = async_req_0_0(vfs_phone, IPC_M_PING);
-
 
177
    if (rc != EOK) {
-
 
178
        async_wait_for(req, NULL);
-
 
179
        async_serialize_end();
-
 
180
        futex_up(&vfs_phone_futex);
-
 
181
        free(mpa);
-
 
182
        return (int) rc;
-
 
183
    }
175
    rc = ipc_data_write_start(vfs_phone, (void *)mpa, mpa_len);
184
    rc = ipc_data_write_start(vfs_phone, (void *)mpa, mpa_len);
176
    if (rc != EOK) {
185
    if (rc != EOK) {
177
        async_wait_for(req, NULL);
186
        async_wait_for(req, NULL);
178
        async_serialize_end();
187
        async_serialize_end();
179
        futex_up(&vfs_phone_futex);
188
        futex_up(&vfs_phone_futex);
180
        free(mpa);
189
        free(mpa);
181
        return (int) rc;
190
        return (int) rc;
182
    }
191
    }
183
    async_wait_for(req, &rc);
192
    async_wait_for(req, &rc);
184
    async_serialize_end();
193
    async_serialize_end();
185
    futex_up(&vfs_phone_futex);
194
    futex_up(&vfs_phone_futex);
186
    free(mpa);
195
    free(mpa);
187
    return (int) rc;
196
    return (int) rc;
188
}
197
}
189
 
198
 
190
static int _open(const char *path, int lflag, int oflag, ...)
199
static int _open(const char *path, int lflag, int oflag, ...)
191
{
200
{
192
    int res;
201
    int res;
193
    ipcarg_t rc;
202
    ipcarg_t rc;
194
    ipc_call_t answer;
203
    ipc_call_t answer;
195
    aid_t req;
204
    aid_t req;
196
   
205
   
197
    size_t pa_len;
206
    size_t pa_len;
198
    char *pa = absolutize(path, &pa_len);
207
    char *pa = absolutize(path, &pa_len);
199
    if (!pa)
208
    if (!pa)
200
        return ENOMEM;
209
        return ENOMEM;
201
   
210
   
202
    futex_down(&vfs_phone_futex);
211
    futex_down(&vfs_phone_futex);
203
    async_serialize_start();
212
    async_serialize_start();
204
    if (vfs_phone < 0) {
213
    if (vfs_phone < 0) {
205
        res = vfs_connect();
214
        res = vfs_connect();
206
        if (res < 0) {
215
        if (res < 0) {
207
            async_serialize_end();
216
            async_serialize_end();
208
            futex_up(&vfs_phone_futex);
217
            futex_up(&vfs_phone_futex);
209
            free(pa);
218
            free(pa);
210
            return res;
219
            return res;
211
        }
220
        }
212
    }
221
    }
213
    req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer);
222
    req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer);
214
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
223
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
215
    if (rc != EOK) {
224
    if (rc != EOK) {
216
        async_wait_for(req, NULL);
225
        async_wait_for(req, NULL);
217
        async_serialize_end();
226
        async_serialize_end();
218
        futex_up(&vfs_phone_futex);
227
        futex_up(&vfs_phone_futex);
219
        free(pa);
228
        free(pa);
220
        return (int) rc;
229
        return (int) rc;
221
    }
230
    }
222
    async_wait_for(req, &rc);
231
    async_wait_for(req, &rc);
223
    async_serialize_end();
232
    async_serialize_end();
224
    futex_up(&vfs_phone_futex);
233
    futex_up(&vfs_phone_futex);
225
    free(pa);
234
    free(pa);
226
 
235
 
227
    if (rc != EOK)
236
    if (rc != EOK)
228
        return (int) rc;
237
        return (int) rc;
229
    return (int) IPC_GET_ARG1(answer);
238
    return (int) IPC_GET_ARG1(answer);
230
}
239
}
231
 
240
 
232
int open(const char *path, int oflag, ...)
241
int open(const char *path, int oflag, ...)
233
{
242
{
234
    return _open(path, L_FILE, oflag);
243
    return _open(path, L_FILE, oflag);
235
}
244
}
236
 
245
 
237
int close(int fildes)
246
int close(int fildes)
238
{
247
{
239
    int res;
248
    int res;
240
    ipcarg_t rc;
249
    ipcarg_t rc;
241
 
250
 
242
    futex_down(&vfs_phone_futex);
251
    futex_down(&vfs_phone_futex);
243
    async_serialize_start();
252
    async_serialize_start();
244
    if (vfs_phone < 0) {
253
    if (vfs_phone < 0) {
245
        res = vfs_connect();
254
        res = vfs_connect();
246
        if (res < 0) {
255
        if (res < 0) {
247
            async_serialize_end();
256
            async_serialize_end();
248
            futex_up(&vfs_phone_futex);
257
            futex_up(&vfs_phone_futex);
249
            return res;
258
            return res;
250
        }
259
        }
251
    }
260
    }
252
       
261
       
253
    rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes);
262
    rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes);
254
 
263
 
255
    async_serialize_end();
264
    async_serialize_end();
256
    futex_up(&vfs_phone_futex);
265
    futex_up(&vfs_phone_futex);
257
   
266
   
258
    return (int)rc;
267
    return (int)rc;
259
}
268
}
260
 
269
 
261
ssize_t read(int fildes, void *buf, size_t nbyte)
270
ssize_t read(int fildes, void *buf, size_t nbyte)
262
{
271
{
263
    int res;
272
    int res;
264
    ipcarg_t rc;
273
    ipcarg_t rc;
265
    ipc_call_t answer;
274
    ipc_call_t answer;
266
    aid_t req;
275
    aid_t req;
267
 
276
 
268
    futex_down(&vfs_phone_futex);
277
    futex_down(&vfs_phone_futex);
269
    async_serialize_start();
278
    async_serialize_start();
270
    if (vfs_phone < 0) {
279
    if (vfs_phone < 0) {
271
        res = vfs_connect();
280
        res = vfs_connect();
272
        if (res < 0) {
281
        if (res < 0) {
273
            async_serialize_end();
282
            async_serialize_end();
274
            futex_up(&vfs_phone_futex);
283
            futex_up(&vfs_phone_futex);
275
            return res;
284
            return res;
276
        }
285
        }
277
    }
286
    }
278
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
287
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
279
    rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte);
288
    rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte);
280
    if (rc != EOK) {
289
    if (rc != EOK) {
281
        async_wait_for(req, NULL);
290
        async_wait_for(req, NULL);
282
        async_serialize_end();
291
        async_serialize_end();
283
        futex_up(&vfs_phone_futex);
292
        futex_up(&vfs_phone_futex);
284
        return (ssize_t) rc;
293
        return (ssize_t) rc;
285
    }
294
    }
286
    async_wait_for(req, &rc);
295
    async_wait_for(req, &rc);
287
    async_serialize_end();
296
    async_serialize_end();
288
    futex_up(&vfs_phone_futex);
297
    futex_up(&vfs_phone_futex);
289
    if (rc == EOK)
298
    if (rc == EOK)
290
        return (ssize_t) IPC_GET_ARG1(answer);
299
        return (ssize_t) IPC_GET_ARG1(answer);
291
    else
300
    else
292
        return -1;
301
        return -1;
293
}
302
}
294
 
303
 
295
ssize_t write(int fildes, const void *buf, size_t nbyte)
304
ssize_t write(int fildes, const void *buf, size_t nbyte)
296
{
305
{
297
    int res;
306
    int res;
298
    ipcarg_t rc;
307
    ipcarg_t rc;
299
    ipc_call_t answer;
308
    ipc_call_t answer;
300
    aid_t req;
309
    aid_t req;
301
 
310
 
302
    futex_down(&vfs_phone_futex);
311
    futex_down(&vfs_phone_futex);
303
    async_serialize_start();
312
    async_serialize_start();
304
    if (vfs_phone < 0) {
313
    if (vfs_phone < 0) {
305
        res = vfs_connect();
314
        res = vfs_connect();
306
        if (res < 0) {
315
        if (res < 0) {
307
            async_serialize_end();
316
            async_serialize_end();
308
            futex_up(&vfs_phone_futex);
317
            futex_up(&vfs_phone_futex);
309
            return res;
318
            return res;
310
        }
319
        }
311
    }
320
    }
312
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
321
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
313
    rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte);
322
    rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte);
314
    if (rc != EOK) {
323
    if (rc != EOK) {
315
        async_wait_for(req, NULL);
324
        async_wait_for(req, NULL);
316
        async_serialize_end();
325
        async_serialize_end();
317
        futex_up(&vfs_phone_futex);
326
        futex_up(&vfs_phone_futex);
318
        return (ssize_t) rc;
327
        return (ssize_t) rc;
319
    }
328
    }
320
    async_wait_for(req, &rc);
329
    async_wait_for(req, &rc);
321
    async_serialize_end();
330
    async_serialize_end();
322
    futex_up(&vfs_phone_futex);
331
    futex_up(&vfs_phone_futex);
323
    if (rc == EOK)
332
    if (rc == EOK)
324
        return (ssize_t) IPC_GET_ARG1(answer);
333
        return (ssize_t) IPC_GET_ARG1(answer);
325
    else
334
    else
326
        return -1;
335
        return -1;
327
}
336
}
328
 
337
 
329
off_t lseek(int fildes, off_t offset, int whence)
338
off_t lseek(int fildes, off_t offset, int whence)
330
{
339
{
331
    int res;
340
    int res;
332
    ipcarg_t rc;
341
    ipcarg_t rc;
333
 
342
 
334
    futex_down(&vfs_phone_futex);
343
    futex_down(&vfs_phone_futex);
335
    async_serialize_start();
344
    async_serialize_start();
336
    if (vfs_phone < 0) {
345
    if (vfs_phone < 0) {
337
        res = vfs_connect();
346
        res = vfs_connect();
338
        if (res < 0) {
347
        if (res < 0) {
339
            async_serialize_end();
348
            async_serialize_end();
340
            futex_up(&vfs_phone_futex);
349
            futex_up(&vfs_phone_futex);
341
            return res;
350
            return res;
342
        }
351
        }
343
    }
352
    }
344
       
353
       
345
    off_t newoffs;
354
    off_t newoffs;
346
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
355
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
347
        (ipcarg_t)&newoffs);
356
        (ipcarg_t)&newoffs);
348
 
357
 
349
    async_serialize_end();
358
    async_serialize_end();
350
    futex_up(&vfs_phone_futex);
359
    futex_up(&vfs_phone_futex);
351
 
360
 
352
    if (rc != EOK)
361
    if (rc != EOK)
353
        return (off_t) -1;
362
        return (off_t) -1;
354
   
363
   
355
    return newoffs;
364
    return newoffs;
356
}
365
}
357
 
366
 
358
int ftruncate(int fildes, off_t length)
367
int ftruncate(int fildes, off_t length)
359
{
368
{
360
    int res;
369
    int res;
361
    ipcarg_t rc;
370
    ipcarg_t rc;
362
   
371
   
363
    futex_down(&vfs_phone_futex);
372
    futex_down(&vfs_phone_futex);
364
    async_serialize_start();
373
    async_serialize_start();
365
    if (vfs_phone < 0) {
374
    if (vfs_phone < 0) {
366
        res = vfs_connect();
375
        res = vfs_connect();
367
        if (res < 0) {
376
        if (res < 0) {
368
            async_serialize_end();
377
            async_serialize_end();
369
            futex_up(&vfs_phone_futex);
378
            futex_up(&vfs_phone_futex);
370
            return res;
379
            return res;
371
        }
380
        }
372
    }
381
    }
373
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
382
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
374
    async_serialize_end();
383
    async_serialize_end();
375
    futex_up(&vfs_phone_futex);
384
    futex_up(&vfs_phone_futex);
376
    return (int) rc;
385
    return (int) rc;
377
}
386
}
378
 
387
 
379
DIR *opendir(const char *dirname)
388
DIR *opendir(const char *dirname)
380
{
389
{
381
    DIR *dirp = malloc(sizeof(DIR));
390
    DIR *dirp = malloc(sizeof(DIR));
382
    if (!dirp)
391
    if (!dirp)
383
        return NULL;
392
        return NULL;
384
    dirp->fd = _open(dirname, L_DIRECTORY, 0);
393
    dirp->fd = _open(dirname, L_DIRECTORY, 0);
385
    if (dirp->fd < 0) {
394
    if (dirp->fd < 0) {
386
        free(dirp);
395
        free(dirp);
387
        return NULL;
396
        return NULL;
388
    }
397
    }
389
    return dirp;
398
    return dirp;
390
}
399
}
391
 
400
 
392
struct dirent *readdir(DIR *dirp)
401
struct dirent *readdir(DIR *dirp)
393
{
402
{
394
    ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
403
    ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
395
    if (len <= 0)
404
    if (len <= 0)
396
        return NULL;
405
        return NULL;
397
    return &dirp->res;
406
    return &dirp->res;
398
}
407
}
399
 
408
 
400
void rewinddir(DIR *dirp)
409
void rewinddir(DIR *dirp)
401
{
410
{
402
    (void) lseek(dirp->fd, 0, SEEK_SET);
411
    (void) lseek(dirp->fd, 0, SEEK_SET);
403
}
412
}
404
 
413
 
405
int closedir(DIR *dirp)
414
int closedir(DIR *dirp)
406
{
415
{
407
    (void) close(dirp->fd);
416
    (void) close(dirp->fd);
408
    free(dirp);
417
    free(dirp);
409
    return 0;
418
    return 0;
410
}
419
}
411
 
420
 
412
int mkdir(const char *path, mode_t mode)
421
int mkdir(const char *path, mode_t mode)
413
{
422
{
414
    int res;
423
    int res;
415
    ipcarg_t rc;
424
    ipcarg_t rc;
416
    aid_t req;
425
    aid_t req;
417
   
426
   
418
    size_t pa_len;
427
    size_t pa_len;
419
    char *pa = absolutize(path, &pa_len);
428
    char *pa = absolutize(path, &pa_len);
420
    if (!pa)
429
    if (!pa)
421
        return ENOMEM;
430
        return ENOMEM;
422
 
431
 
423
    futex_down(&vfs_phone_futex);
432
    futex_down(&vfs_phone_futex);
424
    async_serialize_start();
433
    async_serialize_start();
425
    if (vfs_phone < 0) {
434
    if (vfs_phone < 0) {
426
        res = vfs_connect();
435
        res = vfs_connect();
427
        if (res < 0) {
436
        if (res < 0) {
428
            async_serialize_end();
437
            async_serialize_end();
429
            futex_up(&vfs_phone_futex);
438
            futex_up(&vfs_phone_futex);
430
            free(pa);
439
            free(pa);
431
            return res;
440
            return res;
432
        }
441
        }
433
    }
442
    }
434
    req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL);
443
    req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL);
435
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
444
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
436
    if (rc != EOK) {
445
    if (rc != EOK) {
437
        async_wait_for(req, NULL);
446
        async_wait_for(req, NULL);
438
        async_serialize_end();
447
        async_serialize_end();
439
        futex_up(&vfs_phone_futex);
448
        futex_up(&vfs_phone_futex);
440
        free(pa);
449
        free(pa);
441
        return (int) rc;
450
        return (int) rc;
442
    }
451
    }
443
    async_wait_for(req, &rc);
452
    async_wait_for(req, &rc);
444
    async_serialize_end();
453
    async_serialize_end();
445
    futex_up(&vfs_phone_futex);
454
    futex_up(&vfs_phone_futex);
446
    free(pa);
455
    free(pa);
447
    return rc;
456
    return rc;
448
}
457
}
449
 
458
 
450
static int _unlink(const char *path, int lflag)
459
static int _unlink(const char *path, int lflag)
451
{
460
{
452
    int res;
461
    int res;
453
    ipcarg_t rc;
462
    ipcarg_t rc;
454
    aid_t req;
463
    aid_t req;
455
   
464
   
456
    size_t pa_len;
465
    size_t pa_len;
457
    char *pa = absolutize(path, &pa_len);
466
    char *pa = absolutize(path, &pa_len);
458
    if (!pa)
467
    if (!pa)
459
        return ENOMEM;
468
        return ENOMEM;
460
 
469
 
461
    futex_down(&vfs_phone_futex);
470
    futex_down(&vfs_phone_futex);
462
    async_serialize_start();
471
    async_serialize_start();
463
    if (vfs_phone < 0) {
472
    if (vfs_phone < 0) {
464
        res = vfs_connect();
473
        res = vfs_connect();
465
        if (res < 0) {
474
        if (res < 0) {
466
            async_serialize_end();
475
            async_serialize_end();
467
            futex_up(&vfs_phone_futex);
476
            futex_up(&vfs_phone_futex);
468
            free(pa);
477
            free(pa);
469
            return res;
478
            return res;
470
        }
479
        }
471
    }
480
    }
472
    req = async_send_0(vfs_phone, VFS_UNLINK, NULL);
481
    req = async_send_0(vfs_phone, VFS_UNLINK, NULL);
473
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
482
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
474
    if (rc != EOK) {
483
    if (rc != EOK) {
475
        async_wait_for(req, NULL);
484
        async_wait_for(req, NULL);
476
        async_serialize_end();
485
        async_serialize_end();
477
        futex_up(&vfs_phone_futex);
486
        futex_up(&vfs_phone_futex);
478
        free(pa);
487
        free(pa);
479
        return (int) rc;
488
        return (int) rc;
480
    }
489
    }
481
    async_wait_for(req, &rc);
490
    async_wait_for(req, &rc);
482
    async_serialize_end();
491
    async_serialize_end();
483
    futex_up(&vfs_phone_futex);
492
    futex_up(&vfs_phone_futex);
484
    free(pa);
493
    free(pa);
485
    return rc;
494
    return rc;
486
}
495
}
487
 
496
 
488
int unlink(const char *path)
497
int unlink(const char *path)
489
{
498
{
490
    return _unlink(path, L_NONE);
499
    return _unlink(path, L_NONE);
491
}
500
}
492
 
501
 
493
int rmdir(const char *path)
502
int rmdir(const char *path)
494
{
503
{
495
    return _unlink(path, L_DIRECTORY);
504
    return _unlink(path, L_DIRECTORY);
496
}
505
}
497
 
506
 
498
int rename(const char *old, const char *new)
507
int rename(const char *old, const char *new)
499
{
508
{
500
    int res;
509
    int res;
501
    ipcarg_t rc;
510
    ipcarg_t rc;
502
    aid_t req;
511
    aid_t req;
503
   
512
   
504
    size_t olda_len;
513
    size_t olda_len;
505
    char *olda = absolutize(old, &olda_len);
514
    char *olda = absolutize(old, &olda_len);
506
    if (!olda)
515
    if (!olda)
507
        return ENOMEM;
516
        return ENOMEM;
508
 
517
 
509
    size_t newa_len;
518
    size_t newa_len;
510
    char *newa = absolutize(new, &newa_len);
519
    char *newa = absolutize(new, &newa_len);
511
    if (!newa) {
520
    if (!newa) {
512
        free(olda);
521
        free(olda);
513
        return ENOMEM;
522
        return ENOMEM;
514
    }
523
    }
515
 
524
 
516
    futex_down(&vfs_phone_futex);
525
    futex_down(&vfs_phone_futex);
517
    async_serialize_start();
526
    async_serialize_start();
518
    if (vfs_phone < 0) {
527
    if (vfs_phone < 0) {
519
        res = vfs_connect();
528
        res = vfs_connect();
520
        if (res < 0) {
529
        if (res < 0) {
521
            async_serialize_end();
530
            async_serialize_end();
522
            futex_up(&vfs_phone_futex);
531
            futex_up(&vfs_phone_futex);
523
            free(olda);
532
            free(olda);
524
            free(newa);
533
            free(newa);
525
            return res;
534
            return res;
526
        }
535
        }
527
    }
536
    }
528
    req = async_send_0(vfs_phone, VFS_RENAME, NULL);
537
    req = async_send_0(vfs_phone, VFS_RENAME, NULL);
529
    rc = ipc_data_write_start(vfs_phone, olda, olda_len);
538
    rc = ipc_data_write_start(vfs_phone, olda, olda_len);
530
    if (rc != EOK) {
539
    if (rc != EOK) {
531
        async_wait_for(req, NULL);
540
        async_wait_for(req, NULL);
532
        async_serialize_end();
541
        async_serialize_end();
533
        futex_up(&vfs_phone_futex);
542
        futex_up(&vfs_phone_futex);
534
        free(olda);
543
        free(olda);
535
        free(newa);
544
        free(newa);
536
        return (int) rc;
545
        return (int) rc;
537
    }
546
    }
538
    rc = ipc_data_write_start(vfs_phone, newa, newa_len);
547
    rc = ipc_data_write_start(vfs_phone, newa, newa_len);
539
    if (rc != EOK) {
548
    if (rc != EOK) {
540
        async_wait_for(req, NULL);
549
        async_wait_for(req, NULL);
541
        async_serialize_end();
550
        async_serialize_end();
542
        futex_up(&vfs_phone_futex);
551
        futex_up(&vfs_phone_futex);
543
        free(olda);
552
        free(olda);
544
        free(newa);
553
        free(newa);
545
        return (int) rc;
554
        return (int) rc;
546
    }
555
    }
547
    async_wait_for(req, &rc);
556
    async_wait_for(req, &rc);
548
    async_serialize_end();
557
    async_serialize_end();
549
    futex_up(&vfs_phone_futex);
558
    futex_up(&vfs_phone_futex);
550
    free(olda);
559
    free(olda);
551
    free(newa);
560
    free(newa);
552
    return rc;
561
    return rc;
553
}
562
}
554
 
563
 
555
int chdir(const char *path)
564
int chdir(const char *path)
556
{
565
{
557
    size_t pa_len;
566
    size_t pa_len;
558
    char *pa = absolutize(path, &pa_len);
567
    char *pa = absolutize(path, &pa_len);
559
    if (!pa)
568
    if (!pa)
560
        return ENOMEM;
569
        return ENOMEM;
561
 
570
 
562
    DIR *d = opendir(pa);
571
    DIR *d = opendir(pa);
563
    if (!d) {
572
    if (!d) {
564
        free(pa);
573
        free(pa);
565
        return ENOENT;
574
        return ENOENT;
566
    }
575
    }
567
 
576
 
568
    futex_down(&cwd_futex);
577
    futex_down(&cwd_futex);
569
    if (cwd_dir) {
578
    if (cwd_dir) {
570
        closedir(cwd_dir);
579
        closedir(cwd_dir);
571
        cwd_dir = NULL;
580
        cwd_dir = NULL;
572
        free(cwd_path);
581
        free(cwd_path);
573
        cwd_path = NULL;
582
        cwd_path = NULL;
574
        cwd_len = 0;
583
        cwd_len = 0;
575
    }
584
    }
576
    cwd_dir = d;
585
    cwd_dir = d;
577
    cwd_path = pa;
586
    cwd_path = pa;
578
    cwd_len = pa_len;
587
    cwd_len = pa_len;
579
    futex_up(&cwd_futex);
588
    futex_up(&cwd_futex);
580
}
589
}
581
 
590
 
582
char *getcwd(char *buf, size_t size)
591
char *getcwd(char *buf, size_t size)
583
{
592
{
584
    if (!size)
593
    if (!size)
585
        return NULL;
594
        return NULL;
586
    futex_down(&cwd_futex);
595
    futex_down(&cwd_futex);
587
    if (size < cwd_len + 1) {
596
    if (size < cwd_len + 1) {
588
        futex_up(&cwd_futex);
597
        futex_up(&cwd_futex);
589
        return NULL;
598
        return NULL;
590
    }
599
    }
591
    strcpy(buf, cwd_path);
600
    strcpy(buf, cwd_path);
592
    futex_up(&cwd_futex);
601
    futex_up(&cwd_futex);
593
    return buf;
602
    return buf;
594
}
603
}
595
 
604
 
596
/** @}
605
/** @}
597
 */
606
 */
598
 
607