1 /* 2 * This program is free software; you can redistribute it and/or 3 * modify it under the terms of the GNU General Public License 4 * as published by the Free Software Foundation; either version 2 5 * of the License, or (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software Foundation, 14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 15 * 16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 17 * All rights reserved. 18 */ 19 20 /** \file 21 * \ingroup bli 22 * \brief File and directory operations. 23 * */ 24 25 #pragma once 26 27 #include <stdint.h> 28 #include <stdio.h> 29 #include <sys/stat.h> 30 31 /* for size_t (needed on windows) */ 32 #include <stddef.h> 33 34 #include <limits.h> /* for PATH_MAX */ 35 36 #include "BLI_compiler_attrs.h" 37 #include "BLI_utildefines.h" 38 39 #ifdef __cplusplus 40 extern "C" { 41 #endif 42 43 #ifndef PATH_MAX 44 # define PATH_MAX 4096 45 #endif 46 47 /* Common */ 48 49 int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 50 int BLI_copy(const char *file, const char *to) ATTR_NONNULL(); 51 int BLI_rename(const char *from, const char *to) ATTR_NONNULL(); 52 int BLI_delete(const char *file, bool dir, bool recursive) ATTR_NONNULL(); 53 int BLI_delete_soft(const char *file, const char **error_message) ATTR_NONNULL(); 54 #if 0 /* Unused */ 55 int BLI_move(const char *path, const char *to) ATTR_NONNULL(); 56 int BLI_create_symlink(const char *path, const char *to) ATTR_NONNULL(); 57 #endif 58 59 /* keep in sync with the definition of struct direntry in BLI_fileops_types.h */ 60 #ifdef WIN32 61 # if defined(_MSC_VER) 62 typedef struct _stat64 BLI_stat_t; 63 # else 64 typedef struct _stat BLI_stat_t; 65 # endif 66 #else 67 typedef struct stat BLI_stat_t; 68 #endif 69 70 int BLI_fstat(int fd, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 71 int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 72 int64_t BLI_ftell(FILE *stream) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 73 int BLI_fseek(FILE *stream, int64_t offset, int whence); 74 int64_t BLI_lseek(int fd, int64_t offset, int whence); 75 76 #ifdef WIN32 77 int BLI_wstat(const wchar_t *path, BLI_stat_t *buffer); 78 #endif 79 80 typedef enum eFileAttributes { 81 FILE_ATTR_READONLY = 1 << 0, /* Read-only or Immutable. */ 82 FILE_ATTR_HIDDEN = 1 << 1, /* Hidden or invisible. */ 83 FILE_ATTR_SYSTEM = 1 << 2, /* Used by the Operating System. */ 84 FILE_ATTR_ARCHIVE = 1 << 3, /* Marked as archived. */ 85 FILE_ATTR_COMPRESSED = 1 << 4, /* Compressed. */ 86 FILE_ATTR_ENCRYPTED = 1 << 5, /* Encrypted. */ 87 FILE_ATTR_RESTRICTED = 1 << 6, /* Protected by OS. */ 88 FILE_ATTR_TEMPORARY = 1 << 7, /* Used for temporary storage. */ 89 FILE_ATTR_SPARSE_FILE = 1 << 8, /* Sparse File. */ 90 FILE_ATTR_OFFLINE = 1 << 9, /* Data is not immediately available. */ 91 FILE_ATTR_ALIAS = 1 << 10, /* Mac Alias or Windows Lnk. File-based redirection. */ 92 FILE_ATTR_REPARSE_POINT = 1 << 11, /* File has associated reparse point. */ 93 FILE_ATTR_SYMLINK = 1 << 12, /* Reference to another file. */ 94 FILE_ATTR_JUNCTION_POINT = 1 << 13, /* Folder Symlink. */ 95 FILE_ATTR_MOUNT_POINT = 1 << 14, /* Volume mounted as a folder. */ 96 FILE_ATTR_HARDLINK = 1 << 15, /* Duplicated directory entry. */ 97 } eFileAttributes; 98 99 #define FILE_ATTR_ANY_LINK \ 100 (FILE_ATTR_ALIAS | FILE_ATTR_REPARSE_POINT | FILE_ATTR_SYMLINK | FILE_ATTR_JUNCTION_POINT | \ 101 FILE_ATTR_MOUNT_POINT | FILE_ATTR_HARDLINK) 102 103 /* Directories */ 104 105 struct direntry; 106 107 bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 108 bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 109 bool BLI_dir_create_recursive(const char *dir) ATTR_NONNULL(); 110 double BLI_dir_free_space(const char *dir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 111 char *BLI_current_working_dir(char *dir, const size_t maxncpy) ATTR_WARN_UNUSED_RESULT 112 ATTR_NONNULL(); 113 eFileAttributes BLI_file_attributes(const char *path); 114 115 /* Filelist */ 116 117 unsigned int BLI_filelist_dir_contents(const char *dir, struct direntry **r_filelist); 118 void BLI_filelist_entry_duplicate(struct direntry *dst, const struct direntry *src); 119 void BLI_filelist_duplicate(struct direntry **dest_filelist, 120 struct direntry *const src_filelist, 121 const unsigned int nrentries); 122 void BLI_filelist_entry_free(struct direntry *entry); 123 void BLI_filelist_free(struct direntry *filelist, const unsigned int nrentries); 124 125 void BLI_filelist_entry_size_to_string(const struct stat *st, 126 const uint64_t sz, 127 const bool compact, 128 char r_size[]); 129 void BLI_filelist_entry_mode_to_string( 130 const struct stat *st, const bool compact, char r_mode1[], char r_mode2[], char r_mode3[]); 131 void BLI_filelist_entry_owner_to_string(const struct stat *st, const bool compact, char r_owner[]); 132 void BLI_filelist_entry_datetime_to_string(const struct stat *st, 133 const int64_t ts, 134 const bool compact, 135 char r_time[], 136 char r_date[], 137 bool *r_is_today, 138 bool *r_is_yesterday); 139 140 /* Files */ 141 142 FILE *BLI_fopen(const char *filename, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 143 void *BLI_gzopen(const char *filename, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 144 int BLI_open(const char *filename, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 145 int BLI_access(const char *filename, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 146 147 bool BLI_file_is_writable(const char *file) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 148 bool BLI_file_touch(const char *file) ATTR_NONNULL(); 149 bool BLI_file_alias_target(const char *filepath, char *r_targetpath) ATTR_WARN_UNUSED_RESULT; 150 151 #if 0 /* UNUSED */ 152 int BLI_file_gzip(const char *from, const char *to) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 153 #endif 154 char *BLI_file_ungzip_to_mem(const char *from_file, int *r_size) ATTR_WARN_UNUSED_RESULT 155 ATTR_NONNULL(); 156 size_t BLI_gzip_mem_to_file_at_pos(void *buf, 157 size_t len, 158 FILE *file, 159 size_t gz_stream_offset, 160 int compression_level) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 161 size_t BLI_ungzip_file_to_mem_at_pos(void *buf, size_t len, FILE *file, size_t gz_stream_offset) 162 ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 163 size_t BLI_file_descriptor_size(int file) ATTR_WARN_UNUSED_RESULT; 164 size_t BLI_file_size(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 165 166 /* compare if one was last modified before the other */ 167 bool BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 168 169 /* read ascii file as lines, empty list if reading fails */ 170 struct LinkNode *BLI_file_read_as_lines(const char *file) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); 171 void *BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size); 172 void *BLI_file_read_text_as_mem_with_newline_as_nil(const char *filepath, 173 bool trim_trailing_space, 174 size_t pad_bytes, 175 size_t *r_size); 176 void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size); 177 void BLI_file_free_lines(struct LinkNode *lines); 178 179 /* this weirdo pops up in two places ... */ 180 #if !defined(WIN32) 181 # ifndef O_BINARY 182 # define O_BINARY 0 183 # endif 184 #else 185 void BLI_get_short_name(char short_name[256], const char *filename); 186 #endif 187 188 #ifdef __cplusplus 189 } 190 #endif 191