Subversion Repositories HelenOS

Rev

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

Rev 4296 Rev 4420
1
/*
1
/*
2
 * Copyright (c) 2008 Jakub Jermar
2
 * Copyright (c) 2008 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup libc
29
/** @addtogroup libc
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <vfs/vfs.h>
35
#include <vfs/vfs.h>
36
#include <vfs/canonify.h>
36
#include <vfs/canonify.h>
37
#include <stdlib.h>
37
#include <stdlib.h>
38
#include <unistd.h>
38
#include <unistd.h>
39
#include <dirent.h>
39
#include <dirent.h>
40
#include <fcntl.h>
40
#include <fcntl.h>
41
#include <sys/stat.h>
41
#include <sys/stat.h>
42
#include <stdio.h>
42
#include <stdio.h>
43
#include <sys/types.h>
43
#include <sys/types.h>
44
#include <ipc/ipc.h>
44
#include <ipc/ipc.h>
45
#include <ipc/services.h>
45
#include <ipc/services.h>
46
#include <async.h>
46
#include <async.h>
47
#include <atomic.h>
47
#include <atomic.h>
48
#include <futex.h>
48
#include <futex.h>
49
#include <errno.h>
49
#include <errno.h>
50
#include <string.h>
50
#include <string.h>
51
#include <ipc/devmap.h>
51
#include <devmap.h>
52
#include "../../../srv/vfs/vfs.h"
52
#include "../../../srv/vfs/vfs.h"
53
 
53
 
54
int vfs_phone = -1;
54
int vfs_phone = -1;
55
futex_t vfs_phone_futex = FUTEX_INITIALIZER;
55
futex_t vfs_phone_futex = FUTEX_INITIALIZER;
56
 
56
 
57
futex_t cwd_futex = FUTEX_INITIALIZER;
57
futex_t cwd_futex = FUTEX_INITIALIZER;
58
DIR *cwd_dir = NULL;
58
DIR *cwd_dir = NULL;
59
char *cwd_path = NULL;
59
char *cwd_path = NULL;
60
size_t cwd_size = 0;
60
size_t cwd_size = 0;
61
 
61
 
62
char *absolutize(const char *path, size_t *retlen)
62
char *absolutize(const char *path, size_t *retlen)
63
{
63
{
64
    char *ncwd_path;
64
    char *ncwd_path;
65
    char *ncwd_path_nc;
65
    char *ncwd_path_nc;
66
 
66
 
67
    futex_down(&cwd_futex);
67
    futex_down(&cwd_futex);
68
    size_t size = str_size(path);
68
    size_t size = str_size(path);
69
    if (*path != '/') {
69
    if (*path != '/') {
70
        if (!cwd_path) {
70
        if (!cwd_path) {
71
            futex_up(&cwd_futex);
71
            futex_up(&cwd_futex);
72
            return NULL;
72
            return NULL;
73
        }
73
        }
74
        ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
74
        ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
75
        if (!ncwd_path_nc) {
75
        if (!ncwd_path_nc) {
76
            futex_up(&cwd_futex);
76
            futex_up(&cwd_futex);
77
            return NULL;
77
            return NULL;
78
        }
78
        }
79
        str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
79
        str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
80
        ncwd_path_nc[cwd_size] = '/';
80
        ncwd_path_nc[cwd_size] = '/';
81
        ncwd_path_nc[cwd_size + 1] = '\0';
81
        ncwd_path_nc[cwd_size + 1] = '\0';
82
    } else {
82
    } else {
83
        ncwd_path_nc = malloc(size + 1);
83
        ncwd_path_nc = malloc(size + 1);
84
        if (!ncwd_path_nc) {
84
        if (!ncwd_path_nc) {
85
            futex_up(&cwd_futex);
85
            futex_up(&cwd_futex);
86
            return NULL;
86
            return NULL;
87
        }
87
        }
88
        ncwd_path_nc[0] = '\0';
88
        ncwd_path_nc[0] = '\0';
89
    }
89
    }
90
    str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
90
    str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
91
    ncwd_path = canonify(ncwd_path_nc, retlen);
91
    ncwd_path = canonify(ncwd_path_nc, retlen);
92
    if (!ncwd_path) {
92
    if (!ncwd_path) {
93
        futex_up(&cwd_futex);
93
        futex_up(&cwd_futex);
94
        free(ncwd_path_nc);
94
        free(ncwd_path_nc);
95
        return NULL;
95
        return NULL;
96
    }
96
    }
97
    /*
97
    /*
98
     * We need to clone ncwd_path because canonify() works in-place and thus
98
     * We need to clone ncwd_path because canonify() works in-place and thus
99
     * the address in ncwd_path need not be the same as ncwd_path_nc, even
99
     * the address in ncwd_path need not be the same as ncwd_path_nc, even
100
     * though they both point into the same dynamically allocated buffer.
100
     * though they both point into the same dynamically allocated buffer.
101
     */
