Subversion Repositories HelenOS

Rev

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

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