Debian Codegraph @

modules/lsp-runtime-lib/src/main/io/Path.cpp

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 8 февр. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/common/debug.h>
23 #include <lsp-plug.in/io/Path.h>
24 #include <lsp-plug.in/io/File.h>
25 #include <lsp-plug.in/io/Dir.h>
26 #include <lsp-plug.in/lltl/phashset.h>
27 #include <lsp-plug.in/stdlib/string.h>
28
29 #if defined(PLATFORM_WINDOWS)
30 #include <windows.h>
31 #include <fileapi.h>
32 #include <shlwapi.h>
33 #else
34 #include <fcntl.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #endif /* defined(PLATFORM_WINDOWS) */
39
40 namespace lsp
41 {
42 namespace io
43 {
44 Path::Path()
45 {
46 }
47
48 Path::~Path()
49 {
50 }
51
52 inline void Path::fixup_path()
53 {
54 #ifdef PLATFORM_WINDOWS
55 sPath.replace_all('/', FILE_SEPARATOR_C);
56 #else
57 sPath.replace_all('\\', FILE_SEPARATOR_C);
58 #endif /* PLATFORM_WINDOWS */
59 }
60
61 Path *Path::clone() const
62 {
63 Path *res = new Path();
64
65 if ((res != NULL) && (res->set(this) == STATUS_OK))
66 return res;
67
68 delete res;
69 return NULL;
70 }
71
72 status_t Path::set_native(const char *path, const char *charset)
73 {
74 if (path == NULL)
75 return STATUS_BAD_ARGUMENTS;
76 if (!sPath.set_native(path, charset))
77 return STATUS_NO_MEM;
78 fixup_path();
79 return STATUS_OK;
80 }
81
82 status_t Path::set(const char *path)
83 {
84 if (path == NULL)
85 return STATUS_BAD_ARGUMENTS;
86 if (!sPath.set_utf8(path))
87 return STATUS_NO_MEM;
88 fixup_path();
89 return STATUS_OK;
90 }
91
92 status_t Path::set(const LSPString *path)
93 {
94 if (path == NULL)
95 return STATUS_BAD_ARGUMENTS;
96 if (!sPath.set(path))
97 return STATUS_NO_MEM;
98 fixup_path();
99 return STATUS_OK;
100 }
101
102 status_t Path::set(const Path *path)
103 {
104 if (path == NULL)
105 return STATUS_BAD_ARGUMENTS;
106 if (!sPath.set(&path->sPath))
107 return STATUS_NO_MEM;
108 fixup_path();
109 return STATUS_OK;
110 }
111
112 status_t Path::set(const char *path, const char *child)
113 {
114 Path tmp;
115 status_t res = tmp.set(path);
116 if (res == STATUS_OK)
117 res = tmp.append_child(child);
118 if (res == STATUS_OK)
119 swap(&tmp);
120 return res;
121 }
122
123 status_t Path::set(const char *path, const LSPString *child)
124 {
125 Path tmp;
126 status_t res = tmp.set(path);
127 if (res == STATUS_OK)
128 res = tmp.append_child(child);
129 if (res == STATUS_OK)
130 swap(&tmp);
131 return res;
132 }
133
134 status_t Path::set(const char *path, const Path *child)
135 {
136 Path tmp;
137 status_t res = tmp.set(path);
138 if (res == STATUS_OK)
139 res = tmp.append_child(child);
140 if (res == STATUS_OK)
141 swap(&tmp);
142 return res;
143 }
144
145 status_t Path::set(const LSPString *path, const char *child)
146 {
147 Path tmp;
148 status_t res = tmp.set(path);
149 if (res == STATUS_OK)
150 res = tmp.append_child(child);
151 if (res == STATUS_OK)
152 swap(&tmp);
153 return res;
154 }
155
156 status_t Path::set(const LSPString *path, const LSPString *child)
157 {
158 Path tmp;
159 status_t res = tmp.set(path);
160 if (res == STATUS_OK)
161 res = tmp.append_child(child);
162 if (res == STATUS_OK)
163 swap(&tmp);
164 return res;
165 }
166
167 status_t Path::set(const LSPString *path, const Path *child)
168 {
169 Path tmp;
170 status_t res = tmp.set(path);
171 if (res == STATUS_OK)
172 res = tmp.append_child(child);
173 if (res == STATUS_OK)
174 swap(&tmp);
175 return res;
176 }
177
178 status_t Path::set(const Path *path, const char *child)
179 {
180 Path tmp;
181 status_t res = tmp.set(path);
182 if (res == STATUS_OK)
183 res = tmp.append_child(child);
184 if (res == STATUS_OK)
185 swap(&tmp);
186 return res;
187 }
188
189 status_t Path::set(const Path *path, const LSPString *child)
190 {
191 Path tmp;
192 status_t res = tmp.set(path);
193 if (res == STATUS_OK)
194 res = tmp.append_child(child);
195 if (res == STATUS_OK)
196 swap(&tmp);
197 return res;
198 }
199
200 status_t Path::set(const Path *path, const Path *child)
201 {
202 Path tmp;
203 status_t res = tmp.set(path);
204 if (res == STATUS_OK)
205 res = tmp.append_child(child);
206 if (res == STATUS_OK)
207 swap(&tmp);
208 return res;
209 }
210
211 status_t Path::get(char *path, size_t maxlen) const
212 {
213 if (path == NULL)
214 return STATUS_BAD_ARGUMENTS;
215
216 const char *utf8 = sPath.get_utf8();
217 if (utf8 == NULL)
218 return STATUS_NO_MEM;
219
220 size_t len = ::strlen(utf8);
221 if (len >= maxlen)
222 return STATUS_TOO_BIG;
223
224 ::memcpy(path, utf8, len+1); // Copy including '\0' character
225 return STATUS_OK;
226 }
227
228 status_t Path::get(LSPString *path) const
229 {
230 if (path == NULL)
231 return STATUS_BAD_ARGUMENTS;
232
233 return (path->set(&sPath)) ? STATUS_OK : STATUS_NO_MEM;
234 }
235
236 status_t Path::get(Path *path) const
237 {
238 if (path == NULL)
239 return STATUS_BAD_ARGUMENTS;
240
241 return (path->sPath.set(&sPath)) ? STATUS_OK : STATUS_NO_MEM;
242 }
243
244 status_t Path::set_last(const char *path)
245 {
246 if (path == NULL)
247 return STATUS_BAD_ARGUMENTS;
248 else if (strlen(path) == 0)
249 return remove_last();
250
251 ssize_t len = sPath.length();
252 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
253 idx = (idx < 0) ? 0 : idx + 1;
254 sPath.set_length(idx);
255 if (sPath.append_utf8(path))
256 {
257 fixup_path();
258 return STATUS_OK;
259 }
260
261 sPath.set_length(len);
262 return STATUS_NO_MEM;
263 }
264
265 status_t Path::set_last(const LSPString *path)
266 {
267 if (path == NULL)
268 return STATUS_BAD_ARGUMENTS;
269 else if (path->length() <= 0)
270 return remove_last();
271
272 ssize_t len = sPath.length();
273 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
274 idx = (idx < 0) ? 0 : idx + 1;
275 sPath.set_length(idx);
276 if (sPath.append(path))
277 {
278 fixup_path();
279 return STATUS_OK;
280 }
281
282 sPath.set_length(len);
283 return STATUS_NO_MEM;
284 }
285
286 status_t Path::set_last(const Path *path)
287 {
288 if (path == NULL)
289 return STATUS_BAD_ARGUMENTS;
290 else if (path->sPath.length() <= 0)
291 return remove_last();
292
293 ssize_t len = sPath.length();
294 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
295 idx = (idx < 0) ? 0 : idx + 1;
296 sPath.set_length(idx);
297 if (sPath.append(&path->sPath))
298 {
299 fixup_path();
300 return STATUS_OK;
301 }
302
303 sPath.set_length(len);
304 return STATUS_NO_MEM;
305 }
306
307 status_t Path::get_last(char *path, size_t maxlen) const
308 {
309 if (path == NULL)
310 return STATUS_BAD_ARGUMENTS;
311
312 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
313 idx = (idx < 0) ? 0 : idx + 1;
314
315 const char *utf8 = sPath.get_utf8(idx);
316 if (utf8 == NULL)
317 return STATUS_NO_MEM;
318
319 size_t len = ::strlen(utf8);
320 if (len >= maxlen)
321 return STATUS_TOO_BIG;
322
323 ::memcpy(path, utf8, len + 1);
324 return STATUS_OK;
325 }
326
327 status_t Path::get_last(LSPString *path) const
328 {
329 if (path == NULL)
330 return STATUS_BAD_ARGUMENTS;
331
332 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
333 idx = (idx < 0) ? 0 : idx + 1;
334
335 return (path->set(&sPath, idx)) ? STATUS_OK : STATUS_NO_MEM;
336 }
337
338 status_t Path::get_last(Path *path) const
339 {
340 return (path != NULL) ? get_last(&path->sPath) : STATUS_BAD_ARGUMENTS;
341 }
342
343 status_t Path::get_first(char *path, size_t maxlen) const
344 {
345 if (path == NULL)
346 return STATUS_BAD_ARGUMENTS;
347
348 ssize_t idx = sPath.index_of(FILE_SEPARATOR_C);
349 if (idx < 0)
350 {
351 if (sPath.is_empty())
352 return STATUS_NOT_FOUND;
353 idx = sPath.length();
354 }
355 else if (is_absolute())
356 idx += 1;
357
358 const char *utf8 = sPath.get_utf8(0, idx);
359 if (utf8 == NULL)
360 return STATUS_NO_MEM;
361
362 size_t len = ::strlen(utf8);
363 if (len >= maxlen)
364 return STATUS_TOO_BIG;
365
366 ::memcpy(path, utf8, len + 1);
367 return STATUS_OK;
368 }
369
370 status_t Path::get_first(LSPString *path) const
371 {
372 if (path == NULL)
373 return STATUS_BAD_ARGUMENTS;
374
375 ssize_t idx = sPath.index_of(FILE_SEPARATOR_C);
376 if (idx < 0)
377 {
378 if (sPath.is_empty())
379 return STATUS_NOT_FOUND;
380 idx = sPath.length();
381 }
382 else if (is_absolute())
383 idx += 1;
384
385 return (path->set(&sPath, 0, idx)) ? STATUS_OK : STATUS_NO_MEM;
386 }
387
388 status_t Path::get_first(Path *path) const
389 {
390 if (path == NULL)
391 return STATUS_BAD_ARGUMENTS;
392
393 ssize_t idx = sPath.index_of(FILE_SEPARATOR_C);
394 if (idx < 0)
395 return STATUS_NOT_FOUND;
396 if (is_absolute())
397 ++idx;
398
399 return path->sPath.set(&sPath, 0, idx);
400 }
401
402 status_t Path::get_ext(char *path, size_t maxlen) const
403 {
404 if (path == NULL)
405 return STATUS_BAD_ARGUMENTS;
406
407 ssize_t start, next;
408
409 start = sPath.rindex_of(FILE_SEPARATOR_C);
410 start = (start < 0) ? 0 : start + 1;
411
412 // Lookup for last dot
413 if ((next = sPath.index_of(start, '.')) >= 0)
414 {
415 start = next+1;
416 while ((next = sPath.index_of(start, '.')) >= 0)
417 start = next+1;
418 }
419 else
420 start = sPath.length();
421
422 // Copy data to output
423 const char *utf8 = sPath.get_utf8(start);
424 if (utf8 == NULL)
425 return STATUS_NO_MEM;
426
427 size_t len = ::strlen(utf8);
428 if (len >= maxlen)
429 return STATUS_TOO_BIG;
430
431 ::memcpy(path, utf8, len + 1);
432 return STATUS_OK;
433 }
434
435 status_t Path::get_ext(LSPString *path) const
436 {
437 if (path == NULL)
438 return STATUS_BAD_ARGUMENTS;
439
440 ssize_t start, next;
441
442 start = sPath.rindex_of(FILE_SEPARATOR_C);
443 start = (start < 0) ? 0 : start + 1;
444
445 // Lookup for last dot
446 if ((next = sPath.index_of(start, '.')) >= 0)
447 {
448 start = next+1;
449 while ((next = sPath.index_of(start, '.')) >= 0)
450 start = next+1;
451 }
452 else
453 start = sPath.length();
454
455 return (path->set(&sPath, start)) ? STATUS_OK : STATUS_NO_MEM;
456 }
457
458 status_t Path::get_ext(Path *path) const
459 {
460 return (path != NULL) ? get_ext(&path->sPath) : STATUS_BAD_ARGUMENTS;
461 }
462
463 status_t Path::get_last_noext(char *path, size_t maxlen) const
464 {
465 if (path == NULL)
466 return STATUS_BAD_ARGUMENTS;
467
468 ssize_t start, next, end;
469
470 start = sPath.rindex_of(FILE_SEPARATOR_C);
471 start = (start < 0) ? 0 : start + 1;
472
473 // Lookup for last dot
474 end = sPath.index_of(start, '.');
475 if (end >= 0)
476 {
477 while ((next = sPath.index_of(end + 1, '.')) >= 0)
478 end = next;
479 }
480 if (end < 0)
481 end = sPath.length();
482
483 // Copy data to output
484 const char *utf8 = sPath.get_utf8(start, end);
485 if (utf8 == NULL)
486 return STATUS_NO_MEM;
487
488 size_t len = ::strlen(utf8);
489 if (len >= maxlen)
490 return STATUS_TOO_BIG;
491
492 ::memcpy(path, utf8, len + 1);
493 return STATUS_OK;
494 }
495
496 status_t Path::get_last_noext(LSPString *path) const
497 {
498 if (path == NULL)
499 return STATUS_BAD_ARGUMENTS;
500
501 ssize_t start, next, end;
502
503 start = sPath.rindex_of(FILE_SEPARATOR_C);
504 start = (start < 0) ? 0 : start + 1;
505
506 // Lookup for last dot
507 end = sPath.index_of(start, '.');
508 if (end >= 0)
509 {
510 while ((next = sPath.index_of(end + 1, '.')) >= 0)
511 end = next;
512 }
513 if (end < 0)
514 end = sPath.length();
515
516 return (path->set(&sPath, start, end)) ? STATUS_OK : STATUS_NO_MEM;
517 }
518
519 status_t Path::get_last_noext(Path *path) const
520 {
521 return (path != NULL) ? get_last_noext(&path->sPath) : STATUS_BAD_ARGUMENTS;
522 }
523
524 status_t Path::get_noext(char *path, size_t maxlen) const
525 {
526 if (path == NULL)
527 return STATUS_BAD_ARGUMENTS;
528
529 ssize_t start, next, end;
530
531 start = sPath.rindex_of(FILE_SEPARATOR_C);
532 start = (start < 0) ? 0 : start + 1;
533
534 // Lookup for last dot
535 end = sPath.index_of(start, '.');
536 if (end >= 0)
537 {
538 while ((next = sPath.index_of(end + 1, '.')) >= 0)
539 end = next;
540 }
541 if (end < 0)
542 end = sPath.length();
543
544 // Copy data to output
545 const char *utf8 = sPath.get_utf8(0, end);
546 if (utf8 == NULL)
547 return STATUS_NO_MEM;
548
549 size_t len = ::strlen(utf8);
550 if (len >= maxlen)
551 return STATUS_TOO_BIG;
552
553 ::memcpy(path, utf8, len + 1);
554 return STATUS_OK;
555 }
556
557 status_t Path::get_noext(LSPString *path) const
558 {
559 if (path == NULL)
560 return STATUS_BAD_ARGUMENTS;
561
562 ssize_t start, next, end;
563
564 start = sPath.rindex_of(FILE_SEPARATOR_C);
565 start = (start < 0) ? 0 : start + 1;
566
567 // Lookup for last dot
568 end = sPath.index_of(start, '.');
569 if (end >= 0)
570 {
571 while ((next = sPath.index_of(end + 1, '.')) >= 0)
572 end = next;
573 }
574 if (end < 0)
575 end = sPath.length();
576
577 return (path->set(&sPath, 0, end)) ? STATUS_OK : STATUS_NO_MEM;
578 }
579
580 status_t Path::get_noext(Path *path) const
581 {
582 return (path != NULL) ? get_noext(&path->sPath) : STATUS_BAD_ARGUMENTS;
583 }
584
585 status_t Path::get_parent(char *path, size_t maxlen) const
586 {
587 if (path == NULL)
588 return STATUS_BAD_ARGUMENTS;
589 else if (is_root())
590 return STATUS_NOT_FOUND;
591
592 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
593 if (idx < 0)
594 return STATUS_NOT_FOUND;
595
596 const char *utf8 = sPath.get_utf8(0, idx);
597 if (utf8 == NULL)
598 return STATUS_NO_MEM;
599
600 size_t len = ::strlen(utf8);
601 if (len >= maxlen)
602 return STATUS_TOO_BIG;
603
604 ::memcpy(path, utf8, len + 1);
605 return STATUS_OK;
606 }
607
608 status_t Path::get_parent(LSPString *path) const
609 {
610 if (path == NULL)
611 return STATUS_BAD_ARGUMENTS;
612 else if (is_root())
613 return STATUS_NOT_FOUND;
614
615 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
616 if (idx < 0)
617 return STATUS_NOT_FOUND;
618
619 return (path->set(&sPath, 0, idx)) ? STATUS_OK : STATUS_NO_MEM;
620 }
621
622 status_t Path::get_parent(Path *path) const
623 {
624 if (path == NULL)
625 return STATUS_BAD_ARGUMENTS;
626 else if (is_root())
627 return STATUS_NOT_FOUND;
628
629 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
630 if (idx < 0)
631 return STATUS_NOT_FOUND;
632
633 return (path->sPath.set(&sPath, 0, idx)) ? STATUS_OK : STATUS_NO_MEM;
634 }
635
636 status_t Path::set_parent(const char *path)
637 {
638 if (path == NULL)
639 return STATUS_BAD_ARGUMENTS;
640 else if (is_root())
641 return STATUS_BAD_STATE;
642
643 LSPString tmp;
644 if (!tmp.set_utf8(path))
645 return STATUS_NO_MEM;
646 while (tmp.ends_with(FILE_SEPARATOR_C))
647 tmp.set_length(tmp.length() - 1);
648
649 bool success = tmp.append(FILE_SEPARATOR_C);
650 if (success)
651 success = tmp.append(&sPath);
652 if (success)
653 {
654 sPath.swap(&tmp);
655 fixup_path();
656 }
657 return (success) ? STATUS_OK : STATUS_NO_MEM;
658 }
659
660 status_t Path::set_parent(const LSPString *path)
661 {
662 if (path == NULL)
663 return STATUS_BAD_ARGUMENTS;
664 else if (is_root())
665 return STATUS_BAD_STATE;
666
667 LSPString tmp;
668 if (!tmp.set(path))
669 return STATUS_NO_MEM;
670 while (tmp.ends_with(FILE_SEPARATOR_C))
671 tmp.set_length(tmp.length() - 1);
672
673 bool success = tmp.append(FILE_SEPARATOR_C);
674 if (success)
675 success = tmp.append(&sPath);
676 if (success)
677 {
678 sPath.swap(&tmp);
679 fixup_path();
680 }
681 return (success) ? STATUS_OK : STATUS_NO_MEM;
682 }
683
684 status_t Path::set_parent(const Path *path)
685 {
686 if (path == NULL)
687 return STATUS_BAD_ARGUMENTS;
688 else if (is_root())
689 return STATUS_BAD_STATE;
690
691 LSPString tmp;
692 if (!tmp.set(&path->sPath))
693 return STATUS_NO_MEM;
694 while (tmp.ends_with(FILE_SEPARATOR_C))
695 tmp.set_length(tmp.length() - 1);
696
697 bool success = tmp.append(FILE_SEPARATOR_C);
698 if (success)
699 success = tmp.append(&sPath);
700 if (success)
701 {
702 sPath.swap(&tmp);
703 fixup_path();
704 }
705 return (success) ? STATUS_OK : STATUS_NO_MEM;
706 }
707
708 status_t Path::concat(const char *path)
709 {
710 if (path == NULL)
711 return STATUS_BAD_ARGUMENTS;
712 if (!sPath.append_utf8(path))
713 return STATUS_NO_MEM;
714 fixup_path();
715 return STATUS_OK;
716 }
717
718 status_t Path::concat(const LSPString *path)
719 {
720 if (path == NULL)
721 return STATUS_BAD_ARGUMENTS;
722 if (!sPath.append(path))
723 return STATUS_NO_MEM;
724 fixup_path();
725 return STATUS_OK;
726 }
727
728 status_t Path::concat(const Path *path)
729 {
730 if (path == NULL)
731 return STATUS_BAD_ARGUMENTS;
732 if (!sPath.append(&path->sPath))
733 return STATUS_NO_MEM;
734 fixup_path();
735 return STATUS_OK;
736 }
737
738 status_t Path::append_child(const char *path)
739 {
740 Path tmp;
741 status_t res = tmp.set(path);
742 if (res != STATUS_OK)
743 return res;
744 else if (tmp.is_empty())
745 return STATUS_OK;
746 else if (tmp.is_absolute())
747 return STATUS_INVALID_VALUE;
748
749 size_t len = sPath.length();
750 bool success = ((len <= 0) || (sPath.ends_with(FILE_SEPARATOR_C))) ? true : sPath.append(FILE_SEPARATOR_C);
751 if (success)
752 success = sPath.append(&tmp.sPath);
753 if (success)
754 fixup_path();
755 else
756 sPath.set_length(len);
757
758 return (success) ? STATUS_OK : STATUS_NO_MEM;
759 }
760
761 status_t Path::append_child(const LSPString *path)
762 {
763 Path tmp;
764 status_t res = tmp.set(path);
765 if (res != STATUS_OK)
766 return res;
767 else if (tmp.is_empty())
768 return STATUS_OK;
769 else if (tmp.is_absolute())
770 return STATUS_INVALID_VALUE;
771
772 size_t len = sPath.length();
773 bool success = ((len <= 0) || (sPath.ends_with(FILE_SEPARATOR_C))) ? true : sPath.append(FILE_SEPARATOR_C);
774 if (success)
775 success = sPath.append(&tmp.sPath);
776 if (success)
777 fixup_path();
778 else
779 sPath.set_length(len);
780
781 return (success) ? STATUS_OK : STATUS_NO_MEM;
782 }
783
784 status_t Path::append_child(const Path *path)
785 {
786 if (path == NULL)
787 return STATUS_BAD_ARGUMENTS;
788 else if (path->is_empty())
789 return STATUS_OK;
790 else if (path->is_absolute())
791 return STATUS_INVALID_VALUE;
792
793 size_t len = sPath.length();
794 bool success = ((len <= 0) || (sPath.ends_with(FILE_SEPARATOR_C))) ? true : sPath.append(FILE_SEPARATOR_C);
795 if (success)
796 success = sPath.append(&path->sPath);
797 if (success)
798 fixup_path();
799 else
800 sPath.set_length(len);
801 return (success) ? STATUS_OK : STATUS_NO_MEM;
802 }
803
804 status_t Path::append(const char *path)
805 {
806 if (path == NULL)
807 return STATUS_BAD_ARGUMENTS;
808 LSPString tmp;
809 if (!tmp.set_utf8(path))
810 return STATUS_NO_MEM;
811 return append(&tmp);
812 }
813
814 status_t Path::append(const LSPString *path)
815 {
816 if (path == NULL)
817 return STATUS_BAD_ARGUMENTS;
818 if (!sPath.append(path))
819 return STATUS_NO_MEM;
820 fixup_path();
821 return STATUS_OK;
822 }
823
824 status_t Path::append(const Path *path)
825 {
826 if (path == NULL)
827 return STATUS_BAD_ARGUMENTS;
828 return append(&path->sPath);
829 }
830
831 status_t Path::remove_last()
832 {
833 if (is_root())
834 return STATUS_OK;
835
836 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
837 if (is_relative())
838 {
839 if (idx < 0)
840 idx = 0;
841 sPath.set_length(idx);
842 }
843 else if (idx >= 0)
844 {
845 ssize_t idx2 = (idx > 0) ? sPath.rindex_of(idx - 1, FILE_SEPARATOR_C) : -1;
846 if (idx2 < 0)
847 idx = idx + 1;
848 sPath.set_length(idx);
849 }
850 return STATUS_OK;
851 }
852
853 status_t Path::remove_last(char *path, size_t maxlen)
854 {
855 if (path == NULL)
856 return STATUS_BAD_ARGUMENTS;
857
858 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
859 idx = (idx < 0) ? 0 : idx + 1;
860
861 const char *utf8 = sPath.get_utf8(idx);
862 if (utf8 == NULL)
863 return STATUS_NO_MEM;
864
865 size_t len = ::strlen(utf8);
866 if (len >= maxlen)
867 return STATUS_TOO_BIG;
868
869 ::memcpy(path, utf8, len + 1);
870 sPath.set_length((idx > 0) ? idx - 1 : 0);
871
872 return STATUS_OK;
873 }
874
875 status_t Path::remove_last(LSPString *path)
876 {
877 if (path == NULL)
878 return STATUS_BAD_ARGUMENTS;
879
880 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
881 idx = (idx < 0) ? 0 : idx + 1;
882
883 if (!path->set(&sPath, idx))
884 return STATUS_NO_MEM;
885 sPath.set_length((idx > 0) ? idx - 1 : 0);
886
887 return STATUS_OK;
888 }
889
890 status_t Path::remove_last(Path *path)
891 {
892 return (path != NULL) ? remove_last(&path->sPath) : STATUS_BAD_ARGUMENTS;
893 }
894
895 status_t Path::without_last(char *path, size_t maxlen) const
896 {
897 Path tmp;
898 status_t res = tmp.set(&this->sPath);
899 if (res == STATUS_OK)
900 res = tmp.remove_last();
901 if (res == STATUS_OK)
902 res = tmp.get(path, maxlen);
903 return res;
904 }
905
906 status_t Path::without_last(LSPString *path) const
907 {
908 Path tmp;
909 status_t res = tmp.set(&this->sPath);
910 if (res == STATUS_OK)
911 res = tmp.remove_last();
912 if (res == STATUS_OK)
913 res = tmp.get(path);
914 return res;
915 }
916
917 status_t Path::without_last(Path *path) const
918 {
919 Path tmp;
920 status_t res = tmp.set(&this->sPath);
921 if (res == STATUS_OK)
922 res = tmp.remove_last();
923 if (res == STATUS_OK)
924 res = tmp.get(path);
925 return res;
926 }
927
928 status_t Path::remove_first()
929 {
930 if (is_root())
931 return STATUS_NOT_FOUND;
932
933 ssize_t idx = sPath.index_of(FILE_SEPARATOR_C);
934 if (is_relative())
935 {
936 if (idx < 0)
937 idx = 0;
938 return (sPath.remove(0, idx + 1)) ? STATUS_OK : STATUS_NO_MEM;
939 }
940 else if (idx < 0)
941 return STATUS_NOT_FOUND;
942
943 return (sPath.remove(0, idx + 1)) ? STATUS_OK : STATUS_NO_MEM;
944 }
945
946 status_t Path::remove_first(char *path, size_t maxlen)
947 {
948 if (path == NULL)
949 return STATUS_BAD_ARGUMENTS;
950
951 size_t tail;
952 ssize_t idx = sPath.index_of(FILE_SEPARATOR_C);
953 if (idx < 0)
954 {
955 if (sPath.is_empty())
956 return STATUS_NOT_FOUND;
957 tail = sPath.length();
958 idx = tail;
959 }
960 else
961 {
962 tail = (is_absolute()) ? idx + 1 : idx;
963 idx += 1;
964 }
965
966 const char *utf8 = sPath.get_utf8(0, tail);
967 if (utf8 == NULL)
968 return STATUS_NO_MEM;
969
970 size_t len = ::strlen(utf8);
971 if (len >= maxlen)
972 return STATUS_TOO_BIG;
973
974 ::memcpy(path, utf8, len + 1);
975 sPath.remove(0, idx);
976
977 return STATUS_OK;
978 }
979
980 status_t Path::remove_first(LSPString *path)
981 {
982 if (path == NULL)
983 return STATUS_BAD_ARGUMENTS;
984
985 size_t tail;
986 ssize_t idx = sPath.index_of(FILE_SEPARATOR_C);
987 if (idx < 0)
988 {
989 if (sPath.is_empty())
990 return STATUS_NOT_FOUND;
991 tail = sPath.length();
992 idx = tail;
993 }
994 else
995 {
996 tail = (is_absolute()) ? idx + 1 : idx;
997 idx += 1;
998 }
999
1000 if (!path->set(&sPath, 0, tail))
1001 return STATUS_NO_MEM;
1002
1003 sPath.remove(0, idx);
1004 return STATUS_OK;
1005 }
1006
1007 status_t Path::remove_first(Path *path)
1008 {
1009 return (path != NULL) ? remove_first(&path->sPath) : STATUS_BAD_ARGUMENTS;
1010 }
1011
1012 status_t Path::without_first(char *path, size_t maxlen) const
1013 {
1014 Path tmp;
1015 status_t res = tmp.set(&this->sPath);
1016 if (res == STATUS_OK)
1017 res = tmp.remove_first();
1018 if (res == STATUS_OK)
1019 res = tmp.get(path, maxlen);
1020 return res;
1021 }
1022
1023 status_t Path::without_first(LSPString *path) const
1024 {
1025 Path tmp;
1026 status_t res = tmp.set(&this->sPath);
1027 if (res == STATUS_OK)
1028 res = tmp.remove_first();
1029 if (res == STATUS_OK)
1030 res = tmp.get(path);
1031 return res;
1032 }
1033
1034 status_t Path::without_first(Path *path) const
1035 {
1036 Path tmp;
1037 status_t res = tmp.set(&this->sPath);
1038 if (res == STATUS_OK)
1039 res = tmp.remove_first();
1040 if (res == STATUS_OK)
1041 res = tmp.get(path);
1042 return res;
1043 }
1044
1045 status_t Path::root()
1046 {
1047 if (is_relative())
1048 return STATUS_BAD_STATE;
1049 else if (is_root())
1050 return STATUS_OK;
1051 #if defined(PLATFORM_WINDOWS)
1052 ssize_t idx = sPath.index_of(FILE_SEPARATOR_C);
1053 if (idx < 0)
1054 return STATUS_BAD_STATE;
1055 sPath.set_length(idx+1);
1056 return STATUS_OK;
1057 #else
1058 return (sPath.set(FILE_SEPARATOR_C)) ? STATUS_OK : STATUS_NO_MEM;
1059 #endif
1060 }
1061
1062 status_t Path::remove_root()
1063 {
1064 if (!is_absolute())
1065 return STATUS_OK;
1066
1067 ssize_t idx = sPath.index_of(FILE_SEPARATOR_C);
1068 if (idx < 0)
1069 {
1070 sPath.set_length(0);
1071 return STATUS_OK;
1072 }
1073
1074 return (sPath.remove(idx + 1)) ? STATUS_OK : STATUS_NO_MEM;
1075 }
1076
1077 status_t Path::parent()
1078 {
1079 if (is_root())
1080 return STATUS_OK;
1081 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
1082 if (idx < 0)
1083 idx = 0;
1084 sPath.set_length(idx);
1085 return STATUS_OK;
1086 }
1087
1088 status_t Path::remove_base(const char *path)
1089 {
1090 if (path == NULL)
1091 return STATUS_BAD_ARGUMENTS;
1092
1093 LSPString tmp;
1094 if (!tmp.set_utf8(path))
1095 return STATUS_NO_MEM;
1096 return remove_base(&tmp);
1097 }
1098
1099 status_t Path::remove_base(const LSPString *path)
1100 {
1101 if (path == NULL)
1102 return STATUS_BAD_ARGUMENTS;
1103 if (!sPath.starts_with(path))
1104 return false;
1105 size_t index = path->length(), max = sPath.length();
1106 if (index >= max)
1107 {
1108 sPath.clear();
1109 return STATUS_OK;
1110 }
1111
1112 size_t removed = 0;
1113 while (index < max)
1114 {
1115 if (sPath.char_at(index) != FILE_SEPARATOR_C)
1116 break;
1117 ++removed;
1118 ++index;
1119 }
1120 if (removed <= 0)
1121 return STATUS_INVALID_VALUE;
1122
1123 LSPString tmp;
1124 if (!tmp.set(&sPath, index, max))
1125 return STATUS_NO_MEM;
1126 sPath.swap(&tmp);
1127 return STATUS_OK;
1128 }
1129
1130 status_t Path::remove_base(const Path *path)
1131 {
1132 if (path == NULL)
1133 return STATUS_BAD_ARGUMENTS;
1134 return remove_base(&path->sPath);
1135 }
1136
1137 status_t Path::remove_base()
1138 {
1139 ssize_t idx = sPath.rindex_of(FILE_SEPARATOR_C);
1140 if (idx < 0)
1141 return STATUS_OK;
1142 return (sPath.remove(0, idx + 1)) ? STATUS_OK : STATUS_NO_MEM;
1143 }
1144
1145 bool Path::is_absolute() const
1146 {
1147 if (sPath.length() <= 0)
1148 return false;
1149 #if defined(PLATFORM_WINDOWS)
1150 return !PathIsRelativeW(reinterpret_cast<LPCWSTR>(sPath.get_utf16()));
1151 #else
1152 return (sPath.first() == FILE_SEPARATOR_C);
1153 #endif
1154 }
1155
1156 bool Path::is_relative() const
1157 {
1158 if (sPath.length() <= 0)
1159 return true;
1160 #if defined(PLATFORM_WINDOWS)
1161 return ::PathIsRelativeW(reinterpret_cast<LPCWSTR>(sPath.get_utf16()));
1162 #else
1163 return (sPath.first() != FILE_SEPARATOR_C);
1164 #endif
1165 }
1166
1167 bool Path::is_canonical() const
1168 {
1169 enum state_t
1170 {
1171 S_SEEK,
1172 S_SEPARATOR,
1173 S_DOT,
1174 S_DOTDOT
1175 };
1176
1177 if (is_root())
1178 return true;
1179
1180 lsp_wchar_t c;
1181 size_t len = sPath.length();
1182 const lsp_wchar_t *p = sPath.characters();
1183 const lsp_wchar_t *e = &p[len];
1184 state_t state = S_SEEK;
1185
1186 while (p < e)
1187 {
1188 c = *p++;
1189
1190 switch (state)
1191 {
1192 case S_SEEK:
1193 if (c == FILE_SEPARATOR_C)
1194 state = S_SEPARATOR;
1195 else if (c == '.')
1196 state = S_DOT;
1197 break;
1198 case S_SEPARATOR:
1199 if (c == FILE_SEPARATOR_C)
1200 return false;
1201 else if (c == '.')
1202 state = S_DOT;
1203 else
1204 state = S_SEEK;
1205 break;
1206 case S_DOT:
1207 if (c == FILE_SEPARATOR_C)
1208 return false;
1209 else if (c == '.')
1210 state = S_DOTDOT;
1211 else
1212 state = S_SEEK;
1213 break;
1214 case S_DOTDOT:
1215 if (c == FILE_SEPARATOR_C)
1216 return false;
1217 else
1218 state = S_SEEK;
1219 break;
1220 }
1221 }
1222
1223 return state == S_SEEK;
1224 }
1225
1226 bool Path::is_root() const
1227 {
1228 #if defined(PLATFORM_WINDOWS)
1229 return ::PathIsRootW(reinterpret_cast<LPCWSTR>(sPath.get_utf16()));
1230 #else
1231 return (sPath.length() == 1) &&
1232 (sPath.first() == FILE_SEPARATOR_C);
1233 #endif
1234 }
1235
1236 status_t Path::canonicalize()
1237 {
1238 enum state_t
1239 {
1240 S_SEEK,
1241 S_SEPARATOR,
1242 S_DOT,
1243 S_DOTDOT
1244 };
1245
1246 lsp_wchar_t c;
1247 size_t len = sPath.length();
1248 lsp_wchar_t *path = const_cast<lsp_wchar_t *>(sPath.characters());
1249 lsp_wchar_t *s = path;
1250 lsp_wchar_t *e = &s[len];
1251 state_t state = S_SEEK;
1252
1253 if (is_absolute())
1254 {
1255 while (s < e)
1256 {
1257 if (*(s++) == FILE_SEPARATOR_C)
1258 {
1259 state = S_SEPARATOR;
1260 break;
1261 }
1262 }
1263 if (state != S_SEPARATOR)
1264 return STATUS_OK;
1265 }
1266
1267 lsp_wchar_t *p = s;
1268 lsp_wchar_t *w = s;
1269
1270 while (p < e)
1271 {
1272 c = *p++;
1273
1274 switch (state)
1275 {
1276 case S_SEEK:
1277 if (c == FILE_SEPARATOR_C)
1278 {
1279 state = S_SEPARATOR;
1280 *w++ = c;
1281 }
1282 else if (c == '.')
1283 state = S_DOT;
1284 else
1285 *w++ = c;
1286 break;
1287 case S_SEPARATOR:
1288 if (c == FILE_SEPARATOR_C)
1289 break;
1290 else if (c == '.')
1291 state = S_DOT;
1292 else
1293 {
1294 *w++ = c;
1295 state = S_SEEK;
1296 }
1297 break;
1298 case S_DOT:
1299 if (c == FILE_SEPARATOR_C)
1300 state = S_SEPARATOR;
1301 else if (c == '.')
1302 state = S_DOTDOT;
1303 else
1304 {
1305 *w++ = '.';
1306 *w++ = c;
1307 state = S_SEEK;
1308 }
1309 break;
1310 case S_DOTDOT:
1311 if (c == FILE_SEPARATOR_C)
1312 {
1313 state = S_SEPARATOR;
1314 if (w > s)
1315 {
1316 do // Roll-back path
1317 {
1318 --w;
1319 }
1320 while ((w > path) && (w[-1] != FILE_SEPARATOR_C));
1321 }
1322 }
1323 else
1324 {
1325 *w++ = '.';
1326 *w++ = '.';
1327 *w++ = c;
1328 state = S_SEEK;
1329 }
1330 break;
1331 }
1332 }
1333
1334 while ((w > s) && (w[-1] == FILE_SEPARATOR_C))
1335 --w;
1336
1337 sPath.set_length(w - path);
1338
1339 return STATUS_OK;
1340 }
1341
1342 status_t Path::get_canonical(char *path, size_t maxlen) const
1343 {
1344 if (path == NULL)
1345 return STATUS_BAD_ARGUMENTS;
1346 Path tmp;
1347 status_t res = tmp.set(&sPath);
1348 if (res == STATUS_OK)
1349 res = tmp.canonicalize();
1350 if (res == STATUS_OK)
1351 res = tmp.get(path, maxlen);
1352 return res;
1353 }
1354
1355 status_t Path::get_canonical(LSPString *path) const
1356 {
1357 if (path == NULL)
1358 return STATUS_BAD_ARGUMENTS;
1359 Path tmp;
1360 status_t res = tmp.set(&sPath);
1361 if (res == STATUS_OK)
1362 res = tmp.canonicalize();
1363 if (res == STATUS_OK)
1364 tmp.sPath.swap(path);
1365 return res;
1366 }
1367
1368 status_t Path::get_canonical(Path *path) const
1369 {
1370 if (path == NULL)
1371 return STATUS_BAD_ARGUMENTS;
1372 Path tmp;
1373 status_t res = tmp.set(&sPath);
1374 if (res == STATUS_OK)
1375 res = tmp.canonicalize();
1376 if (res == STATUS_OK)
1377 tmp.swap(path);
1378 return res;
1379 }
1380
1381 bool Path::equals(const Path *path) const
1382 {
1383 return (path != NULL) ? sPath.equals(&path->sPath) : false;
1384 }
1385
1386 bool Path::equals(const LSPString *path) const
1387 {
1388 return (path != NULL) ? sPath.equals(path) : false;
1389 }
1390
1391 bool Path::equals(const char *path) const
1392 {
1393 if (path == NULL)
1394 return false;
1395
1396 LSPString tmp;
1397 return (tmp.set_utf8(path)) ? tmp.equals(&sPath) : false;
1398 }
1399
1400 status_t Path::stat(fattr_t *attr) const
1401 {
1402 return File::stat(&sPath, attr);
1403 }
1404
1405 status_t Path::sym_stat(fattr_t *attr) const
1406 {
1407 return File::sym_stat(&sPath, attr);
1408 }
1409
1410 void Path::drain(LSPString *dst)
1411 {
1412 dst->take(&sPath);
1413 }
1414
1415 status_t Path::to_final_path()
1416 {
1417 Path tmp;
1418 status_t res = final_path(&tmp);
1419 if (res == STATUS_OK)
1420 sPath.swap(tmp.sPath);
1421 return res;
1422 }
1423
1424 status_t Path::final_path(LSPString *path) const
1425 {
1426 if (path == NULL)
1427 return STATUS_INVALID_VALUE;
1428
1429 Path fpath;
1430 status_t res = final_path(&fpath);
1431 if (res == STATUS_OK)
1432 fpath.drain(path);
1433 return res;
1434 }
1435
1436 status_t Path::final_path(Path *path) const
1437 {
1438 if (path == NULL)
1439 return STATUS_INVALID_VALUE;
1440
1441 #ifdef PLATFORM_WINDOWS
1442 #if _WIN32_WINNT >= 0x0600
1443 // Try GetFinalPathNameByHandleW
1444 HANDLE hfile = ::CreateFileW(
1445 sPath.get_utf16(), // lpFileName
1446 0, // dwDesiredAccess
1447 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, // dwShareMode
1448 NULL, // lpSecurityAttributes
1449 OPEN_EXISTING, // dwCreationDisposition
1450 FILE_ATTRIBUTE_NORMAL, // dwFlagsAndAttributes
1451 NULL); // hTemplateFile
1452
1453 if (hfile != INVALID_HANDLE_VALUE)
1454 {
1455 lsp_finally {
1456 ::CloseHandle(hfile);
1457 };
1458
1459 DWORD path_size = ::GetFinalPathNameByHandleW(
1460 hfile, // hFile
1461 NULL, // lpszFilePath
1462 0, // cchFilePath
1463 FILE_NAME_NORMALIZED); // dwFlags
1464
1465 if (path_size > 0)
1466 {
1467 // Allocate buffer to store UTF-16 path
1468 WCHAR *buf = static_cast<WCHAR *>(malloc((path_size + 1) * sizeof(WCHAR)));
1469 if (buf == NULL)
1470 return STATUS_NO_MEM;
1471 lsp_finally {
1472 free(buf);
1473 };
1474
1475 // Obtain the value
1476 path_size = ::GetFinalPathNameByHandleW(
1477 hfile, // hFile
1478 buf, // lpszFilePath
1479 path_size, // cchFilePath
1480 FILE_NAME_NORMALIZED); // dwFlags
1481
1482 if (path_size > 0)
1483 {
1484 buf[path_size] = 0;
1485
1486 // Remove long path name prefix if it is present
1487 const WCHAR *sbuf = buf;
1488 if ((sbuf[0] == '\\') && (sbuf[1] == '\\') && (sbuf[2] == '?') && (sbuf[3] == '\\'))
1489 sbuf += 4;
1490
1491 return path->set_native(reinterpret_cast<const char *>(sbuf), "UTF-16");
1492 }
1493 }
1494 }
1495 #endif /* _WIN32_WINNT */
1496
1497 return path->set(this);
1498 #else
1499 status_t res;
1500 fattr_t attr;
1501 Path tmp, link;
1502 char *buf = NULL;
1503 lsp_finally {
1504 if (buf != NULL)
1505 free(buf);
1506 };
1507
1508 lltl::phashset<Path> visited;
1509 lsp_finally {
1510 for (lltl::iterator<Path> it = visited.values(); it; ++it)
1511 {
1512 Path *p = it.get();
1513 if (p != NULL)
1514 delete p;
1515 }
1516 visited.flush();
1517 };
1518
1519 // Initialize path with self value
1520 if ((res = tmp.set(this)) != STATUS_OK)
1521 return res;
1522
1523 while (true)
1524 {
1525 lsp_trace("File::stat path=%s", tmp.as_native());
1526
1527 // Read file attributes
1528 if ((res = File::stat(&tmp, &attr)) != STATUS_OK)
1529 {
1530 lsp_trace("Failed File::stat: error=%d", int(res));
1531 return res;
1532 }
1533
1534 // We're done if it is not a symbolic link
1535 if (attr.type != fattr_t::FT_SYMLINK)
1536 {
1537 lsp_trace("Not a symbolic link (type=%d), returning path=%s", int(attr.type), tmp.as_native());
1538 tmp.swap(path);
1539 return STATUS_OK;
1540 }
1541
1542 // Allocate buffer in lazy mode
1543 if (buf == NULL)
1544 {
1545 buf = static_cast<char *>(malloc(PATH_MAX * sizeof(char)));
1546 if (buf == NULL)
1547 return STATUS_NO_MEM;
1548 }
1549
1550 // Read the link to the file
1551 ssize_t count = readlink(tmp.as_native(), buf, PATH_MAX - 1);
1552 if (count < 0)
1553 {
1554 const int error = errno;
1555
1556 lsp_trace("readlink for %s failed: errno=%d", tmp.as_native(), int(error));
1557
1558 switch (error)
1559 {
1560 case EFAULT: return STATUS_FAILED;
1561 case EINVAL: return STATUS_INVALID_VALUE;
1562 case EIO: return STATUS_IO_ERROR;
1563 case ENAMETOOLONG: return STATUS_TOO_BIG;
1564 case ELOOP: return STATUS_OVERFLOW;
1565 case ENOENT: return STATUS_NOT_FOUND;
1566 case ENOMEM: return STATUS_NO_MEM;
1567 case ENOTDIR: return STATUS_NOT_DIRECTORY;
1568 default:
1569 break;
1570 }
1571
1572 return STATUS_IO_ERROR;
1573 }
1574 buf[count] = '\0';
1575
1576 lsp_trace("readlink returned: %s", buf);
1577
1578 // Initialize link path
1579 if ((res = link.set_native(buf)) != STATUS_OK)
1580 {
1581 lsp_trace("failed set_native for %s failed: result=%d", tmp.as_native(), int(res));
1582 return res;
1583 }
1584
1585 // Analyze link path: if it is relative symbolic link or not
1586 if (link.is_relative())
1587 {
1588 // We need to glue relative path to the current path
1589 if ((res = tmp.remove_last()) != STATUS_OK)
1590 return res;
1591 if ((res = tmp.append_child(&link)) != STATUS_OK)
1592 return res;
1593 lsp_trace("Link is relative, new path set to: %s", tmp.as_native());
1594 }
1595 else
1596 tmp.swap(&link);
1597 if ((res = tmp.canonicalize()) != STATUS_OK)
1598 return res;
1599 lsp_trace("Canonicalized path: %s", tmp.as_native());
1600
1601 // Test if we already have visited such path and remember it
1602 if (visited.contains(&tmp))
1603 {
1604 lsp_trace("Loop detected for path: %s", tmp.as_native());
1605 return STATUS_OVERFLOW;
1606 }
1607 Path *clone = tmp.clone();
1608 if (clone == NULL)
1609 {
1610 lsp_trace("Failed to clone path: %s", tmp.as_native());
1611 return STATUS_NO_MEM;
1612 }
1613 if (!visited.create(clone))
1614 {
1615 lsp_trace("Failed to register visited path: %s", tmp.as_native());
1616 delete clone;
1617 return STATUS_NO_MEM;
1618 }
1619 lsp_trace("Registered visited path: %s", tmp.as_native());
1620 }
1621 #endif /* PLATFORM_WINDOWS */
1622 }
1623
1624 wssize_t Path::size() const
1625 {
1626 fattr_t attr;
1627 status_t res = File::stat(&sPath, &attr);
1628 return (res == STATUS_OK) ? attr.size : -res;
1629 }
1630
1631 bool Path::exists() const
1632 {
1633 fattr_t attr;
1634 status_t res = File::stat(&sPath, &attr);
1635 return res == STATUS_OK;
1636 }
1637
1638 bool Path::is_reg() const
1639 {
1640 fattr_t attr;
1641 status_t res = File::sym_stat(&sPath, &attr);
1642 return (res == STATUS_OK) && (attr.type == fattr_t::FT_REGULAR);
1643 }
1644
1645 bool Path::is_dir() const
1646 {
1647 fattr_t attr;
1648 status_t res = File::sym_stat(&sPath, &attr);
1649 return (res == STATUS_OK) && (attr.type == fattr_t::FT_DIRECTORY);
1650 }
1651
1652 bool Path::is_block_dev() const
1653 {
1654 fattr_t attr;
1655 status_t res = File::sym_stat(&sPath, &attr);
1656 return (res == STATUS_OK) && (attr.type == fattr_t::FT_BLOCK);
1657 }
1658
1659 bool Path::is_char_dev() const
1660 {
1661 fattr_t attr;
1662 status_t res = File::sym_stat(&sPath, &attr);
1663 return (res == STATUS_OK) && (attr.type == fattr_t::FT_CHARACTER);
1664 }
1665
1666 bool Path::is_fifo() const
1667 {
1668 fattr_t attr;
1669 status_t res = File::sym_stat(&sPath, &attr);
1670 return (res == STATUS_OK) && (attr.type == fattr_t::FT_FIFO);
1671 }
1672
1673 bool Path::is_symlink() const
1674 {
1675 fattr_t attr;
1676 status_t res = File::stat(&sPath, &attr);
1677 return (res == STATUS_OK) && (attr.type == fattr_t::FT_SYMLINK);
1678 }
1679
1680 bool Path::is_socket() const
1681 {
1682 fattr_t attr;
1683 status_t res = File::sym_stat(&sPath, &attr);
1684 return (res == STATUS_OK) && (attr.type == fattr_t::FT_SOCKET);
1685 }
1686
1687 status_t Path::mkdir() const
1688 {
1689 return Dir::create(&sPath);
1690 }
1691
1692 status_t Path::mkdir(bool recursive) const
1693 {
1694 // Try to create directory
1695 status_t res = Dir::create(&sPath);
1696 if ((res == STATUS_OK) || (!recursive))
1697 return res;
1698
1699 // No success?
1700 // First, canonicalize path
1701 Path path;
1702 path.set(this);
1703 res = path.canonicalize();
1704 if (res != STATUS_OK)
1705 return res;
1706
1707 // Prepare the loop
1708 LSPString tmp;
1709 ssize_t off = path.sPath.index_of(0, FILE_SEPARATOR_C);
1710 if (off < 0)
1711 return STATUS_INVALID_VALUE;
1712 else if (path.is_absolute())
1713 {
1714 off = path.sPath.index_of(off+1, FILE_SEPARATOR_C);
1715 if (off < 0) // Tried to create root directory?
1716 return STATUS_OK;
1717 }
1718
1719 // Perform iterative directory creation
1720 while (off >= 0)
1721 {
1722 if (!tmp.set(&path.sPath, 0, off))
1723 return STATUS_NO_MEM;
1724
1725 res = Dir::create(&tmp);
1726 if (res != STATUS_OK)
1727 return res;
1728
1729 // Lookup for next separator
1730 off = path.sPath.index_of(off+1, FILE_SEPARATOR_C);
1731 }
1732
1733 return Dir::create(&sPath);
1734 }
1735
1736 status_t Path::mkparent() const
1737 {
1738 io::Path tmp;
1739 status_t res;
1740 if ((res = tmp.set(this)) != STATUS_OK)
1741 return res;
1742 if ((res = tmp.remove_last()) != STATUS_OK)
1743 return res;
1744
1745 return tmp.mkdir();
1746 }
1747
1748 status_t Path::mkparent(bool recursive) const
1749 {
1750 io::Path tmp;
1751 status_t res;
1752 if ((res = tmp.set(this)) != STATUS_OK)
1753 return res;
1754 if ((res = tmp.remove_last()) != STATUS_OK)
1755 return res;
1756
1757 return tmp.mkdir(recursive);
1758 }
1759
1760 status_t Path::remove() const
1761 {
1762 status_t res = File::remove(&sPath);
1763 if (res == STATUS_IS_DIRECTORY)
1764 res = Dir::remove(&sPath);
1765 return (res == STATUS_NOT_DIRECTORY) ? STATUS_IO_ERROR : res;
1766 }
1767
1768 status_t Path::rename(const char *dst) const
1769 {
1770 return io::File::rename(&sPath, dst);
1771 }
1772
1773 status_t Path::rename(const LSPString *dst) const
1774 {
1775 return io::File::rename(&sPath, dst);
1776 }
1777
1778 status_t Path::rename(const io::Path *dst) const
1779 {
1780 return io::File::rename(&sPath, dst);
1781 }
1782
1783 void Path::take(LSPString *src)
1784 {
1785 sPath.take(src);
1786 fixup_path();
1787 }
1788
1789 status_t Path::current()
1790 {
1791 status_t res = Dir::get_current(&sPath);
1792 if (res == STATUS_OK)
1793 fixup_path();
1794 return res;
1795 }
1796
1797 ssize_t Path::fmt(const char *fmt...)
1798 {
1799 va_list list;
1800 va_start(list, fmt);
1801 ssize_t res = sPath.vfmt_utf8(fmt, list);
1802 va_end(list);
1803 if (res > 0)
1804 fixup_path();
1805 return res;
1806 }
1807
1808 ssize_t Path::fmt(const LSPString *fmt...)
1809 {
1810 va_list list;
1811 va_start(list, fmt);
1812 ssize_t res = sPath.vfmt_utf8(fmt->get_utf8(), list);
1813 va_end(list);
1814 if (res > 0)
1815 fixup_path();
1816 return res;
1817 }
1818
1819 ssize_t Path::vfmt(const char *fmt, va_list args)
1820 {
1821 ssize_t res = sPath.vfmt_utf8(fmt, args);
1822 if (res > 0)
1823 fixup_path();
1824 return res;
1825 }
1826
1827 ssize_t Path::vfmt(const LSPString *fmt, va_list args)
1828 {
1829 ssize_t res = sPath.vfmt_utf8(fmt->get_utf8(), args);
1830 if (res > 0)
1831 fixup_path();
1832 return res;
1833 }
1834
1835
1836 bool Path::is_dot() const
1837 {
1838 size_t len = sPath.length();
1839 if (len < 1)
1840 return false;
1841
1842 const lsp_wchar_t *wc = sPath.characters();
1843 if (len == 1)
1844 return wc[0] == '.';
1845
1846 return (wc[len-2] == FILE_SEPARATOR_C) &&
1847 (wc[len-1] == '.');
1848 }
1849
1850 bool Path::is_dot(const io::Path *path)
1851 {
1852 return (path != NULL) && (path->is_dot());
1853 }
1854
1855 bool Path::is_dot(const LSPString *path)
1856 {
1857 if (path == NULL)
1858 return false;
1859
1860 size_t len = path->length();
1861 if (len < 1)
1862 return false;
1863
1864 const lsp_wchar_t *wc = path->characters();
1865 if (len == 1)
1866 return wc[0] == '.';
1867
1868 return (wc[len-2] == FILE_SEPARATOR_C) &&
1869 (wc[len-1] == '.');
1870 }
1871
1872 bool Path::is_dot(const char *path)
1873 {
1874 if (path == NULL)
1875 return false;
1876
1877 ssize_t len = strlen(path);
1878 if (len < 1)
1879 return false;
1880 else if (len == 1)
1881 return path[0] == '.';
1882
1883 path = &path[len - 2];
1884 return (path[0] == FILE_SEPARATOR_C) &&
1885 (path[1] == '.');
1886 }
1887
1888 bool Path::is_dotdot() const
1889 {
1890 size_t len = sPath.length();
1891 if (len < 2)
1892 return false;
1893
1894 const lsp_wchar_t *wc = sPath.characters();
1895 if (len == 2)
1896 return (wc[0] == '.') &&
1897 (wc[1] == '.');
1898
1899 return (wc[len-3] == FILE_SEPARATOR_C) &&
1900 (wc[len-2] == '.') &&
1901 (wc[len-1] == '.');
1902 }
1903
1904 bool Path::is_dotdot(const io::Path *path)
1905 {
1906 return (path != NULL) && (path->is_dotdot());
1907 }
1908
1909 bool Path::is_dotdot(const LSPString *path)
1910 {
1911 if (path == NULL)
1912 return false;
1913
1914 size_t len = path->length();
1915 if (len < 2)
1916 return false;
1917
1918 const lsp_wchar_t *wc = path->characters();
1919 if (len == 2)
1920 return (wc[0] == '.') &&
1921 (wc[1] == '.');
1922
1923 return (wc[len-3] == FILE_SEPARATOR_C) &&
1924 (wc[len-2] == '.') &&
1925 (wc[len-1] == '.');
1926 }
1927
1928 bool Path::is_dotdot(const char *path)
1929 {
1930 if (path == NULL)
1931 return false;
1932
1933 ssize_t len = strlen(path);
1934 if (len < 2)
1935 return false;
1936 else if (len == 2)
1937 return (path[0] == '.') &&
1938 (path[1] == '.');
1939
1940 return (path[len-3] == FILE_SEPARATOR_C) &&
1941 (path[len-2] == '.') &&
1942 (path[len-1] == '.');
1943 }
1944
1945 bool Path::is_dots() const
1946 {
1947 ssize_t len = sPath.length();
1948 if ((len--) <= 0)
1949 return false;
1950
1951 const lsp_wchar_t *wc = sPath.characters();
1952 if (wc[len] != '.')
1953 return false;
1954 if ((len--) <= 0)
1955 return true;
1956
1957 if (wc[len] == FILE_SEPARATOR_C)
1958 return true;
1959 else if (wc[len] != '.')
1960 return false;
1961 if ((len--) <= 0)
1962 return true;
1963
1964 return wc[len] == FILE_SEPARATOR_C;
1965 }
1966
1967 bool Path::is_dots(const Path *path)
1968 {
1969 return (path != NULL) && (path->is_dots());
1970 }
1971
1972 bool Path::is_dots(const char *path)
1973 {
1974 if (path == NULL)
1975 return false;
1976
1977 ssize_t len = strlen(path);
1978 if ((len--) <= 0)
1979 return false;
1980
1981 if (path[len] != '.')
1982 return false;
1983 if ((len--) <= 0)
1984 return true;
1985
1986 if (path[len] == FILE_SEPARATOR_C)
1987 return true;
1988 else if (path[len] != '.')
1989 return false;
1990 if ((len--) <= 0)
1991 return true;
1992
1993 return path[len] == FILE_SEPARATOR_C;
1994 }
1995
1996 bool Path::is_dots(const LSPString *path)
1997 {
1998 if (path == NULL)
1999 return false;
2000
2001 ssize_t len = path->length();
2002 if ((len--) <= 0)
2003 return false;
2004
2005 const lsp_wchar_t *wc = path->characters();
2006 if (wc[len] != '.')
2007 return false;
2008 if ((len--) <= 0)
2009 return true;
2010
2011 if (wc[len] == FILE_SEPARATOR_C)
2012 return true;
2013 else if (wc[len] != '.')
2014 return false;
2015 if ((len--) <= 0)
2016 return true;
2017
2018 return wc[len] == FILE_SEPARATOR_C;
2019 }
2020
2021 bool Path::valid_file_name(const LSPString *fname)
2022 {
2023 if (fname == NULL)
2024 return false;
2025 size_t n = fname->length();
2026 if (n <= 0)
2027 return false;
2028
2029 const lsp_wchar_t *chars = fname->characters();
2030 for (size_t i=0; i<n; ++i)
2031 {
2032 lsp_wchar_t ch = *(chars++);
2033 if ((ch == '*') || (ch == '?'))
2034 return false;
2035 if ((ch == FILE_SEPARATOR_C) || (ch == '\0'))
2036 return false;
2037 #ifdef PLATFORM_WINDOWS
2038 if ((ch == '/') || (ch == ':') || (ch== '|') || (ch == '<') || (ch == '>'))
2039 return false;
2040 #endif /* PLATFORM_WINDOWS */
2041 }
2042
2043 return true;
2044 }
2045
2046 bool Path::valid_path_name(const LSPString *fname)
2047 {
2048 if (fname == NULL)
2049 return false;
2050 size_t n = fname->length();
2051 if (n <= 0)
2052 return false;
2053
2054 const lsp_wchar_t *chars = fname->characters();
2055 for (size_t i=0; i<n; ++i)
2056 {
2057 lsp_wchar_t ch = *(chars++);
2058 if ((ch == '*') || (ch == '?'))
2059 return false;
2060 if (ch == '\0')
2061 return false;
2062 #ifdef PLATFORM_WINDOWS
2063 if ((ch == '/') || (ch== '|') || (ch == '<') || (ch == '>'))
2064 return false;
2065 #endif /* PLATFORM_WINDOWS */
2066 }
2067
2068 #ifdef PLATFORM_WINDOWS
2069 ssize_t semicolon = fname->index_of(':');
2070 if (semicolon > 0)
2071 {
2072 ssize_t next = fname->index_of(semicolon + 1, ':');
2073 if (next >= 0)
2074 return false;
2075 if (size_t(semicolon + 1) < fname->length()) // Should be "?:\"
2076 {
2077 if (fname->char_at(semicolon + 1) != FILE_SEPARATOR_C)
2078 return false;
2079 }
2080
2081 // Check disk name
2082 const lsp_wchar_t *chars = fname->characters();
2083 while ((semicolon--) > 0)
2084 {
2085 lsp_wchar_t ch = *(chars++);
2086 if ((ch >= 'a') && (ch <= 'z'))
2087 continue;
2088 if ((ch >= 'A') && (ch <= 'Z'))
2089 continue;
2090 return false;
2091 }
2092 }
2093 else if (semicolon == 0)
2094 return false;
2095 #endif /* PLATFORM_WINDOWS */
2096
2097 return true;
2098 }
2099
2100 status_t Path::as_relative(const char *path)
2101 {
2102 status_t res;
2103 io::Path child, base;
2104 if ((res = base.set(path)) != STATUS_OK)
2105 return res;
2106 if ((res = child.set(this)) != STATUS_OK)
2107 return res;
2108
2109 if ((res = child.compute_relative(&base)) == STATUS_OK)
2110 sPath.swap(&child.sPath);
2111 return res;
2112 }
2113
2114 status_t Path::as_relative(const LSPString *path)
2115 {
2116 status_t res;
2117 io::Path child, base;
2118 if ((res = base.set(path)) != STATUS_OK)
2119 return res;
2120 if ((res = child.set(this)) != STATUS_OK)
2121 return res;
2122
2123 if ((res = child.compute_relative(&base)) == STATUS_OK)
2124 sPath.swap(&child.sPath);
2125 return res;
2126 }
2127
2128 status_t Path::as_relative(const Path *path)
2129 {
2130 status_t res;
2131 io::Path child, base;
2132 if ((res = base.set(path)) != STATUS_OK)
2133 return res;
2134 if ((res = child.set(this)) != STATUS_OK)
2135 return res;
2136
2137 if ((res = child.compute_relative(&base)) == STATUS_OK)
2138 sPath.swap(&child.sPath);
2139 return res;
2140 }
2141
2142 status_t Path::compute_relative(Path *base)
2143 {
2144 // Canonicalize both paths
2145 status_t res;
2146 if ((res = canonicalize()) != STATUS_OK)
2147 return res;
2148 if ((res = base->canonicalize()) != STATUS_OK)
2149 return res;
2150
2151 // Check that beginning of paths matches
2152 ssize_t matched = sPath.match(&base->sPath);
2153 if (matched <= 0)
2154 return STATUS_NOT_FOUND;
2155
2156 // The length matches the base file?
2157 ssize_t idx1, idx2;
2158 if (matched == ssize_t(base->length()))
2159 {
2160 if (sPath.length() == base->length())
2161 {
2162 // Lengths match - clear
2163 sPath.clear();
2164 return STATUS_OK;
2165 }
2166 else if (sPath.char_at(matched) == FILE_SEPARATOR_C)
2167 {
2168 // Just remove file base
2169 sPath.remove(0, matched+1);
2170 return STATUS_OK;
2171 }
2172
2173 // Find last matchind file separator
2174 idx1 = sPath.rindex_of(matched, FILE_SEPARATOR_C);
2175 idx2 = base->sPath.rindex_of(matched, FILE_SEPARATOR_C);
2176 if ((idx1 < 0) || (idx2 != idx1))
2177 return STATUS_NOT_FOUND;
2178 }
2179 else if (matched == ssize_t(sPath.length()))
2180 {
2181 if (base->sPath.char_at(matched) != FILE_SEPARATOR_C)
2182 return STATUS_NOT_FOUND;
2183
2184 // All is OK, we're just at the end of the child path
2185 idx1 = matched - 1;
2186 idx2 = matched;
2187 }
2188 else
2189 {
2190 // Special case when the match ended up with file separator
2191 // in one or both path names.
2192 if (sPath.char_at(matched) == FILE_SEPARATOR_C)
2193 {
2194 if (base->sPath.char_at(matched) != FILE_SEPARATOR_C)
2195 --matched;
2196 }
2197 else if (base->sPath.char_at(matched) == FILE_SEPARATOR_C)
2198 --matched;
2199 if (matched < 0)
2200 return STATUS_NOT_FOUND;
2201
2202 // Find last matching file separator
2203 idx1 = sPath.rindex_of(matched, FILE_SEPARATOR_C);
2204 idx2 = base->sPath.rindex_of(matched, FILE_SEPARATOR_C);
2205 if ((idx1 < 0) || (idx2 != idx1))
2206 return STATUS_NOT_FOUND;
2207 }
2208
2209 // Add the necessary amount of '../' references
2210 LSPString tmp;
2211 while (true)
2212 {
2213 idx2 = base->sPath.index_of(idx2 + 1, FILE_SEPARATOR_C);
2214 if (!tmp.append_ascii(".." FILE_SEPARATOR_S))
2215 return STATUS_NO_MEM;
2216 if (idx2 < 0)
2217 break;
2218 }
2219
2220 // Append the rest path
2221 if (!tmp.append(&sPath, idx1 + 1))
2222 return STATUS_NO_MEM;
2223 // Remove the trailing '/' character if present
2224 if (tmp.ends_with(FILE_SEPARATOR_C))
2225 tmp.remove_last();
2226
2227 sPath.swap(&tmp);
2228
2229 return STATUS_OK;
2230 }
2231 } /* namespace io */
2232
2233 namespace lltl
2234 {
2235 size_t hash_spec<io::Path>::hash_func(const void *ptr, size_t size)
2236 {
2237 return (static_cast<const io::Path *>(ptr))->hash();
2238 }
2239
2240 ssize_t compare_spec<io::Path>::cmp_func(const void *a, const void *b, size_t size)
2241 {
2242 const io::Path *sa = static_cast<const io::Path *>(a);
2243 const io::Path *sb = static_cast<const io::Path *>(b);
2244 return sa->compare_to(sb);
2245 }
2246
2247 void *allocator_spec<io::Path>::clone_func(const void *src, size_t size)
2248 {
2249 return (static_cast<const io::Path *>(src))->clone();
2250 }
2251
2252 void allocator_spec<io::Path>::free_func(void *ptr)
2253 {
2254 delete (static_cast<io::Path *>(ptr));
2255 }
2256 } /* namespace lltl */
2257 } /* namespace lsp */