Blender V5.0
storage.cc File Reference
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstring>
#include <fcntl.h>
#include <pwd.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "MEM_guardedalloc.h"
#include "BLI_fileops.h"
#include "BLI_linklist.h"
#include "BLI_path_utils.hh"
#include "BLI_string.h"
#include "BLI_threads.h"
#include "BLI_utildefines.h"

Go to the source code of this file.

Functions

bool BLI_change_working_dir (const char *dir)
char * BLI_current_working_dir (char *dir, const size_t maxncpy)
const char * BLI_dir_home ()
double BLI_dir_free_space (const char *dir)
int64_t BLI_ftell (FILE *stream)
int BLI_fseek (FILE *stream, int64_t offset, int whence)
int64_t BLI_lseek (int fd, int64_t offset, int whence)
size_t BLI_file_descriptor_size (int file)
size_t BLI_file_size (const char *path)
eFileAttributes BLI_file_attributes (const char *path)
bool BLI_file_alias_target (const char *filepath, char r_targetpath[FILE_MAXDIR])
int BLI_exists (const char *path)
int BLI_fstat (int fd, struct stat *buffer)
int BLI_stat (const char *path, struct stat *buffer)
bool BLI_is_dir (const char *path)
bool BLI_is_file (const char *path)
void * BLI_file_read_data_as_mem_from_handle (FILE *fp, bool read_size_exact, size_t pad_bytes, size_t *r_size)
void * BLI_file_read_text_as_mem (const char *filepath, size_t pad_bytes, size_t *r_size)
void * BLI_file_read_binary_as_mem (const char *filepath, size_t pad_bytes, size_t *r_size)
void * BLI_file_read_text_as_mem_with_newline_as_nil (const char *filepath, bool trim_trailing_space, size_t pad_bytes, size_t *r_size)
LinkNodeBLI_file_read_as_lines (const char *filepath)
void BLI_file_free_lines (LinkNode *lines)
bool BLI_file_older (const char *file1, const char *file2)

Detailed Description

Some really low-level file operations.

Definition in file storage.cc.

Function Documentation

◆ BLI_change_working_dir()

bool BLI_change_working_dir ( const char * dir)

Changes the current working directory to the provided path.

Usage of this function is strongly discouraged as it is not thread safe. It will likely cause issues if there is an operation on another thread that does not expect the current working directory to change. This has been added to support USDZ export, which has a problematic "feature" described in this issue #99807. It will be removed if it is possible to resolve that issue upstream in the USD library.

Returns
true on success, false otherwise.

Definition at line 63 of file storage.cc.

◆ BLI_current_working_dir()

char * BLI_current_working_dir ( char * dir,
size_t maxncpy )

Copies the current working directory into *dir (max size maxncpy), and returns a pointer to same.

Note
can return NULL when the size is not big enough

Definition at line 81 of file storage.cc.

◆ BLI_dir_free_space()

double BLI_dir_free_space ( const char * dir)

Returns the number of free bytes on the volume containing the specified path.

Note
Not actually used anywhere.

Definition at line 121 of file storage.cc.

References dirname(), ELEM, FILE_MAXDIR, and len.

◆ BLI_dir_home()

const char * BLI_dir_home ( void )

Get the user's home directory, i.e.

  • Unix: $HOME or #passwd::pw_dir.
  • Windows: userprofile%
Returns
The home directory or null when it cannot be accessed.
Note
By convention, failure to access home means any derived directories fail as well instead of attempting to create a fallback such as /, /tmp, C:\\endiskip ... etc. Although there may be rare cases where a fallback is appropriate.

Definition at line 97 of file storage.cc.

References BLI_getenv().

Referenced by BKE_appdir_folder_default(), BKE_appdir_folder_documents(), BKE_appdir_font_folder_default(), file_expand_directory(), fsmenu_read_system(), get_thumb_dir(), and WM_window_title().

◆ BLI_exists()

int BLI_exists ( const char * path)

