Blender V5.0
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
9
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#ifndef PATH_MAX
26# define PATH_MAX 4096
27#endif
28
29/* -------------------------------------------------------------------- */
32
37int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
38
42int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL();
48int BLI_path_move(const char *path_src, const char *path_dst) ATTR_NONNULL();
49
60int BLI_rename(const char *from, const char *to) ATTR_NONNULL();
61
89int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL();
107int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL();
114int BLI_delete_soft(const char *filepath, const char **r_error_message) ATTR_NONNULL();
115#if 0 /* Unused */
116int BLI_create_symlink(const char *path, const char *path_dst) ATTR_NONNULL();
117#endif
118
119/* Keep in sync with the definition of struct `direntry` in `BLI_fileops_types.h`. */
120#ifdef WIN32
121# if defined(_MSC_VER)
122typedef struct _stat64 BLI_stat_t;
123# else
124typedef struct _stat BLI_stat_t;
125# endif
126#else
127typedef struct stat BLI_stat_t;
128#endif
129
133int BLI_fseek(FILE *stream, int64_t offset, int whence);
134int64_t BLI_lseek(int fd, int64_t offset, int whence);
135
136#ifdef WIN32
137int BLI_wstat(const wchar_t *path, BLI_stat_t *buffer);
138#endif
139
140typedef enum eFileAttributes {
141 FILE_ATTR_READONLY = 1 << 0, /* Read-only or Immutable. */
142 FILE_ATTR_HIDDEN = 1 << 1, /* Hidden or invisible. */
143 FILE_ATTR_SYSTEM = 1 << 2, /* Used by the Operating System. */
144 FILE_ATTR_ARCHIVE = 1 << 3, /* Marked as archived. */
145 FILE_ATTR_COMPRESSED = 1 << 4, /* Compressed. */
146 FILE_ATTR_ENCRYPTED = 1 << 5, /* Encrypted. */
147 FILE_ATTR_RESTRICTED = 1 << 6, /* Protected by OS. */
148 FILE_ATTR_TEMPORARY = 1 << 7, /* Used for temporary storage. */
149 FILE_ATTR_SPARSE_FILE = 1 << 8, /* Sparse File. */
150 FILE_ATTR_OFFLINE = 1 << 9, /* Contents available after a short delay. */
151 FILE_ATTR_ALIAS = 1 << 10, /* Mac Alias or Windows LNK. File-based redirection. */
152 FILE_ATTR_REPARSE_POINT = 1 << 11, /* File has associated re-parse point. */
153 FILE_ATTR_SYMLINK = 1 << 12, /* Reference to another file. */
154 FILE_ATTR_JUNCTION_POINT = 1 << 13, /* Folder Symbolic-link. */
155 FILE_ATTR_MOUNT_POINT = 1 << 14, /* Volume mounted as a folder. */
156 FILE_ATTR_HARDLINK = 1 << 15, /* Duplicated directory entry. */
159
160#define FILE_ATTR_ANY_LINK \
161 (FILE_ATTR_ALIAS | FILE_ATTR_REPARSE_POINT | FILE_ATTR_SYMLINK | FILE_ATTR_JUNCTION_POINT | \
162 FILE_ATTR_MOUNT_POINT | FILE_ATTR_HARDLINK)
163
165
166/* -------------------------------------------------------------------- */
169
188
189bool BLI_file_external_operation_supported(const char *filepath, FileExternalOperation operation);
190bool BLI_file_external_operation_execute(const char *filepath, FileExternalOperation operation);
191
193
194/* -------------------------------------------------------------------- */
197
198struct direntry;
199
225char *BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
226
238const char *BLI_dir_home(void);
239
252bool BLI_change_working_dir(const char *dir);
253
255
256/* -------------------------------------------------------------------- */
259
266unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist);
270void BLI_filelist_entry_duplicate(struct direntry *dst, const struct direntry *src);
274void BLI_filelist_duplicate(struct direntry **dest_filelist,
275 struct direntry *const src_filelist,
276 unsigned int nrentries);
280void BLI_filelist_entry_free(struct direntry *entry);
284void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries);
285
289void BLI_filelist_entry_size_to_string(const struct stat *st,
290 uint64_t st_size_fallback,
291 bool compact,
292 char r_size[FILELIST_DIRENTRY_SIZE_LEN]);
296void BLI_filelist_entry_mode_to_string(const struct stat *st,
297 bool compact,
298 char r_mode1[FILELIST_DIRENTRY_MODE_LEN],
299 char r_mode2[FILELIST_DIRENTRY_MODE_LEN],
300 char r_mode3[FILELIST_DIRENTRY_MODE_LEN]);
304void BLI_filelist_entry_owner_to_string(const struct stat *st,
305 bool compact,
306 char r_owner[FILELIST_DIRENTRY_OWNER_LEN]);
313void BLI_filelist_entry_datetime_to_string(const struct stat *st,
314 int64_t ts,
315 bool compact,
316 char r_time[FILELIST_DIRENTRY_TIME_LEN],
317 char r_date[FILELIST_DIRENTRY_DATE_LEN],
318 bool *r_is_today,
319 bool *r_is_yesterday);
320
322
323/* -------------------------------------------------------------------- */
326
327FILE *BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
328void *BLI_gzopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
329int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
330int BLI_access(const char *filepath, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
331
338int64_t BLI_read(int fd, void *buf, size_t nbytes);
339
351bool BLI_file_touch(const char *filepath) ATTR_NONNULL(1);
357bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1);
358
368bool BLI_file_alias_target(const char *filepath,
369 char r_targetpath[/*FILE_MAXDIR*/ 768]) ATTR_WARN_UNUSED_RESULT;
370
371bool BLI_file_magic_is_gzip(const char header[4]);
372
373size_t BLI_file_zstd_from_mem_at_pos(void *buf,
374 size_t len,
375 FILE *file,
376 size_t file_offset,
377 int compression_level) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
378size_t BLI_file_unzstd_to_mem_at_pos(void *buf, size_t len, FILE *file, size_t file_offset)
380bool BLI_file_magic_is_zstd(const char header[4]);
381
390
396bool BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
397
403struct LinkNode *BLI_file_read_as_lines(const char *filepath) ATTR_WARN_UNUSED_RESULT
404 ATTR_NONNULL();
405
412 bool read_size_exact,
413 size_t pad_bytes,
414 size_t *r_size);
415
416void *BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size);
445void *BLI_file_read_text_as_mem_with_newline_as_nil(const char *filepath,
446 bool trim_trailing_space,
447 size_t pad_bytes,
448 size_t *r_size);
449void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size);
453void BLI_file_free_lines(struct LinkNode *lines);
454
455/* This weirdo pops up in two places. */
456#if !defined(WIN32)
457# ifndef O_BINARY
458# define O_BINARY 0
459# endif
460#else
461void BLI_get_short_name(char short_name[256], const char *filepath);
462#endif
463
#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:237
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:360
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:614
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:217
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:414
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:522
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:448
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:190
struct LinkNode * BLI_file_read_as_lines(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:563
int64_t BLI_lseek(int fd, int64_t offset, int whence)
Definition storage.cc:208
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:461
void BLI_file_free_lines(struct LinkNode *lines)
Definition storage.cc:609
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)
bool BLI_file_alias_target(const char *filepath, char r_targetpath[768]) ATTR_WARN_UNUSED_RESULT
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:226
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:443
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:81
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:528
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:121
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition storage.cc:199
void * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:511
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
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
const char * BLI_dir_home(void)
Definition storage.cc:97
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:454
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:452
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
long long int int64_t
unsigned long long int uint64_t
const char * path
uint len