101
     */
102
    ncwd_path = str_dup(ncwd_path);
102
    ncwd_path = str_dup(ncwd_path);
103
    free(ncwd_path_nc);
103
    free(ncwd_path_nc);
104
    if (!ncwd_path) {
104
    if (!ncwd_path) {
105
        futex_up(&cwd_futex);
105
        futex_up(&cwd_futex);
106
        return NULL;
106
        return NULL;
107
    }
107
    }
108
    futex_up(&cwd_futex);
108
    futex_up(&cwd_futex);
109
    return ncwd_path;
109
    return ncwd_path;
110
}
110
}
111
 
111
 
112
static void vfs_connect(void)
112
static void vfs_connect(void)
113
{
113
{
114
    while (vfs_phone < 0)
114
    while (vfs_phone < 0)
115
        vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0);
115
        vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0);
116
}
116
}
117
 
117
 
118
static int device_get_handle(const char *name, dev_handle_t *handle,
-
 
119
    const unsigned int flags)
-
 
120
{
-
 
121
    int phone;
-
 
122
   
-
 
123
    if (flags & IPC_FLAG_BLOCKING)
-
 
124
        phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0);
-
 
125
    else
-
 
126
        phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0);
-
 
127
   
-
 
128
    if (phone < 0)
-
 
129
        return phone;
-
 
130
   
-
 
131
    ipc_call_t answer;
-
 
132
    aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, flags, 0,
-
 
133
        &answer);
-
 
134
   
-
 
135
    ipcarg_t retval = ipc_data_write_start(phone, name, str_size(name) + 1);
-
 
136
   
-
 
137
    if (retval != EOK) {
-
 
138
        async_wait_for(req, NULL);
-
 
139
        ipc_hangup(phone);
-
 
140
        return retval;
-
 
141
    }
-
 
142
   
-
 
143
    async_wait_for(req, &retval);
-
 
144
   
-
 
145
    if (handle != NULL)
-
 
146
        *handle = -1;
-
 
147
   
-
 
148
    if (retval == EOK) {
-
 
149
        if (handle != NULL)
-
 
150
            *handle = (dev_handle_t) IPC_GET_ARG1(answer);
-
 
151
    }
-
 
152
   
-
 
153
    ipc_hangup(phone);
-
 
154
    return retval;
-
 
155
}
-
 
156
 
-
 
157
int mount(const char *fs_name, const char *mp, const char *dev,
118
int mount(const char *fs_name, const char *mp, const char *dev,
158
    const unsigned int flags)
119
    const char *opts, unsigned int flags)
