Blender V5.3
BLI_fileops.hh
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 <fstream>
13#include <stdint.h>
14#include <stdio.h>
15#include <string>
16#include <sys/stat.h>
17
18/* for size_t (needed on windows) */
19#include <stddef.h>
20
21#include <limits.h> /* for PATH_MAX */
22
23#include "BLI_compiler_attrs.hh"
24#include "BLI_enum_flags.hh"
25#include "BLI_fileops_types.hh"
26#include "BLI_string_ref.hh"
27
28namespace blender {
29
30#ifndef PATH_MAX
31# define PATH_MAX 4096
32#endif
33
34/* -------------------------------------------------------------------- */
37
41bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
42
48
52int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL();
58int BLI_path_move(const char *path_src, const char *path_dst) ATTR_NONNULL();
59
70int BLI_rename(const char *from, const char *to) ATTR_NONNULL();
71
99int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL();
117int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL();
124int BLI_delete_soft(const char *filepath, const char **r_error_message) ATTR_NONNULL();
125#if 0 /* Unused */
126int BLI_create_symlink(const char *path, const char *path_dst) ATTR_NONNULL();
127#endif
128
129/* Keep in sync with the definition of struct `direntry` in `BLI_fileops_types.hh`. */
130#ifdef WIN32
131# if defined(_MSC_VER)
132typedef struct _stat64 BLI_stat_t;
133# else
134typedef struct _stat BLI_stat_t;
135# endif
136#else
137typedef struct stat BLI_stat_t;
138#endif
139
143int BLI_fseek(FILE *stream, int64_t offset, int whence);
144int64_t BLI_lseek(int fd, int64_t offset, int whence);
145
146#ifdef WIN32
147int BLI_wstat(const wchar_t *path, BLI_stat_t *buffer);
148#endif
149
151 FILE_ATTR_READONLY = 1 << 0, /* Read-only or Immutable. */
152 FILE_ATTR_HIDDEN = 1 << 1, /* Hidden or invisible. */
153 FILE_ATTR_SYSTEM = 1 << 2, /* Used by the Operating System. */
154 FILE_ATTR_ARCHIVE = 1 << 3, /* Marked as archived. */
155 FILE_ATTR_COMPRESSED = 1 << 4, /* Compressed. */
156 FILE_ATTR_ENCRYPTED = 1 << 5, /* Encrypted. */
157 FILE_ATTR_RESTRICTED = 1 << 6, /* Protected by OS. */
158 FILE_ATTR_TEMPORARY = 1 << 7, /* Used for temporary storage. */
159 FILE_ATTR_SPARSE_FILE = 1 << 8, /* Sparse File. */
160 FILE_ATTR_OFFLINE = 1 << 9, /* Contents available after a short delay. */
161 FILE_ATTR_ALIAS = 1 << 10, /* Mac Alias or Windows LNK. File-based redirection. */
162 FILE_ATTR_REPARSE_POINT = 1 << 11, /* File has associated re-parse point. */
163 FILE_ATTR_SYMLINK = 1 << 12, /* Reference to another file. */
164 FILE_ATTR_JUNCTION_POINT = 1 << 13, /* Folder Symbolic-link. */
165 FILE_ATTR_MOUNT_POINT = 1 << 14, /* Volume mounted as a folder. */
166 FILE_ATTR_HARDLINK = 1 << 15, /* Duplicated directory entry. */
167};
169
170#define FILE_ATTR_ANY_LINK \
171 (FILE_ATTR_ALIAS | FILE_ATTR_REPARSE_POINT | FILE_ATTR_SYMLINK | FILE_ATTR_JUNCTION_POINT | \
172 FILE_ATTR_MOUNT_POINT | FILE_ATTR_HARDLINK)
173
175
176/* -------------------------------------------------------------------- */
179
198
199bool BLI_file_external_operation_supported(const char *filepath, FileExternalOperation operation);
200bool BLI_file_external_operation_execute(const char *filepath, FileExternalOperation operation);
201
203
204/* -------------------------------------------------------------------- */
207
208struct direntry;
209
214bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
218bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
235char *BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
236
248const char *BLI_dir_home();
249
250eFileAttributes BLI_file_attributes(const char *path);
262bool BLI_change_working_dir(const char *dir);
263
265
266/* -------------------------------------------------------------------- */
269
276unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist);
280void BLI_filelist_entry_duplicate(struct direntry *dst, const struct direntry *src);
284void BLI_filelist_duplicate(struct direntry **dest_filelist,
285 struct direntry *const src_filelist,
286 unsigned int nrentries);
290void BLI_filelist_entry_free(struct direntry *entry);
294void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries);
295
299void BLI_filelist_entry_size_to_string(const struct stat *st,
300 uint64_t st_size_fallback,
301 bool compact,
302 char r_size[FILELIST_DIRENTRY_SIZE_LEN]);
306void BLI_filelist_entry_mode_to_string(const struct stat *st,
307 bool compact,
308 char r_mode1[FILELIST_DIRENTRY_MODE_LEN],
309 char r_mode2[FILELIST_DIRENTRY_MODE_LEN],
310 char r_mode3[FILELIST_DIRENTRY_MODE_LEN]);
314void BLI_filelist_entry_owner_to_string(const struct stat *st,
315 bool compact,
316 char r_owner[FILELIST_DIRENTRY_OWNER_LEN]);
317
319
320/* -------------------------------------------------------------------- */
323
324FILE *BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
325void *BLI_gzopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
326int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
327int BLI_access(const char *filepath, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
328
335int64_t BLI_read(int fd, void *buf, size_t nbytes);
336
348bool BLI_file_touch(const char *filepath) ATTR_NONNULL(1);
354bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1);
355
365bool BLI_file_alias_target(const char *filepath,
366 char r_targetpath[/*FILE_MAXDIR*/ 768]) ATTR_WARN_UNUSED_RESULT;
367
368bool BLI_file_magic_is_gzip(const char header[4]);
369
370size_t BLI_file_zstd_from_mem_at_pos(void *buf,
371 size_t len,
372 FILE *file,
373 size_t file_offset,
374 int compression_level) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
375size_t BLI_file_unzstd_to_mem_at_pos(void *buf, size_t len, FILE *file, size_t file_offset)
377bool BLI_file_magic_is_zstd(const char header[4]);
378
386size_t BLI_file_size(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
387
393bool BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
394
400struct LinkNode *BLI_file_read_as_lines(const char *filepath) ATTR_WARN_UNUSED_RESULT
401 ATTR_NONNULL();
402
409 bool read_size_exact,
410 size_t pad_bytes,
411 size_t *r_size);
412
413char *BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size);
442char *BLI_file_read_text_as_mem_with_newline_as_nil(const char *filepath,
443 bool trim_trailing_space,
444 size_t pad_bytes,
445 size_t *r_size);
446void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size);
450void BLI_file_free_lines(struct LinkNode *lines);
451
452/* This weirdo pops up in two places. */
453#if !defined(WIN32)
454# ifndef O_BINARY
455# define O_BINARY 0
456# endif
457#else
458void BLI_get_short_name(char short_name[256], const char *filepath);
459#endif
460
462
468class fstream : public std::fstream {
469 public:
470 fstream() = default;
471 explicit fstream(const char *filepath,
472 std::ios_base::openmode mode = ios_base::in | ios_base::out);
473 explicit fstream(const std::string &filepath,
474 std::ios_base::openmode mode = ios_base::in | ios_base::out);
475
476 void open(StringRefNull filepath, ios_base::openmode mode = ios_base::in | ios_base::out);
477};
478
479} // namespace blender
#define ATTR_WARN_UNUSED_RESULT
#define ATTR_NONNULL(...)
Some types for dealing with directories.
#define FILELIST_DIRENTRY_SIZE_LEN
#define FILELIST_DIRENTRY_MODE_LEN
#define FILELIST_DIRENTRY_OWNER_LEN
const char * dirname(char *path)
void open(StringRefNull filepath, ios_base::openmode mode=ios_base::in|ios_base::out)
Definition fileops.cc:27
fstream()=default
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ENUM_OPERATORS(Layer::Flags)
BMesh Iterators.
void BLI_filelist_duplicate(struct direntry **dest_filelist, struct direntry *const src_filelist, unsigned int nrentries)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
int BLI_fstat(int fd, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BLI_change_working_dir(const char *dir)
Definition storage.cc:65
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
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])
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:416
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_filelist_entry_duplicate(struct direntry *dst, const struct direntry *src)
int64_t BLI_read(int fd, void *buf, size_t nbytes)
Definition fileops_c.cc:98
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition storage.cc:201
int BLI_file_stat_mode(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:362
bool BLI_file_external_operation_execute(const char *filepath, FileExternalOperation operation)
Definition fileops_c.cc:159
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
eFileAttributes BLI_file_attributes(const char *path)
Definition storage.cc:239
void * BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:550
const char * BLI_dir_home()
Definition storage.cc:99
@ FILE_ATTR_HIDDEN
@ FILE_ATTR_TEMPORARY
@ FILE_ATTR_JUNCTION_POINT
@ FILE_ATTR_READONLY
@ FILE_ATTR_ARCHIVE
@ FILE_ATTR_OFFLINE
@ FILE_ATTR_COMPRESSED
@ FILE_ATTR_ENCRYPTED
@ FILE_ATTR_SPARSE_FILE
@ FILE_ATTR_SYMLINK
@ FILE_ATTR_HARDLINK
@ FILE_ATTR_ALIAS
@ FILE_ATTR_MOUNT_POINT
@ FILE_ATTR_REPARSE_POINT
@ FILE_ATTR_SYSTEM
@ FILE_ATTR_RESTRICTED
struct LinkNode * BLI_file_read_as_lines(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:591
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:482
char * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:539
double BLI_dir_free_space(const char *dir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:123
int BLI_rename(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:474
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
int BLI_path_move(const char *path_src, const char *path_dst) ATTR_NONNULL()
int BLI_access(const char *filepath, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BLI_file_magic_is_zstd(const char header[4])
Definition fileops_c.cc:266
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:541
bool BLI_file_is_writable(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition fileops_c.cc:293
int64_t BLI_lseek(int fd, int64_t offset, int whence)
Definition storage.cc:210
bool BLI_file_touch(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:318
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:222
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:457
size_t BLI_file_size(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:228
int BLI_delete_soft(const char *filepath, const char **r_error_message) ATTR_NONNULL()
void * BLI_gzopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
void BLI_filelist_entry_free(struct direntry *entry)
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
bool BLI_file_magic_is_gzip(const char header[4])
Definition fileops_c.cc:259
bool BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:642
size_t BLI_file_descriptor_size(int file) ATTR_WARN_UNUSED_RESULT
Definition storage.cc:219
char * 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:561
bool ATTR_WARN_UNUSED_RESULT
bool BLI_file_external_operation_supported(const char *filepath, FileExternalOperation operation)
Definition fileops_c.cc:148
struct stat BLI_stat_t
void ATTR_NONNULL(1, 2)
bool BLI_file_alias_target(const char *filepath, char r_targetpath[768]) ATTR_WARN_UNUSED_RESULT
ATTR_WARN_UNUSED_RESULT const BMFlagLayer const short oflag
int64_t BLI_ftell(FILE *stream) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:192
char * BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:83
void BLI_file_free_lines(struct LinkNode *lines)
Definition storage.cc:637
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:469
FileExternalOperation
@ FILE_EXTERNAL_OPERATION_FOLDER_OPEN
@ FILE_EXTERNAL_OPERATION_FOLDER_CMD
@ FILE_EXTERNAL_OPERATION_EDIT
@ FILE_EXTERNAL_OPERATION_FIND
@ FILE_EXTERNAL_OPERATION_SHOW
@ FILE_EXTERNAL_OPERATION_BROWSE
@ FILE_EXTERNAL_OPERATION_INSTALL
@ FILE_EXTERNAL_OPERATION_PROPERTIES
@ FILE_EXTERNAL_OPERATION_PLAY
@ FILE_EXTERNAL_OPERATION_PRINT
@ FILE_EXTERNAL_OPERATION_PREVIEW
@ FILE_EXTERNAL_OPERATION_FOLDER_FIND
@ FILE_EXTERNAL_OPERATION_RUNAS
@ FILE_EXTERNAL_OPERATION_NEW
@ FILE_EXTERNAL_OPERATION_OPEN
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:175
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:454
void BLI_filelist_entry_owner_to_string(const struct stat *st, bool compact, char r_owner[FILELIST_DIRENTRY_OWNER_LEN])
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])