Subversion Repositories HelenOS

Rev

Rev 3747 | Rev 4256 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3747 Rev 4002
Line 29... Line 29...
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>
Line 55... Line 55...
55
futex_t vfs_phone_futex = FUTEX_INITIALIZER;
55
futex_t vfs_phone_futex = FUTEX_INITIALIZER;
56
 
56
 
57
futex_t cwd_futex = FUTEX_INITIALIZER;
57
futex_t cwd_futex = FUTEX_INITIALIZER;
58
DIR *cwd_dir = NULL;
58
DIR *cwd_dir = NULL;
59
char *cwd_path = NULL;
59
char *cwd_path = NULL;
60
size_t cwd_len = 0;
60
size_t cwd_len = 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;
Line 107... Line 107...
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 int vfs_connect(void)
112
static void vfs_connect(void)
113
{
113
{
114
    if (vfs_phone < 0)
114
    while (vfs_phone < 0)
115
        vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0);
115
        vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0);
116
    return vfs_phone;
-
 
117
}
116
}
118
 
117
 
119
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)
120
{
120
{
-
 
121
    int phone;
-
 
122
   
-
 
123
    if (flags & IPC_FLAG_BLOCKING)
121
    int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT,
124
        phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0);
122
        0);
125
    else
-
 
126
        phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0);
-
 
127
   
123
    if (phone < 0)
128
    if (phone < 0)
124
        return phone;
129
        return phone;
125
   
130
   
126
    ipc_call_t answer;
131
    ipc_call_t answer;
127
    aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, 0, 0,
132
    aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, flags, 0,
128
        &answer);
133
        &answer);
129
   
134
   
130
    ipcarg_t retval = ipc_data_write_start(phone, name, strlen(name) + 1);
135
    ipcarg_t retval = ipc_data_write_start(phone, name, strlen(name) + 1);
131
 
136
   
132
    if (retval != EOK) {
137
    if (retval != EOK) {
133
        async_wait_for(req, NULL);
138
        async_wait_for(req, NULL);
134
        ipc_hangup(phone);
139
        ipc_hangup(phone);
135
        return retval;
140
        return retval;
136
    }
141
    }
137
 
142
   
138
    async_wait_for(req, &retval);
143
    async_wait_for(req, &retval);
139
 
144
   
140
    if (handle != NULL)
145
    if (handle != NULL)
141
        *handle = -1;
146
        *handle = -1;
142
   
147
   
143
    if (retval == EOK) {
148
    if (retval == EOK) {
144
        if (handle != NULL)
149
        if (handle != NULL)
Line 147... Line 152...
147
   
152
   
148
    ipc_hangup(phone);
153
    ipc_hangup(phone);
149
    return retval;
154
    return retval;
150
}
155
}
151
 
156
 
152
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)
153
{
159
{
154
    int res;
160
    int res;
155
    ipcarg_t rc;
161
    ipcarg_t rc;
156
    aid_t req;
162
    aid_t req;
157
    dev_handle_t dev_handle;
163
    dev_handle_t dev_handle;
158
   
164
   
159
    res = device_get_handle(dev, &dev_handle);
165
    res = device_get_handle(dev, &dev_handle, flags);
160
    if (res != EOK)
166
    if (res != EOK)
161
        return res;
167
        return res;
162
   
168
   
163
    size_t mpa_len;
169
    size_t mpa_len;
164
    char *mpa = absolutize(mp, &mpa_len);
170
    char *mpa = absolutize(mp, &mpa_len);
165
    if (!mpa)
171
    if (!mpa)
166
        return ENOMEM;
172
        return ENOMEM;
167
 
173
   
168
    futex_down(&vfs_phone_futex);
174
    futex_down(&vfs_phone_futex);
169
    async_serialize_start();
175
    async_serialize_start();
170
    if (vfs_phone < 0) {
-
 
171
        res = vfs_connect();
176
    vfs_connect();
172
        if (res < 0) {
-
 
173
            async_serialize_end();
-
 
174
            futex_up(&vfs_phone_futex);
-
 
175
            free(mpa);
-
 
176
            return res;
-
 
177
        }
-
 
178
    }
177
   
179
    req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL);
178
    req = async_send_2(vfs_phone, VFS_MOUNT, dev_handle, flags, NULL);
180
    rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name));
179
    rc = ipc_data_write_start(vfs_phone, (void *) mpa, mpa_len);
181
    if (rc != EOK) {
-
 
182
        async_wait_for(req, NULL);
-
 
183
        async_serialize_end();
-
 
184
        futex_up(&vfs_phone_futex);
-
 
185
        free(mpa);
-
 
186
        return (int) rc;
-
 
187
    }
