Blender V5.0
fileops_c.cc File Reference
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cerrno>
#include <zlib.h>
#include <zstd.h>
#include <dirent.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <unistd.h>
#include "MEM_guardedalloc.h"
#include "BLI_fileops.h"
#include "BLI_path_utils.hh"
#include "BLI_string.h"
#include "BLI_sys_types.h"
#include "BLI_utildefines.h"

Go to the source code of this file.

Classes

struct  StrBuf

Macros

#define FILE_MAX_STATIC_BUF   256

Typedefs

using RecursiveOp_Callback = int (*)(const char *from, const char *to)

Enumerations

enum  { RecursiveOp_Callback_OK = 0 , RecursiveOp_Callback_StopRecurs = 1 , RecursiveOp_Callback_Error = 2 }

Functions

int64_t BLI_read (int fd, void *buf, size_t nbytes)
bool BLI_file_external_operation_supported (const char *filepath, FileExternalOperation operation)
bool BLI_file_external_operation_execute (const char *filepath, FileExternalOperation operation)
size_t BLI_file_zstd_from_mem_at_pos (void *buf, size_t len, FILE *file, size_t file_offset, int compression_level)
size_t BLI_file_unzstd_to_mem_at_pos (void *buf, size_t len, FILE *file, size_t file_offset)
bool BLI_file_magic_is_gzip (const char header[4])
bool BLI_file_magic_is_zstd (const char header[4])
bool BLI_file_is_writable (const char *filepath)
bool BLI_file_touch (const char *filepath)
static bool dir_create_recursive (const char *dirname, const int len)
bool BLI_dir_create_recursive (const char *dirname)
bool BLI_file_ensure_parent_dir_exists (const char *filepath)
int BLI_rename (const char *from, const char *to)
int BLI_rename_overwrite (const char *from, const char *to)
static bool path_has_trailing_slash (const char *path)
static size_t path_len_no_trailing_slash (const char *path)
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)
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 int delete_callback_post (const char *from, const char *)
static int delete_single_file (const char *from, const char *)
FILE * BLI_fopen (const char *filepath, const char *mode)
void * BLI_gzopen (const char *filepath, const char *mode)
int BLI_open (const char *filepath, int oflag, int pmode)
int BLI_access (const char *filepath, int mode)
int BLI_delete (const char *path, bool dir, bool recursive)
int BLI_delete_soft (const char *filepath, const char **r_error_message)
static bool check_the_same (const char *path_a, const char *path_b)
static int set_permissions (const char *filepath, const struct stat *st)
static int copy_callback_pre (const char *from, const char *to)
static int copy_single_file (const char *from, const char *to)
static int move_callback_pre (const char *from, const char *to)
static int move_single_file (const char *from, const char *to)
int BLI_path_move (const char *path_src, const char *path_dst)
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)
Simple String Buffer
static void strbuf_init (StrBuf *buf, const char *str, size_t str_len, size_t str_len_alloc)
static void strbuf_free (StrBuf *buf)
static void strbuf_append_path (StrBuf *buf, const char *filename)
static void strbuf_trim (StrBuf *buf, size_t len)

Macro Definition Documentation

◆ FILE_MAX_STATIC_BUF

#define FILE_MAX_STATIC_BUF   256

Sizes above this must be allocated.

Definition at line 53 of file fileops_c.cc.

Referenced by BLI_copy().

Typedef Documentation

◆ RecursiveOp_Callback

using RecursiveOp_Callback = int (*)(const char *from, const char *to)

Definition at line 895 of file fileops_c.cc.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
RecursiveOp_Callback_OK 
RecursiveOp_Callback_StopRecurs 
RecursiveOp_Callback_Error 

Definition at line 884 of file fileops_c.cc.

Function Documentation

◆ BLI_access()

◆ BLI_copy()

◆ BLI_delete()

int BLI_delete ( const char * path,
bool dir,
bool recursive )

Deletes the specified file or directory.

Parameters
dirDelete an empty directory instead of a file. The value is ignored when recursive is true but should true to make the intention clear. If the directory is not empty, delete fails.
recursiveRecursively delete files including path which may be a directory of a file.
Note
Symbolic-Links for (UNIX) behave as follows:
  • Never followed, treated as regular files.
  • Links are removed, not the files/directories they references.
  • When path itself links to another directory, deleting path behaves as if a regular file is being deleted.
  • If dir is true and path is a link, delete fails.
Returns
zero on success (matching 'remove' behavior).

Definition at line 1211 of file fileops_c.cc.

References BLI_assert, BLI_path_is_rel(), delete_callback_post(), delete_single_file(), and recursive_operation().

