Rev 2763 | Rev 3077 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2763 | Rev 2771 | ||
---|---|---|---|
Line 37... | Line 37... | ||
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 <sys/types.h> |
43 | #include <sys/types.h> |
43 | #include <ipc/ipc.h> |
44 | #include <ipc/ipc.h> |
44 | #include <ipc/services.h> |
45 | #include <ipc/services.h> |
45 | #include <async.h> |
46 | #include <async.h> |
46 | #include <atomic.h> |
47 | #include <atomic.h> |
Line 55... | Line 56... | ||
55 | futex_t cwd_futex = FUTEX_INITIALIZER; |
56 | futex_t cwd_futex = FUTEX_INITIALIZER; |
56 | DIR *cwd_dir = NULL; |
57 | DIR *cwd_dir = NULL; |
57 | char *cwd_path = NULL; |
58 | char *cwd_path = NULL; |
58 | size_t cwd_len = 0; |
59 | size_t cwd_len = 0; |
59 | 60 | ||
60 | static char *absolutize(const char *path) |
61 | static char *absolutize(const char *path, size_t *retlen) |
61 | { |
62 | { |
62 | char *ncwd_path; |
63 | char *ncwd_path; |
63 | 64 | ||
64 | futex_down(&cwd_futex); |
65 | futex_down(&cwd_futex); |
65 | size_t len = strlen(path); |
66 | size_t len = strlen(path); |
Line 83... | Line 84... | ||
83 | return NULL; |
84 | return NULL; |
84 | } |
85 | } |
85 | ncwd_path[0] = '\0'; |
86 | ncwd_path[0] = '\0'; |
86 | } |
87 | } |
87 | strcat(ncwd_path, path); |
88 | strcat(ncwd_path, path); |
- | 89 | if (!canonify(ncwd_path, retlen)) { |
|
- | 90 | futex_up(&cwd_futex); |
|
- | 91 | free(ncwd_path); |
|
- | 92 | return NULL; |
|
- | 93 | } |
|
88 | futex_up(&cwd_futex); |
94 | futex_up(&cwd_futex); |
89 | return ncwd_path; |
95 | return ncwd_path; |
90 | } |
96 | } |
91 | 97 | ||
92 | static int vfs_connect(void) |
98 | static int vfs_connect(void) |
Line 100... | Line 106... | ||
100 | { |
106 | { |
101 | int res; |
107 | int res; |
102 | ipcarg_t rc; |
108 | ipcarg_t rc; |
103 | aid_t req; |
109 | aid_t req; |
104 | 110 | ||
105 | int dev_handle = 0; /* TODO */ |
111 | dev_handle_t dev_handle = 0; /* TODO */ |
106 | 112 | ||
- | 113 | size_t mpa_len; |
|
107 | char *mpa = absolutize(mp); |
114 | char *mpa = absolutize(mp, &mpa_len); |
108 | if (!mpa) |
115 | if (!mpa) |
109 | return ENOMEM; |
116 | return ENOMEM; |
110 | size_t mpc_len; |
- | |
111 | char *mpc = canonify(mpa, &mpc_len); |
- | |
112 | if (!mpc) { |
- | |
113 | free(mpa); |
- | |
114 | return EINVAL; |
- | |
115 | } |
- | |
116 | 117 | ||
117 | futex_down(&vfs_phone_futex); |
118 | futex_down(&vfs_phone_futex); |
118 | async_serialize_start(); |
119 | async_serialize_start(); |
119 | if (vfs_phone < 0) { |
120 | if (vfs_phone < 0) { |
120 | res = vfs_connect(); |
121 | res = vfs_connect(); |
Line 132... | Line 133... | ||
132 | async_serialize_end(); |
133 | async_serialize_end(); |
133 | futex_up(&vfs_phone_futex); |
134 | futex_up(&vfs_phone_futex); |
134 | free(mpa); |
135 | free(mpa); |
135 | return (int) rc; |
136 | return (int) rc; |
136 | } |
137 | } |
137 | rc = ipc_data_write_start(vfs_phone, (void *)mpc, mpc_len); |
138 | rc = ipc_data_write_start(vfs_phone, (void *)mpa, mpa_len); |
138 | if (rc != EOK) { |
139 | if (rc != EOK) { |
139 | async_wait_for(req, NULL); |
140 | async_wait_for(req, NULL); |
140 | async_serialize_end(); |
141 | async_serialize_end(); |
141 | futex_up(&vfs_phone_futex); |
142 | futex_up(&vfs_phone_futex); |
142 | free(mpa); |
143 | free(mpa); |
Line 154... | Line 155... | ||
154 | int res; |
155 | int res; |
155 | ipcarg_t rc; |
156 | ipcarg_t rc; |
156 | ipc_call_t answer; |
157 | ipc_call_t answer; |
157 | aid_t req; |
158 | aid_t req; |
158 | 159 | ||
- | 160 | size_t pa_len; |
|
159 | char *pa = absolutize(path); |
161 | char *pa = absolutize(path, &pa_len); |
160 | if (!pa) |
162 | if (!pa) |
161 | return ENOMEM; |
163 | return ENOMEM; |
162 | size_t pc_len; |
- | |
163 | char *pc = canonify(pa, &pc_len); |
- | |
164 | if (!pc) { |
- | |
165 | free(pa); |
- | |
166 | return EINVAL; |
- | |
167 | } |
- | |
168 | 164 | ||
169 | futex_down(&vfs_phone_futex); |
165 | futex_down(&vfs_phone_futex); |
170 | async_serialize_start(); |
166 | async_serialize_start(); |
171 | if (vfs_phone < 0) { |
167 | if (vfs_phone < 0) { |
172 | res = vfs_connect(); |
168 | res = vfs_connect(); |
Line 176... | Line 172... | ||
176 | free(pa); |
172 | free(pa); |
177 | return res; |
173 | return res; |
178 | } |
174 | } |
179 | } |
175 | } |
180 | req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer); |
176 | req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer); |
181 | rc = ipc_data_write_start(vfs_phone, pc, pc_len); |
177 | rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
182 | if (rc != EOK) { |
178 | if (rc != EOK) { |
183 | async_wait_for(req, NULL); |
179 | async_wait_for(req, NULL); |
184 | async_serialize_end(); |
180 | async_serialize_end(); |
185 | futex_up(&vfs_phone_futex); |
181 | futex_up(&vfs_phone_futex); |
186 | free(pa); |
182 | free(pa); |
Line 377... | Line 373... | ||
377 | { |
373 | { |
378 | int res; |
374 | int res; |
379 | ipcarg_t rc; |
375 | ipcarg_t rc; |
380 | aid_t req; |
376 | aid_t req; |
381 | 377 | ||
- | 378 | size_t pa_len; |
|
382 | char *pa = absolutize(path); |
379 | char *pa = absolutize(path, &pa_len); |
383 | if (!pa) |
380 | if (!pa) |
384 | return ENOMEM; |
381 | return ENOMEM; |
385 | size_t pc_len; |
- | |
386 | char *pc = canonify(pa, &pc_len); |
- | |
387 | if (!pc) { |
- | |
388 | free(pa); |
- | |
389 | return EINVAL; |
- | |
390 | } |
- | |
391 | 382 | ||
392 | futex_down(&vfs_phone_futex); |
383 | futex_down(&vfs_phone_futex); |
393 | async_serialize_start(); |
384 | async_serialize_start(); |
394 | if (vfs_phone < 0) { |
385 | if (vfs_phone < 0) { |
395 | res = vfs_connect(); |
386 | res = vfs_connect(); |
Line 399... | Line 390... | ||
399 | free(pa); |
390 | free(pa); |
400 | return res; |
391 | return res; |
401 | } |
392 | } |
402 | } |
393 | } |
403 | req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL); |
394 | req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL); |
404 | rc = ipc_data_write_start(vfs_phone, pc, pc_len); |
395 | rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
405 | if (rc != EOK) { |
396 | if (rc != EOK) { |
406 | async_wait_for(req, NULL); |
397 | async_wait_for(req, NULL); |
407 | async_serialize_end(); |
398 | async_serialize_end(); |
408 | futex_up(&vfs_phone_futex); |
399 | futex_up(&vfs_phone_futex); |
409 | free(pa); |
400 | free(pa); |
Line 420... | Line 411... | ||
420 | { |
411 | { |
421 | int res; |
412 | int res; |
422 | ipcarg_t rc; |
413 | ipcarg_t rc; |
423 | aid_t req; |
414 | aid_t req; |
424 | 415 | ||
- | 416 | size_t pa_len; |
|
425 | char *pa = absolutize(path); |
417 | char *pa = absolutize(path, &pa_len); |
426 | if (!pa) |
418 | if (!pa) |
427 | return ENOMEM; |
419 | return ENOMEM; |
428 | size_t pc_len; |
- | |
429 | char *pc = canonify(pa, &pc_len); |
- | |
430 | if (!pc) { |
- | |
431 | free(pa); |
- | |
432 | return EINVAL; |
- | |
433 | } |
420 | |
434 | futex_down(&vfs_phone_futex); |
421 | futex_down(&vfs_phone_futex); |
435 | async_serialize_start(); |
422 | async_serialize_start(); |
436 | if (vfs_phone < 0) { |
423 | if (vfs_phone < 0) { |
437 | res = vfs_connect(); |
424 | res = vfs_connect(); |
438 | if (res < 0) { |
425 | if (res < 0) { |
Line 441... | Line 428... | ||
441 | free(pa); |
428 | free(pa); |
442 | return res; |
429 | return res; |
443 | } |
430 | } |
444 | } |
431 | } |
445 | req = async_send_0(vfs_phone, VFS_UNLINK, NULL); |
432 | req = async_send_0(vfs_phone, VFS_UNLINK, NULL); |
446 | rc = ipc_data_write_start(vfs_phone, pc, pc_len); |
433 | rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
447 | if (rc != EOK) { |
434 | if (rc != EOK) { |
448 | async_wait_for(req, NULL); |
435 | async_wait_for(req, NULL); |
449 | async_serialize_end(); |
436 | async_serialize_end(); |
450 | futex_up(&vfs_phone_futex); |
437 | futex_up(&vfs_phone_futex); |
451 | free(pa); |
438 | free(pa); |
Line 472... | Line 459... | ||
472 | { |
459 | { |
473 | int res; |
460 | int res; |
474 | ipcarg_t rc; |
461 | ipcarg_t rc; |
475 | aid_t req; |
462 | aid_t req; |
476 | 463 | ||
- | 464 | size_t olda_len; |
|
477 | char *olda = absolutize(old); |
465 | char *olda = absolutize(old, &olda_len); |
478 | if (!olda) |
466 | if (!olda) |
479 | return ENOMEM; |
467 | return ENOMEM; |
- | 468 | ||
480 | size_t oldc_len; |
469 | size_t newa_len; |
481 | char *oldc = canonify(olda, &oldc_len); |
- | |
482 | if (!oldc) { |
- | |
483 | free(olda); |
- | |
484 | return EINVAL; |
- | |
485 | } |
- | |
486 | char *newa = absolutize(new); |
470 | char *newa = absolutize(new, &newa_len); |
487 | if (!newa) { |
471 | if (!newa) { |
488 | free(olda); |
472 | free(olda); |
489 | return ENOMEM; |
473 | return ENOMEM; |
490 | } |
474 | } |
491 | size_t newc_len; |
- | |
492 | char *newc = canonify(newa, &newc_len); |
- | |
493 | if (!newc) { |
- | |
494 | free(olda); |
- | |
495 | free(newa); |
- | |
496 | return EINVAL; |
- | |
497 | } |
- | |
498 | 475 | ||
499 | futex_down(&vfs_phone_futex); |
476 | futex_down(&vfs_phone_futex); |
500 | async_serialize_start(); |
477 | async_serialize_start(); |
501 | if (vfs_phone < 0) { |
478 | if (vfs_phone < 0) { |
502 | res = vfs_connect(); |
479 | res = vfs_connect(); |
Line 507... | Line 484... | ||
507 | free(newa); |
484 | free(newa); |
508 | return res; |
485 | return res; |
509 | } |
486 | } |
510 | } |
487 | } |
511 | req = async_send_0(vfs_phone, VFS_RENAME, NULL); |
488 | req = async_send_0(vfs_phone, VFS_RENAME, NULL); |
512 | rc = ipc_data_write_start(vfs_phone, oldc, oldc_len); |
489 | rc = ipc_data_write_start(vfs_phone, olda, olda_len); |
513 | if (rc != EOK) { |
490 | if (rc != EOK) { |
514 | async_wait_for(req, NULL); |
491 | async_wait_for(req, NULL); |
515 | async_serialize_end(); |
492 | async_serialize_end(); |
516 | futex_up(&vfs_phone_futex); |
493 | futex_up(&vfs_phone_futex); |
517 | free(olda); |
494 | free(olda); |
518 | free(newa); |
495 | free(newa); |
519 | return (int) rc; |
496 | return (int) rc; |
520 | } |
497 | } |
521 | rc = ipc_data_write_start(vfs_phone, newc, newc_len); |
498 | rc = ipc_data_write_start(vfs_phone, newa, newa_len); |
522 | if (rc != EOK) { |
499 | if (rc != EOK) { |
523 | async_wait_for(req, NULL); |
500 | async_wait_for(req, NULL); |
524 | async_serialize_end(); |
501 | async_serialize_end(); |
525 | futex_up(&vfs_phone_futex); |
502 | futex_up(&vfs_phone_futex); |
526 | free(olda); |
503 | free(olda); |
Line 535... | Line 512... | ||
535 | return rc; |
512 | return rc; |
536 | } |
513 | } |
537 | 514 | ||
538 | int chdir(const char *path) |
515 | int chdir(const char *path) |
539 | { |
516 | { |
- | 517 | size_t pa_len; |
|
540 | char *pa = absolutize(path); |
518 | char *pa = absolutize(path, &pa_len); |
541 | if (!pa) |
519 | if (!pa) |
542 | return ENOMEM; |
520 | return ENOMEM; |
543 | size_t pc_len; |
- | |
544 | char *pc = canonify(pa, &pc_len); |
- | |
545 | if (!pc) { |
- | |
546 | free(pa); |
- | |
547 | return ENOENT; |
- | |
548 | } |
- | |
549 | 521 | ||
550 | DIR *d = opendir(pc); |
522 | DIR *d = opendir(pa); |
551 | if (!d) { |
523 | if (!d) { |
552 | free(pa); |
524 | free(pa); |
553 | return ENOENT; |
525 | return ENOENT; |
554 | } |
526 | } |
555 | 527 | ||
Line 560... | Line 532... | ||
560 | free(cwd_path); |
532 | free(cwd_path); |
561 | cwd_path = NULL; |
533 | cwd_path = NULL; |
562 | cwd_len = 0; |
534 | cwd_len = 0; |
563 | } |
535 | } |
564 | cwd_dir = d; |
536 | cwd_dir = d; |
565 | cwd_path = pc; |
537 | cwd_path = pa; |
566 | cwd_len = pc_len; |
538 | cwd_len = pa_len; |
567 | futex_up(&cwd_futex); |
539 | futex_up(&cwd_futex); |
568 | } |
540 | } |
569 | 541 | ||
570 | char *getcwd(char *buf, size_t size) |
542 | char *getcwd(char *buf, size_t size) |
571 | { |
543 | { |