Blender V4.3
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)
 
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[])
 
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 85 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 110 of file storage.cc.

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

◆ 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 350 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::compositor::DebugInfo::delete_operation_exports(), blender::io::alembic::AlembicExportTest::deleteArchive(), dir_create_recursive(), do_history(), blender::asset_system::AssetCatalogDefinitionFile::ensure_directory_exists(), blender::ed::asset::index::AbstractFile::exists(), blender::io::alembic::export_endjob(), blender::io::usd::export_endjob(), blender::io::usd::export_endjob_usdz_cleanup(), blender::io::usd::export_in_memory_texture(), file_delete_single(), file_directory_new_exec(), file_execute(), file_sfile_filepath_set(), filepath_drop_exec(), fsmenu_read_bookmarks(), handle_load_file(), MANTA::hasConfig(), MANTA::hasData(), MANTA::hasGuiding(), MANTA::hasMesh(), MANTA::hasNoise(), MANTA::hasParticles(), image_save_exec(), IMB_anim_proxy_get_existing(), IMB_exr_begin_read(), IMB_thumb_delete(), IMB_thumb_manage(), blender::asset_system::AssetCatalogService::load_directory_recursive(), missing_files_find_foreach_path_cb(), 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(), seq_disk_cache_enforce_limits(), seq_disk_cache_handle_versioning(), seq_proxy_build_frame(), seq_proxy_fetch(), sequencer_export_subtitles_exec(), blender::tests::FileOpsTest::SetUp(), blender::io::usd::UsdCurvesTest::TearDown(), 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::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_block_splash_create(), wm_clear_recent_files_exec(), wm_file_write(), wm_homefile_read_ex(), wm_init_splash_show_on_startup_check(), 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(), blender::asset_system::AssetCatalogDefinitionFile::write_to_disk(), and DocumentImporter::writeImage().

◆ BLI_file_alias_target()

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

◆ BLI_file_attributes()

eFileAttributes BLI_file_attributes ( const char * path)

Definition at line 226 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 206 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 600 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 605 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 554 of file storage.cc.

References BLI_fopen(), BLI_fseek(), BLI_ftell(), BLI_linklist_append(), BLI_strdupn(), LinkNodePair::list, 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 513 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 )

Use for both text and binary file reading.

Definition at line 447 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:524
#define printf

Definition at line 524 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 422 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)

Definition at line 179 of file storage.cc.

Referenced by BLI_file_read_as_lines(), and BLI_file_read_data_as_mem_from_handle().

◆ 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 433 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(), blender::ed::asset::index::AssetLibraryIndex::collect_preexisting_file_indices(), blender::asset_system::AssetCatalogDefinitionFile::ensure_directory_exists(), file_browse_exec(), 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_entry_refresh_valid(), fsmenu_read_system(), 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(), seq_disk_cache_get_files(), seq_disk_cache_handle_versioning(), 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 197 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 427 of file storage.cc.

Referenced by BLI_file_older(), and BLI_file_size().