Blender V4.3
BLI_fileops.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10#pragma once
11
12#include <stdint.h>
13#include <stdio.h>
14#include <sys/stat.h>
15
16/* for size_t (needed on windows) */
17#include <stddef.h>
18
19#include <limits.h> /* for PATH_MAX */
20
21#include "BLI_compiler_attrs.h"
22#include "BLI_fileops_types.h"
23#include "BLI_utildefines.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#ifndef PATH_MAX
30# define PATH_MAX 4096
31#endif
32
33/* -------------------------------------------------------------------- */
41int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
42
46int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL();
52int BLI_path_move(const char *path_src, const char *path_dst) ATTR_NONNULL();
53
64int BLI_rename(const char *from, const char *to) ATTR_NONNULL();
65
93int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL();
111int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL();
118int BLI_delete_soft(const char *filepath, const char **r_error_message) ATTR_NONNULL();
119#if 0 /* Unused */
120int BLI_create_symlink(const char *path, const char *path_dst) ATTR_NONNULL();
121#endif
122
123/* Keep in sync with the definition of struct `direntry` in `BLI_fileops_types.h`. */
124#ifdef WIN32
125# if defined(_MSC_VER)
126typedef struct _stat64 BLI_stat_t;
127# else
128typedef struct _stat BLI_stat_t;
129# endif
130#else
131typedef struct stat BLI_stat_t;
132#endif
133
137int BLI_fseek(FILE *stream, int64_t offset, int whence);
138int64_t BLI_lseek(int fd, int64_t offset, int whence);
139
140#ifdef WIN32
141int BLI_wstat(const wchar_t *path, BLI_stat_t *buffer);
142#endif
143
144typedef enum eFileAttributes {
145 FILE_ATTR_READONLY = 1 << 0, /* Read-only or Immutable. */
146 FILE_ATTR_HIDDEN = 1 << 1, /* Hidden or invisible. */
147 FILE_ATTR_SYSTEM = 1 << 2, /* Used by the Operating System. */
148 FILE_ATTR_ARCHIVE = 1 << 3, /* Marked as archived. */
149 FILE_ATTR_COMPRESSED = 1 << 4, /* Compressed. */
150 FILE_ATTR_ENCRYPTED = 1 << 5, /* Encrypted. */
151 FILE_ATTR_RESTRICTED = 1 << 6, /* Protected by OS. */
152 FILE_ATTR_TEMPORARY = 1 << 7, /* Used for temporary storage. */
153 FILE_ATTR_SPARSE_FILE = 1 << 8, /* Sparse File. */
154 FILE_ATTR_OFFLINE = 1 << 9, /* Contents available after a short delay. */
155 FILE_ATTR_ALIAS = 1 << 10, /* Mac Alias or Windows LNK. File-based redirection. */
156 FILE_ATTR_REPARSE_POINT = 1 << 11, /* File has associated re-parse point. */
157 FILE_ATTR_SYMLINK = 1 << 12, /* Reference to another file. */
158 FILE_ATTR_JUNCTION_POINT = 1 << 13, /* Folder Symbolic-link. */
159 FILE_ATTR_MOUNT_POINT = 1 << 14, /* Volume mounted as a folder. */
160 FILE_ATTR_HARDLINK = 1 << 15, /* Duplicated directory entry. */
163
164#define FILE_ATTR_ANY_LINK \
165 (FILE_ATTR_ALIAS | FILE_ATTR_REPARSE_POINT | FILE_ATTR_SYMLINK | FILE_ATTR_JUNCTION_POINT | \
166 FILE_ATTR_MOUNT_POINT | FILE_ATTR_HARDLINK)
167
170/* -------------------------------------------------------------------- */
192
193bool BLI_file_external_operation_supported(const char *filepath, FileExternalOperation operation);
194bool BLI_file_external_operation_execute(const char *filepath, FileExternalOperation operation);
195
198/* -------------------------------------------------------------------- */
202struct direntry;
203
229char *BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
242bool BLI_change_working_dir(const char *dir);
243
246/* -------------------------------------------------------------------- */
256unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist);
260void BLI_filelist_entry_duplicate(struct direntry *dst, const struct direntry *src);
264void BLI_filelist_duplicate(struct direntry **dest_filelist,
265 struct direntry *const src_filelist,
266 unsigned int nrentries);
270void BLI_filelist_entry_free(struct direntry *entry);
274void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries);
275
279void BLI_filelist_entry_size_to_string(const struct stat *st,
280 uint64_t st_size_fallback,
281 bool compact,
282 char r_size[FILELIST_DIRENTRY_SIZE_LEN]);
286void BLI_filelist_entry_mode_to_string(const struct stat *st,
287 bool compact,
288 char r_mode1[FILELIST_DIRENTRY_MODE_LEN],
289 char r_mode2[FILELIST_DIRENTRY_MODE_LEN],
290 char r_mode3[FILELIST_DIRENTRY_MODE_LEN]);
294void BLI_filelist_entry_owner_to_string(const struct stat *st,
295 bool compact,
296 char r_owner[FILELIST_DIRENTRY_OWNER_LEN]);
303void BLI_filelist_entry_datetime_to_string(const struct stat *st,
304 int64_t ts,
305 bool compact,
306 char r_time[FILELIST_DIRENTRY_TIME_LEN],
307 char r_date[FILELIST_DIRENTRY_DATE_LEN],
308 bool *r_is_today,
309 bool *r_is_yesterday);
310
313/* -------------------------------------------------------------------- */
317FILE *BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
318void *BLI_gzopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
319int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
320int BLI_access(const char *filepath, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
321
328int64_t BLI_read(int fd, void *buf, size_t nbytes);
329
341bool BLI_file_touch(const char *filepath) ATTR_NONNULL(1);
347bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1);
348
349bool BLI_file_alias_target(const char *filepath, char *r_targetpath) ATTR_WARN_UNUSED_RESULT;
350
351bool BLI_file_magic_is_gzip(const char header[4]);
352
353size_t BLI_file_zstd_from_mem_at_pos(void *buf,
354 size_t len,
355 FILE *file,
356 size_t file_offset,
357 int compression_level) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
358size_t BLI_file_unzstd_to_mem_at_pos(void *buf, size_t len, FILE *file, size_t file_offset)
360bool BLI_file_magic_is_zstd(const char header[4]);
361
370
376bool BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
377
383struct LinkNode *BLI_file_read_as_lines(const char *filepath) ATTR_WARN_UNUSED_RESULT
384 ATTR_NONNULL();
385
392 bool read_size_exact,
393 size_t pad_bytes,
394 size_t *r_size);
395
396void *BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size);
425void *BLI_file_read_text_as_mem_with_newline_as_nil(const char *filepath,
426 bool trim_trailing_space,
427 size_t pad_bytes,
428 size_t *r_size);
429void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size);
433void BLI_file_free_lines(struct LinkNode *lines);
434
435#ifdef __APPLE__
441const char *BLI_expand_tilde(const char *path_with_tilde);
442#endif
443/* This weirdo pops up in two places. */
444#if !defined(WIN32)
445# ifndef O_BINARY
446# define O_BINARY 0
447# endif
448#else
449void BLI_get_short_name(char short_name[256], const char *filepath);
450#endif
451
454#ifdef __cplusplus
455}
456#endif
#define ATTR_WARN_UNUSED_RESULT
#define ATTR_NONNULL(...)
bool BLI_change_working_dir(const char *dir)
Definition storage.cc:63
bool BLI_file_touch(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:316
void * BLI_gzopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
eFileAttributes BLI_file_attributes(const char *path)
Definition storage.cc:226
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:350
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()
Definition fileops_c.cc:173
bool BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:605
bool BLI_file_is_writable(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition fileops_c.cc:291
int BLI_delete_soft(const char *filepath, const char **r_error_message) ATTR_NONNULL()
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BLI_file_magic_is_gzip(const char header[4])
Definition fileops_c.cc:257
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_filelist_entry_owner_to_string(const struct stat *st, bool compact, char r_owner[FILELIST_DIRENTRY_OWNER_LEN])
size_t BLI_file_descriptor_size(int file) ATTR_WARN_UNUSED_RESULT
Definition storage.cc:206
bool BLI_file_external_operation_supported(const char *filepath, FileExternalOperation operation)
Definition fileops_c.cc:146
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:391
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void * BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:513
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:438
FileExternalOperation
@ FILE_EXTERNAL_OPERATION_FOLDER_OPEN
@ FILE_EXTERNAL_OPERATION_FOLDER_CMD
@ FILE_EXTERNAL_OPERATION_OPEN
@ FILE_EXTERNAL_OPERATION_PRINT
@ FILE_EXTERNAL_OPERATION_INSTALL
@ FILE_EXTERNAL_OPERATION_PLAY
@ FILE_EXTERNAL_OPERATION_BROWSE
@ FILE_EXTERNAL_OPERATION_PREVIEW
@ FILE_EXTERNAL_OPERATION_RUNAS
@ FILE_EXTERNAL_OPERATION_FOLDER_FIND
@ FILE_EXTERNAL_OPERATION_NEW
@ FILE_EXTERNAL_OPERATION_EDIT
@ FILE_EXTERNAL_OPERATION_PROPERTIES
@ FILE_EXTERNAL_OPERATION_FIND
@ FILE_EXTERNAL_OPERATION_SHOW
int64_t BLI_ftell(FILE *stream) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:179
struct LinkNode * BLI_file_read_as_lines(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:554
int64_t BLI_lseek(int fd, int64_t offset, int whence)
Definition storage.cc:197
void BLI_filelist_entry_size_to_string(const struct stat *st, uint64_t st_size_fallback, bool compact, char r_size[FILELIST_DIRENTRY_SIZE_LEN])
int BLI_rename(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:438
void BLI_file_free_lines(struct LinkNode *lines)
Definition storage.cc:600
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()
Definition fileops_c.cc:220
void BLI_filelist_duplicate(struct direntry **dest_filelist, struct direntry *const src_filelist, unsigned int nrentries)
void BLI_filelist_entry_mode_to_string(const struct stat *st, bool compact, char r_mode1[FILELIST_DIRENTRY_MODE_LEN], char r_mode2[FILELIST_DIRENTRY_MODE_LEN], char r_mode3[FILELIST_DIRENTRY_MODE_LEN])
size_t BLI_file_size(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:215
bool BLI_file_magic_is_zstd(const char header[4])
Definition fileops_c.cc:264
struct stat BLI_stat_t
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:433
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
int BLI_fstat(int fd, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_filelist_entry_free(struct direntry *entry)
char * BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:85
int BLI_access(const char *filepath, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:505
void BLI_filelist_entry_duplicate(struct direntry *dst, const struct direntry *src)
void BLI_filelist_entry_datetime_to_string(const struct stat *st, int64_t ts, bool compact, char r_time[FILELIST_DIRENTRY_TIME_LEN], char r_date[FILELIST_DIRENTRY_DATE_LEN], bool *r_is_today, bool *r_is_yesterday)
eFileAttributes
@ FILE_ATTR_SPARSE_FILE
@ FILE_ATTR_COMPRESSED
@ FILE_ATTR_ENCRYPTED
@ FILE_ATTR_HARDLINK
@ FILE_ATTR_ALIAS
@ FILE_ATTR_TEMPORARY
@ FILE_ATTR_ARCHIVE
@ FILE_ATTR_REPARSE_POINT
@ FILE_ATTR_MOUNT_POINT
@ FILE_ATTR_HIDDEN
@ FILE_ATTR_JUNCTION_POINT
@ FILE_ATTR_READONLY
@ FILE_ATTR_RESTRICTED
@ FILE_ATTR_SYMLINK
@ FILE_ATTR_SYSTEM
@ FILE_ATTR_OFFLINE
double BLI_dir_free_space(const char *dir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:110
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition storage.cc:188
void * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:502
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
bool BLI_file_alias_target(const char *filepath, char *r_targetpath) ATTR_WARN_UNUSED_RESULT
int BLI_path_move(const char *path_src, const char *path_dst) ATTR_NONNULL()
bool BLI_file_external_operation_execute(const char *filepath, FileExternalOperation operation)
Definition fileops_c.cc:157
void * BLI_file_read_data_as_mem_from_handle(FILE *fp, bool read_size_exact, size_t pad_bytes, size_t *r_size)
Definition storage.cc:447
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int64_t BLI_read(int fd, void *buf, size_t nbytes)
Definition fileops_c.cc:96
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:429
Some types for dealing with directories.
#define FILELIST_DIRENTRY_SIZE_LEN
#define FILELIST_DIRENTRY_MODE_LEN
#define FILELIST_DIRENTRY_OWNER_LEN
#define FILELIST_DIRENTRY_DATE_LEN
#define FILELIST_DIRENTRY_TIME_LEN
#define ENUM_OPERATORS(_type, _max)
const char * dirname(char *path)
ATTR_WARN_UNUSED_RESULT const BMFlagLayer const short oflag
int len
__int64 int64_t
Definition stdint.h:89
unsigned __int64 uint64_t
Definition stdint.h:90
const char * BLI_expand_tilde(const char *path_with_tilde)
const char * path