Referenced by blender::bke::asset_edit_id_delete(), BKE_packedfile_write_to_file(), BKE_ptcache_id_clear(), BKE_tempdir_session_purge(), BLI_rename_overwrite(), blender::gpu::VKShaderCompiler::cache_dir_clear_old(), cdf_remove(), blender::io::obj::OBJMTLParserTest::check_string(), blender::io::obj::OBJExportRegressionTest::compare_obj_export_to_golden(), blender::io::stl::STLExportTest::compare_to_golden(), blender::ed::asset::index::AssetLibraryIndex::delete_file_index(), blender::io::alembic::AlembicExportTest::deleteArchive(), blender::io::alembic::export_endjob(), blender::io::usd::export_endjob(), blender::io::usd::export_endjob_usdz_cleanup(), IMB_thumb_delete(), IMB_thumb_manage(), blender::io::usd::load_image(), blender::io::usd::USDMaterialReader::load_tex_image(), blender::io::usd::perform_usdz_conversion(), blender::io::hydra::USDSceneDelegate::populate(), preferences_extension_repo_remove_exec(), RE_RenderAnim(), blender::asset_system::tests::AssetLibraryServiceTest::TearDown(), blender::asset_system::tests::AssetLibraryTestBase::TearDown(), blender::io::usd::UsdExportTest::TearDown(), blender::tests::ChangeWorkingDirectoryTest::TearDown(), blender::tests::FileOpsTest::TearDown(), blender::tests::TEST_F(), blender::ed::object::bake_simulation::try_delete_bake(), blender::bke::bake::unpack_geometry_nodes_bake(), wm_autosave_delete(), and blender::io::hydra::USDSceneDelegate::~USDSceneDelegate().

◆ BLI_delete_soft()

int BLI_delete_soft ( const char * filepath,
const char ** r_error_message )

Soft deletes the specified file or directory (depending on dir) by moving the files to the recycling bin, optionally doing recursive delete of directory contents.

Returns
zero on success (matching 'remove' behavior).

Definition at line 1228 of file fileops_c.cc.

References BLI_assert, BLI_assert_unreachable, BLI_path_is_rel(), BLI_string_elem_split_by_delim(), error(), result, STREQ, and UNLIKELY.

Referenced by file_delete_single().

◆ BLI_dir_create_recursive()

◆ BLI_file_ensure_parent_dir_exists()

◆ BLI_file_external_operation_execute()

bool BLI_file_external_operation_execute ( const char * filepath,
FileExternalOperation operation )

◆ BLI_file_external_operation_supported()

bool BLI_file_external_operation_supported ( const char * filepath,
FileExternalOperation operation )

Definition at line 146 of file fileops_c.cc.

References BLI_windows_external_operation_supported(), and UNUSED_VARS.

Referenced by file_os_operations_menu_item().

◆ BLI_file_is_writable()

bool BLI_file_is_writable ( const char * filepath)

Returns true if the file with the specified name can be written. This implementation uses access(2), which makes the check according to the real UID and GID of the process, not its effective UID and GID. This shouldn't matter for Blender, which is not going to run privileged anyway.

Definition at line 291 of file fileops_c.cc.

References BLI_access(), BLI_path_split_dir_part(), and FILE_MAX.

Referenced by blender::bke::asset_link_id(), image_save_exec(), blender::ed::vse::sequencer_export_subtitles_exec(), txt_write_file(), and wm_file_write().

◆ BLI_file_magic_is_gzip()

bool BLI_file_magic_is_gzip ( const char header[4])

◆ BLI_file_magic_is_zstd()

bool BLI_file_magic_is_zstd ( const char header[4])

◆ BLI_file_touch()

bool BLI_file_touch ( const char * filepath)

Creates the file with nothing in it, or updates its last-modified date if it already exists. Returns true if successful (like the unix touch command).

Definition at line 316 of file fileops_c.cc.

References BLI_fopen().

Referenced by blender::gpu::VKPipelinePool::read_from_disk(), blender::gpu::read_spirv_from_disk(), blender::asset_system::tests::AssetCatalogTest::save_from_memory_into_existing_asset_lib(), blender::ed::vse::sequencer_export_subtitles_exec(), blender::tests::TEST_F(), blender::tests::TEST_F(), and touch_file().

◆ BLI_file_unzstd_to_mem_at_pos()

size_t BLI_file_unzstd_to_mem_at_pos ( void * buf,
size_t len,
FILE * file,
size_t file_offset )

Definition at line 220 of file fileops_c.cc.

References input, len, MEM_freeN(), MEM_mallocN(), output, and ret.

◆ BLI_file_zstd_from_mem_at_pos()

