filesystem.odin (8844B)
1 // This file is part of Blend2D project <https://blend2d.com> 2 // 3 // See blend2d.h or LICENSE.md for license and copyright information 4 // SPDX-License-Identifier: Zlib 5 package blend2d 6 7 import "core:c" 8 9 when ODIN_OS == .Windows { 10 foreign import lib "blend2d.lib" 11 } else when ODIN_OS == .Darwin { 12 foreign import lib "libblend2d.a" 13 } else when ODIN_OS == .Linux { 14 foreign import lib "libblend2d.a" 15 } 16 17 18 //! File information flags, used by \ref BLFileInfo. 19 FileInfoFlags :: enum u32 { 20 //! File owner has read permission (compatible with 0400 octal notation). 21 OWNER_R = 256, 22 23 //! File owner has write permission (compatible with 0200 octal notation). 24 OWNER_W = 128, 25 26 //! File owner has execute permission (compatible with 0100 octal notation). 27 OWNER_X = 64, 28 29 //! A combination of \ref BL_FILE_INFO_OWNER_R, \ref BL_FILE_INFO_OWNER_W, and \ref BL_FILE_INFO_OWNER_X. 30 OWNER_MASK = 448, 31 32 //! File group owner has read permission (compatible with 040 octal notation). 33 GROUP_R = 32, 34 35 //! File group owner has write permission (compatible with 020 octal notation). 36 GROUP_W = 16, 37 38 //! File group owner has execute permission (compatible with 010 octal notation). 39 GROUP_X = 8, 40 41 //! A combination of \ref BL_FILE_INFO_GROUP_R, \ref BL_FILE_INFO_GROUP_W, and \ref BL_FILE_INFO_GROUP_X. 42 GROUP_MASK = 56, 43 44 //! Other users have read permission (compatible with 04 octal notation). 45 OTHER_R = 4, 46 47 //! Other users have write permission (compatible with 02 octal notation). 48 OTHER_W = 2, 49 50 //! Other users have execute permission (compatible with 01 octal notation). 51 OTHER_X = 1, 52 53 //! A combination of \ref BL_FILE_INFO_OTHER_R, \ref BL_FILE_INFO_OTHER_W, and \ref BL_FILE_INFO_OTHER_X. 54 OTHER_MASK = 7, 55 56 //! Set user ID to file owner user ID on execution (compatible with 04000 octal notation). 57 SUID = 2048, 58 59 //! Set group ID to file's user group ID on execution (compatible with 02000 octal notation). 60 SGID = 1024, 61 62 //! A combination of all file permission bits. 63 PERMISSIONS_MASK = 4095, 64 65 //! A flag specifying that this is a regular file. 66 REGULAR = 65536, 67 68 //! A flag specifying that this is a directory. 69 DIRECTORY = 131072, 70 71 //! A flag specifying that this is a symbolic link. 72 SYMLINK = 262144, 73 74 //! A flag describing a character device. 75 CHAR_DEVICE = 1048576, 76 77 //! A flag describing a block device. 78 BLOCK_DEVICE = 2097152, 79 80 //! A flag describing a FIFO (named pipe). 81 FIFO = 4194304, 82 83 //! A flag describing a socket. 84 SOCKET = 8388608, 85 86 //! A flag describing a hidden file (Windows only). 87 HIDDEN = 16777216, 88 89 //! A flag describing a hidden file (Windows only). 90 EXECUTABLE = 33554432, 91 92 //! A flag describing an archive (Windows only). 93 ARCHIVE = 67108864, 94 95 //! A flag describing a system file (Windows only). 96 SYSTEM = 134217728, 97 98 //! File information is valid (the request succeeded). 99 VALID = 2147483648, 100 FORCE_UINT = 4294967295, 101 } 102 103 //! File open flags, see \ref BLFile::open(). 104 FileOpenFlags :: enum u32 { 105 //! No flags. 106 NO_FLAGS = 0, 107 108 //! Opens the file for reading. 109 //! 110 //! The following system flags are used when opening the file: 111 //! - `O_RDONLY` (Posix) 112 //! - `GENERIC_READ` (Windows) 113 READ = 1, 114 115 //! Opens the file for writing: 116 //! 117 //! The following system flags are used when opening the file: 118 //! - `O_WRONLY` (Posix) 119 //! - `GENERIC_WRITE` (Windows) 120 WRITE = 2, 121 122 //! Opens the file for reading & writing. 123 //! 124 //! The following system flags are used when opening the file: 125 //! - `O_RDWR` (Posix) 126 //! - `GENERIC_READ | GENERIC_WRITE` (Windows) 127 RW = 3, 128 129 //! Creates the file if it doesn't exist or opens it if it does. 130 //! 131 //! The following system flags are used when opening the file: 132 //! - `O_CREAT` (Posix) 133 //! - `CREATE_ALWAYS` or `OPEN_ALWAYS` depending on other flags (Windows) 134 CREATE = 4, 135 136 //! Opens the file for deleting or renaming (Windows). 137 //! 138 //! Adds `DELETE` flag when opening the file to `ACCESS_MASK`. 139 DELETE = 8, 140 141 //! Truncates the file. 142 //! 143 //! The following system flags are used when opening the file: 144 //! - `O_TRUNC` (Posix) 145 //! - `TRUNCATE_EXISTING` (Windows) 146 TRUNCATE = 16, 147 148 //! Opens the file for reading in exclusive mode (Windows). 149 //! 150 //! Exclusive mode means to not specify the `FILE_SHARE_READ` option. 151 READ_EXCLUSIVE = 268435456, 152 153 //! Opens the file for writing in exclusive mode (Windows). 154 //! 155 //! Exclusive mode means to not specify the `FILE_SHARE_WRITE` option. 156 WRITE_EXCLUSIVE = 536870912, 157 158 //! Opens the file for both reading and writing (Windows). 159 //! 160 //! This is a combination of both `BL_FILE_OPEN_READ_EXCLUSIVE` and `BL_FILE_OPEN_WRITE_EXCLUSIVE`. 161 RW_EXCLUSIVE = 805306368, 162 163 //! Creates the file in exclusive mode - fails if the file already exists. 164 //! 165 //! The following system flags are used when opening the file: 166 //! - `O_EXCL` (Posix) 167 //! - `CREATE_NEW` (Windows) 168 CREATE_EXCLUSIVE = 1073741824, 169 170 //! Opens the file for deleting or renaming in exclusive mode (Windows). 171 //! 172 //! Exclusive mode means to not specify the `FILE_SHARE_DELETE` option. 173 DELETE_EXCLUSIVE = 2147483648, 174 FORCE_UINT = 4294967295, 175 } 176 177 //! File seek mode, see \ref BLFile::seek(). 178 //! 179 //! \note Seek constants should be compatible with constants used by both POSIX 180 //! and Windows API. 181 FileSeekType :: enum u32 { 182 //! Seek from the beginning of the file (SEEK_SET). 183 SET = 0, 184 185 //! Seek from the current position (SEEK_CUR). 186 CUR = 1, 187 188 //! Seek from the end of the file (SEEK_END). 189 END = 2, 190 191 //! Maximum value of `BLFileSeekType`. 192 MAX_VALUE = 3, 193 FORCE_UINT = 4294967295, 194 } 195 196 //! File read flags used by \ref BLFileSystem::read_file(). 197 FileReadFlags :: enum u32 { 198 //! No flags. 199 NO_FLAGS = 0, 200 201 //! Use memory mapping to read the content of the file. 202 //! 203 //! The destination buffer `BLArray<>` would be configured to use the memory mapped buffer instead of allocating its 204 //! own. 205 MMAP_ENABLED = 1, 206 207 //! Avoid memory mapping of small files. 208 //! 209 //! The size of small file is determined by Blend2D, however, you should expect it to be 16kB or 64kB depending on 210 //! host operating system. 211 MMAP_AVOID_SMALL = 2, 212 213 //! Do not fallback to regular read if memory mapping fails. It's worth noting that memory mapping would fail for 214 //! files stored on filesystem that is not local (like a mounted network filesystem, etc...). 215 MMAP_NO_FALLBACK = 8, 216 FORCE_UINT = 4294967295, 217 } 218 219 //! A thin abstraction over a native OS file IO [C API]. 220 FileCore :: struct { 221 //! A file handle - either a file descriptor used by POSIX or file handle used by Windows. On both platforms the 222 //! handle is always `intptr_t` to make FFI easier (it's basically the size of a pointer / machine register). 223 //! 224 //! \note A handle of value `-1` is considered invalid and/or uninitialized. This value also matches Windows API 225 //! `INVALID_HANDLE_VALUE`, which is also defined to be -1. 226 handle: c.intptr_t, 227 } 228 229 //! File information. 230 FileInfo :: struct { 231 //! \name Members 232 //! \{ 233 size: u64, 234 modified_time: i64, 235 flags: FileInfoFlags, 236 uid: u32, 237 gid: u32, 238 reserved: [5]u32, 239 } 240 241 @(default_calling_convention="c", link_prefix="bl_") 242 foreign lib { 243 //! \name BLFile C API Functions 244 //! 245 //! File read/write functionality is provided by \ref BLFileCore in C API and wrapped by \ref BLFile in C++ API. 246 //! 247 //! \{ 248 file_init :: proc(self: ^FileCore) -> Result --- 249 file_reset :: proc(self: ^FileCore) -> Result --- 250 file_open :: proc(self: ^FileCore, file_name: cstring, open_flags: FileOpenFlags) -> Result --- 251 file_close :: proc(self: ^FileCore) -> Result --- 252 file_seek :: proc(self: ^FileCore, offset: i64, seek_type: FileSeekType, position_out: ^i64) -> Result --- 253 file_read :: proc(self: ^FileCore, buffer: rawptr, n: c.size_t, bytes_read_out: ^c.size_t) -> Result --- 254 file_write :: proc(self: ^FileCore, buffer: rawptr, n: c.size_t, bytes_written_out: ^c.size_t) -> Result --- 255 file_truncate :: proc(self: ^FileCore, max_size: i64) -> Result --- 256 file_get_info :: proc(self: ^FileCore, info_out: ^FileInfo) -> Result --- 257 file_get_size :: proc(self: ^FileCore, file_size_out: ^u64) -> Result --- 258 259 //! \name BLFileSystem C API Functions 260 //! 261 //! \{ 262 file_system_get_info :: proc(file_name: cstring, info_out: ^FileInfo) -> Result --- 263 file_system_read_file :: proc(file_name: cstring, dst: ^ArrayCore, max_size: c.size_t, read_flags: FileReadFlags) -> Result --- 264 file_system_write_file :: proc(file_name: cstring, data: rawptr, size: c.size_t, bytes_written_out: ^c.size_t) -> Result --- 265 } 266