1 #pragma once 2 #ifndef IWP_H 3 #define IWP_H 4 5 /************************************************************************************************** 6 * IOWOW library 7 * 8 * MIT License 9 * 10 * Copyright (c) 2012-2022 Softmotions Ltd <info@softmotions.com> 11 * 12 * Permission is hereby granted, free of charge, to any person obtaining a copy 13 * of this software and associated documentation files (the "Software"), to deal 14 * in the Software without restriction, including without limitation the rights 15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 * copies of the Software, and to permit persons to whom the Software is 17 * furnished to do so, subject to the following conditions: 18 * 19 * The above copyright notice and this permission notice shall be included in all 20 * copies or substantial portions of the Software. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 * SOFTWARE. 29 *************************************************************************************************/ 30 31 32 /** @file 33 * @author Anton Adamansky (adamansky@softmotions.com) 34 **/ 35 36 #include "basedefs.h" 37 #include <stdint.h> 38 #include <stdio.h> 39 #include <stdbool.h> 40 #include <sys/time.h> 41 42 #ifndef CLOCK_REALTIME 43 #define CLOCK_REALTIME 0 44 #endif 45 46 #ifndef CLOCK_MONOTONIC 47 #define CLOCK_MONOTONIC 1 48 #endif 49 50 #define IWCPU_SSE 0x1 51 #define IWCPU_SSE2 0x2 52 #define IWCPU_SSE3 0x4 53 #define IWCPU_SSE4_1 0x8 54 #define IWCPU_SSE4_2 0x10 55 #define IWCPU_AVX 0x20 56 #define IWCPU_AVX2 0x40 57 #define IWCPU_AVX512F 0x80 58 59 /** 60 * Flags supported by current CPU. 61 * `iwp_init()` must be called. 62 * Zero on non `x86` platforms. 63 */ 64 extern unsigned int iwcpuflags; 65 66 /** 67 * @enum iwp_ecode 68 * @brief Error codes. 69 */ 70 typedef enum { 71 _IWP_ERROR_FS_START = (IW_ERROR_START + 2000UL), 72 _IWP_ERROR_FS_END, 73 } iwp_ecode; 74 75 /** File locking mode acquired by process opened this file. */ 76 typedef uint8_t iwp_lockmode; 77 /** Do not acquire lock on file. */ 78 #define IWP_NOLOCK ((iwp_lockmode) 0x00U) 79 /** Acquire read lock on file. */ 80 #define IWP_RLOCK ((iwp_lockmode) 0x01U) 81 /** Acquire write lock on file. */ 82 #define IWP_WLOCK ((iwp_lockmode) 0x02U) 83 /** Do not block current thread if file have been locked by another process. 84 * In this case error will be raised. */ 85 #define IWP_NBLOCK ((iwp_lockmode) 0x04U) 86 87 /** 88 * @enum iwp_file_type 89 * @brief File type. 90 */ 91 typedef enum { 92 IWP_TYPE_FILE, /**< Ordinary file. */ 93 IWP_TYPE_DIR, /**< Directory. */ 94 IWP_LINK, /**< Symlink. */ 95 IWP_OTHER, /**< Other file types, eg soc, block, pipe.. */ 96 } iwp_file_type; 97 98 typedef enum { 99 IWP_SEEK_SET = 1, 100 IWP_SEEK_CUR, 101 IWP_SEEK_END, 102 } iwp_seek_origin; 103 104 /** 105 * Portable version of `int clock_gettime(clockid_t clk_id, struct timespec *tp)` 106 */ 107 IW_EXPORT iwrc iwp_clock_get_time(int clock_id, struct timespec *t); 108 109 /** 110 * @brief Get current time in milliseconds. 111 * 112 * @param [out] time Time returned 113 * @return `0` for success, or error code 114 */ 115 IW_EXPORT iwrc iwp_current_time_ms(uint64_t *time, bool monotonic); 116 117 /** 118 * @brief File info. 119 */ 120 typedef struct IWP_FILE_STAT { 121 uint64_t size; /**< File size. */ 122 uint64_t atime; /**< Time of last access. */ 123 uint64_t ctime; /**< Time of last status change. */ 124 uint64_t mtime; /**< Time of last modification. */ 125 iwp_file_type ftype; /**< File type. */ 126 } IWP_FILE_STAT; 127 128 /** 129 * @brief Stat the file specified by @a path. 130 * 131 * @param path File path 132 * @param [out] stat File stat info placeholder. 133 * @return `0` on sucess or error code. 134 */ 135 IW_EXPORT iwrc iwp_fstat(const char *path, IWP_FILE_STAT *stat); 136 137 IW_EXPORT iwrc iwp_fstath(HANDLE fh, IWP_FILE_STAT *stat); 138 139 /** 140 * @brief Lock the file. 141 * 142 * @param fh File handle. 143 * @param lmode Lock mode specified. 144 * @return `0` on sucess or error code. 145 */ 146 IW_EXPORT iwrc iwp_flock(HANDLE fh, iwp_lockmode lmode); 147 148 /** 149 * @brief Unlock the file specified by @a fh 150 * @param fh File handle 151 * @return `0` on sucess or error code. 152 */ 153 IW_EXPORT iwrc iwp_unlock(HANDLE fh); 154 155 /** 156 * @brief Close the specified file handle (File descriptor). 157 * @param fh File handle. 158 */ 159 IW_EXPORT iwrc iwp_closefh(HANDLE fh); 160 161 /** 162 * @brief Read @a siz bytes from file @a fh 163 * into @a buf at the specified offset @a off. 164 * 165 * @param fh File handle. 166 * @param off Offset from start of the file. 167 * @param [out] buf Buffer into which bytes will read. 168 * @param siz Number of bytes to read. 169 * @param [out] sp Number of bytes read actually 170 * @return `0` on sucess or error code. 171 */ 172 IW_EXPORT iwrc iwp_pread(HANDLE fh, off_t off, void *buf, size_t siz, size_t *sp); 173 174 /** 175 * @brief Write @a siz bytes into file @a fh 176 * at the specified offset @a off 177 * from buffer @a buf. 178 * 179 * @param fh File handle. 180 * @param off Offset from start of the file. 181 * @param buf Data buffer to write. 182 * @param siz Number of bytes to write. 183 * @param [out] sp Number of bytes written. 184 * @return `0` on sucess or error code. 185 */ 186 IW_EXPORT iwrc iwp_pwrite(HANDLE fh, off_t off, const void *buf, size_t siz, size_t *sp); 187 188 IW_EXPORT iwrc iwp_write(HANDLE fh, const void *buf, size_t count); 189 190 IW_EXPORT iwrc iwp_read(HANDLE fh, void *buf, size_t count, size_t *sp); 191 192 IW_EXPORT iwrc iwp_lseek(HANDLE fh, off_t offset, iwp_seek_origin origin, off_t *pos); 193 194 /** 195 * @brief Copy data within a file 196 * @param off Data offset 197 * @param siz Data size 198 * @param noff New data offset 199 */ 200 IW_EXPORT iwrc iwp_copy_bytes( 201 HANDLE fh, 202 off_t off, size_t siz, 203 off_t noff); 204 205 /** 206 * @brief Get system page size. 207 */ 208 IW_EXPORT size_t iwp_page_size(void); 209 210 211 /** 212 * @brief Minimal address space aligment for memory mapping. 213 */ 214 IW_EXPORT size_t iwp_alloc_unit(void); 215 216 /** 217 * @brief Truncate a file specified by @a fh to a size of @a len bytes 218 * @param fh File handle 219 * @param len File size 220 * @return `0` on sucess or error code. 221 */ 222 IW_EXPORT iwrc iwp_ftruncate(HANDLE fh, off_t len); 223 224 /** 225 * @brief Allocate extra space for a file. 226 * @param fh File handle 227 * @param len New file size 228 * @return `0` on sucess or error code. 229 */ 230 IW_EXPORT iwrc iwp_fallocate(HANDLE fh, off_t len); 231 232 /** 233 * @brief Pause execution of current thread 234 * to the specified @a ms time in milliseconds. 235 * @param ms Thread pause time 236 */ 237 IW_EXPORT iwrc iwp_sleep(uint64_t ms); 238 239 /** 240 * @brief Recursive directory removal specified by @a path. 241 * @param path Directory path 242 */ 243 IW_EXPORT iwrc iwp_removedir(const char *path); 244 245 /** 246 * @brief Make directory specified by `path` 247 * as well as all parent directories. 248 * 249 * @param path Path to directory. 250 */ 251 IW_EXPORT iwrc iwp_mkdirs(const char *path); 252 253 /** 254 * @brief Make directory `dirname(path)` of specified file 255 * as well as all parent directories. 256 */ 257 IW_EXPORT iwrc iwp_mkdirs_for_file(const char *path); 258 259 /** 260 * @brief Platform neutral version of basename. 261 * @note Modifies its `path` argument. 262 */ 263 IW_EXPORT char* iwp_basename(char *path); 264 265 /** 266 * @brief Platform neutral version of dirname. 267 * @note Modifies its `path` argument. 268 */ 269 IW_EXPORT char* iwp_dirname(char *path); 270 271 /** 272 * @brief Get executable path for the current process. 273 * It will be writein into @a opath 274 * @param opath Allocated buffer at least `PATH_MAX` length 275 */ 276 IW_EXPORT iwrc iwp_exec_path(char *opath, size_t opath_maxlen); 277 278 /** 279 * @brief Return number of CPU cores. 280 */ 281 IW_EXPORT uint16_t iwp_num_cpu_cores(void); 282 283 /** 284 * @brief Init iwp module. 285 * @return `0` on success or error code. 286 */ 287 IW_EXPORT WUR iwrc iwp_init(void); 288 289 IW_EXPORT iwrc iwp_fsync(HANDLE fh); 290 291 IW_EXPORT iwrc iwp_fdatasync(HANDLE fh); 292 293 /** 294 * Write system tmp directory path into provided `out` buffer. 295 * Write at most `len` bytes not including terminating `NULL` char. 296 * @return Number of bytes writen. Zero on error. 297 */ 298 IW_EXPORT size_t iwp_tmpdir(char *out, size_t len); 299 300 /** 301 * Allocates unique temp file path. Caller should use `free()` 302 * to release path buffer. 303 * @return Zero on error. 304 */ 305 IW_EXPORT char* iwp_allocate_tmpfile_path(const char *prefix); 306 307 /** 308 * Set name of the current thread. On some platforms 309 * thread name canot be longer than 16 bytes including zero terminator. 310 */ 311 IW_EXPORT void iwp_set_current_thread_name(const char *name); 312 313 #endif 314