Subversion Repositories HelenOS

Rev

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

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