159
{
120
{
160
    int res;
121
    int res;
161
    ipcarg_t rc;
122
    ipcarg_t rc;
162
    aid_t req;
123
    aid_t req;
163
    dev_handle_t dev_handle;
124
    dev_handle_t dev_handle;
164
   
125
   
165
    res = device_get_handle(dev, &dev_handle, flags);
126
    res = devmap_device_get_handle(dev, &dev_handle, flags);
166
    if (res != EOK)
127
    if (res != EOK)
167
        return res;
128
        return res;
168
   
129
   
169
    size_t mpa_size;
130
    size_t mpa_size;
170
    char *mpa = absolutize(mp, &mpa_size);
131
    char *mpa = absolutize(mp, &mpa_size);
171
    if (!mpa)
132
    if (!mpa)
172
        return ENOMEM;
133
        return ENOMEM;
173
   
134
   
174
    futex_down(&vfs_phone_futex);
135
    futex_down(&vfs_phone_futex);
175
    async_serialize_start();
136
    async_serialize_start();
176
    vfs_connect();
137
    vfs_connect();
177
   
138
   
178
    req = async_send_2(vfs_phone, VFS_MOUNT, dev_handle, flags, NULL);
139
    req = async_send_2(vfs_phone, VFS_MOUNT, dev_handle, flags, NULL);
179
    rc = ipc_data_write_start(vfs_phone, (void *) mpa, mpa_size);
140
    rc = ipc_data_write_start(vfs_phone, (void *) mpa, mpa_size);
180
    if (rc != EOK) {
141
    if (rc != EOK) {
181
        async_wait_for(req, NULL);
142
        async_wait_for(req, NULL);
182
        async_serialize_end();
143
        async_serialize_end();
183
        futex_up(&vfs_phone_futex);
144
        futex_up(&vfs_phone_futex);
184
        free(mpa);
145
        free(mpa);
185
        return (int) rc;
146
        return (int) rc;
186
    }
147
    }
187
   
148
   
-
 
149
    rc = ipc_data_write_start(vfs_phone, (void *) opts, str_size(opts));
-
 
150
    if (rc != EOK) {
-
 
151
        async_wait_for(req, NULL);
-
 
152
        async_serialize_end();
-
 
153
        futex_up(&vfs_phone_futex);
-
 
154
        free(mpa);
-
 
155
        return (int) rc;
-
 
156
    }
-
 
157
 
188
    rc = ipc_data_write_start(vfs_phone, (void *) fs_name, str_size(fs_name));
158
    rc = ipc_data_write_start(vfs_phone, (void *) fs_name, str_size(fs_name));
189
    if (rc != EOK) {
159
    if (rc != EOK) {
-
 
160
        async_wait_for(req, NULL);
-
 
161
        async_serialize_end();
-
 
162
        futex_up(&vfs_phone_futex);
-
 
163
        free(mpa);
-
 
164
        return (int) rc;
-
 
165
    }
-
 
166
 
-
 
167
    /* Ask VFS whether it likes fs_name. */
-
 
168
    rc = async_req_0_0(vfs_phone, IPC_M_PING);
-
 
169
    if (rc != EOK) {
190
        async_wait_for(req, NULL);
170
        async_wait_for(req, NULL);
191
        async_serialize_end();
171
        async_serialize_end();
192
        futex_up(&vfs_phone_futex);
172
        futex_up(&vfs_phone_futex);
193
        free(mpa);
173
        free(mpa);
194
        return (int) rc;
174
        return (int) rc;
195
    }
175
    }
196
   
176
   
197
    async_wait_for(req, &rc);
177
    async_wait_for(req, &rc);
198
    async_serialize_end();
178
    async_serialize_end();
199
    futex_up(&vfs_phone_futex);
179
    futex_up(&vfs_phone_futex);
200
    free(mpa);
180
    free(mpa);
201
   
181
   
202
    return (int) rc;
182
    return (int) rc;
203
}
183
}
204
 
184
 
205
static int _open(const char *path, int lflag, int oflag, ...)
185
static int _open(const char *path, int lflag, int oflag, ...)
206
{
186
{
207
    ipcarg_t rc;
187
    ipcarg_t rc;
208
    ipc_call_t answer;
188
    ipc_call_t answer;
209
    aid_t req;
189
    aid_t req;
210
   
190
   
211
    size_t pa_size;
191
    size_t pa_size;
212
    char *pa = absolutize(path, &pa_size);
192
    char *pa = absolutize(path, &pa_size);
213
    if (!pa)
193
    if (!pa)
214
        return ENOMEM;
194
        return ENOMEM;
215
   
195
   
216
    futex_down(&vfs_phone_futex);
196
    futex_down(&vfs_phone_futex);
217
    async_serialize_start();
197
    async_serialize_start();
218
    vfs_connect();
198
    vfs_connect();
219
   
199
   
220
    req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer);
200
    req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer);
221
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
201
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
222
    if (rc != EOK) {
202
    if (rc != EOK) {
223
        async_wait_for(req, NULL);
203
        async_wait_for(req, NULL);
224
        async_serialize_end();
204
        async_serialize_end();
225
        futex_up(&vfs_phone_futex);
205
        futex_up(&vfs_phone_futex);
226
        free(pa);
206
        free(pa);
227
        return (int) rc;
207
        return (int) rc;
228
    }
208
    }
229
    async_wait_for(req, &rc);
209
    async_wait_for(req, &rc);
230
    async_serialize_end();
210
    async_serialize_end();
231
    futex_up(&vfs_phone_futex);
211
    futex_up(&vfs_phone_futex);
232
    free(pa);
212
    free(pa);
233
 
213
 
234
    if (rc != EOK)