size_t BLI_file_zstd_from_mem_at_pos ( void * buf,
size_t len,
FILE * file,
size_t file_offset,
int compression_level )

Definition at line 173 of file fileops_c.cc.

References input, len, MEM_freeN(), MEM_mallocN(), output, and ret.

◆ BLI_fopen()

◆ BLI_gzopen()

void * BLI_gzopen ( const char * filepath,
const char * mode )

Definition at line 1190 of file fileops_c.cc.

References BLI_assert, and BLI_path_is_rel().

Referenced by MANTA::readConfiguration(), and MANTA::writeConfiguration().

◆ BLI_open()

◆ BLI_path_move()

int BLI_path_move ( const char * path_src,
const char * path_dst )

When path_src points to a directory, moves all its contents into path_dst, else rename path_src itself to path_dst.

Returns
0 on success.

Definition at line 1512 of file fileops_c.cc.

References delete_callback_post(), delete_single_file(), move_callback_pre(), move_single_file(), recursive_operation(), and ret.

Referenced by blender::io::usd::perform_usdz_conversion().

◆ BLI_read()

int64_t BLI_read ( int fd,
void * buf,
size_t nbytes )

A version of read with the following differences:

  • continues reading until failure or the requested size is met.
  • Reads size_t bytes instead of int on WIN32.
    Returns
    the number of bytes read.

Definition at line 96 of file fileops_c.cc.

References BLI_assert_unreachable, read, and UNLIKELY.

Referenced by BKE_packedfile_compare_to_file(), BKE_packedfile_new(), buffer_from_filepath(), file_read(), imb_test_image_read_header_from_filepath(), prefetch_read_file_to_memory(), and proxy_thread_next_frame().

◆ BLI_rename()

int BLI_rename ( const char * from,
const char * to )

Rename a file or directory, unless to already exists.

Note
This matches Windows rename logic, not Unix one. It does not allow to replace an existing target. Use BLI_rename_overwrite instead if existing file should be replaced.
Parameters
fromThe path to rename from (return failure if it does not exist).
toThe destination path.
Returns
zero on success (matching 'rename' behavior).

Definition at line 461 of file fileops_c.cc.

References BLI_exists(), ret, and urename().

Referenced by renamebutton_cb(), and blender::tests::TEST_F().

◆ BLI_rename_overwrite()

int BLI_rename_overwrite ( const char * from,
const char * to )

Rename a file or directory, replacing target to path if it exists.

Note
This matches Unix rename logic. It does allow to replace an existing target. Use BLI_rename instead if existing file should never be replaced. However, if to is an existing, non-empty directory, the operation will fail.
There is still no feature-parity between behaviors on Windows and Unix, in case the target to exists and is opened by some process in the system:
Warning
Due to internal limitation/implementation, on Windows, in case paths point to directories, it's up to the caller to ensure that from and to are not the same directory. Since to is being deleted to make room for from, this will result in from being deleted as well.

See BLI_path_move to move directories.

Parameters
fromThe path to rename from (return failure if it does not exist).
toThe destination path. This will be deleted if it already exists, unless it's a directory which will fail.
Returns
zero on success (matching 'rename' behavior).

Definition at line 528 of file fileops_c.cc.

References BLI_delete(), BLI_exists(), BLI_is_dir(), and urename().

Referenced by BKE_packedfile_write_to_file(), BKE_ptcache_disk_cache_rename(), BLO_write_file_impl(), do_history(), blender::tests::TEST_F(), thumb_create_ex(), wm_autosave_delete(), and blender::asset_system::AssetCatalogDefinitionFile::write_to_disk().

◆ check_the_same()

bool check_the_same ( const char * path_a,
const char * path_b )
static

Do the two paths denote the same file-system object?

Definition at line 1321 of file fileops_c.cc.

Referenced by copy_callback_pre(), and copy_single_file().

◆ copy_callback_pre()

int copy_callback_pre ( const char * from,
const char * to )
static

Definition at line 1356 of file fileops_c.cc.

References check_the_same(), RecursiveOp_Callback_Error, and RecursiveOp_Callback_OK.

Referenced by BLI_copy(), and move_callback_pre().

◆ copy_single_file()

int copy_single_file ( const char * from,
const char * to )
static

◆ delete_callback_post()

int delete_callback_post ( const char * from,
const char *  )
static

Definition at line 1161 of file fileops_c.cc.

References RecursiveOp_Callback_Error, and RecursiveOp_Callback_OK.

Referenced by BLI_delete(), and BLI_path_move().

◆ delete_single_file()

int delete_single_file ( const char * from,
const char *  )
static

Definition at line 1172 of file fileops_c.cc.

