Blender  V3.3
Functions
storage.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.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_util.h"
#include "BLI_string.h"
#include "BLI_utildefines.h"

Go to the source code of this file.

Functions

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 *file)
 
bool BLI_is_file (const char *path)
 
static voidfile_read_data_as_mem_impl (FILE *fp, bool read_size_exact, size_t pad_bytes, size_t *r_size)
 
voidBLI_file_read_text_as_mem (const char *filepath, size_t pad_bytes, size_t *r_size)
 
voidBLI_file_read_binary_as_mem (const char *filepath, size_t pad_bytes, size_t *r_size)
 
voidBLI_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.c.

Function Documentation

◆ 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 59 of file storage.c.

References BLI_getenv(), BLI_strncpy_wchar_as_utf8(), BLI_strnlen(), and NULL.

Referenced by BLI_path_abs_from_cwd().

◆ BLI_dir_free_space()

double BLI_dir_free_space ( const char *  dir)

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

Note
Not actually used anywhere.

Definition at line 83 of file storage.c.

References 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 314 of file storage.c.

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

Referenced by BKE_appdir_app_template_has_userpref(), BKE_appdir_font_folder_default(), BKE_appdir_program_python_search(), BKE_cachefile_filepath_get(), BKE_packedfile_compare_to_file(), BKE_packedfile_unpack_to_file(), BKE_ptcache_id_exist(), BKE_text_file_modified_check(), BKE_text_file_modified_ignore(), BKE_volume_load(), blf_dir_metrics_search(), blf_dir_search(), BLF_load_font_stack(), BLI_dir_create_recursive(), BLI_is_dir(), BLI_is_file(), BLI_path_program_search(), BLI_rename(), check_missing_files_foreach_path_cb(), blender::io::usd::copy_single_file(), blender::io::usd::copy_tiled_textures(), blender::compositor::DebugInfo::delete_operation_exports(), blender::io::alembic::AlembicExportTest::deleteArchive(), do_history(), blender::bke::AssetCatalogDefinitionFile::ensure_directory_exists(), blender::ed::asset::index::AbstractFile::exists(), blender::io::alembic::export_endjob(), blender::io::usd::export_endjob(), 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(), 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::bke::AssetCatalogService::load_directory_recursive(), missing_files_find_foreach_path_cb(), movieclip_calc_length(), namebutton_fn(), new_folder_path(), openanim(), blender::io::path_reference_copy(), RE_RenderAnim(), renamebutton_cb(), blender::bke::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::bke::tests::TEST_F(), txt_write_file(), where_am_i(), wm_autosave_delete(), wm_autosave_location(), wm_block_create_splash(), wm_file_write(), wm_homefile_read_ex(), WM_lib_reload(), wm_lib_relocate_exec_do(), workspace_config_file_read(), workspace_system_file_read(), blender::bke::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 198 of file storage.c.

◆ BLI_file_descriptor_size()

size_t BLI_file_descriptor_size ( int  file)

Returns the file size of an opened file descriptor.

Definition at line 178 of file storage.c.

References BLI_fstat(), file, and usdtokens::st().

Referenced by BKE_packedfile_new(), build_pict_list_ex(), IMB_loadifffile(), imb_loadtilefile(), IMMapStream::IMMapStream(), 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 564 of file storage.c.

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 569 of file storage.c.

References BLI_stat().

Referenced by imb_cache_filename(), 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 *  file)

Reads the contents of a text file.

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

Definition at line 518 of file storage.c.

References BLI_fopen(), BLI_fseek(), BLI_ftell(), BLI_linklist_append(), BLI_strdupn(), LinkNodePair::list, MEM_freeN, MEM_mallocN, NULL, 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 477 of file storage.c.

References BLI_fopen(), file_read_data_as_mem_impl(), and NULL.

Referenced by BKE_icon_geom_from_file().

◆ BLI_file_read_text_as_mem()

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

◆ 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.

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.c:488

Definition at line 488 of file storage.c.

References BLI_file_read_text_as_mem(), ELEM, and NULL.

◆ BLI_file_size()

size_t BLI_file_size ( const char *  path)

◆ BLI_fseek()

int BLI_fseek ( FILE *  stream,
int64_t  offset,
int  whence 
)

◆ BLI_fstat()

int BLI_fstat ( int  fd,
struct stat *  buffer 
)

Definition at line 386 of file storage.c.

References buffer.

Referenced by BLI_file_descriptor_size(), and file_read_data_as_mem_impl().

◆ BLI_ftell()

int64_t BLI_ftell ( FILE *  stream)

◆ BLI_is_dir()

bool BLI_is_dir ( const char *  path)

◆ BLI_is_file()

bool BLI_is_file ( const char *  path)

◆ BLI_lseek()

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

Definition at line 169 of file storage.c.

References offset.

Referenced by BLI_filereader_new_mmap(), BLI_mmap_open(), and file_seek().

◆ BLI_stat()

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

Definition at line 391 of file storage.c.

References buffer.

Referenced by BLI_file_older(), and BLI_file_size().

◆ file_read_data_as_mem_impl()

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

Use for both text and binary file reading.

Definition at line 411 of file storage.c.

References BLI_fseek(), BLI_fstat(), BLI_ftell(), L, MEM_freeN, MEM_mallocN, MEM_reallocN, NULL, S_ISDIR, and usdtokens::st().

Referenced by BLI_file_read_binary_as_mem(), and BLI_file_read_text_as_mem().