Blender  V3.3
BLI_fileops.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
9 #pragma once
10 
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <sys/stat.h>
14 
15 /* for size_t (needed on windows) */
16 #include <stddef.h>
17 
18 #include <limits.h> /* for PATH_MAX */
19 
20 #include "BLI_compiler_attrs.h"
21 #include "BLI_fileops_types.h"
22 #include "BLI_utildefines.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #ifndef PATH_MAX
29 # define PATH_MAX 4096
30 #endif
31 
32 /* -------------------------------------------------------------------- */
40 int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
41 int BLI_copy(const char *file, const char *to) ATTR_NONNULL();
45 int BLI_rename(const char *from, const char *to) ATTR_NONNULL();
52 int BLI_delete(const char *file, bool dir, bool recursive) ATTR_NONNULL();
59 int BLI_delete_soft(const char *file, const char **error_message) ATTR_NONNULL();
60 #if 0 /* Unused */
61 int BLI_move(const char *path, const char *to) ATTR_NONNULL();
62 int BLI_create_symlink(const char *path, const char *to) ATTR_NONNULL();
63 #endif
64 
65 /* Keep in sync with the definition of struct `direntry` in `BLI_fileops_types.h`. */
66 #ifdef WIN32
67 # if defined(_MSC_VER)
68 typedef struct _stat64 BLI_stat_t;
69 # else
70 typedef struct _stat BLI_stat_t;
71 # endif
72 #else
73 typedef struct stat BLI_stat_t;
74 #endif
75 
79 int BLI_fseek(FILE *stream, int64_t offset, int whence);
80 int64_t BLI_lseek(int fd, int64_t offset, int whence);
81 
82 #ifdef WIN32
83 int BLI_wstat(const wchar_t *path, BLI_stat_t *buffer);
84 #endif
85 
86 typedef enum eFileAttributes {
87  FILE_ATTR_READONLY = 1 << 0, /* Read-only or Immutable. */
88  FILE_ATTR_HIDDEN = 1 << 1, /* Hidden or invisible. */
89  FILE_ATTR_SYSTEM = 1 << 2, /* Used by the Operating System. */
90  FILE_ATTR_ARCHIVE = 1 << 3, /* Marked as archived. */
91  FILE_ATTR_COMPRESSED = 1 << 4, /* Compressed. */
92  FILE_ATTR_ENCRYPTED = 1 << 5, /* Encrypted. */
93  FILE_ATTR_RESTRICTED = 1 << 6, /* Protected by OS. */
94  FILE_ATTR_TEMPORARY = 1 << 7, /* Used for temporary storage. */
95  FILE_ATTR_SPARSE_FILE = 1 << 8, /* Sparse File. */
96  FILE_ATTR_OFFLINE = 1 << 9, /* Contents available after a short delay. */
97  FILE_ATTR_ALIAS = 1 << 10, /* Mac Alias or Windows LNK. File-based redirection. */
98  FILE_ATTR_REPARSE_POINT = 1 << 11, /* File has associated re-parse point. */
99  FILE_ATTR_SYMLINK = 1 << 12, /* Reference to another file. */
100  FILE_ATTR_JUNCTION_POINT = 1 << 13, /* Folder Symbolic-link. */
101  FILE_ATTR_MOUNT_POINT = 1 << 14, /* Volume mounted as a folder. */
102  FILE_ATTR_HARDLINK = 1 << 15, /* Duplicated directory entry. */
104 
105 #define FILE_ATTR_ANY_LINK \
106  (FILE_ATTR_ALIAS | FILE_ATTR_REPARSE_POINT | FILE_ATTR_SYMLINK | FILE_ATTR_JUNCTION_POINT | \
107  FILE_ATTR_MOUNT_POINT | FILE_ATTR_HARDLINK)
108 
111 /* -------------------------------------------------------------------- */
115 struct direntry;
116 
129 bool BLI_dir_create_recursive(const char *dir) ATTR_NONNULL();
142 char *BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
144 
147 /* -------------------------------------------------------------------- */
157 unsigned int BLI_filelist_dir_contents(const char *dir, struct direntry **r_filelist);
161 void BLI_filelist_entry_duplicate(struct direntry *dst, const struct direntry *src);
165 void BLI_filelist_duplicate(struct direntry **dest_filelist,
166  struct direntry *const src_filelist,
167  unsigned int nrentries);
171 void BLI_filelist_entry_free(struct direntry *entry);
175 void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries);
176 
180 void BLI_filelist_entry_size_to_string(const struct stat *st,
181  uint64_t st_size_fallback,
182  bool compact,
183  char r_size[FILELIST_DIRENTRY_SIZE_LEN]);
187 void BLI_filelist_entry_mode_to_string(const struct stat *st,
188  bool compact,
189  char r_mode1[FILELIST_DIRENTRY_MODE_LEN],
190  char r_mode2[FILELIST_DIRENTRY_MODE_LEN],
191  char r_mode3[FILELIST_DIRENTRY_MODE_LEN]);
195 void BLI_filelist_entry_owner_to_string(const struct stat *st,
196  bool compact,
197  char r_owner[FILELIST_DIRENTRY_OWNER_LEN]);
204 void BLI_filelist_entry_datetime_to_string(const struct stat *st,
205  int64_t ts,
206  bool compact,
207  char r_time[FILELIST_DIRENTRY_TIME_LEN],
208  char r_date[FILELIST_DIRENTRY_DATE_LEN],
209  bool *r_is_today,
210  bool *r_is_yesterday);
211 
214 /* -------------------------------------------------------------------- */
218 FILE *BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
219 void *BLI_gzopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
220 int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
221 int BLI_access(const char *filepath, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
222 
229 bool BLI_file_is_writable(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
234 bool BLI_file_touch(const char *file) ATTR_NONNULL();
235 bool BLI_file_alias_target(const char *filepath, char *r_targetpath) ATTR_WARN_UNUSED_RESULT;
236 
237 bool BLI_file_magic_is_gzip(const char header[4]);
238 
239 size_t BLI_file_zstd_from_mem_at_pos(void *buf,
240  size_t len,
241  FILE *file,
242  size_t file_offset,
243  int compression_level) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
244 size_t BLI_file_unzstd_to_mem_at_pos(void *buf, size_t len, FILE *file, size_t file_offset)
246 bool BLI_file_magic_is_zstd(const char header[4]);
247 
256 
262 bool BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
263 
270 void *BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size);
298 void *BLI_file_read_text_as_mem_with_newline_as_nil(const char *filepath,
299  bool trim_trailing_space,
300  size_t pad_bytes,
301  size_t *r_size);
302 void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size);
306 void BLI_file_free_lines(struct LinkNode *lines);
307 
308 #ifdef __APPLE__
314 const char *BLI_expand_tilde(const char *path_with_tilde);
315 #endif
316 /* This weirdo pops up in two places. */
317 #if !defined(WIN32)
318 # ifndef O_BINARY
319 # define O_BINARY 0
320 # endif
321 #else
322 void BLI_get_short_name(char short_name[256], const char *filepath);
323 #endif
324 
327 #ifdef __cplusplus
328 }
329 #endif
#define ATTR_WARN_UNUSED_RESULT
#define ATTR_NONNULL(...)
eFileAttributes BLI_file_attributes(const char *path)
Definition: storage.c:198
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:314
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:49
void * BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition: storage.c:477
bool BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:569
bool BLI_file_is_writable(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: fileops.c:167
bool BLI_file_touch(const char *file) ATTR_NONNULL()
Definition: fileops.c:192
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: fileops.c:906
void * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition: storage.c:466
bool BLI_file_magic_is_gzip(const char header[4])
Definition: fileops.c:133
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.c:178
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:402
char * BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:59
int BLI_delete(const char *file, bool dir, bool recursive) ATTR_NONNULL()
Definition: fileops.c:934
void * BLI_gzopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: fileops.c:913
int64_t BLI_ftell(FILE *stream) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:151
unsigned int BLI_filelist_dir_contents(const char *dir, struct direntry **r_filelist)
Definition: BLI_filelist.c:218
int64_t BLI_lseek(int fd, int64_t offset, int whence)
Definition: storage.c:169
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:1268
void BLI_file_free_lines(struct LinkNode *lines)
Definition: storage.c:564
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:96
void BLI_filelist_duplicate(struct direntry **dest_filelist, struct direntry *const src_filelist, unsigned int nrentries)
Definition: BLI_filelist.c:396
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.c:187
bool BLI_file_magic_is_zstd(const char header[4])
Definition: fileops.c:140
struct stat BLI_stat_t
Definition: BLI_fileops.h:73
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:397
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
Definition: BLI_filelist.c:420
bool BLI_dir_create_recursive(const char *dir) ATTR_NONNULL()
Definition: fileops.c:1219
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
int BLI_fstat(int fd, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_filelist_entry_free(struct direntry *entry)
Definition: BLI_filelist.c:410
struct LinkNode * BLI_file_read_as_lines(const char *file) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:518
int BLI_access(const char *filepath, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: fileops.c:927
void BLI_filelist_entry_duplicate(struct direntry *dst, const struct direntry *src)
Definition: BLI_filelist.c:385
int BLI_copy(const char *file, const char *to) ATTR_NONNULL()
Definition: fileops.c:1198
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)
Definition: BLI_filelist.c:323
eFileAttributes
Definition: BLI_fileops.h:86
@ FILE_ATTR_SPARSE_FILE
Definition: BLI_fileops.h:95
@ FILE_ATTR_COMPRESSED
Definition: BLI_fileops.h:91
@ FILE_ATTR_ENCRYPTED
Definition: BLI_fileops.h:92
@ FILE_ATTR_HARDLINK
Definition: BLI_fileops.h:102
@ FILE_ATTR_ALIAS
Definition: BLI_fileops.h:97
@ FILE_ATTR_TEMPORARY
Definition: BLI_fileops.h:94
@ FILE_ATTR_ARCHIVE
Definition: BLI_fileops.h:90
@ FILE_ATTR_REPARSE_POINT
Definition: BLI_fileops.h:98
@ FILE_ATTR_MOUNT_POINT
Definition: BLI_fileops.h:101
@ FILE_ATTR_HIDDEN
Definition: BLI_fileops.h:88
@ FILE_ATTR_JUNCTION_POINT
Definition: BLI_fileops.h:100
@ FILE_ATTR_READONLY
Definition: BLI_fileops.h:87
@ FILE_ATTR_RESTRICTED
Definition: BLI_fileops.h:93
@ FILE_ATTR_SYMLINK
Definition: BLI_fileops.h:99
@ FILE_ATTR_SYSTEM
Definition: BLI_fileops.h:89
@ FILE_ATTR_OFFLINE
Definition: BLI_fileops.h:96
double BLI_dir_free_space(const char *dir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:83
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition: storage.c:160
int BLI_delete_soft(const char *file, const char **error_message) ATTR_NONNULL()
Definition: fileops.c:947
bool BLI_file_alias_target(const char *filepath, char *r_targetpath) ATTR_WARN_UNUSED_RESULT
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: fileops.c:920
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
ATTR_WARN_UNUSED_RESULT const BMFlagLayer const short oflag
StackEntry * from
FILE * file
SyclQueue void void * src
int len
Definition: draw_manager.c:108
ccl_global float * buffer
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
static const pxr::TfToken st("st", pxr::TfToken::Immortal)
__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