214
    if (rc != EOK)
235
        return (int) rc;
215
        return (int) rc;
236
    return (int) IPC_GET_ARG1(answer);
216
    return (int) IPC_GET_ARG1(answer);
237
}
217
}
238
 
218
 
239
int open(const char *path, int oflag, ...)
219
int open(const char *path, int oflag, ...)
240
{
220
{
241
    return _open(path, L_FILE, oflag);
221
    return _open(path, L_FILE, oflag);
242
}
222
}
243
 
223
 
244
int close(int fildes)
224
int close(int fildes)
245
{
225
{
246
    ipcarg_t rc;
226
    ipcarg_t rc;
247
   
227
   
248
    futex_down(&vfs_phone_futex);
228
    futex_down(&vfs_phone_futex);
249
    async_serialize_start();
229
    async_serialize_start();
250
    vfs_connect();
230
    vfs_connect();
251
   
231
   
252
    rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes);
232
    rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes);
253
   
233
   
254
    async_serialize_end();
234
    async_serialize_end();
255
    futex_up(&vfs_phone_futex);
235
    futex_up(&vfs_phone_futex);
256
   
236
   
257
    return (int)rc;
237
    return (int)rc;
258
}
238
}
259
 
239
 
260
ssize_t read(int fildes, void *buf, size_t nbyte)
240
ssize_t read(int fildes, void *buf, size_t nbyte)
261
{
241
{
262
    ipcarg_t rc;
242
    ipcarg_t rc;
263
    ipc_call_t answer;
243
    ipc_call_t answer;
264
    aid_t req;
244
    aid_t req;
265
 
245
 
266
    futex_down(&vfs_phone_futex);
246
    futex_down(&vfs_phone_futex);
267
    async_serialize_start();
247
    async_serialize_start();
268
    vfs_connect();
248
    vfs_connect();
269
   
249
   
270
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
250
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
271
    rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte);
251
    rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte);
272
    if (rc != EOK) {
252
    if (rc != EOK) {
273
        async_wait_for(req, NULL);
253
        async_wait_for(req, NULL);
274
        async_serialize_end();
254
        async_serialize_end();
275
        futex_up(&vfs_phone_futex);
255
        futex_up(&vfs_phone_futex);
276
        return (ssize_t) rc;
256
        return (ssize_t) rc;
277
    }
257
    }
278
    async_wait_for(req, &rc);
258
    async_wait_for(req, &rc);
279
    async_serialize_end();
259
    async_serialize_end();
280
    futex_up(&vfs_phone_futex);
260
    futex_up(&vfs_phone_futex);
281
    if (rc == EOK)
261
    if (rc == EOK)
282
        return (ssize_t) IPC_GET_ARG1(answer);
262
        return (ssize_t) IPC_GET_ARG1(answer);
283
    else
263
    else
284
        return rc;
264
        return rc;
285
}
265
}
286
 
266
 
287
ssize_t write(int fildes, const void *buf, size_t nbyte)
267
ssize_t write(int fildes, const void *buf, size_t nbyte)
288
{
268
{
289
    ipcarg_t rc;
269
    ipcarg_t rc;
290
    ipc_call_t answer;
270
    ipc_call_t answer;
291
    aid_t req;
271
    aid_t req;
292
 
272
 
293
    futex_down(&vfs_phone_futex);
273
    futex_down(&vfs_phone_futex);
294
    async_serialize_start();
274
    async_serialize_start();
295
    vfs_connect();
275
    vfs_connect();
296
   
276
   
297
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
277
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
298
    rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte);
278
    rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte);
299
    if (rc != EOK) {
279
    if (rc != EOK) {
300
        async_wait_for(req, NULL);
280
        async_wait_for(req, NULL);
301
        async_serialize_end();
281
        async_serialize_end();
302
        futex_up(&vfs_phone_futex);
282
        futex_up(&vfs_phone_futex);
303
        return (ssize_t) rc;
283
        return (ssize_t) rc;
304
    }
284
    }
305
    async_wait_for(req, &rc);
285
    async_wait_for(req, &rc);
306
    async_serialize_end();
286
    async_serialize_end();
307
    futex_up(&vfs_phone_futex);
287
    futex_up(&vfs_phone_futex);
308
    if (rc == EOK)
288
    if (rc == EOK)
309
        return (ssize_t) IPC_GET_ARG1(answer);
