Subversion Repositories HelenOS

Rev

Rev 4305 | Rev 4482 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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