Returns the st_mode from stat-ing the specified path name, or 0 if stat fails (most likely doesn't exist or no access).

Definition at line 360 of file storage.cc.

References alloc_utf16_from_8(), BLI_assert, BLI_path_is_rel(), ELEM, free(), L, and len.

Referenced by BKE_appdir_app_template_has_userpref(), BKE_appdir_font_folder_default(), BKE_appdir_program_python_search(), BKE_blendfile_userdef_write_app_template(), BKE_cachefile_filepath_get(), BKE_packedfile_unpack_to_file(), BKE_packedfile_write_to_file(), BKE_ptcache_id_exist(), BKE_text_file_modified_check(), BKE_text_file_modified_ignore(), BKE_volume_load(), blf_dir_metrics_search(), BLF_load_unique(), BLI_dir_create_recursive(), BLI_is_dir(), BLI_is_file(), BLI_path_program_search(), BLI_rename(), BLI_rename_overwrite(), blender::io::usd::cache_image_color(), blender::io::hydra::cache_image_file(), blender::seq::check_media_missing(), check_missing_files_foreach_path_cb(), blender::seq::check_sound_media_missing(), blender::io::usd::copy_single_file(), blender::io::usd::copy_tiled_textures(), blender::io::alembic::AlembicExportTest::deleteArchive(), dir_create_recursive(), do_history(), blender::asset_system::AssetCatalogDefinitionFile::ensure_directory_exists(), blender::bke::preferences::exists(), blender::ed::asset::index::AbstractFile::exists(), blender::asset_system::AssetCatalogDefinitionFile::exists_on_disk(), blender::io::alembic::export_endjob(), blender::io::usd::export_endjob(), blender::io::usd::export_endjob_usdz_cleanup(), blender::io::usd::export_in_memory_imbuf(), file_delete_single(), file_directory_new_exec(), file_execute(), file_sfile_filepath_set(), filepath_drop_exec(), handle_load_file(), MANTA::hasConfig(), MANTA::hasData(), MANTA::hasGuiding(), MANTA::hasMesh(), MANTA::hasNoise(), MANTA::hasParticles(), image_save_exec(), IMB_exr_begin_read(), IMB_thumb_delete(), IMB_thumb_manage(), blender::asset_system::AssetCatalogService::load_directory_recursive(), missing_files_find_foreach_path_cb(), MOV_get_existing_proxies(), movieclip_calc_length(), blender::ed::outliner::namebutton_fn(), new_folder_path(), openanim(), blender::io::path_reference_copy(), blender::io::usd::perform_usdz_conversion(), RE_RenderAnim(), blender::gpu::VKPipelinePool::read_from_disk(), blender::gpu::read_spirv_from_disk(), renamebutton_cb(), blender::asset_system::tests::AssetCatalogTest::save_from_memory_into_existing_asset_lib(), screen_opengl_render_anim_step(), blender::seq::seq_proxy_build_frame(), blender::seq::seq_proxy_fetch(), blender::ed::vse::sequencer_export_subtitles_exec(), blender::tests::FileOpsTest::SetUp(), blender::io::usd::UsdExportTest::TearDown(), blender::tests::FileOpsTest::TearDown(), blender::tests::TEST(), blender::asset_system::tests::TEST_F(), blender::asset_system::tests::TEST_F(), blender::asset_system::tests::TEST_F(), blender::asset_system::tests::TEST_F(), blender::asset_system::tests::TEST_F(), blender::tests::TEST_F(), blender::tests::TEST_F(), blender::tests::TEST_F(), touch_file(), blender::ed::object::bake_simulation::try_delete_bake(), txt_write_file(), ui_but_menu_add_path_operators(), ui_popup_context_menu_for_button(), ui_tooltip_from_vfont(), uiTemplateRecentFiles_tooltip_func(), where_am_i(), wm_autosave_delete(), wm_clear_recent_files_exec(), wm_file_write(), wm_homefile_read_ex(), WM_lib_reload(), wm_lib_relocate_exec_do(), wm_save_as_mainfile_exec(), workspace_config_file_read(), workspace_system_file_read(), blender::ui::string_search::write_recent_searches_file(), and blender::asset_system::AssetCatalogDefinitionFile::write_to_disk().

◆ BLI_file_alias_target()

bool BLI_file_alias_target ( const char * filepath,
char r_targetpath[FILE_MAXDIR] )

◆ BLI_file_attributes()

eFileAttributes BLI_file_attributes ( const char * path)

Definition at line 237 of file storage.cc.

◆ BLI_file_descriptor_size()

size_t BLI_file_descriptor_size ( int file)

Returns the file size of an opened file descriptor or size_t(-1) on failure.

Definition at line 217 of file storage.cc.

References BLI_fstat().

Referenced by BKE_packedfile_new(), buffer_from_filepath(), prefetch_read_file_to_memory(), and proxy_thread_next_frame().

◆ BLI_file_free_lines()

void BLI_file_free_lines ( struct LinkNode * lines)

Frees memory from a previous call to BLI_file_read_as_lines.

Definition at line 609 of file storage.cc.

References BLI_linklist_freeN().

Referenced by make(), studiolight_load_solid_light(), wm_history_file_read(), and wm_platform_support_check_approval().

◆ BLI_file_older()

bool BLI_file_older ( const char * file1,
const char * file2 )

Compare if one was last modified before the other.

Returns
true when is file1 older than file2.

Definition at line 614 of file storage.cc.

References BLI_stat().

Referenced by IMB_thumb_manage(), and blender::ed::asset::index::AssetIndexFile::is_older_than().

◆ BLI_file_read_as_lines()

LinkNode * BLI_file_read_as_lines ( const char * filepath)

Reads the contents of a text file.

Returns
the lines in a linked list (an empty list when file reading fails).

Definition at line 563 of file storage.cc.

References BLI_fopen(), BLI_fseek(), BLI_ftell(), BLI_linklist_append(), BLI_strdupn(), i, LinkNodePair::list, MEM_calloc_arrayN(), MEM_freeN(), size(), and UNLIKELY.

Referenced by make(), studiolight_load_solid_light(), wm_history_file_read(), and wm_platform_support_check_approval().

◆ BLI_file_read_binary_as_mem()

void * BLI_file_read_binary_as_mem ( const char * filepath,
size_t pad_bytes,
size_t * r_size )

Definition at line 522 of file storage.cc.

References BLI_file_read_data_as_mem_from_handle(), and BLI_fopen().

Referenced by BKE_icon_geom_from_file().

◆ BLI_file_read_data_as_mem_from_handle()

void * BLI_file_read_data_as_mem_from_handle ( FILE * fp,
bool read_size_exact,
size_t pad_bytes,
size_t * r_size )

Read the contents of fp, returning the result as a buffer or null when it can't be read.

Parameters
r_sizeThe size of the file contents read into the buffer (excluding pad_bytes).

Definition at line 454 of file storage.cc.

References BLI_fseek(), BLI_fstat(), BLI_ftell(), L, MEM_freeN(), MEM_mallocN(), MEM_reallocN, and S_ISDIR.

Referenced by BLI_file_read_binary_as_mem(), BLI_file_read_text_as_mem(), and python_compat_wrapper_PyRun_FileExFlags().

◆ BLI_file_read_text_as_mem()

◆ BLI_file_read_text_as_mem_with_newline_as_nil()

void * BLI_file_read_text_as_mem_with_newline_as_nil ( const char * filepath,
bool trim_trailing_space,
size_t pad_bytes,
size_t * r_size )

Return the text file data with:

  • Newlines replaced with '\0'.
  • Optionally trim white-space, replacing trailing <space> & <tab> with '\0'.

This is an alternative to using BLI_file_read_as_lines, allowing us to loop over lines without converting it into a linked list with individual allocations.

Parameters
trim_trailing_spaceReplace trailing spaces & tabs with nil. This arguments prevents the caller from counting blank lines (if that's important).
pad_bytesWhen this is non-zero, the first byte is set to nil, to simplify parsing the file. It's recommended to pass in 1, so all text is nil terminated.
r_sizeThe size of the file contents read into the buffer (excluding pad_bytes).

Example looping over lines:

size_t data_len;
char *data = BLI_file_read_text_as_mem_with_newline_as_nil(filepath, true, 1, &data_len);
char *data_end = data + data_len;
for (char *line = data; line != data_end; line = strlen(line) + 1) {
printf("line='%s'\n", line);
}
void * BLI_file_read_text_as_mem_with_newline_as_nil(const char *filepath, bool trim_trailing_space, size_t pad_bytes, size_t *r_size)
Definition storage.cc:533
BMesh const char void * data
#define printf(...)

Definition at line 533 of file storage.cc.

References BLI_file_read_text_as_mem(), and ELEM.

◆ BLI_file_size()

◆ BLI_fseek()

◆ BLI_fstat()

int BLI_fstat ( int fd,
struct stat * buffer )

Definition at line 432 of file storage.cc.

Referenced by BLI_file_descriptor_size(), and BLI_file_read_data_as_mem_from_handle().

◆ BLI_ftell()

int64_t BLI_ftell ( FILE * stream)

◆ BLI_is_dir()

bool BLI_is_dir ( const char * path)

Does the specified path point to a directory?

Note
Would be better in fileops.cc except that it needs stat.h so add here.

Definition at line 443 of file storage.cc.

References BLI_exists(), and S_ISDIR.

Referenced by blender::asset_system::all_valid_asset_library_refs(), blender::ed::object::bake_simulation::bake_directory_has_data(), BKE_appdir_app_template_id_search(), BKE_appdir_folder_caches(), BKE_appdir_folder_documents(), BKE_blendfile_library_path_explode(), BKE_tempdir_session_purge(), BLI_change_working_dir(), BLI_rename_overwrite(), BLI_temp_directory_path_copy_if_valid(), bookmark_cleanup_exec(), can_create_dir_from_user_input(), blender::ed::asset::index::AssetLibraryIndex::collect_preexisting_file_indices(), dir_create_recursive(), blender::asset_system::AssetCatalogDefinitionFile::ensure_directory_exists(), file_sfile_filepath_set(), filelist_checkdir_dir(), filelist_checkdir_lib(), filelist_readjob_list_dir(), filelist_readjob_recursive_dir_add_items(), blender::bke::bake::find_sorted_meta_files(), fsmenu_read_system(), blender::asset_system::PreferencesOnDiskAssetLibrary::library_reference(), blender::io::usd::load_image(), blender::io::usd::USDMaterialReader::load_tex_image(), blender::bke::bake::pack_files_from_directory(), blender::io::usd::parent_dir_exists_on_file_system(), path_destination_ensure_filename(), preferences_extension_repo_remove_exec(), blender::io::usd::process_usdz_textures(), tempdir_session_create(), blender::tests::TEST(), test_env_path(), blender::asset_system::tests::TEST_F(), blender::asset_system::tests::TEST_F(), and test_path().

◆ BLI_is_file()

◆ BLI_lseek()

int64_t BLI_lseek ( int fd,
int64_t offset,
int whence )

Definition at line 208 of file storage.cc.

Referenced by BLI_mmap_open(), and file_seek().

◆ BLI_stat()

int BLI_stat ( const char * path,
struct stat * buffer )

Definition at line 437 of file storage.cc.

Referenced by BLI_file_older(), and BLI_file_size().