-
 
188
    /* Ask VFS whether it likes fs_name. */
-
 
189
    rc = async_req_0_0(vfs_phone, IPC_M_PING);
-
 
190
    if (rc != EOK) {
180
    if (rc != EOK) {
191
        async_wait_for(req, NULL);
181
        async_wait_for(req, NULL);
192
        async_serialize_end();
182
        async_serialize_end();
193
        futex_up(&vfs_phone_futex);
183
        futex_up(&vfs_phone_futex);
194
        free(mpa);
184
        free(mpa);
195
        return (int) rc;
185
        return (int) rc;
196
    }
186
    }
-
 
187
   
197
    rc = ipc_data_write_start(vfs_phone, (void *)mpa, mpa_len);
188
    rc = ipc_data_write_start(vfs_phone, (void *) fs_name, strlen(fs_name));
198
    if (rc != EOK) {
189
    if (rc != EOK) {
199
        async_wait_for(req, NULL);
190
        async_wait_for(req, NULL);
200
        async_serialize_end();
191
        async_serialize_end();
201
        futex_up(&vfs_phone_futex);
192
        futex_up(&vfs_phone_futex);
202
        free(mpa);
193
        free(mpa);
203
        return (int) rc;
194
        return (int) rc;
204
    }
195
    }
-
 
196
   
205
    async_wait_for(req, &rc);
197
    async_wait_for(req, &rc);
206
    async_serialize_end();
198
    async_serialize_end();
207
    futex_up(&vfs_phone_futex);
199
    futex_up(&vfs_phone_futex);
208
    free(mpa);
200
    free(mpa);
-
 
201
   
209
    return (int) rc;
202
    return (int) rc;
210
}
203
}
211
 
204
 
212
static int _open(const char *path, int lflag, int oflag, ...)
205
static int _open(const char *path, int lflag, int oflag, ...)
213
{
206
{
214
    int res;
-
 
215
    ipcarg_t rc;
207
    ipcarg_t rc;
216
    ipc_call_t answer;
208
    ipc_call_t answer;
217
    aid_t req;
209
    aid_t req;
218
   
210
   
219
    size_t pa_len;
211
    size_t pa_len;
Line 221... Line 213...
221
    if (!pa)
213
    if (!pa)
222
        return ENOMEM;
214
        return ENOMEM;
223
   
215
   
224
    futex_down(&vfs_phone_futex);
216
    futex_down(&vfs_phone_futex);
225
    async_serialize_start();
217
    async_serialize_start();
226
    if (vfs_phone < 0) {
-
 
227
        res = vfs_connect();
218
    vfs_connect();
228
        if (res < 0) {
-
 
229
            async_serialize_end();
-
 
230
            futex_up(&vfs_phone_futex);
-
 
231
            free(pa);
-
 
232
            return res;
-
 
233
        }
-
 
234
    }
219
   
235
    req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer);
220
    req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer);
236
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
221
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
237
    if (rc != EOK) {
222
    if (rc != EOK) {
238
        async_wait_for(req, NULL);
223
        async_wait_for(req, NULL);
239
        async_serialize_end();
224
        async_serialize_end();
Line 256... Line 241...
256
    return _open(path, L_FILE, oflag);
241
    return _open(path, L_FILE, oflag);
257
}
242
}
258
 
243
 
259
int close(int fildes)
244
int close(int fildes)
260
{
245
{
261
    int res;
-
 
262
    ipcarg_t rc;
246
    ipcarg_t rc;
263
 
247
   
264
    futex_down(&vfs_phone_futex);
248
    futex_down(&vfs_phone_futex);
265
    async_serialize_start();
249
    async_serialize_start();
266
    if (vfs_phone < 0) {
-
 
267
        res = vfs_connect();
250
    vfs_connect();
268
        if (res < 0) {
-
 
269
            async_serialize_end();
-
 
270
            futex_up(&vfs_phone_futex);
-
 
271
            return res;
-
 
272
        }
-
 
273
    }
-
 
274
       
251
   
275
    rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes);
252
    rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes);
276
 
253
   
277
    async_serialize_end();
254
    async_serialize_end();
278
    futex_up(&vfs_phone_futex);
255
    futex_up(&vfs_phone_futex);
279
   
256
   
280
    return (int)rc;
257
    return (int)rc;
281
}
258
}
282
 
259
 
