Subversion Repositories HelenOS

Rev

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

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