Subversion Repositories HelenOS

Rev

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

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