• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************************************
2  * IOWOW library
3  *
4  * MIT License
5  *
6  * Copyright (c) 2012-2022 Softmotions Ltd <info@softmotions.com>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all
16  * copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  *************************************************************************************************/
26 
27 #include "iwcfg.h"
28 #include "log/iwlog.h"
29 #include "platform/iwp.h"
30 #include "iwfile.h"
31 #include "iwutils.h"
32 
33 #include <fcntl.h>
34 #include <limits.h>
35 #include <unistd.h>
36 
37 #ifndef O_CLOEXEC
38 #define O_CLOEXEC 0
39 #endif
40 
41 #ifdef _WIN32
42 #include <libiberty/libiberty.h>
43 #define strndup xstrndup
44 #endif
45 
46 typedef struct IWFS_FILE_IMPL {
47   HANDLE fh;               /**< File handle. */
48   iwfs_openstatus ostatus; /**< File open status. */
49   IWFS_FILE_OPTS  opts;    /**< File open options. */
50 } IWF;
51 
_iwfs_write(struct IWFS_FILE * f,off_t off,const void * buf,size_t siz,size_t * sp)52 static iwrc _iwfs_write(struct IWFS_FILE *f, off_t off, const void *buf, size_t siz, size_t *sp) {
53   assert(f);
54   IWF *impl = f->impl;
55   if (!impl) {
56     return IW_ERROR_INVALID_STATE;
57   }
58   if (!(impl->opts.omode & IWFS_OWRITE)) {
59     return IW_ERROR_READONLY;
60   }
61   iwrc rc = iwp_pwrite(impl->fh, off, buf, siz, sp);
62   if (!rc && impl->opts.dlsnr) {
63     rc = impl->opts.dlsnr->onwrite(impl->opts.dlsnr, off, buf, siz, 0);
64   }
65   return rc;
66 }
67 
_iwfs_read(struct IWFS_FILE * f,off_t off,void * buf,size_t siz,size_t * sp)68 static iwrc _iwfs_read(struct IWFS_FILE *f, off_t off, void *buf, size_t siz, size_t *sp) {
69   assert(f);
70   IWF *impl = f->impl;
71   if (!impl) {
72     return IW_ERROR_INVALID_STATE;
73   }
74   return iwp_pread(impl->fh, off, buf, siz, sp);
75 }
76 
_iwfs_close(struct IWFS_FILE * f)77 static iwrc _iwfs_close(struct IWFS_FILE *f) {
78   if (!f || !f->impl) {
79     return 0;
80   }
81   iwrc rc = 0;
82   IWF *impl = f->impl;
83   IWFS_FILE_OPTS *opts = &impl->opts;
84 #ifndef _WIN32
85   if (opts->path && (opts->omode & IWFS_OUNLINK)) {
86     unlink(opts->path);
87   }
88 #endif
89   if (opts->lock_mode != IWP_NOLOCK) {
90     IWRC(iwp_unlock(impl->fh), rc);
91   }
92   IWRC(iwp_closefh(impl->fh), rc);
93   if (opts->path) {
94     free((char*) opts->path);
95     opts->path = 0;
96   }
97   free(f->impl);
98   f->impl = 0;
99   return rc;
100 }
101 
_iwfs_sync(struct IWFS_FILE * f,iwfs_sync_flags flags)102 static iwrc _iwfs_sync(struct IWFS_FILE *f, iwfs_sync_flags flags) {
103   assert(f);
104   iwrc rc = 0;
105   if (!f->impl) {
106     return IW_ERROR_INVALID_STATE;
107   }
108   IWF *impl = f->impl;
109   if (flags & IWFS_FDATASYNC) {
110 #ifdef __APPLE__
111     if (fcntl(impl->fh, F_FULLFSYNC) == -1) {
112       return iwrc_set_errno(IW_ERROR_IO_ERRNO, errno);
113     }
114 #else
115     if (iwp_fdatasync(impl->fh) == -1) {
116       return iwrc_set_errno(IW_ERROR_IO_ERRNO, errno);
117     }
118 #endif
119   } else if (iwp_fsync(impl->fh) == -1) {
120     return iwrc_set_errno(IW_ERROR_IO_ERRNO, errno);
121   }
122   if (impl->opts.dlsnr) {
123     rc = impl->opts.dlsnr->onsynced(impl->opts.dlsnr, 0);
124   }
125   return rc;
126 }
127 
_iwfs_state(struct IWFS_FILE * f,IWFS_FILE_STATE * state)128 static iwrc _iwfs_state(struct IWFS_FILE *f, IWFS_FILE_STATE *state) {
129   assert(f);
130   assert(state);
131   memset(state, 0, sizeof(*state));
132   IWF *impl = f->impl;
133   state->is_open = (impl != 0);
134   if (!state->is_open) {
135     return 0;
136   }
137   state->ostatus = impl->ostatus;
138   state->opts = impl->opts;
139   state->fh = impl->fh;
140   return 0;
141 }
142 
_iwfs_copy(struct IWFS_FILE * f,off_t off,size_t siz,off_t noff)143 static iwrc _iwfs_copy(struct IWFS_FILE *f, off_t off, size_t siz, off_t noff) {
144   assert(f);
145   IWF *impl = f->impl;
146   if (!impl) {
147     return IW_ERROR_INVALID_STATE;
148   }
149   if (!(impl->opts.omode & IWFS_OWRITE)) {
150     return IW_ERROR_READONLY;
151   }
152   iwrc rc = iwp_copy_bytes(impl->fh, off, siz, noff);
153   if (!rc && impl->opts.dlsnr) {
154     rc = impl->opts.dlsnr->oncopy(impl->opts.dlsnr, off, siz, noff, 0);
155   }
156   return rc;
157 }
158 
iwfs_file_open(IWFS_FILE * f,const IWFS_FILE_OPTS * _opts)159 iwrc iwfs_file_open(IWFS_FILE *f, const IWFS_FILE_OPTS *_opts) {
160   if (!f || !_opts || !_opts->path) {
161     return IW_ERROR_INVALID_ARGS;
162   }
163 
164   IWFS_FILE_OPTS *opts;
165   IWF *impl;
166   IWP_FILE_STAT fstat;
167   iwfs_omode omode;
168   iwrc rc;
169   int mode;
170 
171   memset(f, 0, sizeof(*f));
172   rc = iwfs_file_init();
173   RCRET(rc);
174 
175   f->write = _iwfs_write;
176   f->read = _iwfs_read;
177   f->close = _iwfs_close;
178   f->sync = _iwfs_sync;
179   f->state = _iwfs_state;
180   f->copy = _iwfs_copy;
181 
182   impl = f->impl = calloc(sizeof(IWF), 1);
183   if (!impl) {
184     return iwrc_set_errno(IW_ERROR_ALLOC, errno);
185   }
186 
187   impl->opts = *_opts;
188   opts = &impl->opts;
189 
190   if (opts->dlsnr) {
191     IWDLSNR *l = opts->dlsnr;
192     if (  !l->onopen || !l->onclosing || !l->oncopy || !l->onresize
193        || !l->onset || !l->onsynced || !l->onwrite) {
194       iwlog_ecode_error2(IW_ERROR_INVALID_ARGS, "Invalid 'opts->dlsnr' specified");
195       return IW_ERROR_INVALID_ARGS;
196     }
197   }
198 
199   if (opts->omode & IWFS_OTMP) {
200     opts->path = iwp_allocate_tmpfile_path(opts->path);
201     if (!opts->path) {
202       rc = iwrc_set_errno(IW_ERROR_ERRNO, errno);
203       goto finish;
204     }
205   } else {
206     opts->path = strndup(_opts->path, MAXPATHLEN);
207     if (!opts->path) {
208       rc = iwrc_set_errno(IW_ERROR_ALLOC, errno);
209       goto finish;
210     }
211   }
212 
213   if (!opts->lock_mode) {
214     opts->lock_mode = IWFS_DEFAULT_LOCKMODE; // -V1048
215   }
216   if (!opts->omode) {
217     opts->omode = IWFS_DEFAULT_OMODE;
218   }
219   if (!opts->filemode) {
220     opts->filemode = IWFS_DEFAULT_FILEMODE;
221   }
222   opts->omode |= IWFS_OREAD;
223   if (opts->omode & IWFS_OTMP) {
224     opts->omode |= IWFS_OTRUNC;
225     opts->lock_mode |= IWP_WLOCK;
226   }
227   if (opts->omode & IWFS_OTRUNC) {
228     opts->omode |= IWFS_OWRITE;
229     opts->omode |= IWFS_OCREATE;
230   }
231   if (opts->omode & IWFS_OUNLINK) {
232     opts->omode |= IWFS_OWRITE;
233   }
234   if ((opts->omode & IWFS_OCREATE) || (opts->omode & IWFS_OTRUNC)) {
235     opts->omode |= IWFS_OWRITE;
236   }
237   omode = opts->omode;
238 
239   if (!(opts->omode & IWFS_OWRITE) && (opts->lock_mode & IWP_WLOCK)) {
240     opts->lock_mode &= ~IWP_WLOCK;
241   }
242   rc = iwp_fstat(opts->path, &fstat);
243   if (!rc && !(opts->omode & IWFS_OTRUNC)) {
244     impl->ostatus = IWFS_OPEN_EXISTING;
245   } else {
246     impl->ostatus = IWFS_OPEN_NEW;
247   }
248   rc = 0;
249   mode = O_RDONLY | O_CLOEXEC;
250   if (omode & IWFS_OWRITE) {
251     mode = O_RDWR;
252     if (omode & IWFS_OCREATE) {
253       mode |= O_CREAT;
254     }
255     if (omode & IWFS_OTRUNC) {
256       mode |= O_TRUNC;
257     }
258   }
259 #ifndef _WIN32
260   impl->fh = open(opts->path, mode, opts->filemode);
261   if (INVALIDHANDLE(impl->fh)) {
262     if (errno == ENOENT) {
263       rc = IW_ERROR_NOT_EXISTS;
264     } else {
265       rc = iwrc_set_errno(IW_ERROR_IO_ERRNO, errno);
266     }
267     goto finish;
268   }
269 #else
270   DWORD womode = GENERIC_READ;
271   DWORD wcmode = OPEN_EXISTING;
272   if (omode & IWFS_OWRITE) {
273     womode |= GENERIC_WRITE;
274     if (omode & IWFS_OTRUNC) {
275       wcmode = CREATE_ALWAYS;
276     } else if (omode & IWFS_OCREATE) {
277       wcmode = OPEN_ALWAYS;
278     }
279   }
280   DWORD smode = FILE_SHARE_READ | FILE_SHARE_WRITE;
281   DWORD flags = FILE_ATTRIBUTE_NORMAL;
282   if (opts->omode & IWFS_OUNLINK) {
283     smode |= FILE_SHARE_DELETE;
284     flags |= FILE_FLAG_DELETE_ON_CLOSE;
285   }
286   impl->fh = CreateFile(opts->path, womode, smode, NULL, wcmode, flags, NULL);
287   if (INVALIDHANDLE(impl->fh)) {
288     uint32_t err = GetLastError();
289     if (err == ERROR_FILE_NOT_FOUND) {
290       rc = IW_ERROR_NOT_EXISTS;
291     } else {
292       rc = iwrc_set_werror(IW_ERROR_IO_ERRNO, err);
293     }
294     goto finish;
295   }
296 #endif
297   if (opts->lock_mode != IWP_NOLOCK) {
298     rc = iwp_flock(impl->fh, opts->lock_mode);
299     RCGO(rc, finish);
300   }
301 finish:
302   if (rc) {
303     impl->ostatus = IWFS_OPEN_FAIL;
304     if (opts->path) {
305       free((char*) opts->path);
306     }
307     f->impl = 0;
308     free(impl);
309   }
310   return rc;
311 }
312 
iwfs_file_init(void)313 iwrc iwfs_file_init(void) {
314   return iw_init();
315 }
316