Subversion Repositories HelenOS

Rev

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

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