283
ssize_t read(int fildes, void *buf, size_t nbyte)
260
ssize_t read(int fildes, void *buf, size_t nbyte)
284
{
261
{
285
    int res;
-
 
286
    ipcarg_t rc;
262
    ipcarg_t rc;
287
    ipc_call_t answer;
263
    ipc_call_t answer;
288
    aid_t req;
264
    aid_t req;
289
 
265
 
290
    futex_down(&vfs_phone_futex);
266
    futex_down(&vfs_phone_futex);
291
    async_serialize_start();
267
    async_serialize_start();
292
    if (vfs_phone < 0) {
-
 
293
        res = vfs_connect();
268
    vfs_connect();
294
        if (res < 0) {
-
 
295
            async_serialize_end();
-
 
296
            futex_up(&vfs_phone_futex);
-
 
297
            return res;
-
 
298
        }
-
 
299
    }
269
   
300
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
270
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
301
    rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte);
271
    rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte);
302
    if (rc != EOK) {
272
    if (rc != EOK) {
303
        async_wait_for(req, NULL);
273
        async_wait_for(req, NULL);
304
        async_serialize_end();
274
        async_serialize_end();
Line 314... Line 284...
314
        return rc;
284
        return rc;
315
}
285
}
316
 
286
 
317
ssize_t write(int fildes, const void *buf, size_t nbyte)
287
ssize_t write(int fildes, const void *buf, size_t nbyte)
318
{
288
{
319
    int res;
-
 
320
    ipcarg_t rc;
289
    ipcarg_t rc;
321
    ipc_call_t answer;
290
    ipc_call_t answer;
322
    aid_t req;
291
    aid_t req;
323
 
292
 
324
    futex_down(&vfs_phone_futex);
293
    futex_down(&vfs_phone_futex);
325
    async_serialize_start();
294
    async_serialize_start();
326
    if (vfs_phone < 0) {
-
 
327
        res = vfs_connect();
295
    vfs_connect();
328
        if (res < 0) {
-
 
329
            async_serialize_end();
-
 
330
            futex_up(&vfs_phone_futex);
-
 
331
            return res;
-
 
332
        }
-
 
333
    }
296
   
334
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
297
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
335
    rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte);
298
    rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte);
336
    if (rc != EOK) {
299
    if (rc != EOK) {
337
        async_wait_for(req, NULL);
300
        async_wait_for(req, NULL);
338
        async_serialize_end();
301
        async_serialize_end();
Line 348... Line 311...
348
        return -1;
311
        return -1;
349
}
312
}
350
 
313
 
351
off_t lseek(int fildes, off_t offset, int whence)
314
off_t lseek(int fildes, off_t offset, int whence)
352
{
315
{
353
    int res;
-
 
354
    ipcarg_t rc;
316
    ipcarg_t rc;
355
 
317
 
356
    futex_down(&vfs_phone_futex);
318
    futex_down(&vfs_phone_futex);
357
    async_serialize_start();
319
    async_serialize_start();
358
    if (vfs_phone < 0) {
-
 
359
        res = vfs_connect();
320
    vfs_connect();
360
        if (res < 0) {
-
 
361
            async_serialize_end();
-
 
362
            futex_up(&vfs_phone_futex);
-
 
363
            return res;
-
 
364
        }
-
 
365
    }
-
 
366
       
321
   
367
    ipcarg_t newoffs;
322
    ipcarg_t newoffs;
368
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
323
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
369
        &newoffs);
324
        &newoffs);
370
 
325
 
371
    async_serialize_end();
326
    async_serialize_end();
Line 377... Line 332...
377
    return (off_t) newoffs;
332
    return (off_t) newoffs;
378
}
333
}
379
 
334
 
380
int ftruncate(int fildes, off_t length)
335
int ftruncate(int fildes, off_t length)
381
{
336
{
382
    int res;
-
 
383
    ipcarg_t rc;
337
    ipcarg_t rc;
384
   
338
   
385
    futex_down(&vfs_phone_futex);
339
    futex_down(&vfs_phone_futex);
386
    async_serialize_start();
340
    async_serialize_start();
387
    if (vfs_phone < 0) {
-
 
388
        res = vfs_connect();
341
    vfs_connect();
389
        if (res < 0) {
-
 
390
            async_serialize_end();
-
 
391
            futex_up(&vfs_phone_futex);
-
 
392
            return res;
-
 
393
        }
-
 
394
    }
342
   
395
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
343
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
396
    async_serialize_end();
344
    async_serialize_end();
397
    futex_up(&vfs_phone_futex);
345
    futex_up(&vfs_phone_futex);
398
    return (int) rc;
346
    return (int) rc;
399
}
347
}
Line 431... Line 379...
431
    return 0;
379
    return 0;
432
}
380
}
433
 
381
 