289
        return (ssize_t) IPC_GET_ARG1(answer);
310
    else
290
    else
311
        return -1;
291
        return -1;
312
}
292
}
313
 
293
 
314
off_t lseek(int fildes, off_t offset, int whence)
294
off_t lseek(int fildes, off_t offset, int whence)
315
{
295
{
316
    ipcarg_t rc;
296
    ipcarg_t rc;
317
 
297
 
318
    futex_down(&vfs_phone_futex);
298
    futex_down(&vfs_phone_futex);
319
    async_serialize_start();
299
    async_serialize_start();
320
    vfs_connect();
300
    vfs_connect();
321
   
301
   
322
    ipcarg_t newoffs;
302
    ipcarg_t newoffs;
323
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
303
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
324
        &newoffs);
304
        &newoffs);
325
 
305
 
326
    async_serialize_end();
306
    async_serialize_end();
327
    futex_up(&vfs_phone_futex);
307
    futex_up(&vfs_phone_futex);
328
 
308
 
329
    if (rc != EOK)
309
    if (rc != EOK)
330
        return (off_t) -1;
310
        return (off_t) -1;
331
   
311
   
332
    return (off_t) newoffs;
312
    return (off_t) newoffs;
333
}
313
}
334
 
314
 
335
int ftruncate(int fildes, off_t length)
315
int ftruncate(int fildes, off_t length)
336
{
316
{
337
    ipcarg_t rc;
317
    ipcarg_t rc;
338
   
318
   
339
    futex_down(&vfs_phone_futex);
319
    futex_down(&vfs_phone_futex);
340
    async_serialize_start();
320
    async_serialize_start();
341
    vfs_connect();
321
    vfs_connect();
342
   
322
   
343
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
323
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
344
    async_serialize_end();
324
    async_serialize_end();
345
    futex_up(&vfs_phone_futex);
325
    futex_up(&vfs_phone_futex);
346
    return (int) rc;
326
    return (int) rc;
347
}
327
}
348
 
328
 
349
DIR *opendir(const char *dirname)
329
DIR *opendir(const char *dirname)
350
{
330
{
351
    DIR *dirp = malloc(sizeof(DIR));
331
    DIR *dirp = malloc(sizeof(DIR));
352
    if (!dirp)
332
    if (!dirp)
353
        return NULL;
333
        return NULL;
354
    dirp->fd = _open(dirname, L_DIRECTORY, 0);
334
    dirp->fd = _open(dirname, L_DIRECTORY, 0);
355
    if (dirp->fd < 0) {
335
    if (dirp->fd < 0) {
356
        free(dirp);
336
        free(dirp);
357
        return NULL;
337
        return NULL;
358
    }
338
    }
359
    return dirp;
339
    return dirp;
360
}
340
}
361
 
341
 
362
struct dirent *readdir(DIR *dirp)
342
struct dirent *readdir(DIR *dirp)
363
{
343
{
364
    ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
344
    ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
365
    if (len <= 0)
345
    if (len <= 0)
366
        return NULL;
346
        return NULL;
367
    return &dirp->res;
347
    return &dirp->res;
368
}
348
}
369
 
349
 
370
void rewinddir(DIR *dirp)
350
void rewinddir(DIR *dirp)
371
{
351
{
372
    (void) lseek(dirp->fd, 0, SEEK_SET);
352
    (void) lseek(dirp->fd, 0, SEEK_SET);
373
}
353
}
374
 
354
 
375
int closedir(DIR *dirp)
355
int closedir(DIR *dirp)
376
{
356
{
377
    (void) close(dirp->fd);
357
    (void) close(dirp->fd);
378
    free(dirp);
358
    free(dirp);
379
    return 0;
359
    return 0;
380
}
360
}
381
 
361
 
382
int mkdir(const char *path, mode_t mode)
362
int mkdir(const char *path, mode_t mode)
383
{
363
{
384
    ipcarg_t rc;
364
    ipcarg_t rc;
385
    aid_t req;
365
    aid_t req;
386
   
366
   
387
    size_t pa_size;
367
    size_t pa_size;
388
    char *pa = absolutize(path, &pa_size);
368
    char *pa = absolutize(path, &pa_size);
389
    if (!pa)
369
    if (!pa)
390
        return ENOMEM;
370
        return ENOMEM;
391
   
371
   
392
    futex_down(&vfs_phone_futex);
372
    futex_down(&vfs_phone_futex);
393
    async_serialize_start();
373
    async_serialize_start();
394
    vfs_connect();
374
    vfs_connect();
395
   
375
   
396
    req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL);
376
    req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL);
