• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-2020 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(HANDLE fh,
201                               off_t off, size_t siz,
202                               off_t noff);
203 /**
204  * @brief Get system page size.
205  */
206 IW_EXPORT size_t iwp_page_size(void);
207 
208 
209 /**
210  * @brief Minimal address space aligment for memory mapping.
211  */
212 IW_EXPORT size_t iwp_alloc_unit(void);
213 
214 /**
215  * @brief Truncate a file specified by @a fh to a size of @a len bytes
216  * @param fh File handle
217  * @param len File size
218  * @return `0` on sucess or error code.
219  */
220 IW_EXPORT iwrc iwp_ftruncate(HANDLE fh, off_t len);
221 
222 /**
223  * @brief Allocate extra space for a file.
224  * @param fh File handle
225  * @param len New file size
226  * @return `0` on sucess or error code.
227  */
228 IW_EXPORT iwrc iwp_fallocate(HANDLE fh, off_t len);
229 
230 /**
231  * @brief Pause execution of current thread
232  *        to the specified @a ms time in milliseconds.
233  * @param ms Thread pause time
234  */
235 IW_EXPORT iwrc iwp_sleep(uint64_t ms);
236 
237 /**
238  * @brief Recursive directory removal specified by @a path.
239  * @param path Directory path
240  */
241 IW_EXPORT iwrc iwp_removedir(const char *path);
242 
243 /**
244  * @brief Make directory specified by `path`
245  * as well as all parent directories.
246  *
247  * @param path Path to directory.
248  */
249 IW_EXPORT iwrc iwp_mkdirs(const char *path);
250 
251 /**
252  * @brief Get executable path for the current process.
253  * It will be writein into @a opath
254  * @param opath Allocated buffer at least `PATH_MAX` length
255  */
256 IW_EXPORT iwrc iwp_exec_path(char *opath);
257 
258 /**
259  * @brief Return number of CPU cores.
260  */
261 IW_EXPORT uint16_t iwp_num_cpu_cores(void);
262 
263 /**
264  * @brief Init iwp module.
265  * @return `0` on success or error code.
266  */
267 IW_EXPORT WUR iwrc iwp_init(void);
268 
269 IW_EXPORT iwrc iwp_fsync(HANDLE fh);
270 
271 IW_EXPORT iwrc iwp_fdatasync(HANDLE fh);
272 
273 /**
274  * Write system tmp directory path into provided `out` buffer.
275  * Write at most `len` bytes not including terminating `NULL` char.
276  * @return Number of bytes writen. Zero on error.
277  */
278 IW_EXPORT size_t iwp_tmpdir(char *out, size_t len);
279 
280 /**
281  * Allocates unique temp file path. Caller should use `free()`
282  * to release path buffer.
283  * @return Zero on error.
284  */
285 IW_EXPORT char* iwp_allocate_tmpfile_path(const char *prefix);
286 
287 #endif
288