Subversion Repositories HelenOS

Rev

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

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