Debian Codegraph @

modules/lsp-runtime-lib/src/main/io/File.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: 6 мар. 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/io/File.h>
23 #include <lsp-plug.in/io/NativeFile.h>
24 #include <lsp-plug.in/common/debug.h>
25 #include <lsp-plug.in/stdlib/stdio.h>
26
27 #ifdef PLATFORM_WINDOWS
28 #include <windows.h>
29 #include <fileapi.h>
30 #include <io.h>
31 #include <winbase.h>
32 #else
33 #include <sys/stat.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #endif /* PLATFORM_WINDOWS */
38
39 namespace lsp
40 {
41 namespace io
42 {
43
44 #ifdef PLATFORM_WINDOWS
45 template <typename S>
46 static inline void decode_file_type(fattr_t *attr, const S *hfi)
47 {
48 attr->type = fattr_t::FT_REGULAR;
49 if (hfi->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
50 attr->type = fattr_t::FT_DIRECTORY;
51 else if (hfi->dwFileAttributes & FILE_ATTRIBUTE_DEVICE)
52 attr->type = fattr_t::FT_BLOCK;
53
54 attr->blk_size = 4096;
55 attr->size = (wsize_t(hfi->nFileSizeHigh) << 32) | hfi->nFileSizeLow;
56 attr->inode = 0;
57 attr->ctime = ((wsize_t(hfi->ftCreationTime.dwHighDateTime) << 32) | hfi->ftCreationTime.dwLowDateTime) / 10000;
58 attr->mtime = ((wsize_t(hfi->ftLastWriteTime.dwHighDateTime) << 32) | hfi->ftLastWriteTime.dwLowDateTime) / 10000;
59 attr->atime = ((wsize_t(hfi->ftLastAccessTime.dwHighDateTime) << 32) | hfi->ftLastAccessTime.dwLowDateTime) / 10000;
60 }
61 #else
62 static inline void decode_file_type(fattr_t *attr, const struct stat *sb)
63 {
64 // Decode file type
65 switch (sb->st_mode & S_IFMT) {
66 case S_IFBLK: attr->type = fattr_t::FT_BLOCK; break;
67 case S_IFCHR: attr->type = fattr_t::FT_CHARACTER; break;
68 case S_IFDIR: attr->type = fattr_t::FT_DIRECTORY; break;
69 case S_IFIFO: attr->type = fattr_t::FT_FIFO; break;
70 case S_IFLNK: attr->type = fattr_t::FT_SYMLINK; break;
71 case S_IFREG: attr->type = fattr_t::FT_REGULAR; break;
72 case S_IFSOCK: attr->type = fattr_t::FT_SOCKET; break;
73 default: attr->type = fattr_t::FT_UNKNOWN; break;
74 }
75
76 attr->blk_size = sb->st_blksize;
77 attr->size = sb->st_size;
78 attr->inode = sb->st_ino;
79
80 // In C headers st_atime, st_mtime and st_ctime macros are defined for backward comparibility
81 // to provide access to field tv_sec of st_atim, st_mtim and st_ctim structures.
82 // In MacOS, these fields have another names: st_atimespec, st_ctimespec and st_mtimespec
83 #if defined(PLATFORM_MACOSX)
84 attr->ctime = (sb->st_ctimespec.tv_sec * 1000LL) + (sb->st_ctimespec.tv_nsec / 1000000);
85 attr->mtime = (sb->st_mtimespec.tv_sec * 1000LL) + (sb->st_mtimespec.tv_nsec / 1000000);
86 attr->atime = (sb->st_atimespec.tv_sec * 1000LL) + (sb->st_atimespec.tv_nsec / 1000000);
87 #elif defined(st_ctime) || defined(st_mtime) || defined(st_atime)
88 attr->ctime = (sb->st_ctim.tv_sec * 1000LL) + (sb->st_ctim.tv_nsec / 1000000);
89 attr->mtime = (sb->st_mtim.tv_sec * 1000LL) + (sb->st_mtim.tv_nsec / 1000000);
90 attr->atime = (sb->st_atim.tv_sec * 1000LL) + (sb->st_atim.tv_nsec / 1000000);
91 #else
92 attr->ctime = sb->st_ctime * 1000LL;
93 attr->mtime = sb->st_mtime * 1000LL;
94 attr->atime = sb->st_atime * 1000LL;
95 #endif
96 }
97 #endif /* PLATFORM_POSIX */
98
99 File::File()
100 {
101 nErrorCode = STATUS_OK;
102 }
103
104 File::~File()
105 {
106 }
107
108 ssize_t File::read(void *dst, size_t count)
109 {
110 return -set_error(STATUS_NOT_SUPPORTED);
111 }
112
113 ssize_t File::pread(wsize_t pos, void *dst, size_t count)
114 {
115 return -set_error(STATUS_NOT_SUPPORTED);
116 }
117
118 ssize_t File::write(const void *src, size_t count)
119 {
120 return -set_error(STATUS_NOT_SUPPORTED);
121 }
122
123 ssize_t File::pwrite(wsize_t pos, const void *src, size_t count)
124 {
125 return -set_error(STATUS_NOT_SUPPORTED);
126 }
127
128 status_t File::seek(wssize_t pos, size_t type)
129 {
130 return set_error(STATUS_NOT_SUPPORTED);
131 }
132
133 wssize_t File::position()
134 {
135 return -set_error(STATUS_NOT_SUPPORTED);
136 }
137
138 wssize_t File::size()
139 {
140 return -set_error(STATUS_NOT_SUPPORTED);
141 }
142
143 status_t File::stat(fattr_t *attr)
144 {
145 return -set_error(STATUS_NOT_SUPPORTED);
146 }
147
148 status_t File::truncate(wsize_t length)
149 {
150 return set_error(STATUS_NOT_SUPPORTED);
151 }
152
153 status_t File::flush()
154 {
155 return set_error(STATUS_NOT_SUPPORTED);
156 }
157
158 status_t File::sync()
159 {
160 return set_error(STATUS_NOT_SUPPORTED);
161 }
162
163 status_t File::close()
164 {
165 return set_error(STATUS_OK);
166 }
167
168 status_t File::stat(const char *path, fattr_t *attr)
169 {
170 if ((path == NULL) || (attr == NULL))
171 return STATUS_BAD_ARGUMENTS;
172
173 LSPString spath;
174 if (!spath.set_utf8(path))
175 return STATUS_NO_MEM;
176 return stat(&spath, attr);
177 }
178
179
180 status_t File::stat(const Path *path, fattr_t *attr)
181 {
182 if ((path == NULL) || (attr == NULL))
183 return STATUS_BAD_ARGUMENTS;
184 return stat(path->as_string(), attr);
185 }
186
187 status_t File::sym_stat(const char *path, fattr_t *attr)
188 {
189 if ((path == NULL) || (attr == NULL))
190 return STATUS_BAD_ARGUMENTS;
191
192 LSPString spath;
193 if (!spath.set_utf8(path))
194 return STATUS_NO_MEM;
195 return sym_stat(&spath, attr);
196 }
197
198 status_t File::sym_stat(const Path *path, fattr_t *attr)
199 {
200 if ((path == NULL) || (attr == NULL))
201 return STATUS_BAD_ARGUMENTS;
202 return sym_stat(path->as_string(), attr);
203 }
204
205 status_t File::stat(FILE *fd, fattr_t *attr)
206 {
207 if (fd == NULL)
208 return STATUS_BAD_ARGUMENTS;
209
210 #ifdef PLATFORM_WINDOWS
211 return stat((HANDLE)::_get_osfhandle(::_fileno(fd)), attr);
212 #else
213 return stat(fileno(fd), attr);
214 #endif
215 }
216
217 status_t File::stat(const LSPString *path, fattr_t *attr)
218 {
219 if ((path == NULL) || (attr == NULL))
220 return STATUS_BAD_ARGUMENTS;
221
222 #ifdef PLATFORM_WINDOWS
223 WIN32_FIND_DATAW hfi;
224
225 HANDLE dh = ::FindFirstFileW(path->get_utf16(), &hfi);
226 if (dh == INVALID_HANDLE_VALUE)
227 {
228 DWORD err = ::GetLastError();
229 switch (err)
230 {
231 case ERROR_PATH_NOT_FOUND:
232 case ERROR_FILE_NOT_FOUND:
233 case ERROR_INVALID_ACCESS:
234 case ERROR_INVALID_DRIVE:
235 case ERROR_CANNOT_MAKE:
236 return STATUS_NOT_FOUND;
237 case ERROR_ACCESS_DENIED:
238 return STATUS_PERMISSION_DENIED;
239 case ERROR_TOO_MANY_OPEN_FILES:
240 case ERROR_NOT_ENOUGH_MEMORY:
241 case ERROR_OUTOFMEMORY:
242 return STATUS_NO_MEM;
243 case ERROR_FILE_EXISTS:
244 case ERROR_ALREADY_EXISTS:
245 case ERROR_DIRECTORY:
246 return STATUS_ALREADY_EXISTS;
247 case ERROR_BUFFER_OVERFLOW:
248 return STATUS_OVERFLOW;
249 case ERROR_INVALID_NAME:
250 return STATUS_INVALID_VALUE;
251 default: break;
252 }
253
254 return STATUS_IO_ERROR;
255 }
256 ::FindClose(dh);
257
258 decode_file_type(attr, &hfi);
259 #else
260 struct stat sb;
261 const char * const s = path->get_native();
262 if (::lstat(s, &sb) != 0)
263 {
264 const int code = errno;
265 lsp_trace("lstat() falied for %s: errno=%d", s, code);
266
267 switch (code)
268 {
269 case EACCES: return STATUS_PERMISSION_DENIED;
270 case EBADF: return STATUS_INVALID_VALUE;
271 case ENAMETOOLONG: return STATUS_OVERFLOW;
272 case EOVERFLOW: return STATUS_OVERFLOW;
273 case ENOENT: return STATUS_NOT_FOUND;
274 case ENOMEM: return STATUS_NO_MEM;
275 default: break;
276 }
277 return STATUS_IO_ERROR;
278 }
279
280 decode_file_type(attr, &sb);
281 #endif /* PLATFORM_WINDOWS */
282
283 return STATUS_OK;
284 }
285
286 status_t File::stat(fhandle_t fd, fattr_t *attr)
287 {
288 if (attr == NULL)
289 return STATUS_BAD_ARGUMENTS;
290
291 #ifdef PLATFORM_WINDOWS
292 BY_HANDLE_FILE_INFORMATION hfi;
293
294 if (!::GetFileInformationByHandle(fd, &hfi))
295 {
296 DWORD err = ::GetLastError();
297
298 switch (err)
299 {
300 case ERROR_PATH_NOT_FOUND:
301 case ERROR_FILE_NOT_FOUND:
302 case ERROR_INVALID_ACCESS:
303 case ERROR_INVALID_DRIVE:
304 case ERROR_CANNOT_MAKE:
305 return STATUS_NOT_FOUND;
306 case ERROR_ACCESS_DENIED:
307 return STATUS_PERMISSION_DENIED;
308 case ERROR_TOO_MANY_OPEN_FILES:
309 case ERROR_NOT_ENOUGH_MEMORY:
310 case ERROR_OUTOFMEMORY:
311 return STATUS_NO_MEM;
312 case ERROR_FILE_EXISTS:
313 case ERROR_ALREADY_EXISTS:
314 case ERROR_DIRECTORY:
315 return STATUS_ALREADY_EXISTS;
316 case ERROR_BUFFER_OVERFLOW:
317 return STATUS_OVERFLOW;
318 case ERROR_INVALID_NAME:
319 return STATUS_INVALID_VALUE;
320 default: break;
321 }
322 return STATUS_IO_ERROR;
323 }
324
325 decode_file_type(attr, &hfi);
326 #else
327 struct stat sb;
328 if (::fstat(fd, &sb) != 0)
329 {
330 int code = errno;
331 switch (code)
332 {
333 case EACCES: return STATUS_PERMISSION_DENIED;
334 case EBADF: return STATUS_INVALID_VALUE;
335 case ENAMETOOLONG: return STATUS_OVERFLOW;
336 case EOVERFLOW: return STATUS_OVERFLOW;
337 case ENOENT: return STATUS_NOT_FOUND;
338 case ENOMEM: return STATUS_NO_MEM;
339 default: break;
340 }
341 return STATUS_IO_ERROR;
342 }
343
344 decode_file_type(attr, &sb);
345 #endif /* PLATFORM_WINDOWS */
346
347 return STATUS_OK;
348 }
349
350 status_t File::sym_stat(const LSPString *path, fattr_t *attr)
351 {
352 if ((path == NULL) || (attr == NULL))
353 return STATUS_BAD_ARGUMENTS;
354
355 #ifdef PLATFORM_WINDOWS
356 WIN32_FIND_DATAW hfi;
357
358 HANDLE dh = ::FindFirstFileW(path->get_utf16(), &hfi);
359 if (dh == INVALID_HANDLE_VALUE)
360 {
361 DWORD err = ::GetLastError();
362 switch (err)
363 {
364 case ERROR_PATH_NOT_FOUND:
365 case ERROR_FILE_NOT_FOUND:
366 case ERROR_INVALID_ACCESS:
367 case ERROR_INVALID_DRIVE:
368 case ERROR_CANNOT_MAKE:
369 return STATUS_NOT_FOUND;
370 case ERROR_ACCESS_DENIED:
371 return STATUS_PERMISSION_DENIED;
372 case ERROR_TOO_MANY_OPEN_FILES:
373 case ERROR_NOT_ENOUGH_MEMORY:
374 case ERROR_OUTOFMEMORY:
375 return STATUS_NO_MEM;
376 case ERROR_FILE_EXISTS:
377 case ERROR_ALREADY_EXISTS:
378 case ERROR_DIRECTORY:
379 return STATUS_ALREADY_EXISTS;
380 case ERROR_BUFFER_OVERFLOW:
381 return STATUS_OVERFLOW;
382 case ERROR_INVALID_NAME:
383 return STATUS_INVALID_VALUE;
384 default: break;
385 }
386
387 return STATUS_IO_ERROR;
388 }
389 ::FindClose(dh);
390
391 decode_file_type(attr, &hfi);
392 #else
393 struct stat sb;
394 const char * const s = path->get_native();
395 if (::stat(s, &sb) != 0)
396 {
397 const int code = errno;
398 lsp_trace("stat falied for %s: errno=%d", s, code);
399
400 switch (code)
401 {
402 case EACCES: return STATUS_PERMISSION_DENIED;
403 case EBADF: return STATUS_INVALID_VALUE;
404 case ENAMETOOLONG: return STATUS_OVERFLOW;
405 case EOVERFLOW: return STATUS_OVERFLOW;
406 case ENOENT: return STATUS_NOT_FOUND;
407 case ENOMEM: return STATUS_NO_MEM;
408 default: break;
409 }
410 return STATUS_IO_ERROR;
411 }
412
413 decode_file_type(attr, &sb);
414 #endif /* PLATFORM_WINDOWS */
415
416 return STATUS_OK;
417 }
418
419 status_t File::remove(const char *path)
420 {
421 if (path == NULL)
422 return STATUS_BAD_ARGUMENTS;
423
424 LSPString spath;
425 if (!spath.set_utf8(path))
426 return STATUS_NO_MEM;
427 return remove(&spath);
428 }
429
430 status_t File::remove(const Path *path)
431 {
432 if (path == NULL)
433 return STATUS_BAD_ARGUMENTS;
434 return remove(path->as_string());
435 }
436
437 status_t File::remove(const LSPString *path)
438 {
439 if (path == NULL)
440 return STATUS_BAD_ARGUMENTS;
441
442 #ifdef PLATFORM_WINDOWS
443 if (::DeleteFileW(path->get_utf16()))
444 return STATUS_OK;
445
446 // Analyze error code
447 DWORD code = ::GetLastError();
448 switch (code)
449 {
450 case ERROR_ACCESS_DENIED:
451 {
452 fattr_t attr;
453 status_t res = stat(path, &attr);
454 if ((res == STATUS_OK) && (attr.type == fattr_t::FT_DIRECTORY))
455 return STATUS_IS_DIRECTORY;
456 return STATUS_PERMISSION_DENIED;
457 }
458 case ERROR_PATH_NOT_FOUND:
459 return STATUS_NOT_FOUND;
460 default:
461 return STATUS_IO_ERROR;
462 }
463 #else
464 // Try to remove file
465 if (::unlink(path->get_native()) == 0)
466 return STATUS_OK;
467
468 // Analyze error code
469 int code = errno;
470 lsp_trace("code=%d", int(code));
471 switch (code)
472 {
473 case EACCES:
474 return STATUS_PERMISSION_DENIED;
475 case EPERM:
476 {
477 fattr_t attr;
478 status_t res = stat(path, &attr);
479 if ((res == STATUS_OK) && (attr.type == fattr_t::FT_DIRECTORY))
480 return STATUS_IS_DIRECTORY;
481 return STATUS_PERMISSION_DENIED;
482 }
483 case EDQUOT:
484 case ENOSPC:
485 return STATUS_OVERFLOW;
486 case EISDIR:
487 return STATUS_IS_DIRECTORY;
488 case EFAULT:
489 case EINVAL:
490 case ENAMETOOLONG:
491 return STATUS_BAD_ARGUMENTS;
492 case ENOTDIR:
493 return STATUS_BAD_TYPE;
494 case ENOENT:
495 return STATUS_NOT_FOUND;
496 case ENOTEMPTY:
497 return STATUS_NOT_EMPTY;
498 default:
499 return STATUS_IO_ERROR;
500 }
501 #endif /* PLATFORM_WINDOWS */
502 return STATUS_OK;
503 }
504
505 status_t File::rename(const char *from, const char *to)
506 {
507 if ((from == NULL) || (to == NULL))
508 return STATUS_BAD_ARGUMENTS;
509
510 LSPString f, t;
511 if (!f.set_utf8(from))
512 return STATUS_NO_MEM;
513 if (!t.set_utf8(to))
514 return STATUS_NO_MEM;
515 return rename(&f, &t);
516 }
517
518 status_t File::rename(const LSPString *from, const char *to)
519 {
520 if ((from == NULL) || (to == NULL))
521 return STATUS_BAD_ARGUMENTS;
522
523 LSPString t;
524 if (!t.set_utf8(to))
525 return STATUS_NO_MEM;
526 return rename(from, &t);
527 }
528
529 status_t File::rename(const Path *from, const char *to)
530 {
531 if ((from == NULL) || (to == NULL))
532 return STATUS_BAD_ARGUMENTS;
533
534 LSPString t;
535 if (!t.set_utf8(to))
536 return STATUS_NO_MEM;
537 return rename(from->as_string(), &t);
538 }
539
540 status_t File::rename(const char *from, const LSPString *to)
541 {
542 if ((from == NULL) || (to == NULL))
543 return STATUS_BAD_ARGUMENTS;
544
545 LSPString f;
546 if (!f.set_native(from))
547 return STATUS_NO_MEM;
548 return rename(&f, to);
549 }
550
551 status_t File::rename(const Path *from, const LSPString *to)
552 {
553 if ((from == NULL) || (to == NULL))
554 return STATUS_BAD_ARGUMENTS;
555 return rename(from->as_string(), to);
556 }
557
558 status_t File::rename(const char *from, const Path *to)
559 {
560 if ((from == NULL) || (to == NULL))
561 return STATUS_BAD_ARGUMENTS;
562
563 LSPString f;
564 if (!f.set_utf8(from))
565 return STATUS_NO_MEM;
566 return rename(&f, to->as_string());
567 }
568
569 status_t File::rename(const LSPString *from, const Path *to)
570 {
571 if ((from == NULL) || (to == NULL))
572 return STATUS_BAD_ARGUMENTS;
573
574 return rename(from, to->as_string());
575 }
576
577 status_t File::rename(const Path *from, const Path *to)
578 {
579 if ((from == NULL) || (to == NULL))
580 return STATUS_BAD_ARGUMENTS;
581
582 return rename(from->as_string(), to->as_string());
583 }
584
585 status_t File::rename(const LSPString *from, const LSPString *to)
586 {
587 if ((from == NULL) || (to == NULL))
588 return STATUS_BAD_ARGUMENTS;
589
590 #ifdef PLATFORM_WINDOWS
591 if (::MoveFileExW(from->get_utf16(), to->get_utf16(), MOVEFILE_REPLACE_EXISTING))
592 return STATUS_OK;
593
594 // Analyze error code
595 DWORD code = ::GetLastError();
596 switch (code)
597 {
598 case ERROR_ACCESS_DENIED:
599 return STATUS_PERMISSION_DENIED;
600 case ERROR_PATH_NOT_FOUND:
601 return STATUS_NOT_FOUND;
602 default:
603 break;;
604 }
605 #else
606 // Try to remove file
607 if (::rename(from->get_native(), to->get_native()) == 0)
608 return STATUS_OK;
609
610 switch (errno)
611 {
612 case EPERM:
613 case EACCES:
614 case EROFS:
615 case EXDEV:
616 return STATUS_PERMISSION_DENIED;
617 case EBUSY:
618 return STATUS_LOCKED;
619 case EFAULT:
620 return STATUS_BAD_PATH;
621 case EINVAL:
622 return STATUS_INVALID_VALUE;
623 case EISDIR:
624 return STATUS_IS_DIRECTORY;
625 case EDQUOT:
626 case ELOOP:
627 case EMLINK:
628 return STATUS_OVERFLOW;
629 case ENAMETOOLONG:
630 return STATUS_BAD_ARGUMENTS;
631 case ENOENT:
632 return STATUS_NOT_FOUND;
633 case ENOMEM:
634 case ENOSPC:
635 return STATUS_NO_MEM;
636 case ENOTDIR:
637 return STATUS_NOT_DIRECTORY;
638 case ENOTEMPTY:
639 case EEXIST:
640 return STATUS_ALREADY_EXISTS;
641 default:
642 break;
643 }
644 #endif /* PLATFORM_WINDOWS */
645
646 return STATUS_IO_ERROR;
647 }
648
649 wssize_t File::copy(const char *from, const char *to, size_t io_buf_size)
650 {
651 if ((from == NULL) || (to == NULL))
652 return -STATUS_BAD_ARGUMENTS;
653
654 LSPString f, t;
655 if (!f.set_utf8(from))
656 return -STATUS_NO_MEM;
657 if (!t.set_utf8(to))
658 return -STATUS_NO_MEM;
659 return copy(&f, &t, io_buf_size);
660 }
661
662 wssize_t File::copy(const LSPString *from, const char *to, size_t io_buf_size)
663 {
664 if ((from == NULL) || (to == NULL))
665 return -STATUS_BAD_ARGUMENTS;
666
667 LSPString t;
668 if (!t.set_utf8(to))
669 return -STATUS_NO_MEM;
670 return copy(from, &t, io_buf_size);
671 }
672
673 wssize_t File::copy(const Path *from, const char *to, size_t io_buf_size)
674 {
675 if ((from == NULL) || (to == NULL))
676 return -STATUS_BAD_ARGUMENTS;
677
678 LSPString t;
679 if (!t.set_utf8(to))
680 return -STATUS_NO_MEM;
681 return copy(from->as_string(), &t, io_buf_size);
682 }
683
684 wssize_t File::copy(const char *from, const LSPString *to, size_t io_buf_size)
685 {
686 if ((from == NULL) || (to == NULL))
687 return -STATUS_BAD_ARGUMENTS;
688
689 LSPString f;
690 if (!f.set_native(from))
691 return -STATUS_NO_MEM;
692 return copy(&f, to, io_buf_size);
693 }
694
695 wssize_t File::copy(const Path *from, const LSPString *to, size_t io_buf_size)
696 {
697 if ((from == NULL) || (to == NULL))
698 return -STATUS_BAD_ARGUMENTS;
699 return copy(from->as_string(), to, io_buf_size);
700 }
701
702 wssize_t File::copy(const char *from, const Path *to, size_t io_buf_size)
703 {
704 if ((from == NULL) || (to == NULL))
705 return -STATUS_BAD_ARGUMENTS;
706
707 LSPString f;
708 if (!f.set_utf8(from))
709 return -STATUS_NO_MEM;
710 return copy(&f, to->as_string(), io_buf_size);
711 }
712
713 wssize_t File::copy(const LSPString *from, const Path *to, size_t io_buf_size)
714 {
715 if ((from == NULL) || (to == NULL))
716 return -STATUS_BAD_ARGUMENTS;
717
718 return copy(from, to->as_string(), io_buf_size);
719 }
720
721 wssize_t File::copy(const Path *from, const Path *to, size_t io_buf_size)
722 {
723 if ((from == NULL) || (to == NULL))
724 return -STATUS_BAD_ARGUMENTS;
725
726 return copy(from->as_string(), to->as_string(), io_buf_size);
727 }
728
729 wssize_t File::copy(const LSPString *from, const LSPString *to, size_t io_buf_size)
730 {
731 io::NativeFile src, dst;
732 status_t res = STATUS_OK, xres;
733 ssize_t copied = 0;
734
735 // Open source file
736 if ((res = src.open(from, File::FM_READ)) == STATUS_OK)
737 {
738 // Open destination file
739 if ((res = dst.open(to, File::FM_READWRITE_NEW)) == STATUS_OK)
740 {
741 // Allocate I/O buffer
742 io_buf_size = lsp_max(io_buf_size, 0x100U);
743 uint8_t *buf = static_cast<uint8_t *>(malloc(io_buf_size));
744 if (buf != NULL)
745 {
746 // Perform copy
747 do
748 {
749 // Read block
750 ssize_t nread = src.read(buf, io_buf_size);
751 if (nread < 0)
752 {
753 res = (nread == -STATUS_EOF) ? STATUS_OK : status_t(-nread);
754 break;
755 }
756
757 // Write block to destination file
758 for (ssize_t i=0; i<nread; ++i)
759 {
760 ssize_t nwritten = dst.write(&buf[i], nread - i);
761 if (nwritten < 0)
762 {
763 res = status_t(-nwritten);
764 break;
765 }
766
767 i += nwritten;
768 }
769
770 // Update number of copied bytes
771 copied += nread;
772 } while (res == STATUS_OK);
773
774 // Free allocated buffer
775 free(buf);
776 }
777 else
778 res = STATUS_NO_MEM;
779
780 // Close destination file
781 xres = dst.close();
782 if (res == STATUS_OK)
783 res = xres;
784 }
785
786 // Close source file
787 xres = src.close();
788 if (res == STATUS_OK)
789 res = xres;
790 }
791
792 return (res == STATUS_OK) ? copied : -res;
793 }
794
795 status_t File::mkparent(const char *path)
796 {
797 io::Path tmp;
798 status_t res;
799 if ((res = tmp.set(path)) != STATUS_OK)
800 return res;
801 if ((res = tmp.remove_last()) != STATUS_OK)
802 return res;
803
804 return tmp.mkdir();
805 }
806
807 status_t File::mkparent(const LSPString *path)
808 {
809 io::Path tmp;
810 status_t res;
811 if ((res = tmp.set(path)) != STATUS_OK)
812 return res;
813 if ((res = tmp.remove_last()) != STATUS_OK)
814 return res;
815
816 return tmp.mkdir();
817 }
818
819 status_t File::mkparent(const io::Path *path)
820 {
821 return path->mkparent();
822 }
823
824 status_t File::mkparent(const char *path, bool recursive)
825 {
826 io::Path tmp;
827 status_t res;
828 if ((res = tmp.set(path)) != STATUS_OK)
829 return res;
830 if ((res = tmp.remove_last()) != STATUS_OK)
831 return res;
832
833 return tmp.mkdir(recursive);
834 }
835
836 status_t File::mkparent(const LSPString *path, bool recursive)
837 {
838 io::Path tmp;
839 status_t res;
840 if ((res = tmp.set(path)) != STATUS_OK)
841 return res;
842 if ((res = tmp.remove_last()) != STATUS_OK)
843 return res;
844
845 return tmp.mkdir(recursive);
846 }
847
848 status_t File::mkparent(const io::Path *path, bool recursive)
849 {
850 return path->mkparent(recursive);
851 }
852 } /* namespace io */
853 } /* namespace lsp */