397
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
377
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
398
    if (rc != EOK) {
378
    if (rc != EOK) {
399
        async_wait_for(req, NULL);
379
        async_wait_for(req, NULL);
400
        async_serialize_end();
380
        async_serialize_end();
401
        futex_up(&vfs_phone_futex);
381
        futex_up(&vfs_phone_futex);
402
        free(pa);
382
        free(pa);
403
        return (int) rc;
383
        return (int) rc;
404
    }
384
    }
405
    async_wait_for(req, &rc);
385
    async_wait_for(req, &rc);
406
    async_serialize_end();
386
    async_serialize_end();
407
    futex_up(&vfs_phone_futex);
387
    futex_up(&vfs_phone_futex);
408
    free(pa);
388
    free(pa);
409
    return rc;
389
    return rc;
410
}
390
}
411
 
391
 
412
static int _unlink(const char *path, int lflag)
392
static int _unlink(const char *path, int lflag)
413
{
393
{
414
    ipcarg_t rc;
394
    ipcarg_t rc;
415
    aid_t req;
395
    aid_t req;
416
   
396
   
417
    size_t pa_size;
397
    size_t pa_size;
418
    char *pa = absolutize(path, &pa_size);
398
    char *pa = absolutize(path, &pa_size);
419
    if (!pa)
399
    if (!pa)
420
        return ENOMEM;
400
        return ENOMEM;
421
 
401
 
422
    futex_down(&vfs_phone_futex);
402
    futex_down(&vfs_phone_futex);
423
    async_serialize_start();
403
    async_serialize_start();
424
    vfs_connect();
404
    vfs_connect();
425
   
405
   
426
    req = async_send_0(vfs_phone, VFS_UNLINK, NULL);
406
    req = async_send_0(vfs_phone, VFS_UNLINK, NULL);
427
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
407
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
428
    if (rc != EOK) {
408
    if (rc != EOK) {
429
        async_wait_for(req, NULL);
409
        async_wait_for(req, NULL);
430
        async_serialize_end();
410
        async_serialize_end();
431
        futex_up(&vfs_phone_futex);
411
        futex_up(&vfs_phone_futex);
432
        free(pa);
412
        free(pa);
433
        return (int) rc;
413
        return (int) rc;
434
    }
414
    }
435
    async_wait_for(req, &rc);
415
    async_wait_for(req, &rc);
436
    async_serialize_end();
416
    async_serialize_end();
437
    futex_up(&vfs_phone_futex);
417
    futex_up(&vfs_phone_futex);
438
    free(pa);
418
    free(pa);
439
    return rc;
419
    return rc;
440
}
420
}
441
 
421
 
442
int unlink(const char *path)
422
int unlink(const char *path)
443
{
423
{
444
    return _unlink(path, L_NONE);
424
    return _unlink(path, L_NONE);
445
}
425
}
446
 
426
 
447
int rmdir(const char *path)
427
int rmdir(const char *path)
448
{
428
{
449
    return _unlink(path, L_DIRECTORY);
429
    return _unlink(path, L_DIRECTORY);
450
}
430
}
451
 
431
 