References RecursiveOp_Callback_Error, and RecursiveOp_Callback_OK.

Referenced by BLI_delete(), and BLI_path_move().

◆ dir_create_recursive()

bool dir_create_recursive ( const char * dirname,
const int len )
static

Create the given directory and its parents if necessary.

If the directory already exists, this function is a no-op.

Parameters
dirnameThe directory to create.
lenThe number of bytes of dirname to use as path to create. This makes the recursive call possible without doing string duplication for each parent directory.

Definition at line 354 of file fileops_c.cc.

References BLI_assert, BLI_assert_msg, BLI_exists(), BLI_is_dir(), BLI_path_is_win32_drive_only(), BLI_path_parent_dir_end(), BLI_path_slash_is_native_compat(), dir_create_recursive(), dirname(), len, ret, S_ISDIR, STREQ, and umkdir().

Referenced by BLI_dir_create_recursive(), dir_create_recursive(), and blender::tests::TEST_F().

◆ move_callback_pre()

int move_callback_pre ( const char * from,
const char * to )
static

Definition at line 1490 of file fileops_c.cc.

References copy_callback_pre(), RecursiveOp_Callback_StopRecurs, and ret.

Referenced by BLI_path_move().

◆ move_single_file()

int move_single_file ( const char * from,
const char * to )
static

Definition at line 1501 of file fileops_c.cc.

References copy_single_file(), RecursiveOp_Callback_OK, and ret.

Referenced by BLI_path_move().

◆ path_destination_ensure_filename()

const char * path_destination_ensure_filename ( const char * path_src,
const char * path_dst,
char * buf,
size_t buf_size )
static

◆ path_has_trailing_slash()

bool path_has_trailing_slash ( const char * path)
static

Definition at line 897 of file fileops_c.cc.

References BLI_path_slash_is_native_compat(), and direntry::path.

Referenced by recursive_operation_impl(), and strbuf_append_path().

◆ path_len_no_trailing_slash()

size_t path_len_no_trailing_slash ( const char * path)
static

Definition at line 906 of file fileops_c.cc.

References BLI_path_slash_is_native_compat(), len, and direntry::path.

Referenced by recursive_operation().

◆ recursive_operation()

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

Scans path_src, generating a corresponding destination name for each item found by prefixing it with path_dst, recursively scanning subdirectories, and invoking the specified callbacks for files and subdirectories found as appropriate.

Note
Symbolic links are not followed, even when path_src links to a directory, it wont be recursed down. Support for this could be added.
Parameters
path_srcTop-level source path.
path_dstTop-level destination path.
callback_dir_preOptional, to be invoked before entering a subdirectory, can return RecursiveOp_Callback_StopRecurs to skip the subdirectory.
callback_fileOptional, to be invoked on each file found.
callback_dir_postOptional, to be invoked after leaving a subdirectory.
Returns
Zero on success.

Definition at line 1128 of file fileops_c.cc.

References FILE_MAX, path_len_no_trailing_slash(), recursive_operation_impl(), result, strbuf_free(), and strbuf_init().

Referenced by BLI_copy(), BLI_delete(), and BLI_path_move().

◆ recursive_operation_impl()

◆ set_permissions()

int set_permissions ( const char * filepath,
const struct stat * st )
static

Sets the mode and ownership of file to the values from st.

Definition at line 1339 of file fileops_c.cc.

Referenced by copy_single_file().

◆ strbuf_append_path()

void strbuf_append_path ( StrBuf * buf,
const char * filename )
static

Appending of filename to dir (ensures for buffer size before appending).

Definition at line 951 of file fileops_c.cc.

References BLI_assert, BLI_path_slash_is_native_compat(), len, path_has_trailing_slash(), SEP, StrBuf::str, StrBuf::str_len, and StrBuf::str_len_alloc.

Referenced by recursive_operation_impl().

◆ strbuf_free()

void strbuf_free ( StrBuf * buf)
static

Definition at line 943 of file fileops_c.cc.

References free(), and StrBuf::str.

Referenced by recursive_operation().

◆ strbuf_init()

void strbuf_init ( StrBuf * buf,
const char * str,
size_t str_len,
size_t str_len_alloc )
static

Definition at line 933 of file fileops_c.cc.

References str, StrBuf::str, StrBuf::str_len, and StrBuf::str_len_alloc.

Referenced by recursive_operation().

◆ strbuf_trim()

void strbuf_trim ( StrBuf * buf,
size_t len )
static

Definition at line 972 of file fileops_c.cc.

References BLI_assert, len, StrBuf::str, and StrBuf::str_len.

Referenced by recursive_operation_impl().