Blender V5.3
fileops_c.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstdlib> /* malloc */
11#include <cstring>
12
13#include <fcntl.h>
14#include <sys/stat.h>
15#include <sys/types.h>
16
17#include <cerrno>
18
19#include <zlib.h>
20#include <zstd.h>
21
22#ifdef WIN32
23# include "BLI_fileops_types.hh"
24# include "BLI_string_utils.hh"
25# include "BLI_winstuff.hh"
26# include "utf_winfunc.hh"
27# include "utfconv.hh"
28# include <io.h>
29# include <shellapi.h>
30# include <shobjidl.h>
31# include <windows.h>
32#else
33# if defined(__APPLE__)
34# include <CoreFoundation/CoreFoundation.h>
35# include <objc/message.h>
36# include <objc/runtime.h>
37# endif
38# include <dirent.h>
39# include <sys/param.h>
40# include <sys/wait.h>
41# include <unistd.h>
42#endif
43
44#include "MEM_guardedalloc.h"
45
46#include "BLI_fileops.hh"
47#include "BLI_path_utils.hh"
48#include "BLI_string.hh"
49#include "BLI_sys_types.hh" /* For `intptr_t` support. */
50#include "BLI_utildefines.hh"
51
52namespace blender {
53
55#define FILE_MAX_STATIC_BUF 256
56
57#ifdef WIN32
58/* Text string used as the "verb" for Windows shell operations. */
59static const char *windows_operation_string(FileExternalOperation operation)
60{
61 switch (operation) {
63 return "open";
65 return "open";
67 return "edit";
69 return "new";
71 return "find";
73 return "show";
75 return "play";
77 return "browse";
79 return "preview";
81 return "print";
83 return "install";
85 return "runas";
87 return "properties";
89 return "find";
91 return "cmd";
92 }
94 return "";
95}
96#endif
97
98int64_t BLI_read(int fd, void *buf, size_t nbytes)
99{
100 /* Define our own read as `read` is not guaranteed to read the number of bytes requested.
101 * This happens rarely but was observed with larger than 2GB files on Linux, see: #113473.
102 *
103 * Even though this is a loop, the most common code-path will exit with "Success" case.
104 * In the case where read more data than the file contains, it will loop twice,
105 * exiting on EOF with the second iteration. */
106 int64_t nbytes_read_total = 0;
107 while (true) {
108 int64_t nbytes_read = read(fd,
109 buf,
110#ifdef WIN32
111 /* Read must not exceed INT_MAX on WIN32, clamp. */
112 std::min<size_t>(nbytes, INT_MAX)
113#else
114 nbytes
115#endif
116 );
117 if (nbytes_read == nbytes) {
118 /* Success (common case). */
119 return nbytes_read_total + nbytes_read;
120 }
121 if (nbytes_read == 0) {
122 /* EOF (common case for the second iteration when reading more data than `fd` contains). */
123 return nbytes_read_total;
124 }
125 if (nbytes_read < 0) {
126 /* Error. */
127 return nbytes_read;
128 }
129
130 if (nbytes_read > nbytes) [[unlikely]] {
131 /* Badly behaving LIBC, reading more bytes than requested should never happen.
132 * Possibly an invalid internal state/corruption, only check to prevent an eternal loop. */
134 /* Set the IO-error so there is some indication an error occurred. */
135 if (errno == 0) {
136 errno = EIO;
137 }
138 return -1;
139 }
140
141 /* If this is reached, fewer bytes were read than were requested. */
142 buf = static_cast<void *>((static_cast<char *>(buf)) + nbytes_read);
143 nbytes_read_total += nbytes_read;
144 nbytes -= nbytes_read;
145 }
146}
147
149{
150#ifdef WIN32
151 const char *opstring = windows_operation_string(operation);
152 return BLI_windows_external_operation_supported(filepath, opstring);
153#else
154 UNUSED_VARS(filepath, operation);
155 return false;
156#endif
157}
158
160{
161#ifdef WIN32
162 const char *opstring = windows_operation_string(operation);
163 if (BLI_windows_external_operation_supported(filepath, opstring) &&
164 BLI_windows_external_operation_execute(filepath, opstring))
165 {
166 return true;
167 }
168 return false;
169#else
170 UNUSED_VARS(filepath, operation);
171 return false;
172#endif
173}
174
176 void *buf, size_t len, FILE *file, size_t file_offset, int compression_level)
177{
178 fseek(file, file_offset, SEEK_SET);
179
180 ZSTD_CCtx *ctx = ZSTD_createCCtx();
181 ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, compression_level);
182
183 ZSTD_inBuffer input = {buf, len, 0};
184
185 size_t out_len = ZSTD_CStreamOutSize();
186 std::byte *out_buf = MEM_new_array_uninitialized<std::byte>(out_len, __func__);
187 size_t total_written = 0;
188
189 /* Compress block and write it out until the input has been consumed. */
190 while (input.pos < input.size) {
191 ZSTD_outBuffer output = {out_buf, out_len, 0};
192 size_t ret = ZSTD_compressStream2(ctx, &output, &input, ZSTD_e_continue);
193 if (ZSTD_isError(ret)) {
194 break;
195 }
196 if (fwrite(out_buf, 1, output.pos, file) != output.pos) {
197 break;
198 }
199 total_written += output.pos;
200 }
201
202 /* Finalize the `Zstd` frame. */
203 size_t ret = 1;
204 while (ret != 0) {
205 ZSTD_outBuffer output = {out_buf, out_len, 0};
206 ret = ZSTD_compressStream2(ctx, &output, &input, ZSTD_e_end);
207 if (ZSTD_isError(ret)) {
208 break;
209 }
210 if (fwrite(out_buf, 1, output.pos, file) != output.pos) {
211 break;
212 }
213 total_written += output.pos;
214 }
215
216 MEM_delete(out_buf);
217 ZSTD_freeCCtx(ctx);
218
219 return ZSTD_isError(ret) ? 0 : total_written;
220}
221
222size_t BLI_file_unzstd_to_mem_at_pos(void *buf, size_t len, FILE *file, size_t file_offset)
223{
224 fseek(file, file_offset, SEEK_SET);
225
226 ZSTD_DCtx *ctx = ZSTD_createDCtx();
227
228 size_t in_len = ZSTD_DStreamInSize();
229 std::byte *in_buf = MEM_new_array_uninitialized<std::byte>(in_len, __func__);
230 ZSTD_inBuffer input = {in_buf, in_len, 0};
231
232 ZSTD_outBuffer output = {buf, len, 0};
233
234 size_t ret = 0;
235 /* Read and decompress chunks of input data until we have enough output. */
236 while (output.pos < output.size && !ZSTD_isError(ret)) {
237 input.size = fread(in_buf, 1, in_len, file);
238 if (input.size == 0) {
239 break;
240 }
241
242 /* Consume input data until we run out or have enough output. */
243 input.pos = 0;
244 while (input.pos < input.size && output.pos < output.size) {
245 ret = ZSTD_decompressStream(ctx, &output, &input);
246
247 if (ZSTD_isError(ret)) {
248 break;
249 }
250 }
251 }
252
253 MEM_delete(in_buf);
254 ZSTD_freeDCtx(ctx);
255
256 return ZSTD_isError(ret) ? 0 : output.pos;
257}
258
259bool BLI_file_magic_is_gzip(const char header[4])
260{
261 /* GZIP itself starts with the magic bytes 0x1f 0x8b.
262 * The third byte indicates the compression method, which is 0x08 for DEFLATE. */
263 return header[0] == 0x1f && header[1] == 0x8b && header[2] == 0x08;
264}
265
266bool BLI_file_magic_is_zstd(const char header[4])
267{
268 /* ZSTD files consist of concatenated frames, each either a ZSTD frame or a skippable frame.
269 * Both types of frames start with a magic number: `0xFD2FB528` for ZSTD frames and `0x184D2A5`
270 * for skippable frames, with the * being anything from 0 to F.
271 *
272 * To check whether a file is ZSTD-compressed, we just check whether the first frame matches
273 * either. Seeking through the file until a ZSTD frame is found would make things more
274 * complicated and the probability of a false positive is rather low anyways.
275 *
276 * Note that LZ4 uses a compatible format, so even though its compressed frames have a
277 * different magic number, a valid LZ4 file might also start with a skippable frame matching
278 * the second check here.
279 *
280 * For more details, see https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md
281 */
282
283 uint32_t magic = *(reinterpret_cast<uint32_t *>(const_cast<char *>(header)));
284 if (magic == 0xFD2FB528) {
285 return true;
286 }
287 if ((magic >> 4) == 0x184D2A5) {
288 return true;
289 }
290 return false;
291}
292
293bool BLI_file_is_writable(const char *filepath)
294{
295 bool writable;
296 if (BLI_access(filepath, W_OK) == 0) {
297 /* File exists and I can write to it. */
298 writable = true;
299 }
300 else if (errno != ENOENT) {
301 /* Most likely file or containing directory cannot be accessed. */
302 writable = false;
303 }
304 else {
305 /* File doesn't exist -- check I can create it in parent directory. */
306 char parent[FILE_MAX];
307 BLI_path_split_dir_part(filepath, parent, sizeof(parent));
308#ifdef WIN32
309 /* Windows does not have X_OK. */
310 writable = BLI_access(parent, W_OK) == 0;
311#else
312 writable = BLI_access(parent, X_OK | W_OK) == 0;
313#endif
314 }
315 return writable;
316}
317
318bool BLI_file_touch(const char *filepath)
319{
320 FILE *f = BLI_fopen(filepath, "r+b");
321
322 if (f != nullptr) {
323 int c = getc(f);
324
325 if (c == EOF) {
326 /* Empty file, reopen in truncate write mode. */
327 fclose(f);
328 f = BLI_fopen(filepath, "w+b");
329 }
330 else {
331 /* Otherwise, rewrite first byte. */
332 rewind(f);
333 putc(c, f);
334 }
335 }
336 else {
337 f = BLI_fopen(filepath, "wb");
338 }
339 if (f) {
340 fclose(f);
341 return true;
342 }
343 return false;
344}
345
356static bool dir_create_recursive(const char *dirname, const int len)
357{
358 BLI_assert(strlen(dirname) == len);
359 /* Caller must ensure the path doesn't have trailing slashes. */
361 "Paths must not end with a slash!");
363 STREQ(dirname + (len - 2), "..")),
364 "Paths containing \"..\" components must be normalized first!");
365
366 bool ret = true;
367 char *dirname_parent_end = const_cast<char *>(BLI_path_parent_dir_end(dirname, len));
368 if (dirname_parent_end) {
369 const char dirname_parent_end_value = *dirname_parent_end;
370 *dirname_parent_end = '\0';
371#ifdef WIN32
372 /* Check special case `c:\foo`, don't try create `c:`, harmless but unnecessary. */
374#endif
375 {
376 const int mode = BLI_file_stat_mode(dirname);
377 if (mode != 0) {
378 if (!S_ISDIR(mode)) {
379 ret = false;
380 }
381 }
382 else if (!dir_create_recursive(dirname, dirname_parent_end - dirname)) {
383 ret = false;
384 }
385 }
386 *dirname_parent_end = dirname_parent_end_value;
387 }
388 if (ret) {
389 /* Ignore errors when the directory was created (probably by another process) in between the
390 * earlier call to BLI_exists() and this call to mkdir. Since this function only creates a
391 * directory if it doesn't exist yet, this is actually not seen as an error, even though
392 * mkdir() failed. */
393#ifdef WIN32
394 if (umkdir(dirname) == -1) {
395 if (GetLastError() == ERROR_ALREADY_EXISTS && BLI_is_dir(dirname)) {
396 return true;
397 }
398
399 /* Any other error should bubble up as an actual error. */
400 ret = false;
401 }
402#else
403 if (mkdir(dirname, 0777) != 0) {
404 if (errno == EEXIST && BLI_is_dir(dirname)) {
405 return true;
406 }
407
408 /* Any other error should bubble up as an actual error. */
409 ret = false;
410 }
411#endif
412 }
413 return ret;
414}
415
417{
418 const int mode = BLI_file_stat_mode(dirname);
419 if (mode != 0) {
420 /* The file exists, either it's a directory (ok), or not,
421 * in which case this function can't do anything useful
422 * (the caller could remove it and re-run this function). */
423 return S_ISDIR(mode) ? true : false;
424 }
425
426 char dirname_static_buf[FILE_MAX];
427 char *dirname_mut = dirname_static_buf;
428
429 size_t len = strlen(dirname);
430 if (len >= sizeof(dirname_static_buf)) {
431 dirname_mut = MEM_new_array_zeroed<char>(len + 1, __func__);
432 }
433 memcpy(dirname_mut, dirname, len + 1);
434
435 /* Strip trailing chars, important for first entering #dir_create_recursive
436 * when then ensures this is the case for recursive calls. */
437 while ((len > 0) && BLI_path_slash_is_native_compat(dirname_mut[len - 1])) {
438 len--;
439 }
440 dirname_mut[len] = '\0';
441
442 const bool ret = (len > 0) && dir_create_recursive(dirname_mut, len);
443
444 /* Ensure the string was properly restored. */
445 BLI_assert(memcmp(dirname, dirname_mut, len) == 0);
446
447 if (dirname_mut != dirname_static_buf) {
448 MEM_delete(dirname_mut);
449 }
450
451 return ret;
452}
453
454bool BLI_file_ensure_parent_dir_exists(const char *filepath)
455{
456 char di[FILE_MAX];
457 BLI_path_split_dir_part(filepath, di, sizeof(di));
458
459 if (di[0] == '\0') {
460 /* This means the filepath did not have any directory component, and thus the current directory
461 * should be made to exist. However, if it doesn't exist, Blender fails (at least on Linux) to
462 * even get the CWD. */
463 BLI_path_abs_from_cwd(di, sizeof(di));
464 if (di[0] == '\0') {
465 /* Still empty, which means getting CWD failed. */
466 return false;
467 }
468 }
469
470 /* Make if the dir doesn't exist. */
471 return BLI_dir_create_recursive(di);
472}
473
474int BLI_rename(const char *from, const char *to)
475{
476 if (!BLI_exists(from)) {
477 return 1;
478 }
479
480 /* NOTE(@ideasman42): there are no checks that `from` & `to` *aren't* the same file.
481 * It's up to the caller to ensure this. In practice these paths are often generated
482 * and known to be different rather than arbitrary user input.
483 * In the case of arbitrary paths (renaming a file in the file-selector for example),
484 * the caller must ensure file renaming doesn't cause user data loss.
485 *
486 * Support for checking the files aren't the same could be added, however path comparison
487 * alone is *not* a guarantee the files are different (given the possibility of accessing
488 * the same file through different paths via symbolic-links), we could instead support a
489 * version of Python's `os.path.samefile(..)` which compares the I-node & device.
490 * In this particular case we would not want to follow symbolic-links as well.
491 * Since this functionality isn't required at the moment, leave this as-is.
492 * Noting it as a potential improvement. */
493
494 /* NOTE: To avoid the concurrency 'time of check/time of use' (TOC/TOU) issue, this code attempts
495 * to use available solutions for an 'atomic' (file-system wise) rename operation, instead of
496 * first checking for an existing `to` target path, and then doing the rename operation if it
497 * does not exists at the time of check.
498 *
499 * Windows (through `MoveFileExW`) by default does not allow replacing an existing path. It is
500 * however not clear whether its API is exposed to the TOC/TOU issue or not.
501 *
502 * On Linux or OSX, to keep operations atomic, special non-standardized variants of `rename` must
503 * be used, depending on the OS. Note that there may also be failure due to file system not
504 * supporting this operation, although in practice this should not be a problem in modern
505 * systems.
506 * - https://man7.org/linux/man-pages/man2/rename.2.html
507 * - https://www.unix.com/man-page/mojave/2/renameatx_np/
508 *
509 * BSD systems do not have any such thing currently, and are therefore exposed to the TOC/TOU
510 * issue. */
511
512#ifdef WIN32
513 return urename(from, to, false);
514#else
515# if defined(__APPLE__)
516 int ret = renamex_np(from, to, RENAME_EXCL);
517 if (!(ret < 0 && errno == ENOTSUP)) {
518 return ret;
519 }
520# endif
521
522# if defined(__GLIBC_PREREQ)
523# if __GLIBC_PREREQ(2, 28)
524 /* Most common Linux case, use `RENAME_NOREPLACE` when available. */
525 int ret = renameat2(AT_FDCWD, from, AT_FDCWD, to, RENAME_NOREPLACE);
526 if (!(ret < 0 && errno == EINVAL)) {
527 return ret;
528 }
529# endif /* __GLIBC_PREREQ(2, 28) */
530# endif /* __GLIBC_PREREQ */
531 /* A naive non-atomic implementation, which is used for OS where atomic rename is not supported
532 * at all, or not implemented for specific file systems (for example NFS, Samba, exFAT, NTFS,
533 * etc). For those see #116049, #119966. */
534 if (BLI_exists(to)) {
535 return 1;
536 }
537 return rename(from, to);
538#endif /* !defined(WIN32) */
539}
540
541int BLI_rename_overwrite(const char *from, const char *to)
542{
543 if (!BLI_exists(from)) {
544 return 1;
545 }
546
547#ifdef WIN32
548 /* `urename` from `utfconv` intern utils uses `MoveFileExW`, which allows to replace an existing
549 * file, but not an existing directory, even if empty. This will only delete empty directories.
550 */
551 if (BLI_is_dir(to)) {
552 if (BLI_delete(to, true, false)) {
553 return 1;
554 }
555 }
556 return urename(from, to, true);
557#else
558 return rename(from, to);
559#endif
560}
561
562#ifdef WIN32
563
564static void callLocalErrorCallBack(const char *err)
565{
566 printf("%s\n", err);
567}
568
569FILE *BLI_fopen(const char *filepath, const char *mode)
570{
571 BLI_assert(!BLI_path_is_rel(filepath));
572
573 return ufopen(filepath, mode);
574}
575
576void BLI_get_short_name(char short_name[256], const char *filepath)
577{
578 wchar_t short_name_16[256];
579 int i = 0;
580
581 UTF16_ENCODE(filepath);
582
583 GetShortPathNameW(filepath_16, short_name_16, 256);
584
585 for (i = 0; i < 256; i++) {
586 short_name[i] = char(short_name_16[i]);
587 }
588
589 UTF16_UN_ENCODE(filepath);
590}
591
592void *BLI_gzopen(const char *filepath, const char *mode)
593{
594 gzFile gzfile;
595
596 BLI_assert(!BLI_path_is_rel(filepath));
597
598 /* XXX: Creates file before transcribing the path. */
599 if (mode[0] == 'w') {
600 FILE *file = ufopen(filepath, "a");
601 if (file == nullptr) {
602 /* File couldn't be opened, e.g. due to permission error. */
603 return nullptr;
604 }
605 fclose(file);
606 }
607
608 /* Temporary `#if` until we update all libraries to 1.2.7 for correct wide char path handling. */
609# if ZLIB_VERNUM >= 0x1270
610 UTF16_ENCODE(filepath);
611
612 gzfile = gzopen_w(filepath_16, mode);
613
614 UTF16_UN_ENCODE(filepath);
615# else
616 {
617 char short_name[256];
618 BLI_get_short_name(short_name, filepath);
619 gzfile = gzopen(short_name, mode);
620 }
621# endif
622
623 return gzfile;
624}
625
626int BLI_open(const char *filepath, int oflag, int pmode)
627{
628 BLI_assert(!BLI_path_is_rel(filepath));
629
630 return uopen(filepath, oflag, pmode);
631}
632
633int BLI_access(const char *filepath, int mode)
634{
635 BLI_assert(!BLI_path_is_rel(filepath));
636
637 return uaccess(filepath, mode);
638}
639
640static bool delete_soft(const wchar_t *path_16, const char **r_error_message)
641{
642 /* Deletes file or directory to recycling bin. The latter moves all contained files and
643 * directories recursively to the recycling bin as well. */
644 IFileOperation *pfo;
645 IShellItem *psi;
646
647 HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
648
649 if (SUCCEEDED(hr)) {
650 /* This is also the case when COM was previously initialized and CoInitializeEx returns
651 * S_FALSE, which is not an error. Both HRESULT values S_OK and S_FALSE indicate success. */
652
653 hr = CoCreateInstance(
654 CLSID_FileOperation, nullptr, CLSCTX_ALL, IID_IFileOperation, (void **)&pfo);
655
656 if (SUCCEEDED(hr)) {
657 /* Flags for deletion:
658 * FOF_ALLOWUNDO: Enables moving file to recycling bin.
659 * FOF_SILENT: Don't show progress dialog box.
660 * FOF_WANTNUKEWARNING: Show dialog box if file can't be moved to recycling bin. */
661 hr = pfo->SetOperationFlags(FOF_ALLOWUNDO | FOF_SILENT | FOF_WANTNUKEWARNING);
662
663 if (SUCCEEDED(hr)) {
664 hr = SHCreateItemFromParsingName(path_16, nullptr, IID_IShellItem, (void **)&psi);
665
666 if (SUCCEEDED(hr)) {
667 hr = pfo->DeleteItem(psi, nullptr);
668
669 if (SUCCEEDED(hr)) {
670 hr = pfo->PerformOperations();
671
672 if (FAILED(hr)) {
673 *r_error_message = "Failed to prepare delete operation";
674 }
675 }
676 else {
677 *r_error_message = "Failed to prepare delete operation";
678 }
679 psi->Release();
680 }
681 else {
682 *r_error_message = "Failed to parse path";
683 }
684 }
685 else {
686 *r_error_message = "Failed to set operation flags";
687 }
688 pfo->Release();
689 }
690 else {
691 *r_error_message = "Failed to create FileOperation instance";
692 }
693 CoUninitialize();
694 }
695 else {
696 *r_error_message = "Failed to initialize COM";
697 }
698
699 return FAILED(hr);
700}
701
702static bool delete_unique(const char *path, const bool dir)
703{
704 bool err;
705
706 UTF16_ENCODE(path);
707
708 if (dir) {
709 err = !RemoveDirectoryW(path_16);
710 if (err) {
711 printf("Unable to remove directory\n");
712 }
713 }
714 else {
715 err = !DeleteFileW(path_16);
716 if (err) {
717 callLocalErrorCallBack("Unable to delete file");
718 }
719 }
720
721 UTF16_UN_ENCODE(path);
722
723 return err;
724}
725
726static bool delete_recursive(const char *dir)
727{
728 struct direntry *filelist, *fl;
729 bool err = false;
730 uint filelist_num, i;
731
732 i = filelist_num = BLI_filelist_dir_contents(dir, &filelist);
733 fl = filelist;
734 while (i--) {
735 if (FILENAME_IS_CURRPAR(fl->relname)) {
736 /* Skip! */
737 }
738 else if (S_ISDIR(fl->type)) {
739 char path[FILE_MAXDIR];
740
741 /* dir listing produces dir path without trailing slash... */
742 STRNCPY(path, fl->path);
743 BLI_path_slash_ensure(path, sizeof(path));
744
745 if (delete_recursive(path)) {
746 err = true;
747 }
748 }
749 else {
750 if (delete_unique(fl->path, false)) {
751 err = true;
752 }
753 }
754 fl++;
755 }
756
757 if (!err && delete_unique(dir, true)) {
758 err = true;
759 }
760
761 BLI_filelist_free(filelist, filelist_num);
762
763 return err;
764}
765
766int BLI_delete(const char *path, bool dir, bool recursive)
767{
768 int err;
769
771
772 /* Not an error but avoid ambiguous arguments (recursive file deletion isn't meaningful). */
773 BLI_assert(!(dir == false && recursive == true));
774
775 if (recursive) {
776 err = delete_recursive(path);
777 }
778 else {
779 err = delete_unique(path, dir);
780 }
781
782 return err;
783}
784
788int BLI_delete_soft(const char *file, const char **r_error_message)
789{
790 int err;
791
793
794 UTF16_ENCODE(file);
795
796 err = delete_soft(file_16, r_error_message);
797
798 UTF16_UN_ENCODE(file);
799
800 return err;
801}
802
810static const char *path_destination_ensure_filename(const char *path_src,
811 const char *path_dst,
812 char *buf,
813 size_t buf_size)
814{
815 const char *filename_src = BLI_path_basename(path_src);
816 /* Unlikely but possible this has no slashes. */
817 if (filename_src != path_src) {
818 const size_t path_dst_len = strlen(path_dst);
819 /* Check if `path_dst` points to a directory. */
820 if (path_dst_len && BLI_path_slash_is_native_compat(path_dst[path_dst_len - 1])) {
821 size_t buf_size_needed = path_dst_len + strlen(filename_src) + 1;
822 char *path_dst_with_filename = (buf_size_needed <= buf_size) ?
823 buf :
824 MEM_new_array_zeroed<char>(buf_size_needed, __func__);
825 BLI_string_join(path_dst_with_filename, buf_size_needed, path_dst, filename_src);
826 return path_dst_with_filename;
827 }
828 }
829 return path_dst;
830}
831
832int BLI_path_move(const char *path_src, const char *path_dst)
833{
834 char path_dst_buf[FILE_MAX_STATIC_BUF];
835 const char *path_dst_with_filename = path_destination_ensure_filename(
836 path_src, path_dst, path_dst_buf, sizeof(path_dst_buf));
837
838 int err;
839
840 UTF16_ENCODE(path_src);
841 UTF16_ENCODE(path_dst_with_filename);
842 err = !MoveFileW(path_src_16, path_dst_with_filename_16);
843 UTF16_UN_ENCODE(path_dst_with_filename);
844 UTF16_UN_ENCODE(path_src);
845
846 if (err) {
847 callLocalErrorCallBack("Unable to move file");
848 printf(" Move from '%s' to '%s' failed\n", path_src, path_dst_with_filename);
849 }
850
851 if (!ELEM(path_dst_with_filename, path_dst_buf, path_dst)) {
852 MEM_delete(path_dst_with_filename);
853 }
854
855 return err;
856}
857
858int BLI_copy(const char *path_src, const char *path_dst)
859{
860 char path_dst_buf[FILE_MAX_STATIC_BUF];
861 const char *path_dst_with_filename = path_destination_ensure_filename(
862 path_src, path_dst, path_dst_buf, sizeof(path_dst_buf));
863 int err;
864
865 UTF16_ENCODE(path_src);
866 UTF16_ENCODE(path_dst_with_filename);
867 err = !CopyFileW(path_src_16, path_dst_with_filename_16, false);
868 UTF16_UN_ENCODE(path_dst_with_filename);
869 UTF16_UN_ENCODE(path_src);
870
871 if (err) {
872 callLocalErrorCallBack("Unable to copy file!");
873 printf(" Copy from '%s' to '%s' failed\n", path_src, path_dst_with_filename);
874 }
875
876 if (!ELEM(path_dst_with_filename, path_dst_buf, path_dst)) {
877 MEM_delete(path_dst_with_filename);
878 }
879
880 return err;
881}
882
883# if 0
884int BLI_create_symlink(const char *path_src, const char *path_dst)
885{
886 /* See patch from #30870, should this ever become needed. */
887 callLocalErrorCallBack("Linking files is unsupported on Windows");
888 (void)path_src;
889 (void)path_dst;
890 return 1;
891}
892# endif
893
894#else /* The UNIX world */
895
896/* results from recursive_operation and its callbacks */
897enum {
898 /* operation succeeded */
900
901 /* operation requested not to perform recursive digging for current path */
903
904 /* error occurred in callback and recursive walking should stop immediately */
906};
907
908using RecursiveOp_Callback = int (*)(const char *from, const char *to);
909
910[[maybe_unused]] static bool path_has_trailing_slash(const char *path)
911{
912 const int path_len = strlen(path);
913 if (path_len == 0) {
914 return false;
915 }
916 return BLI_path_slash_is_native_compat(path[path_len - 1]);
917}
918
919static size_t path_len_no_trailing_slash(const char *path)
920{
921 int len = strlen(path);
922 int len_found = len;
923 while (len) {
924 len--;
926 break;
927 }
928 len_found = len;
929 }
930 return len_found;
931}
932
933/* -------------------------------------------------------------------- */
936
940struct StrBuf {
941 char *str;
942 size_t str_len;
944};
945
946static void strbuf_init(StrBuf *buf, const char *str, size_t str_len, size_t str_len_alloc)
947{
948 str_len_alloc = std::max(str_len + 1, str_len_alloc);
949 buf->str = static_cast<char *>(malloc(str_len_alloc));
950 memcpy(buf->str, str, str_len);
951 buf->str[str_len] = '\0';
952 buf->str_len = str_len;
953 buf->str_len_alloc = str_len_alloc;
954}
955
956static void strbuf_free(StrBuf *buf)
957{
958 free(buf->str);
959}
960
964static void strbuf_append_path(StrBuf *buf, const char *filename)
965{
966 BLI_assert(strlen(buf->str) == buf->str_len);
968 bool has_slash = (buf->str_len > 0 &&
970 const size_t filename_len = strlen(filename);
971 const size_t len = buf->str_len + (has_slash ? 0 : 1) + filename_len;
972
973 if (buf->str_len_alloc <= len) {
974 buf->str = static_cast<char *>(realloc(static_cast<void *>(buf->str), len + 1));
975 buf->str_len_alloc = len + 1;
976 }
977 if (has_slash == false) {
978 buf->str[buf->str_len++] = SEP;
979 }
980 memcpy(buf->str + buf->str_len, filename, filename_len + 1);
981 buf->str_len += filename_len;
982 BLI_assert(buf->str_len < buf->str_len_alloc);
983}
984
985static void strbuf_trim(StrBuf *buf, size_t len)
986{
987 BLI_assert(len <= buf->str_len);
988 buf->str_len = len;
989 buf->str[len] = '\0';
990}
991
993
995 StrBuf *dst_buf,
996 RecursiveOp_Callback callback_dir_pre,
997 RecursiveOp_Callback callback_file,
998 RecursiveOp_Callback callback_dir_post)
999{
1000 /* NOTE(@ideasman42): This function must *not* use any `MEM_*` functions
1001 * as it's used to purge temporary files on when the processed is aborted,
1002 * in this case the `MEM_*` state may have already been freed (e.g. memory usage tracking)
1003 * causing freed memory access, potentially crashing. This constraint doesn't apply to the
1004 * callbacks themselves - unless they might also be called when aborting. */
1005 struct stat st;
1006 int ret = 0;
1007
1008 dirent **dirlist = nullptr;
1009 int dirlist_num = 0;
1010
1011 /* Check there's no trailing slash in file paths. */
1013 BLI_assert(!(dst_buf && path_has_trailing_slash(dst_buf->str)));
1014
1015 do { /* once */
1016
1017 ret = lstat(src_buf->str, &st);
1018 if (ret < 0) {
1019 /* source wasn't found, nothing to operate with */
1020 break;
1021 }
1022
1023 if (!S_ISDIR(st.st_mode)) {
1024 /* source isn't a directory, can't do recursive walking for it,
1025 * so just call file callback and leave */
1026 if (callback_file != nullptr) {
1027 ret = callback_file(src_buf->str, dst_buf ? dst_buf->str : nullptr);
1029 ret = -1;
1030 }
1031 }
1032 break;
1033 }
1034
1035 dirlist_num = scandir(src_buf->str, &dirlist, nullptr, alphasort);
1036 if (dirlist_num < 0) {
1037 /* error opening directory for listing */
1038 perror("scandir");
1039 ret = -1;
1040 break;
1041 }
1042
1043 if (callback_dir_pre != nullptr) {
1044 ret = callback_dir_pre(src_buf->str, dst_buf ? dst_buf->str : nullptr);
1047 /* callback requested not to perform recursive walking, not an error */
1048 ret = 0;
1049 }
1050 else {
1051 ret = -1;
1052 }
1053 break;
1054 }
1055 }
1056 const size_t src_len = src_buf->str_len;
1057 const size_t dst_len = dst_buf ? dst_buf->str_len : 0;
1058
1059 for (int i = 0; i < dirlist_num; i++) {
1060 const dirent *const dirent = dirlist[i];
1061
1063 continue;
1064 }
1065
1066 strbuf_append_path(src_buf, dirent->d_name);
1067 if (dst_buf) {
1068 strbuf_append_path(dst_buf, dirent->d_name);
1069 }
1070
1071 bool is_dir;
1072
1073# ifdef __HAIKU__
1074 {
1075 struct stat st_dir;
1076 lstat(src_buf->str, &st_dir);
1077 is_dir = S_ISDIR(st_dir.st_mode);
1078 }
1079# else
1080 is_dir = (dirent->d_type == DT_DIR);
1081# endif
1082
1083 if (is_dir) {
1084 /* Recurse into sub-directories. */
1086 src_buf, dst_buf, callback_dir_pre, callback_file, callback_dir_post);
1087 }
1088 else if (callback_file != nullptr) {
1089 ret = callback_file(src_buf->str, dst_buf ? dst_buf->str : nullptr);
1091 ret = -1;
1092 }
1093 }
1094 strbuf_trim(src_buf, src_len);
1095 if (dst_buf) {
1096 strbuf_trim(dst_buf, dst_len);
1097 }
1098
1099 if (ret != 0) {
1100 break;
1101 }
1102 }
1103 if (ret != 0) {
1104 break;
1105 }
1106
1107 if (callback_dir_post != nullptr) {
1108 ret = callback_dir_post(src_buf->str, dst_buf ? dst_buf->str : nullptr);
1110 ret = -1;
1111 }
1112 }
1113 } while (false);
1114
1115 if (dirlist != nullptr) {
1116 for (int i = 0; i < dirlist_num; i++) {
1117 free(dirlist[i]);
1118 }
1119 free(dirlist);
1120 }
1121
1122 return ret;
1123}
1124
1141static int recursive_operation(const char *path_src,
1142 const char *path_dst,
1143 RecursiveOp_Callback callback_dir_pre,
1144 RecursiveOp_Callback callback_file,
1145 RecursiveOp_Callback callback_dir_post)
1146
1147{
1148 StrBuf src_buf_stack = {};
1149 StrBuf dst_buf_stack = {};
1150 StrBuf *src_buf = &src_buf_stack;
1151 StrBuf *dst_buf = path_dst ? &dst_buf_stack : nullptr;
1152# ifndef NDEBUG
1153 /* Don't over allocate to ensure resizing works as expected. */
1154 const size_t str_len_over_alloc = 0;
1155# else
1156 const size_t str_len_over_alloc = FILE_MAX;
1157# endif
1158
1159 strbuf_init(src_buf, path_src, path_len_no_trailing_slash(path_src), str_len_over_alloc);
1160 if (dst_buf) {
1161 strbuf_init(dst_buf, path_dst, path_len_no_trailing_slash(path_dst), str_len_over_alloc);
1162 }
1163
1164 const int result = recursive_operation_impl(
1165 src_buf, dst_buf, callback_dir_pre, callback_file, callback_dir_post);
1166
1167 strbuf_free(src_buf);
1168 if (dst_buf) {
1169 strbuf_free(dst_buf);
1170 }
1171 return result;
1172}
1173
1174static int delete_callback_post(const char *from, const char * /*to*/)
1175{
1176 if (rmdir(from)) {
1177 perror("rmdir");
1178
1180 }
1181
1183}
1184
1185static int delete_single_file(const char *from, const char * /*to*/)
1186{
1187 if (unlink(from)) {
1188 perror("unlink");
1189
1191 }
1192
1194}
1195
1196FILE *BLI_fopen(const char *filepath, const char *mode)
1197{
1198 BLI_assert(!BLI_path_is_rel(filepath));
1199
1200 return fopen(filepath, mode);
1201}
1202
1203void *BLI_gzopen(const char *filepath, const char *mode)
1204{
1205 BLI_assert(!BLI_path_is_rel(filepath));
1206
1207 return gzopen(filepath, mode);
1208}
1209
1210int BLI_open(const char *filepath, int oflag, int pmode)
1211{
1212 BLI_assert(!BLI_path_is_rel(filepath));
1213
1214 return open(filepath, oflag, pmode);
1215}
1216
1217int BLI_access(const char *filepath, int mode)
1218{
1219 BLI_assert(!BLI_path_is_rel(filepath));
1220
1221 return access(filepath, mode);
1222}
1223
1224int BLI_delete(const char *path, bool dir, bool recursive)
1225{
1227 /* Not an error but avoid ambiguous arguments (recursive file deletion isn't meaningful). */
1228 BLI_assert(!(dir == false && recursive == true));
1229
1230 if (recursive) {
1231 return recursive_operation(path, nullptr, nullptr, delete_single_file, delete_callback_post);
1232 }
1233 if (dir) {
1234 return rmdir(path);
1235 }
1236 return remove(path);
1237}
1238
1239/* Apple version is defined in fileops_apple.mm */
1240# ifndef __APPLE__
1241int BLI_delete_soft(const char *filepath, const char **r_error_message)
1242{
1243 BLI_assert(!BLI_path_is_rel(filepath));
1244
1245 const char *args[5];
1246 const char *process_failed;
1247
1248 /* May contain `:` delimiter characters according to version 1.5 of the spec:
1249 * https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html */
1250 const char *xdg_current_desktop = [] {
1251 /* Account for VSCode overriding this value (TSK!), see: #133921. */
1252 const char *key = "ORIGINAL_XDG_CURRENT_DESKTOP";
1253 const char *value = getenv(key);
1254 return value ? value : getenv(key + 9);
1255 }();
1256 const char *xdg_session_desktop = getenv("XDG_SESSION_DESKTOP");
1257
1258 if ((xdg_current_desktop && BLI_string_elem_split_by_delim(xdg_current_desktop, ':', "KDE")) ||
1259 (xdg_session_desktop && STREQ(xdg_session_desktop, "KDE")))
1260 {
1261 args[0] = "kioclient5";
1262 args[1] = "move";
1263 args[2] = filepath;
1264 args[3] = "trash:/";
1265 args[4] = nullptr;
1266 process_failed = "kioclient5 reported failure";
1267 }
1268 else {
1269 args[0] = "gio";
1270 args[1] = "trash";
1271 args[2] = filepath;
1272 args[3] = nullptr;
1273 process_failed = "gio reported failure";
1274 }
1275
1276 /* Restore when there are no errors. */
1277 const int errno_prev = errno;
1278 errno = 0;
1279
1280 int pid = fork();
1281 if (pid == -1) [[unlikely]] {
1282 *r_error_message = errno ? strerror(errno) : "unable to fork process";
1283 return -1;
1284 }
1285
1286 if (pid == 0) {
1287 /* Child process. */
1288 execvp(args[0], (char **)args);
1289 /* This should only be reached if `execvp` fails and stack isn't replaced. */
1290
1291 /* Ensure outputs are flushed as `_exit` doesn't flush. */
1292 fflush(stdout);
1293 fflush(stderr);
1294
1295 /* Use `_exit` instead of `exit` so Blender's `atexit` cleanup functions don't run. */
1296 _exit(errno);
1298 return -1;
1299 }
1300
1301 /* Parent process. */
1302 int wstatus = 0;
1303 waitpid(pid, &wstatus, 0);
1304
1305 int result = 0; /* Success. */
1306 if (WIFEXITED(wstatus)) {
1307 const int errno_child = WEXITSTATUS(wstatus);
1308 if (errno_child) {
1309 *r_error_message = process_failed;
1310 result = -1;
1311
1312 /* Forward to the error so the caller may set the message. */
1313 errno = errno_child;
1314 }
1315 }
1316 else {
1317 *r_error_message =
1318 "Blender may not support moving files or directories to trash on your system.";
1319 result = -1;
1320 }
1321
1322 if (result == 0) {
1323 /* Only overwrite the value if there was an error. */
1324 errno = errno_prev;
1325 }
1326
1327 return result;
1328}
1329# endif
1330
1334static bool check_the_same(const char *path_a, const char *path_b)
1335{
1336 struct stat st_a, st_b;
1337
1338 if (lstat(path_a, &st_a)) {
1339 return false;
1340 }
1341
1342 if (lstat(path_b, &st_b)) {
1343 return false;
1344 }
1345
1346 return st_a.st_dev == st_b.st_dev && st_a.st_ino == st_b.st_ino;
1347}
1348
1352static int set_permissions(const char *filepath, const struct stat *st)
1353{
1354 if (chown(filepath, st->st_uid, st->st_gid)) {
1355 perror("chown");
1356 return -1;
1357 }
1358
1359 if (chmod(filepath, st->st_mode)) {
1360 perror("chmod");
1361 return -1;
1362 }
1363
1364 return 0;
1365}
1366
1367/* pre-recursive callback for copying operation
1368 * creates a destination directory where all source content fill be copied to */
1369static int copy_callback_pre(const char *from, const char *to)
1370{
1371 struct stat st;
1372
1373 if (check_the_same(from, to)) {
1374 fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
1376 }
1377
1378 if (lstat(from, &st)) {
1379 perror("stat");
1381 }
1382
1383 /* Create a directory. */
1384 if (mkdir(to, st.st_mode)) {
1385 perror("mkdir");
1387 }
1388
1389 /* Set proper owner and group on new directory. */
1390 if (chown(to, st.st_uid, st.st_gid)) {
1391 perror("chown");
1393 }
1394
1396}
1397
1398static int copy_single_file(const char *from, const char *to)
1399{
1400 FILE *from_stream, *to_stream;
1401 struct stat st;
1402 char buf[4096];
1403 size_t len;
1404
1405 if (check_the_same(from, to)) {
1406 fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
1408 }
1409
1410 if (lstat(from, &st)) {
1411 perror("lstat");
1413 }
1414
1415 if (S_ISLNK(st.st_mode)) {
1416 /* Symbolic links should be copied in special way. */
1417 char *link_buffer;
1418 int need_free;
1419 int64_t link_len;
1420
1421 /* Get large enough buffer to read link content. */
1422 if ((st.st_size + 1) < sizeof(buf)) {
1423 link_buffer = buf;
1424 need_free = 0;
1425 }
1426 else {
1427 link_buffer = MEM_new_array_zeroed<char>(st.st_size + 2, "copy_single_file link_buffer");
1428 need_free = 1;
1429 }
1430
1431 link_len = readlink(from, link_buffer, st.st_size + 1);
1432 if (link_len < 0) {
1433 perror("readlink");
1434
1435 if (need_free) {
1436 MEM_delete(link_buffer);
1437 }
1438
1440 }
1441
1442 link_buffer[link_len] = '\0';
1443
1444 if (symlink(link_buffer, to)) {
1445 perror("symlink");
1446 if (need_free) {
1447 MEM_delete(link_buffer);
1448 }
1450 }
1451
1452 if (need_free) {
1453 MEM_delete(link_buffer);
1454 }
1455
1457 }
1458 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) || S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
1459 /* Copy special type of file. */
1460 if (mknod(to, st.st_mode, st.st_rdev)) {
1461 perror("mknod");
1463 }
1464
1465 if (set_permissions(to, &st)) {
1467 }
1468
1470 }
1471 if (!S_ISREG(st.st_mode)) {
1472 fprintf(stderr, "Copying of this kind of files isn't supported yet\n");
1474 }
1475
1476 from_stream = fopen(from, "rb");
1477 if (!from_stream) {
1478 perror("fopen");
1480 }
1481
1482 to_stream = fopen(to, "wb");
1483 if (!to_stream) {
1484 perror("fopen");
1485 fclose(from_stream);
1487 }
1488
1489 while ((len = fread(buf, 1, sizeof(buf), from_stream)) > 0) {
1490 fwrite(buf, 1, len, to_stream);
1491 }
1492
1493 fclose(to_stream);
1494 fclose(from_stream);
1495
1496 if (set_permissions(to, &st)) {
1498 }
1499
1501}
1502
1503static int move_callback_pre(const char *from, const char *to)
1504{
1505 int ret = rename(from, to);
1506
1507 if (ret) {
1508 return copy_callback_pre(from, to);
1509 }
1510
1512}
1513
1514static int move_single_file(const char *from, const char *to)
1515{
1516 int ret = rename(from, to);
1517
1518 if (ret) {
1519 return copy_single_file(from, to);
1520 }
1521
1523}
1524
1525int BLI_path_move(const char *path_src, const char *path_dst)
1526{
1527 int ret = recursive_operation(path_src, path_dst, move_callback_pre, move_single_file, nullptr);
1528
1529 if (ret && ret != -1) {
1530 return recursive_operation(
1531 path_src, nullptr, nullptr, delete_single_file, delete_callback_post);
1532 }
1533
1534 return ret;
1535}
1536
1537static const char *path_destination_ensure_filename(const char *path_src,
1538 const char *path_dst,
1539 char *buf,
1540 size_t buf_size)
1541{
1542 if (BLI_is_dir(path_dst)) {
1543 char *path_src_no_slash = BLI_strdup(path_src);
1544 BLI_path_slash_rstrip(path_src_no_slash);
1545 const char *filename_src = BLI_path_basename(path_src_no_slash);
1546 if (filename_src != path_src_no_slash) {
1547 const size_t buf_size_needed = strlen(path_dst) + 1 + strlen(filename_src) + 1;
1548 char *path_dst_with_filename = (buf_size_needed <= buf_size) ?
1549 buf :
1550 MEM_new_array_zeroed<char>(buf_size_needed, __func__);
1551 BLI_path_join(path_dst_with_filename, buf_size_needed, path_dst, filename_src);
1552 path_dst = path_dst_with_filename;
1553 }
1554 MEM_delete(path_src_no_slash);
1555 }
1556 return path_dst;
1557}
1558
1559int BLI_copy(const char *path_src, const char *path_dst)
1560{
1561 char path_dst_buf[FILE_MAX_STATIC_BUF];
1562 const char *path_dst_with_filename = path_destination_ensure_filename(
1563 path_src, path_dst, path_dst_buf, sizeof(path_dst_buf));
1564 int ret;
1565
1567 path_src, path_dst_with_filename, copy_callback_pre, copy_single_file, nullptr);
1568
1569 if (!ELEM(path_dst_with_filename, path_dst_buf, path_dst)) {
1570 MEM_delete(path_dst_with_filename);
1571 }
1572
1573 return ret;
1574}
1575
1576# if 0
1577int BLI_create_symlink(const char *path_src, const char *path_dst)
1578{
1579 return symlink(path_dst, path_src);
1580}
1581# endif
1582
1583#endif
1584
1585} // namespace blender
File and directory operations.
Some types for dealing with directories.
#define FILE_MAX
#define BLI_path_join(...)
#define FILENAME_IS_CURRPAR(_n)
#define FILE_MAXDIR
#define BLI_string_join(...)
Compatibility-like things for windows.
#define S_ISDIR(x)
const char * dirname(char *path)
#define S_ISREG(x)
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define BLI_assert(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define output
#define printf
#define str(s)
#define FILE_MAX_STATIC_BUF
Definition fileops_c.cc:55
#define INT_MAX
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void free(Subdiv *subdiv)
Definition subdiv.cc:192
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const char * BLI_path_parent_dir_end(const char *path, size_t path_len) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
__int64 int64_t
Definition BKE_text.h:90
char * STRNCPY(char(&dst)[N], const char *src)
static int recursive_operation(const char *path_src, const char *path_dst, RecursiveOp_Callback callback_dir_pre, RecursiveOp_Callback callback_file, RecursiveOp_Callback callback_dir_post)
unsigned int uint
Definition BKE_text.h:55
static void strbuf_append_path(StrBuf *buf, const char *filename)
Definition fileops_c.cc:964
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:416
static void strbuf_free(StrBuf *buf)
Definition fileops_c.cc:956
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BLI_windows_external_operation_execute(const char *filepath, const char *operation)
BLI_INLINE bool BLI_path_slash_is_native_compat(const char ch)
int64_t BLI_read(int fd, void *buf, size_t nbytes)
Definition fileops_c.cc:98
int BLI_file_stat_mode(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:362
bool BLI_file_external_operation_execute(const char *filepath, FileExternalOperation operation)
Definition fileops_c.cc:159
static int delete_single_file(const char *from, const char *)
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static int copy_callback_pre(const char *from, const char *to)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool bool int bool BLI_string_elem_split_by_delim(const char *haystack, const char delim, const char *needle) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
static void strbuf_init(StrBuf *buf, const char *str, size_t str_len, size_t str_len_alloc)
Definition fileops_c.cc:946
unsigned int uint32_t
Definition BKE_text.h:81
static int copy_single_file(const char *from, const char *to)
void BLI_path_slash_rstrip(char *path) ATTR_NONNULL(1)
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
static size_t path_len_no_trailing_slash(const char *path)
Definition fileops_c.cc:919
static bool path_has_trailing_slash(const char *path)
Definition fileops_c.cc:910
int BLI_rename(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:474
@ RecursiveOp_Callback_StopRecurs
Definition fileops_c.cc:902
@ RecursiveOp_Callback_OK
Definition fileops_c.cc:899
@ RecursiveOp_Callback_Error
Definition fileops_c.cc:905
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
int BLI_path_move(const char *path_src, const char *path_dst) ATTR_NONNULL()
int BLI_access(const char *filepath, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static bool dir_create_recursive(const char *dirname, const int len)
Definition fileops_c.cc:356
static int set_permissions(const char *filepath, const struct stat *st)
static int move_callback_pre(const char *from, const char *to)
static bool check_the_same(const char *path_a, const char *path_b)
bool BLI_path_abs_from_cwd(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
bool BLI_file_magic_is_zstd(const char header[4])
Definition fileops_c.cc:266
static void strbuf_trim(StrBuf *buf, size_t len)
Definition fileops_c.cc:985
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:541
bool BLI_file_is_writable(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition fileops_c.cc:293
bool BLI_path_is_win32_drive_only(const char *path)
int BLI_path_slash_ensure(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
bool BLI_file_touch(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:318
size_t BLI_file_unzstd_to_mem_at_pos(void *buf, size_t len, FILE *file, size_t file_offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition fileops_c.cc:222
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:457
int BLI_delete_soft(const char *filepath, const char **r_error_message) ATTR_NONNULL()
void * BLI_gzopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static const char * path_destination_ensure_filename(const char *path_src, const char *path_dst, char *buf, size_t buf_size)
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
int(*)(const char *from, const char *to) RecursiveOp_Callback
Definition fileops_c.cc:908
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_file_magic_is_gzip(const char header[4])
Definition fileops_c.cc:259
bool BLI_windows_external_operation_supported(const char *filepath, const char *operation)
bool BLI_file_external_operation_supported(const char *filepath, FileExternalOperation operation)
Definition fileops_c.cc:148
constexpr char SEP
ATTR_WARN_UNUSED_RESULT const BMFlagLayer const short oflag
static int recursive_operation_impl(StrBuf *src_buf, StrBuf *dst_buf, RecursiveOp_Callback callback_dir_pre, RecursiveOp_Callback callback_file, RecursiveOp_Callback callback_dir_post)
Definition fileops_c.cc:994
FileExternalOperation
@ FILE_EXTERNAL_OPERATION_FOLDER_OPEN
@ FILE_EXTERNAL_OPERATION_FOLDER_CMD
@ FILE_EXTERNAL_OPERATION_EDIT
@ FILE_EXTERNAL_OPERATION_FIND
@ FILE_EXTERNAL_OPERATION_SHOW
@ FILE_EXTERNAL_OPERATION_BROWSE
@ FILE_EXTERNAL_OPERATION_INSTALL
@ FILE_EXTERNAL_OPERATION_PROPERTIES
@ FILE_EXTERNAL_OPERATION_PLAY
@ FILE_EXTERNAL_OPERATION_PRINT
@ FILE_EXTERNAL_OPERATION_PREVIEW
@ FILE_EXTERNAL_OPERATION_FOLDER_FIND
@ FILE_EXTERNAL_OPERATION_RUNAS
@ FILE_EXTERNAL_OPERATION_NEW
@ FILE_EXTERNAL_OPERATION_OPEN
static int move_single_file(const char *from, const char *to)
size_t BLI_file_zstd_from_mem_at_pos(void *buf, size_t len, FILE *file, size_t file_offset, int compression_level) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition fileops_c.cc:175
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:454
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static int delete_callback_post(const char *from, const char *)
size_t str_len_alloc
Definition fileops_c.cc:943
char * d_name
int urename(const char *oldname, const char *newname, const bool do_replace)
FILE * ufopen(const char *filename, const char *mode)
int umkdir(const char *pathname)
int uopen(const char *filename, int oflag, int pmode)
int uaccess(const char *filename, int mode)
#define UTF16_ENCODE(in8str)
Definition utfconv.hh:80
#define UTF16_UN_ENCODE(in8str)
Definition utfconv.hh:84