452
int rename(const char *old, const char *new)
432
int rename(const char *old, const char *new)
453
{
433
{
454
    ipcarg_t rc;
434
    ipcarg_t rc;
455
    aid_t req;
435
    aid_t req;
456
   
436
   
457
    size_t olda_size;
437
    size_t olda_size;
458
    char *olda = absolutize(old, &olda_size);
438
    char *olda = absolutize(old, &olda_size);
459
    if (!olda)
439
    if (!olda)
460
        return ENOMEM;
440
        return ENOMEM;
461
 
441
 
462
    size_t newa_size;
442
    size_t newa_size;
463
    char *newa = absolutize(new, &newa_size);
443
    char *newa = absolutize(new, &newa_size);
464
    if (!newa) {
444
    if (!newa) {
465
        free(olda);
445
        free(olda);
466
        return ENOMEM;
446
        return ENOMEM;
467
    }
447
    }
468
 
448
 
469
    futex_down(&vfs_phone_futex);
449
    futex_down(&vfs_phone_futex);
470
    async_serialize_start();
450
    async_serialize_start();
471
    vfs_connect();
451
    vfs_connect();
472
   
452
   
473
    req = async_send_0(vfs_phone, VFS_RENAME, NULL);
453
    req = async_send_0(vfs_phone, VFS_RENAME, NULL);
474
    rc = ipc_data_write_start(vfs_phone, olda, olda_size);
454
    rc = ipc_data_write_start(vfs_phone, olda, olda_size);
475
    if (rc != EOK) {
455
    if (rc != EOK) {
476
        async_wait_for(req, NULL);
456
        async_wait_for(req, NULL);
477
        async_serialize_end();
457
        async_serialize_end();
478
        futex_up(&vfs_phone_futex);
458
        futex_up(&vfs_phone_futex);
479
        free(olda);
459
        free(olda);
480
        free(newa);
460
        free(newa);
481
        return (int) rc;
461
        return (int) rc;
482
    }
462
    }
483
    rc = ipc_data_write_start(vfs_phone, newa, newa_size);
463
    rc = ipc_data_write_start(vfs_phone, newa, newa_size);
484
    if (rc != EOK) {
464
    if (rc != EOK) {
485
        async_wait_for(req, NULL);
465
        async_wait_for(req, NULL);
486
        async_serialize_end();
466
        async_serialize_end();
487
        futex_up(&vfs_phone_futex);
467
        futex_up(&vfs_phone_futex);
488
        free(olda);
468
        free(olda);
489
        free(newa);
469
        free(newa);
490
        return (int) rc;
470
        return (int) rc;
491
    }
471
    }
492
    async_wait_for(req, &rc);
472
    async_wait_for(req, &rc);
493
    async_serialize_end();
473
    async_serialize_end();
494
    futex_up(&vfs_phone_futex);
474
    futex_up(&vfs_phone_futex);
495
    free(olda);
475
    free(olda);
496
    free(newa);
476
    free(newa);
497
    return rc;
477
    return rc;
498
}
478
}
499
 
479
 
500
int chdir(const char *path)
480
int chdir(const char *path)
501
{
481
{
502
    size_t pa_size;
482
    size_t pa_size;
503
    char *pa = absolutize(path, &pa_size);
483
    char *pa = absolutize(path, &pa_size);
504
    if (!pa)
484
    if (!pa)
505
        return ENOMEM;
485
        return ENOMEM;
506
 
486
 
507
    DIR *d = opendir(pa);
487
    DIR *d = opendir(pa);
508
    if (!d) {
488
    if (!d) {
509
        free(pa);
489
        free(pa);
510
        return ENOENT;
490
        return ENOENT;
511
    }
491
    }
512
 
492
 
513
    futex_down(&cwd_futex);
493
    futex_down(&cwd_futex);
514
    if (cwd_dir) {
494
    if (cwd_dir) {
515
        closedir(cwd_dir);
495
        closedir(cwd_dir);
516
        cwd_dir = NULL;
496
        cwd_dir = NULL;
517
        free(cwd_path);
497
        free(cwd_path);
518
        cwd_path = NULL;
498
        cwd_path = NULL;
519
        cwd_size = 0;
499
        cwd_size = 0;
520
    }
500
    }
521
    cwd_dir = d;
501
    cwd_dir = d;
522
    cwd_path = pa;
502
    cwd_path = pa;
523
    cwd_size = pa_size;
503
    cwd_size = pa_size;
524
    futex_up(&cwd_futex);
504
    futex_up(&cwd_futex);
525
    return EOK;
505
    return EOK;
526
}
506
}
527
 
507
 
528
char *getcwd(char *buf, size_t size)
508
char *getcwd(char *buf, size_t size)
529
{
509
{
530
    if (!size)
510
    if (!size)
531
        return NULL;
511
        return NULL;
532
    futex_down(&cwd_futex);
512
    futex_down(&cwd_futex);
533
    if (size < cwd_size + 1) {
513
    if (size < cwd_size + 1) {
534
        futex_up(&cwd_futex);
514
        futex_up(&cwd_futex);
535
        return NULL;
515
        return NULL;
536
    }
516
    }
537
    str_cpy(buf, size, cwd_path);
517
    str_cpy(buf, size, cwd_path);
538
    futex_up(&cwd_futex);
518
    futex_up(&cwd_futex);
539
    return buf;
519
    return buf;
540
}
520
}
541
 
521
 
542
/** @}
522
/** @}
543
 */
523
 */
544
 
524