Subversion Repositories HelenOS

Rev

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

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