33# if defined(__APPLE__)
34# include <CoreFoundation/CoreFoundation.h>
35# include <objc/message.h>
36# include <objc/runtime.h>
39# include <sys/param.h>
55#define FILE_MAX_STATIC_BUF 256
112 std::min<size_t>(nbytes,
INT_MAX)
117 if (nbytes_read == nbytes) {
119 return nbytes_read_total + nbytes_read;
121 if (nbytes_read == 0) {
123 return nbytes_read_total;
125 if (nbytes_read < 0) {
130 if (nbytes_read > nbytes) [[unlikely]] {
142 buf =
static_cast<void *
>((
static_cast<char *
>(buf)) + nbytes_read);
143 nbytes_read_total += nbytes_read;
144 nbytes -= nbytes_read;
151 const char *opstring = windows_operation_string(operation);
162 const char *opstring = windows_operation_string(operation);
176 void *buf,
size_t len, FILE *file,
size_t file_offset,
int compression_level)
178 fseek(file, file_offset, SEEK_SET);
180 ZSTD_CCtx *ctx = ZSTD_createCCtx();
181 ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, compression_level);
183 ZSTD_inBuffer input = {buf,
len, 0};
185 size_t out_len = ZSTD_CStreamOutSize();
187 size_t total_written = 0;
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)) {
196 if (fwrite(out_buf, 1,
output.pos, file) !=
output.pos) {
199 total_written +=
output.pos;
205 ZSTD_outBuffer
output = {out_buf, out_len, 0};
206 ret = ZSTD_compressStream2(ctx, &
output, &input, ZSTD_e_end);
207 if (ZSTD_isError(
ret)) {
210 if (fwrite(out_buf, 1,
output.pos, file) !=
output.pos) {
213 total_written +=
output.pos;
219 return ZSTD_isError(
ret) ? 0 : total_written;
224 fseek(file, file_offset, SEEK_SET);
226 ZSTD_DCtx *ctx = ZSTD_createDCtx();
228 size_t in_len = ZSTD_DStreamInSize();
230 ZSTD_inBuffer input = {in_buf, in_len, 0};
237 input.size = fread(in_buf, 1, in_len, file);
238 if (input.size == 0) {
244 while (input.pos < input.size &&
output.pos <
output.size) {
245 ret = ZSTD_decompressStream(ctx, &
output, &input);
247 if (ZSTD_isError(
ret)) {
256 return ZSTD_isError(
ret) ? 0 :
output.pos;
263 return header[0] == 0x1f && header[1] == 0x8b && header[2] == 0x08;
283 uint32_t magic = *(
reinterpret_cast<uint32_t *
>(
const_cast<char *
>(header)));
284 if (magic == 0xFD2FB528) {
287 if ((magic >> 4) == 0x184D2A5) {
300 else if (errno != ENOENT) {
312 writable =
BLI_access(parent, X_OK | W_OK) == 0;
361 "Paths must not end with a slash!");
364 "Paths containing \"..\" components must be normalized first!");
368 if (dirname_parent_end) {
369 const char dirname_parent_end_value = *dirname_parent_end;
370 *dirname_parent_end =
'\0';
386 *dirname_parent_end = dirname_parent_end_value;
403 if (mkdir(
dirname, 0777) != 0) {
427 char *dirname_mut = dirname_static_buf;
430 if (
len >=
sizeof(dirname_static_buf)) {
440 dirname_mut[
len] =
'\0';
447 if (dirname_mut != dirname_static_buf) {
448 MEM_delete(dirname_mut);
513 return urename(from, to,
false);
515# if defined(__APPLE__)
516 int ret = renamex_np(from, to, RENAME_EXCL);
517 if (!(
ret < 0 && errno == ENOTSUP)) {
522# if defined(__GLIBC_PREREQ)
523# if __GLIBC_PREREQ(2, 28)
525 int ret = renameat2(AT_FDCWD, from, AT_FDCWD, to, RENAME_NOREPLACE);
526 if (!(
ret < 0 && errno == EINVAL)) {
537 return rename(from, to);
556 return urename(from, to,
true);
558 return rename(from, to);
564static void callLocalErrorCallBack(
const char *err)
569FILE *
BLI_fopen(
const char *filepath,
const char *mode)
573 return ufopen(filepath, mode);
576void BLI_get_short_name(
char short_name[256],
const char *filepath)
578 wchar_t short_name_16[256];
583 GetShortPathNameW(filepath_16, short_name_16, 256);
585 for (i = 0; i < 256; i++) {
586 short_name[i] = char(short_name_16[i]);
592void *
BLI_gzopen(
const char *filepath,
const char *mode)
599 if (mode[0] ==
'w') {
600 FILE *file =
ufopen(filepath,
"a");
601 if (file ==
nullptr) {
609# if ZLIB_VERNUM >= 0x1270
612 gzfile = gzopen_w(filepath_16, mode);
617 char short_name[256];
618 BLI_get_short_name(short_name, filepath);
619 gzfile = gzopen(short_name, mode);
637 return uaccess(filepath, mode);
640static bool delete_soft(
const wchar_t *path_16,
const char **r_error_message)
647 HRESULT hr = CoInitializeEx(
nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
653 hr = CoCreateInstance(
654 CLSID_FileOperation,
nullptr, CLSCTX_ALL, IID_IFileOperation, (
void **)&pfo);
661 hr = pfo->SetOperationFlags(FOF_ALLOWUNDO | FOF_SILENT | FOF_WANTNUKEWARNING);
664 hr = SHCreateItemFromParsingName(path_16,
nullptr, IID_IShellItem, (
void **)&psi);
667 hr = pfo->DeleteItem(psi,
nullptr);
670 hr = pfo->PerformOperations();
673 *r_error_message =
"Failed to prepare delete operation";
677 *r_error_message =
"Failed to prepare delete operation";
682 *r_error_message =
"Failed to parse path";
686 *r_error_message =
"Failed to set operation flags";
691 *r_error_message =
"Failed to create FileOperation instance";
696 *r_error_message =
"Failed to initialize COM";
702static bool delete_unique(
const char *path,
const bool dir)
709 err = !RemoveDirectoryW(path_16);
711 printf(
"Unable to remove directory\n");
715 err = !DeleteFileW(path_16);
717 callLocalErrorCallBack(
"Unable to delete file");
726static bool delete_recursive(
const char *dir)
730 uint filelist_num, i;
745 if (delete_recursive(path)) {
750 if (delete_unique(fl->path,
false)) {
757 if (!err && delete_unique(dir,
true)) {
766int BLI_delete(
const char *path,
bool dir,
bool recursive)
773 BLI_assert(!(dir ==
false && recursive ==
true));
776 err = delete_recursive(path);
779 err = delete_unique(path, dir);
796 err = delete_soft(file_16, r_error_message);
811 const char *path_dst,
817 if (filename_src != path_src) {
818 const size_t path_dst_len = strlen(path_dst);
821 size_t buf_size_needed = path_dst_len + strlen(filename_src) + 1;
822 char *path_dst_with_filename = (buf_size_needed <= buf_size) ?
825 BLI_string_join(path_dst_with_filename, buf_size_needed, path_dst, filename_src);
826 return path_dst_with_filename;
836 path_src, path_dst, path_dst_buf,
sizeof(path_dst_buf));
842 err = !MoveFileW(path_src_16, path_dst_with_filename_16);
847 callLocalErrorCallBack(
"Unable to move file");
848 printf(
" Move from '%s' to '%s' failed\n", path_src, path_dst_with_filename);
851 if (!
ELEM(path_dst_with_filename, path_dst_buf, path_dst)) {
852 MEM_delete(path_dst_with_filename);
858int BLI_copy(
const char *path_src,
const char *path_dst)
862 path_src, path_dst, path_dst_buf,
sizeof(path_dst_buf));
867 err = !CopyFileW(path_src_16, path_dst_with_filename_16,
false);
872 callLocalErrorCallBack(
"Unable to copy file!");
873 printf(
" Copy from '%s' to '%s' failed\n", path_src, path_dst_with_filename);
876 if (!
ELEM(path_dst_with_filename, path_dst_buf, path_dst)) {
877 MEM_delete(path_dst_with_filename);
884int BLI_create_symlink(
const char *path_src,
const char *path_dst)
887 callLocalErrorCallBack(
"Linking files is unsupported on Windows");
912 const int path_len = strlen(path);
921 int len = strlen(path);
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';
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;
974 buf->
str =
static_cast<char *
>(realloc(
static_cast<void *
>(buf->
str),
len + 1));
977 if (has_slash ==
false) {
1008 dirent **dirlist =
nullptr;
1009 int dirlist_num = 0;
1017 ret = lstat(src_buf->
str, &st);
1026 if (callback_file !=
nullptr) {
1027 ret = callback_file(src_buf->
str, dst_buf ? dst_buf->
str :
nullptr);
1035 dirlist_num = scandir(src_buf->
str, &dirlist,
nullptr, alphasort);
1036 if (dirlist_num < 0) {
1043 if (callback_dir_pre !=
nullptr) {
1044 ret = callback_dir_pre(src_buf->
str, dst_buf ? dst_buf->
str :
nullptr);
1056 const size_t src_len = src_buf->
str_len;
1057 const size_t dst_len = dst_buf ? dst_buf->
str_len : 0;
1059 for (
int i = 0; i < dirlist_num; i++) {
1076 lstat(src_buf->
str, &st_dir);
1077 is_dir =
S_ISDIR(st_dir.st_mode);
1080 is_dir = (
dirent->d_type == DT_DIR);
1086 src_buf, dst_buf, callback_dir_pre, callback_file, callback_dir_post);
1088 else if (callback_file !=
nullptr) {
1089 ret = callback_file(src_buf->
str, dst_buf ? dst_buf->
str :
nullptr);
1107 if (callback_dir_post !=
nullptr) {
1108 ret = callback_dir_post(src_buf->
str, dst_buf ? dst_buf->
str :
nullptr);
1115 if (dirlist !=
nullptr) {
1116 for (
int i = 0; i < dirlist_num; i++) {
1142 const char *path_dst,
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;
1154 const size_t str_len_over_alloc = 0;
1156 const size_t str_len_over_alloc =
FILE_MAX;
1165 src_buf, dst_buf, callback_dir_pre, callback_file, callback_dir_post);
1200 return fopen(filepath, mode);
1207 return gzopen(filepath, mode);
1214 return open(filepath,
oflag, pmode);
1221 return access(filepath, mode);
1228 BLI_assert(!(dir ==
false && recursive ==
true));
1236 return remove(path);
1241int BLI_delete_soft(
const char *filepath,
const char **r_error_message)
1245 const char *args[5];
1246 const char *process_failed;
1250 const char *xdg_current_desktop = [] {
1252 const char *key =
"ORIGINAL_XDG_CURRENT_DESKTOP";
1253 const char *value = getenv(key);
1254 return value ? value : getenv(key + 9);
1256 const char *xdg_session_desktop = getenv(
"XDG_SESSION_DESKTOP");
1259 (xdg_session_desktop &&
STREQ(xdg_session_desktop,
"KDE")))
1261 args[0] =
"kioclient5";
1264 args[3] =
"trash:/";
1266 process_failed =
"kioclient5 reported failure";
1273 process_failed =
"gio reported failure";
1277 const int errno_prev = errno;
1281 if (pid == -1) [[unlikely]] {
1282 *r_error_message = errno ? strerror(errno) :
"unable to fork process";
1288 execvp(args[0], (
char **)args);
1303 waitpid(pid, &wstatus, 0);
1306 if (WIFEXITED(wstatus)) {
1307 const int errno_child = WEXITSTATUS(wstatus);
1309 *r_error_message = process_failed;
1313 errno = errno_child;
1318 "Blender may not support moving files or directories to trash on your system.";
1336 struct stat st_a, st_b;
1338 if (lstat(path_a, &st_a)) {
1342 if (lstat(path_b, &st_b)) {
1346 return st_a.st_dev == st_b.st_dev && st_a.st_ino == st_b.st_ino;
1354 if (chown(filepath, st->st_uid, st->st_gid)) {
1359 if (chmod(filepath, st->st_mode)) {
1374 fprintf(stderr,
"%s: '%s' is the same as '%s'\n", __func__, from, to);
1378 if (lstat(from, &st)) {
1384 if (mkdir(to, st.st_mode)) {
1390 if (chown(to, st.st_uid, st.st_gid)) {
1400 FILE *from_stream, *to_stream;
1406 fprintf(stderr,
"%s: '%s' is the same as '%s'\n", __func__, from, to);
1410 if (lstat(from, &st)) {
1415 if (S_ISLNK(st.st_mode)) {
1422 if ((st.st_size + 1) <
sizeof(buf)) {
1431 link_len = readlink(from, link_buffer, st.st_size + 1);
1436 MEM_delete(link_buffer);
1442 link_buffer[link_len] =
'\0';
1444 if (symlink(link_buffer, to)) {
1447 MEM_delete(link_buffer);
1453 MEM_delete(link_buffer);
1458 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) || S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
1460 if (mknod(to, st.st_mode, st.st_rdev)) {
1472 fprintf(stderr,
"Copying of this kind of files isn't supported yet\n");
1476 from_stream = fopen(from,
"rb");
1482 to_stream = fopen(to,
"wb");
1485 fclose(from_stream);
1489 while ((
len = fread(buf, 1,
sizeof(buf), from_stream)) > 0) {
1490 fwrite(buf, 1,
len, to_stream);
1494 fclose(from_stream);
1505 int ret = rename(from, to);
1516 int ret = rename(from, to);
1538 const char *path_dst,
1543 char *path_src_no_slash =
BLI_strdup(path_src);
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) ?
1551 BLI_path_join(path_dst_with_filename, buf_size_needed, path_dst, filename_src);
1552 path_dst = path_dst_with_filename;
1554 MEM_delete(path_src_no_slash);
1559int BLI_copy(
const char *path_src,
const char *path_dst)
1563 path_src, path_dst, path_dst_buf,
sizeof(path_dst_buf));
1569 if (!
ELEM(path_dst_with_filename, path_dst_buf, path_dst)) {
1570 MEM_delete(path_dst_with_filename);
1577int BLI_create_symlink(
const char *path_src,
const char *path_dst)
1579 return symlink(path_dst, path_src);
File and directory operations.
Some types for dealing with directories.
#define BLI_path_join(...)
#define FILENAME_IS_CURRPAR(_n)
#define BLI_string_join(...)
Compatibility-like things for windows.
const char * dirname(char *path)
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
#define FILE_MAX_STATIC_BUF
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
void free(Subdiv *subdiv)
std::string filename(const std::string_view &str, size_t pos)
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
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)
static void strbuf_append_path(StrBuf *buf, const char *filename)
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()
static void strbuf_free(StrBuf *buf)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
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)
int BLI_file_stat_mode(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BLI_file_external_operation_execute(const char *filepath, FileExternalOperation operation)
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)
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)
static bool path_has_trailing_slash(const char *path)
int BLI_rename(const char *from, const char *to) ATTR_NONNULL()
@ RecursiveOp_Callback_StopRecurs
@ RecursiveOp_Callback_OK
@ RecursiveOp_Callback_Error
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)
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])
static void strbuf_trim(StrBuf *buf, size_t len)
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL()
bool BLI_file_is_writable(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
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)
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()
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
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
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])
bool BLI_windows_external_operation_supported(const char *filepath, const char *operation)
bool BLI_file_external_operation_supported(const char *filepath, FileExternalOperation operation)
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)
@ 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()
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
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 *)
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)
#define UTF16_UN_ENCODE(in8str)