Subversion Repositories HelenOS

Rev

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

Rev 4153 Rev 4263
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_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 len = strlen(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_len + 1 + len + 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
        strcpy(ncwd_path_nc, cwd_path);
79
        str_ncpy(ncwd_path_nc, cwd_path, cwd_size + 1 + size + 1);
80
        ncwd_path_nc[cwd_len] = '/';
80
        ncwd_path_nc[cwd_size] = '/';
81
        ncwd_path_nc[cwd_len + 1] = '\0';
81
        ncwd_path_nc[cwd_size + 1] = '\0';
82
    } else {
82
    } else {
83
        ncwd_path_nc = malloc(len + 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';
Line 130... Line 130...
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, strlen(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;
Line 164... Line 164...
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_len;
169
    size_t mpa_size;
170
    char *mpa = absolutize(mp, &mpa_len);
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_len);
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, strlen(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);
Line 206... Line 206...
206
{
206
{
207
    ipcarg_t rc;
207
    ipcarg_t rc;
208
    ipc_call_t answer;
208
    ipc_call_t answer;
209
    aid_t req;
209
    aid_t req;
210
   
210
   
211
    size_t pa_len;
211
    size_t pa_size;
212
    char *pa = absolutize(path, &pa_len);
212
    char *pa = absolutize(path, &pa_size);
213
    if (!pa)
213
    if (!pa)
214
        return ENOMEM;
214
        return ENOMEM;
215
   
215
   
216
    futex_down(&vfs_phone_futex);
216
    futex_down(&vfs_phone_futex);
217
    async_serialize_start();
217
    async_serialize_start();
218
    vfs_connect();
218
    vfs_connect();
219
   
219
   
220
    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);
221
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
221
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
222
    if (rc != EOK) {
222
    if (rc != EOK) {
223
        async_wait_for(req, NULL);
223
        async_wait_for(req, NULL);
224
        async_serialize_end();
224
        async_serialize_end();
225
        futex_up(&vfs_phone_futex);
225
        futex_up(&vfs_phone_futex);
226
        free(pa);
226
        free(pa);
Line 382... Line 382...
382
int mkdir(const char *path, mode_t mode)
382
int mkdir(const char *path, mode_t mode)
383
{
383
{
384
    ipcarg_t rc;
384
    ipcarg_t rc;
385
    aid_t req;
385
    aid_t req;
386
   
386
   
387
    size_t pa_len;
387
    size_t pa_size;
388
    char *pa = absolutize(path, &pa_len);
388
    char *pa = absolutize(path, &pa_size);
389
    if (!pa)
389
    if (!pa)
390
        return ENOMEM;
390
        return ENOMEM;
391
   
391
   
392
    futex_down(&vfs_phone_futex);
392
    futex_down(&vfs_phone_futex);
393
    async_serialize_start();
393
    async_serialize_start();
394
    vfs_connect();
394
    vfs_connect();
395
   
395
   
396
    req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL);
396
    req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL);
397
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
397
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
398
    if (rc != EOK) {
398
    if (rc != EOK) {
399
        async_wait_for(req, NULL);
399
        async_wait_for(req, NULL);
400
        async_serialize_end();
400
        async_serialize_end();
401
        futex_up(&vfs_phone_futex);
401
        futex_up(&vfs_phone_futex);
402
        free(pa);
402
        free(pa);
Line 412... Line 412...
412
static int _unlink(const char *path, int lflag)
412
static int _unlink(const char *path, int lflag)
413
{
413
{
414
    ipcarg_t rc;
414
    ipcarg_t rc;
415
    aid_t req;
415
    aid_t req;
416
   
416
   
417
    size_t pa_len;
417
    size_t pa_size;
418
    char *pa = absolutize(path, &pa_len);
418
    char *pa = absolutize(path, &pa_size);
419
    if (!pa)
419
    if (!pa)
420
        return ENOMEM;
420
        return ENOMEM;
421
 
421
 
422
    futex_down(&vfs_phone_futex);
422
    futex_down(&vfs_phone_futex);
423
    async_serialize_start();
423
    async_serialize_start();
424
    vfs_connect();
424
    vfs_connect();
425
   
425
   
426
    req = async_send_0(vfs_phone, VFS_UNLINK, NULL);
426
    req = async_send_0(vfs_phone, VFS_UNLINK, NULL);
427
    rc = ipc_data_write_start(vfs_phone, pa, pa_len);
427
    rc = ipc_data_write_start(vfs_phone, pa, pa_size);
428
    if (rc != EOK) {
428
    if (rc != EOK) {
429
        async_wait_for(req, NULL);
429
        async_wait_for(req, NULL);
430
        async_serialize_end();
430
        async_serialize_end();
431
        futex_up(&vfs_phone_futex);
431
        futex_up(&vfs_phone_futex);
432
        free(pa);
432
        free(pa);
Line 452... Line 452...
452
int rename(const char *old, const char *new)
452
int rename(const char *old, const char *new)
453
{
453
{
454
    ipcarg_t rc;
454
    ipcarg_t rc;
455
    aid_t req;
455
    aid_t req;
456
   
456
   
457
    size_t olda_len;
457
    size_t olda_size;
458
    char *olda = absolutize(old, &olda_len);
458
    char *olda = absolutize(old, &olda_size);
459
    if (!olda)
459
    if (!olda)
460
        return ENOMEM;
460
        return ENOMEM;
461
 
461
 
462
    size_t newa_len;
462
    size_t newa_size;
463
    char *newa = absolutize(new, &newa_len);
463
    char *newa = absolutize(new, &newa_size);
464
    if (!newa) {
464
    if (!newa) {
465
        free(olda);
465
        free(olda);
466
        return ENOMEM;
466
        return ENOMEM;
467
    }
467
    }
468
 
468
 
469
    futex_down(&vfs_phone_futex);
469
    futex_down(&vfs_phone_futex);
470
    async_serialize_start();
470
    async_serialize_start();
471
    vfs_connect();
471
    vfs_connect();
472
   
472
   
473
    req = async_send_0(vfs_phone, VFS_RENAME, NULL);
473
    req = async_send_0(vfs_phone, VFS_RENAME, NULL);
474
    rc = ipc_data_write_start(vfs_phone, olda, olda_len);
474
    rc = ipc_data_write_start(vfs_phone, olda, olda_size);
475
    if (rc != EOK) {
475
    if (rc != EOK) {
476
        async_wait_for(req, NULL);
476
        async_wait_for(req, NULL);
477
        async_serialize_end();
477
        async_serialize_end();
478
        futex_up(&vfs_phone_futex);
478
        futex_up(&vfs_phone_futex);
479
        free(olda);
479
        free(olda);
480
        free(newa);
480
        free(newa);
481
        return (int) rc;
481
        return (int) rc;
482
    }
482
    }
483
    rc = ipc_data_write_start(vfs_phone, newa, newa_len);
483
    rc = ipc_data_write_start(vfs_phone, newa, newa_size);
484
    if (rc != EOK) {
484
    if (rc != EOK) {
485
        async_wait_for(req, NULL);
485
        async_wait_for(req, NULL);
486
        async_serialize_end();
486
        async_serialize_end();
487
        futex_up(&vfs_phone_futex);
487
        futex_up(&vfs_phone_futex);
488
        free(olda);
488
        free(olda);
Line 497... Line 497...
497
    return rc;
497
    return rc;
498
}
498
}
499
 
499
 
500
int chdir(const char *path)
500
int chdir(const char *path)
501
{
501
{
502
    size_t pa_len;
502
    size_t pa_size;
503
    char *pa = absolutize(path, &pa_len);
503
    char *pa = absolutize(path, &pa_size);
504
    if (!pa)
504
    if (!pa)
505
        return ENOMEM;
505
        return ENOMEM;
506
 
506
 
507
    DIR *d = opendir(pa);
507
    DIR *d = opendir(pa);
508
    if (!d) {
508
    if (!d) {
Line 514... Line 514...
514
    if (cwd_dir) {
514
    if (cwd_dir) {
515
        closedir(cwd_dir);
515
        closedir(cwd_dir);
516
        cwd_dir = NULL;
516
        cwd_dir = NULL;
517
        free(cwd_path);
517
        free(cwd_path);
518
        cwd_path = NULL;
518
        cwd_path = NULL;
519
        cwd_len = 0;
519
        cwd_size = 0;
520
    }
520
    }
521
    cwd_dir = d;
521
    cwd_dir = d;
522
    cwd_path = pa;
522
    cwd_path = pa;
523
    cwd_len = pa_len;
523
    cwd_size = pa_size;
524
    futex_up(&cwd_futex);
524
    futex_up(&cwd_futex);
525
    return EOK;
525
    return EOK;
526
}
526
}
527
 
527
 
528
char *getcwd(char *buf, size_t size)
528
char *getcwd(char *buf, size_t size)
529
{
529
{
530
    if (!size)
530
    if (!size)
531
        return NULL;
531
        return NULL;
532
    futex_down(&cwd_futex);
532
    futex_down(&cwd_futex);
533
    if (size < cwd_len + 1) {
533
    if (size < cwd_size + 1) {
534
        futex_up(&cwd_futex);
534
        futex_up(&cwd_futex);
535
        return NULL;
535
        return NULL;
536
    }
536
    }
537
    strcpy(buf, cwd_path);
537
    str_ncpy(buf, cwd_path, size);
538
    futex_up(&cwd_futex);
538
    futex_up(&cwd_futex);
539
    return buf;
539
    return buf;
540
}
540
}
541
 
541
 
542
/** @}
542
/** @}