434
int mkdir(const char *path, mode_t mode)
382
int mkdir(const char *path, mode_t mode)
435
{
383
{
436
    int res;
-
 
437
    ipcarg_t rc;
384
    ipcarg_t rc;
438
    aid_t req;
385
    aid_t req;
439
   
386
   
440
    size_t pa_len;
387
    size_t pa_len;
441
    char *pa = absolutize(path, &pa_len);
388
    char *pa = absolutize(path, &pa_len);
442
    if (!pa)
389
    if (!pa)
443
        return ENOMEM;
390
        return ENOMEM;
444
 
391
   
445
    futex_down(&vfs_phone_futex);
392
    futex_down(&vfs_phone_futex);
446
    async_serialize_start();
393
    async_serialize_start();
447
    if (vfs_phone < 0) {
-
 
448
        res = vfs_connect();
394
    vfs_connect();
449
        if (res < 0) {
-
 
450
            async_serialize_end();
-
 
451
            futex_up(&vfs_phone_futex);
-
 
452
            free(pa);
-
 
453
            return res;
-
 
454
        }
-
 
455
    }
395
   
456
    req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL);
396
    req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL);
457
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
397
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
458
    if (rc != EOK) {
398
    if (rc != EOK) {
459
        async_wait_for(req, NULL);
399
        async_wait_for(req, NULL);
460
        async_serialize_end();
400
        async_serialize_end();
Line 469... Line 409...
469
    return rc;
409
    return rc;
470
}
410
}
471
 
411
 
472
static int _unlink(const char *path, int lflag)
412
static int _unlink(const char *path, int lflag)
473
{
413
{
474
    int res;
-
 
475
    ipcarg_t rc;
414
    ipcarg_t rc;
476
    aid_t req;
415
    aid_t req;
477
   
416
   
478
    size_t pa_len;
417
    size_t pa_len;
479
    char *pa = absolutize(path, &pa_len);
418
    char *pa = absolutize(path, &pa_len);
480
    if (!pa)
419
    if (!pa)
481
        return ENOMEM;
420
        return ENOMEM;
482
 
421
 
483
    futex_down(&vfs_phone_futex);
422
    futex_down(&vfs_phone_futex);
484
    async_serialize_start();
423
    async_serialize_start();
485
    if (vfs_phone < 0) {
-
 
486
        res = vfs_connect();
424
    vfs_connect();
487
        if (res < 0) {
-
 
488
            async_serialize_end();
-
 
489
            futex_up(&vfs_phone_futex);
-
 
490
            free(pa);
-
 
491
            return res;
-
 
492
        }
-
 
493
    }
425
   
494
    req = async_send_0(vfs_phone, VFS_UNLINK, NULL);
426
    req = async_send_0(vfs_phone, VFS_UNLINK, NULL);
495
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
427
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
496
    if (rc != EOK) {
428
    if (rc != EOK) {
497
        async_wait_for(req, NULL);
429
        async_wait_for(req, NULL);
498
        async_serialize_end();
430
        async_serialize_end();
Line 517... Line 449...
517
    return _unlink(path, L_DIRECTORY);
449
    return _unlink(path, L_DIRECTORY);
518
}
450
}
519
 
451
 
520
int rename(const char *old, const char *new)
452
int rename(const char *old, const char *new)
521
{
453
{
522
    int res;
-
 
523
    ipcarg_t rc;
454
    ipcarg_t rc;
524
    aid_t req;
455
    aid_t req;
525
   
456
   
526
    size_t olda_len;
457
    size_t olda_len;
527
    char *olda = absolutize(old, &olda_len);
458
    char *olda = absolutize(old, &olda_len);
Line 535... Line 466...
535
        return ENOMEM;
466
        return ENOMEM;
536
    }
467
    }
537
 
468
 
538
    futex_down(&vfs_phone_futex);
469
    futex_down(&vfs_phone_futex);
539
    async_serialize_start();
470
    async_serialize_start();
540
    if (vfs_phone < 0) {
-
 
541
        res = vfs_connect();
471
    vfs_connect();
542
        if (res < 0) {
-
 
543
            async_serialize_end();
-
 
544
            futex_up(&vfs_phone_futex);
-
 
545
            free(olda);
-
 
546
            free(newa);
-
 
547
            return res;
-
 
548
        }
-
 
549
    }
472
   
550
    req = async_send_0(vfs_phone, VFS_RENAME, NULL);
473
    req = async_send_0(vfs_phone, VFS_RENAME, NULL);
551
    rc = ipc_data_write_start(vfs_phone, olda, olda_len);
474
    rc = ipc_data_write_start(vfs_phone, olda, olda_len);
552
    if (rc != EOK) {
475
    if (rc != EOK) {
553
        async_wait_for(req, NULL);
476
        async_wait_for(req, NULL);
554
        async_serialize_end();
477
        async_serialize_end();