/src/netcdf-c/libsrc/posixio.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018, University Corporation for Atmospheric Research |
3 | | * See netcdf/COPYRIGHT file for copying and redistribution conditions. |
4 | | */ |
5 | | /* $Id: posixio.c,v 1.89 2010/05/22 21:59:08 dmh Exp $ */ |
6 | | |
7 | | /* For MinGW Build */ |
8 | | |
9 | | #if HAVE_CONFIG_H |
10 | | #include <config.h> |
11 | | #endif |
12 | | |
13 | | #include <stdio.h> |
14 | | #include <assert.h> |
15 | | #include <stdlib.h> |
16 | | #include <errno.h> |
17 | | #include <string.h> |
18 | | #include <stdint.h> |
19 | | |
20 | | #ifdef HAVE_FCNTL_H |
21 | | #include <fcntl.h> |
22 | | #endif |
23 | | #ifdef HAVE_SYS_TYPES_H |
24 | | #include <sys/types.h> |
25 | | #endif |
26 | | #ifdef HAVE_SYS_STAT_H |
27 | | #include <sys/stat.h> |
28 | | #endif |
29 | | |
30 | | /* Windows platforms, including MinGW, Cygwin, Visual Studio */ |
31 | | #if defined(_WIN32) || defined(_WIN64) |
32 | | #include <windows.h> |
33 | | #include <winbase.h> |
34 | | #include <io.h> |
35 | | #endif |
36 | | |
37 | | #ifdef HAVE_UNISTD_H |
38 | | #include <unistd.h> |
39 | | #endif |
40 | | |
41 | | #ifndef NC_NOERR |
42 | | #define NC_NOERR 0 |
43 | | #endif |
44 | | |
45 | | #ifndef SEEK_SET |
46 | | #define SEEK_SET 0 |
47 | | #define SEEK_CUR 1 |
48 | | #define SEEK_END 2 |
49 | | #endif |
50 | | |
51 | | #include "ncpathmgr.h" |
52 | | #include "ncio.h" |
53 | | #include "fbits.h" |
54 | | #include "rnd.h" |
55 | | |
56 | | /* #define INSTRUMENT 1 */ |
57 | | #if INSTRUMENT /* debugging */ |
58 | | #undef NDEBUG |
59 | | #include <stdio.h> |
60 | | #include "instr.h" |
61 | | #endif |
62 | | |
63 | | #undef MIN /* system may define MIN somewhere and complain */ |
64 | 0 | #define MIN(mm,nn) (((mm) < (nn)) ? (mm) : (nn)) |
65 | | |
66 | | #if !defined(NDEBUG) && !defined(X_INT_MAX) |
67 | | #define X_INT_MAX 2147483647 |
68 | | #endif |
69 | | |
70 | | #if 0 /* !defined(NDEBUG) && !defined(X_ALIGN) */ |
71 | | #define X_ALIGN 4 |
72 | | #else |
73 | | #undef X_ALIGN |
74 | | #endif |
75 | | |
76 | | /* These are needed on mingw to get a dll to compile. They really |
77 | | * should be provided in sys/stats.h, but what the heck. Let's not be |
78 | | * too picky! */ |
79 | | #ifndef S_IRGRP |
80 | | #define S_IRGRP 0000040 |
81 | | #endif |
82 | | #ifndef S_IROTH |
83 | | #define S_IROTH 0000004 |
84 | | #endif |
85 | | #ifndef S_IWGRP |
86 | | #define S_IWGRP 0000020 |
87 | | #endif |
88 | | #ifndef S_IWOTH |
89 | | #define S_IWOTH 0000002 |
90 | | #endif |
91 | | |
92 | | /*Forward*/ |
93 | | static int ncio_px_filesize(ncio *nciop, off_t *filesizep); |
94 | | static int ncio_px_pad_length(ncio *nciop, off_t length); |
95 | | static int ncio_px_close(ncio *nciop, int doUnlink); |
96 | | static int ncio_spx_close(ncio *nciop, int doUnlink); |
97 | | |
98 | | |
99 | | /* |
100 | | * Define the following for debugging. |
101 | | */ |
102 | | /* #define ALWAYS_NC_SHARE 1 */ |
103 | | |
104 | | /* Begin OS */ |
105 | | |
106 | | #ifndef POSIXIO_DEFAULT_PAGESIZE |
107 | 0 | #define POSIXIO_DEFAULT_PAGESIZE 4096 |
108 | | #endif |
109 | | |
110 | | /*! Cross-platform file length. |
111 | | * |
112 | | * Some versions of Visual Studio are throwing errno 132 |
113 | | * when fstat is used on large files. This function is |
114 | | * an attempt to get around that. |
115 | | * |
116 | | * @par fd File Descriptor. |
117 | | * @return -1 on error, length of file (in bytes) otherwise. |
118 | | */ |
119 | 0 | static off_t nc_get_filelen(const int fd) { |
120 | |
|
121 | 0 | off_t flen; |
122 | |
|
123 | | #ifdef HAVE_FILE_LENGTH_I64 |
124 | | int64_t file_len = 0; |
125 | | if ((file_len = _filelengthi64(fd)) < 0) { |
126 | | return file_len; |
127 | | } |
128 | | flen = (off_t)file_len; |
129 | | |
130 | | #else |
131 | 0 | int res = 0; |
132 | 0 | struct stat sb; |
133 | 0 | if((res = fstat(fd,&sb)) <0) |
134 | 0 | return res; |
135 | | |
136 | 0 | flen = sb.st_size; |
137 | 0 | #endif |
138 | |
|
139 | 0 | return flen; |
140 | |
|
141 | 0 | } |
142 | | |
143 | | |
144 | | /* |
145 | | * What is the system pagesize? |
146 | | */ |
147 | | static size_t |
148 | | pagesize(void) |
149 | 0 | { |
150 | 0 | size_t pgsz; |
151 | | #if defined(_WIN32) || defined(_WIN64) |
152 | | SYSTEM_INFO info; |
153 | | #endif |
154 | | /* Hmm, aren't standards great? */ |
155 | | #if defined(_SC_PAGE_SIZE) && !defined(_SC_PAGESIZE) |
156 | | #define _SC_PAGESIZE _SC_PAGE_SIZE |
157 | | #endif |
158 | | |
159 | | /* For MinGW Builds */ |
160 | | #if defined(_WIN32) || defined(_WIN64) |
161 | | GetSystemInfo(&info); |
162 | | pgsz = (size_t)info.dwPageSize; |
163 | | #elif defined(_SC_PAGESIZE) |
164 | 0 | pgsz = (size_t)sysconf(_SC_PAGESIZE); |
165 | | #elif defined(HAVE_GETPAGESIZE) |
166 | | pgsz = (size_t) getpagesize(); |
167 | | #endif |
168 | 0 | if(pgsz > 0) |
169 | 0 | return (size_t) pgsz; |
170 | 0 | return (size_t)POSIXIO_DEFAULT_PAGESIZE; |
171 | 0 | } |
172 | | |
173 | | /* |
174 | | * What is the preferred I/O block size? |
175 | | */ |
176 | | static size_t |
177 | | blksize(int fd) |
178 | 0 | { |
179 | | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
180 | | #ifdef HAVE_SYS_STAT_H |
181 | | struct stat sb; |
182 | | if (fstat(fd, &sb) > -1) |
183 | | { |
184 | | if(sb.st_blksize >= 8192) |
185 | | return (size_t) sb.st_blksize; |
186 | | return 8192; |
187 | | } |
188 | | /* else, silent in the face of error */ |
189 | | #else |
190 | | NC_UNUSED(fd); |
191 | | #endif |
192 | | #else |
193 | 0 | NC_UNUSED(fd); |
194 | 0 | #endif |
195 | 0 | return (size_t) 2 * pagesize(); |
196 | 0 | } |
197 | | |
198 | | |
199 | | /* |
200 | | * Sortof like ftruncate, except won't make the |
201 | | * file shorter. |
202 | | */ |
203 | | static int |
204 | | fgrow(const int fd, const off_t len) |
205 | 0 | { |
206 | 0 | struct stat sb; |
207 | 0 | if (fstat(fd, &sb) < 0) |
208 | 0 | return errno; |
209 | 0 | if (len < sb.st_size) |
210 | 0 | return NC_NOERR; |
211 | 0 | { |
212 | 0 | const long dumb = 0; |
213 | | /* we don't use ftruncate() due to problem with FAT32 file systems */ |
214 | | /* cache current position */ |
215 | 0 | const off_t pos = lseek(fd, 0, SEEK_CUR); |
216 | 0 | if(pos < 0) |
217 | 0 | return errno; |
218 | 0 | if (lseek(fd, len-sizeof(dumb), SEEK_SET) < 0) |
219 | 0 | return errno; |
220 | 0 | if(write(fd, &dumb, sizeof(dumb)) < 0) |
221 | 0 | return errno; |
222 | 0 | if (lseek(fd, pos, SEEK_SET) < 0) |
223 | 0 | return errno; |
224 | 0 | } |
225 | 0 | return NC_NOERR; |
226 | 0 | } |
227 | | |
228 | | |
229 | | /* |
230 | | * Sortof like ftruncate, except won't make the file shorter. Differs |
231 | | * from fgrow by only writing one byte at designated seek position, if |
232 | | * needed. |
233 | | */ |
234 | | static int |
235 | | fgrow2(const int fd, const off_t len) |
236 | 0 | { |
237 | | |
238 | | |
239 | | /* There is a problem with fstat on Windows based systems |
240 | | which manifests (so far) when Config RELEASE is built. |
241 | | Use _filelengthi64 isntead. |
242 | | |
243 | | See https://github.com/Unidata/netcdf-c/issues/188 |
244 | | |
245 | | */ |
246 | | |
247 | |
|
248 | 0 | off_t file_len = nc_get_filelen(fd); |
249 | 0 | if(file_len < 0) return errno; |
250 | 0 | if(len <= file_len) |
251 | 0 | return NC_NOERR; |
252 | 0 | { |
253 | 0 | const char dumb = 0; |
254 | | /* we don't use ftruncate() due to problem with FAT32 file systems */ |
255 | | /* cache current position */ |
256 | 0 | const off_t pos = lseek(fd, 0, SEEK_CUR); |
257 | 0 | if(pos < 0) |
258 | 0 | return errno; |
259 | 0 | if (lseek(fd, len-1, SEEK_SET) < 0) |
260 | 0 | return errno; |
261 | 0 | if(write(fd, &dumb, sizeof(dumb)) < 0) |
262 | 0 | return errno; |
263 | 0 | if (lseek(fd, pos, SEEK_SET) < 0) |
264 | 0 | return errno; |
265 | 0 | } |
266 | 0 | return NC_NOERR; |
267 | 0 | } |
268 | | /* End OS */ |
269 | | /* Begin px */ |
270 | | |
271 | | /* The px_ functions are for posix systems, when NC_SHARE is not in |
272 | | effect. */ |
273 | | |
274 | | /* Write out a "page" of data to the file. The size of the page |
275 | | (i.e. the extent) varies. |
276 | | |
277 | | nciop - pointer to the file metadata. |
278 | | offset - where in the file should this page be written. |
279 | | extent - how many bytes should be written. |
280 | | vp - pointer to the data to write. |
281 | | posp - pointer to current position in file, updated after write. |
282 | | */ |
283 | | static int |
284 | | px_pgout(ncio *const nciop, |
285 | | off_t const offset, const size_t extent, |
286 | | void *const vp, off_t *posp) |
287 | 0 | { |
288 | 0 | ssize_t partial; |
289 | 0 | size_t nextent; |
290 | 0 | char *nvp; |
291 | | #ifdef X_ALIGN |
292 | | assert(offset % X_ALIGN == 0); |
293 | | #endif |
294 | |
|
295 | 0 | assert(*posp == OFF_NONE || *posp == lseek(nciop->fd, 0, SEEK_CUR)); |
296 | | |
297 | 0 | if(*posp != offset) |
298 | 0 | { |
299 | 0 | if(lseek(nciop->fd, offset, SEEK_SET) != offset) |
300 | 0 | { |
301 | 0 | return errno; |
302 | 0 | } |
303 | 0 | *posp = offset; |
304 | 0 | } |
305 | | /* Old write, didn't handle partial writes correctly */ |
306 | | /* if(write(nciop->fd, vp, extent) != (ssize_t) extent) */ |
307 | | /* { */ |
308 | | /* return errno; */ |
309 | | /* } */ |
310 | 0 | nextent = extent; |
311 | 0 | nvp = vp; |
312 | 0 | while((partial = write(nciop->fd, nvp, nextent)) != -1) { |
313 | 0 | if(partial == nextent) |
314 | 0 | break; |
315 | 0 | nvp += partial; |
316 | 0 | nextent -= partial; |
317 | 0 | } |
318 | 0 | if(partial == -1) |
319 | 0 | return errno; |
320 | 0 | *posp += extent; |
321 | |
|
322 | 0 | return NC_NOERR; |
323 | 0 | } |
324 | | |
325 | | /*! Read in a page of data. |
326 | | |
327 | | @param[in] nciop A pointer to the ncio struct for this file. |
328 | | @param[in] offset The byte offset in file where read starts. |
329 | | @param[in] extent The size of the page that will be read. |
330 | | @param[in] vp A pointer to where the data will end up. |
331 | | |
332 | | @param[in,out] nreadp Returned number of bytes actually read (may be less than extent). |
333 | | @param[in,out] posp The pointer to current position in file, updated after read. |
334 | | @return Return 0 on success, otherwise an error code. |
335 | | */ |
336 | | static int |
337 | | px_pgin(ncio *const nciop, |
338 | | off_t const offset, const size_t extent, |
339 | | void *const vp, size_t *nreadp, off_t *posp) |
340 | 0 | { |
341 | 0 | int status; |
342 | 0 | ssize_t nread; |
343 | | #ifdef X_ALIGN |
344 | | assert(offset % X_ALIGN == 0); |
345 | | assert(extent % X_ALIGN == 0); |
346 | | #endif |
347 | | /* *posp == OFF_NONE (-1) on first call. This |
348 | | is problematic because lseek also returns -1 |
349 | | on error. Use errno instead. */ |
350 | 0 | if(*posp != OFF_NONE && *posp != lseek(nciop->fd, 0, SEEK_CUR)) { |
351 | 0 | if(errno) { |
352 | 0 | status = errno; |
353 | 0 | printf("Error %d: %s\n",errno,strerror(errno)); |
354 | 0 | return status; |
355 | 0 | } |
356 | 0 | } |
357 | | |
358 | 0 | if(*posp != offset) |
359 | 0 | { |
360 | 0 | if(lseek(nciop->fd, offset, SEEK_SET) != offset) |
361 | 0 | { |
362 | 0 | status = errno; |
363 | 0 | return status; |
364 | 0 | } |
365 | 0 | *posp = offset; |
366 | 0 | } |
367 | | |
368 | 0 | errno = 0; |
369 | | /* Handle the case where the read is interrupted |
370 | | by a signal (see NCF-337, |
371 | | http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html) |
372 | | |
373 | | When this happens, nread will (should) be the bytes read, and |
374 | | errno will be set to EINTR. On older systems nread might be -1. |
375 | | If this is the case, there's not a whole lot we can do about it |
376 | | as we can't compute any offsets, so we will attempt to read again. |
377 | | This *feels* like it could lead to an infinite loop, but it shouldn't |
378 | | unless the read is being constantly interrupted by a signal, and is |
379 | | on an older system which returns -1 instead of bytexs read. |
380 | | |
381 | | The case where it's a short read is already handled by the function |
382 | | (according to the comment below, at least). */ |
383 | 0 | do { |
384 | 0 | nread = read(nciop->fd,vp,extent); |
385 | 0 | } while (nread == -1 && errno == EINTR); |
386 | | |
387 | |
|
388 | 0 | if(nread != (ssize_t)extent) { |
389 | 0 | status = errno; |
390 | 0 | if( nread == -1 || (status != EINTR && status != NC_NOERR)) |
391 | 0 | return status; |
392 | | /* else it's okay we read less than asked for */ |
393 | 0 | (void) memset((char *)vp + nread, 0, (ssize_t)extent - nread); |
394 | 0 | } |
395 | | |
396 | 0 | *nreadp = nread; |
397 | 0 | *posp += nread; |
398 | |
|
399 | 0 | return NC_NOERR; |
400 | 0 | } |
401 | | |
402 | | /* This struct is for POSIX systems, with NC_SHARE not in effect. If |
403 | | NC_SHARE is used, see ncio_spx. |
404 | | |
405 | | blksz - block size for reads and writes to file. |
406 | | pos - current read/write position in file. |
407 | | bf_offset - file offset corresponding to start of memory buffer |
408 | | bf_extent - number of bytes in I/O request |
409 | | bf_cnt - number of bytes available in buffer |
410 | | bf_base - pointer to beginning of buffer. |
411 | | bf_rflags - buffer region flags (defined in ncio.h) tell the lock |
412 | | status, read/write permissions, and modification status of regions |
413 | | of data in the buffer. |
414 | | bf_refcount - buffer reference count. |
415 | | slave - used in moves. |
416 | | */ |
417 | | typedef struct ncio_px { |
418 | | size_t blksz; |
419 | | off_t pos; |
420 | | /* buffer */ |
421 | | off_t bf_offset; |
422 | | size_t bf_extent; |
423 | | size_t bf_cnt; |
424 | | void *bf_base; |
425 | | int bf_rflags; |
426 | | int bf_refcount; |
427 | | /* chain for double buffering in px_move */ |
428 | | struct ncio_px *slave; |
429 | | } ncio_px; |
430 | | |
431 | | |
432 | | /*ARGSUSED*/ |
433 | | /* This function indicates the file region starting at offset may be |
434 | | released. |
435 | | |
436 | | This is for POSIX, without NC_SHARE. If called with RGN_MODIFIED |
437 | | flag, sets the modified flag in pxp->bf_rflags and decrements the |
438 | | reference count. |
439 | | |
440 | | pxp - pointer to posix non-share ncio_px struct. |
441 | | |
442 | | offset - file offset for beginning of to region to be |
443 | | released. |
444 | | |
445 | | rflags - only RGN_MODIFIED is relevant to this function, others ignored |
446 | | */ |
447 | | static int |
448 | | px_rel(ncio_px *const pxp, off_t offset, int rflags) |
449 | 0 | { |
450 | 0 | assert(pxp->bf_offset <= offset |
451 | 0 | && offset < pxp->bf_offset + (off_t) pxp->bf_extent); |
452 | 0 | assert(pIf(fIsSet(rflags, RGN_MODIFIED), |
453 | 0 | fIsSet(pxp->bf_rflags, RGN_WRITE))); |
454 | 0 | NC_UNUSED(offset); |
455 | |
|
456 | 0 | if(fIsSet(rflags, RGN_MODIFIED)) |
457 | 0 | { |
458 | 0 | fSet(pxp->bf_rflags, RGN_MODIFIED); |
459 | 0 | } |
460 | 0 | pxp->bf_refcount--; |
461 | |
|
462 | 0 | return NC_NOERR; |
463 | 0 | } |
464 | | |
465 | | /* This function indicates the file region starting at offset may be |
466 | | released. Each read or write to the file is bracketed by a call to |
467 | | the "get" region function and a call to the "rel" region function. |
468 | | If you only read from the memory region, release it with a flag of |
469 | | 0, if you modify the region, release it with a flag of |
470 | | RGN_MODIFIED. |
471 | | |
472 | | For POSIX system, without NC_SHARE, this becomes the rel function |
473 | | pointed to by the ncio rel function pointer. It merely checks for |
474 | | file write permission, then calls px_rel to do everything. |
475 | | |
476 | | nciop - pointer to ncio struct. |
477 | | offset - num bytes from beginning of buffer to region to be |
478 | | released. |
479 | | rflags - only RGN_MODIFIED is relevant to this function, others ignored |
480 | | */ |
481 | | static int |
482 | | ncio_px_rel(ncio *const nciop, off_t offset, int rflags) |
483 | 0 | { |
484 | 0 | ncio_px *const pxp = (ncio_px *)nciop->pvt; |
485 | |
|
486 | 0 | if(fIsSet(rflags, RGN_MODIFIED) && !fIsSet(nciop->ioflags, NC_WRITE)) |
487 | 0 | return EPERM; /* attempt to write readonly file */ |
488 | | |
489 | 0 | return px_rel(pxp, offset, rflags); |
490 | 0 | } |
491 | | |
492 | | /* POSIX get. This will "make a region available." Since we're using |
493 | | buffered IO, this means that if needed, we'll fetch a new page from |
494 | | the file, otherwise, just return a pointer to what's in memory |
495 | | already. |
496 | | |
497 | | nciop - pointer to ncio struct, containing file info. |
498 | | pxp - pointer to ncio_px struct, which contains special metadate |
499 | | for posix files without NC_SHARE. |
500 | | offset - start byte of region to get. |
501 | | extent - how many bytes to read. |
502 | | rflags - One of the RGN_* flags defined in ncio.h. |
503 | | vpp - pointer to pointer that will receive data. |
504 | | |
505 | | NOTES: |
506 | | |
507 | | * For blkoffset round offset down to the nearest pxp->blksz. This |
508 | | provides the offset (in bytes) to the beginning of the block that |
509 | | holds the current offset. |
510 | | |
511 | | * diff tells how far into the current block we are. |
512 | | |
513 | | * For blkextent round up to the number of bytes at the beginning of |
514 | | the next block, after the one that holds our current position, plus |
515 | | whatever extra (i.e. the extent) that we are about to grab. |
516 | | |
517 | | * The blkextent can't be more than twice the pxp->blksz. That's |
518 | | because the pxp->blksize is the sizehint, and in ncio_px_init2 the |
519 | | buffer (pointed to by pxp->bf-base) is allocated with 2 * |
520 | | *sizehintp. This is checked (unnecessarily) more than once in |
521 | | asserts. |
522 | | |
523 | | * If this is called on a newly opened file, pxp->bf_offset will be |
524 | | OFF_NONE and we'll jump to label pgin to immediately read in a |
525 | | page. |
526 | | */ |
527 | | static int |
528 | | px_get(ncio *const nciop, ncio_px *const pxp, |
529 | | off_t offset, size_t extent, |
530 | | int rflags, |
531 | | void **const vpp) |
532 | 0 | { |
533 | 0 | int status = NC_NOERR; |
534 | |
|
535 | 0 | const off_t blkoffset = _RNDDOWN(offset, (off_t)pxp->blksz); |
536 | 0 | off_t diff = (size_t)(offset - blkoffset); |
537 | 0 | off_t blkextent = _RNDUP(diff + extent, pxp->blksz); |
538 | |
|
539 | 0 | assert(extent != 0); |
540 | 0 | assert(extent < X_INT_MAX); /* sanity check */ |
541 | 0 | assert(offset >= 0); /* sanity check */ |
542 | | |
543 | 0 | if(2 * pxp->blksz < blkextent) |
544 | 0 | return E2BIG; /* TODO: temporary kludge */ |
545 | 0 | if(pxp->bf_offset == OFF_NONE) |
546 | 0 | { |
547 | | /* Uninitialized */ |
548 | 0 | if(pxp->bf_base == NULL) |
549 | 0 | { |
550 | 0 | assert(pxp->bf_extent == 0); |
551 | 0 | assert(blkextent <= 2 * pxp->blksz); |
552 | 0 | pxp->bf_base = malloc(2 * pxp->blksz); |
553 | 0 | if(pxp->bf_base == NULL) |
554 | 0 | return ENOMEM; |
555 | 0 | } |
556 | 0 | goto pgin; |
557 | 0 | } |
558 | | /* else */ |
559 | 0 | assert(blkextent <= 2 * pxp->blksz); |
560 | | |
561 | 0 | if(blkoffset == pxp->bf_offset) |
562 | 0 | { |
563 | | /* hit */ |
564 | 0 | if(blkextent > pxp->bf_extent) |
565 | 0 | { |
566 | | /* page in upper */ |
567 | 0 | void *const middle = |
568 | 0 | (void *)((char *)pxp->bf_base + pxp->blksz); |
569 | 0 | assert(pxp->bf_extent == pxp->blksz); |
570 | 0 | status = px_pgin(nciop, |
571 | 0 | pxp->bf_offset + (off_t)pxp->blksz, |
572 | 0 | pxp->blksz, |
573 | 0 | middle, |
574 | 0 | &pxp->bf_cnt, |
575 | 0 | &pxp->pos); |
576 | 0 | if(status != NC_NOERR) |
577 | 0 | return status; |
578 | 0 | pxp->bf_extent = 2 * pxp->blksz; |
579 | 0 | pxp->bf_cnt += pxp->blksz; |
580 | 0 | } |
581 | 0 | goto done; |
582 | 0 | } |
583 | | /* else */ |
584 | | |
585 | 0 | if(pxp->bf_extent > pxp->blksz |
586 | 0 | && blkoffset == pxp->bf_offset + (off_t)pxp->blksz) |
587 | 0 | { |
588 | | /* hit in upper half */ |
589 | 0 | if(blkextent == pxp->blksz) |
590 | 0 | { |
591 | | /* all in upper half, no fault needed */ |
592 | 0 | diff += pxp->blksz; |
593 | 0 | goto done; |
594 | 0 | } |
595 | | /* else */ |
596 | 0 | if(pxp->bf_cnt > pxp->blksz) |
597 | 0 | { |
598 | | /* data in upper half */ |
599 | 0 | void *const middle = |
600 | 0 | (void *)((char *)pxp->bf_base + pxp->blksz); |
601 | 0 | assert(pxp->bf_extent == 2 * pxp->blksz); |
602 | 0 | if(fIsSet(pxp->bf_rflags, RGN_MODIFIED)) |
603 | 0 | { |
604 | | /* page out lower half */ |
605 | 0 | assert(pxp->bf_refcount <= 0); |
606 | 0 | status = px_pgout(nciop, |
607 | 0 | pxp->bf_offset, |
608 | 0 | pxp->blksz, |
609 | 0 | pxp->bf_base, |
610 | 0 | &pxp->pos); |
611 | 0 | if(status != NC_NOERR) |
612 | 0 | return status; |
613 | 0 | } |
614 | 0 | pxp->bf_cnt -= pxp->blksz; |
615 | | /* copy upper half into lower half */ |
616 | 0 | (void) memcpy(pxp->bf_base, middle, pxp->bf_cnt); |
617 | 0 | } |
618 | 0 | else /* added to fix nofill bug */ |
619 | 0 | { |
620 | 0 | assert(pxp->bf_extent == 2 * pxp->blksz); |
621 | | /* still have to page out lower half, if modified */ |
622 | 0 | if(fIsSet(pxp->bf_rflags, RGN_MODIFIED)) |
623 | 0 | { |
624 | 0 | assert(pxp->bf_refcount <= 0); |
625 | 0 | status = px_pgout(nciop, |
626 | 0 | pxp->bf_offset, |
627 | 0 | pxp->blksz, |
628 | 0 | pxp->bf_base, |
629 | 0 | &pxp->pos); |
630 | 0 | if(status != NC_NOERR) |
631 | 0 | return status; |
632 | 0 | } |
633 | 0 | } |
634 | 0 | pxp->bf_offset = blkoffset; |
635 | | /* pxp->bf_extent = pxp->blksz; */ |
636 | |
|
637 | 0 | assert(blkextent == 2 * pxp->blksz); |
638 | 0 | { |
639 | | /* page in upper */ |
640 | 0 | void *const middle = |
641 | 0 | (void *)((char *)pxp->bf_base + pxp->blksz); |
642 | 0 | status = px_pgin(nciop, |
643 | 0 | pxp->bf_offset + (off_t)pxp->blksz, |
644 | 0 | pxp->blksz, |
645 | 0 | middle, |
646 | 0 | &pxp->bf_cnt, |
647 | 0 | &pxp->pos); |
648 | 0 | if(status != NC_NOERR) |
649 | 0 | return status; |
650 | 0 | pxp->bf_extent = 2 * pxp->blksz; |
651 | 0 | pxp->bf_cnt += pxp->blksz; |
652 | 0 | } |
653 | 0 | goto done; |
654 | 0 | } |
655 | | /* else */ |
656 | | |
657 | 0 | if(blkoffset == pxp->bf_offset - (off_t)pxp->blksz) |
658 | 0 | { |
659 | | /* wants the page below */ |
660 | 0 | void *const middle = |
661 | 0 | (void *)((char *)pxp->bf_base + pxp->blksz); |
662 | 0 | size_t upper_cnt = 0; |
663 | 0 | if(pxp->bf_cnt > pxp->blksz) |
664 | 0 | { |
665 | | /* data in upper half */ |
666 | 0 | assert(pxp->bf_extent == 2 * pxp->blksz); |
667 | 0 | if(fIsSet(pxp->bf_rflags, RGN_MODIFIED)) |
668 | 0 | { |
669 | | /* page out upper half */ |
670 | 0 | assert(pxp->bf_refcount <= 0); |
671 | 0 | status = px_pgout(nciop, |
672 | 0 | pxp->bf_offset + (off_t)pxp->blksz, |
673 | 0 | pxp->bf_cnt - pxp->blksz, |
674 | 0 | middle, |
675 | 0 | &pxp->pos); |
676 | 0 | if(status != NC_NOERR) |
677 | 0 | return status; |
678 | 0 | } |
679 | 0 | pxp->bf_cnt = pxp->blksz; |
680 | 0 | pxp->bf_extent = pxp->blksz; |
681 | 0 | } |
682 | 0 | if(pxp->bf_cnt > 0) |
683 | 0 | { |
684 | | /* copy lower half into upper half */ |
685 | 0 | (void) memcpy(middle, pxp->bf_base, pxp->blksz); |
686 | 0 | upper_cnt = pxp->bf_cnt; |
687 | 0 | } |
688 | | /* read page below into lower half */ |
689 | 0 | status = px_pgin(nciop, |
690 | 0 | blkoffset, |
691 | 0 | pxp->blksz, |
692 | 0 | pxp->bf_base, |
693 | 0 | &pxp->bf_cnt, |
694 | 0 | &pxp->pos); |
695 | 0 | if(status != NC_NOERR) |
696 | 0 | return status; |
697 | 0 | pxp->bf_offset = blkoffset; |
698 | 0 | if(upper_cnt != 0) |
699 | 0 | { |
700 | 0 | pxp->bf_extent = 2 * pxp->blksz; |
701 | 0 | pxp->bf_cnt = pxp->blksz + upper_cnt; |
702 | 0 | } |
703 | 0 | else |
704 | 0 | { |
705 | 0 | pxp->bf_extent = pxp->blksz; |
706 | 0 | } |
707 | 0 | goto done; |
708 | 0 | } |
709 | | /* else */ |
710 | | |
711 | | /* no overlap */ |
712 | 0 | if(fIsSet(pxp->bf_rflags, RGN_MODIFIED)) |
713 | 0 | { |
714 | 0 | assert(pxp->bf_refcount <= 0); |
715 | 0 | status = px_pgout(nciop, |
716 | 0 | pxp->bf_offset, |
717 | 0 | pxp->bf_cnt, |
718 | 0 | pxp->bf_base, |
719 | 0 | &pxp->pos); |
720 | 0 | if(status != NC_NOERR) |
721 | 0 | return status; |
722 | 0 | pxp->bf_rflags = 0; |
723 | 0 | } |
724 | | |
725 | 0 | pgin: |
726 | 0 | status = px_pgin(nciop, |
727 | 0 | blkoffset, |
728 | 0 | blkextent, |
729 | 0 | pxp->bf_base, |
730 | 0 | &pxp->bf_cnt, |
731 | 0 | &pxp->pos); |
732 | 0 | if(status != NC_NOERR) |
733 | 0 | return status; |
734 | 0 | pxp->bf_offset = blkoffset; |
735 | 0 | pxp->bf_extent = blkextent; |
736 | |
|
737 | 0 | done: |
738 | 0 | extent += diff; |
739 | 0 | if(pxp->bf_cnt < extent) |
740 | 0 | pxp->bf_cnt = extent; |
741 | 0 | assert(pxp->bf_cnt <= pxp->bf_extent); |
742 | | |
743 | 0 | pxp->bf_rflags |= rflags; |
744 | 0 | pxp->bf_refcount++; |
745 | |
|
746 | 0 | *vpp = (void *)((signed char*)pxp->bf_base + diff); |
747 | 0 | return NC_NOERR; |
748 | 0 | } |
749 | | |
750 | | /* Request that the region (offset, extent) be made available through |
751 | | *vpp. |
752 | | |
753 | | This function converts a file region specified by an offset and |
754 | | extent to a memory pointer. The region may be locked until the |
755 | | corresponding call to rel(). |
756 | | |
757 | | For POSIX systems, without NC_SHARE. This function gets a page of |
758 | | size extent? |
759 | | |
760 | | This is a wrapper for the function px_get, which does all the heavy |
761 | | lifting. |
762 | | |
763 | | nciop - pointer to ncio struct for this file. |
764 | | offset - offset (from beginning of file?) to the data we want to |
765 | | read. |
766 | | extent - the number of bytes to read from the file. |
767 | | rflags - One of the RGN_* flags defined in ncio.h. |
768 | | vpp - handle to point at data when it's been read. |
769 | | */ |
770 | | static int |
771 | | ncio_px_get(ncio *const nciop, |
772 | | off_t offset, size_t extent, |
773 | | int rflags, |
774 | | void **const vpp) |
775 | 0 | { |
776 | 0 | ncio_px *const pxp = (ncio_px *)nciop->pvt; |
777 | |
|
778 | 0 | if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE)) |
779 | 0 | return EPERM; /* attempt to write readonly file */ |
780 | | |
781 | | /* reclaim space used in move */ |
782 | 0 | if(pxp->slave != NULL) |
783 | 0 | { |
784 | 0 | if(pxp->slave->bf_base != NULL) |
785 | 0 | { |
786 | 0 | free(pxp->slave->bf_base); |
787 | 0 | pxp->slave->bf_base = NULL; |
788 | 0 | pxp->slave->bf_extent = 0; |
789 | 0 | pxp->slave->bf_offset = OFF_NONE; |
790 | 0 | } |
791 | 0 | free(pxp->slave); |
792 | 0 | pxp->slave = NULL; |
793 | 0 | } |
794 | 0 | return px_get(nciop, pxp, offset, extent, rflags, vpp); |
795 | 0 | } |
796 | | |
797 | | |
798 | | /* ARGSUSED */ |
799 | | static int |
800 | | px_double_buffer(ncio *const nciop, off_t to, off_t from, |
801 | | size_t nbytes, int rflags) |
802 | 0 | { |
803 | 0 | ncio_px *const pxp = (ncio_px *)nciop->pvt; |
804 | 0 | int status = NC_NOERR; |
805 | 0 | void *src; |
806 | 0 | void *dest; |
807 | 0 | NC_UNUSED(rflags); |
808 | |
|
809 | | #if INSTRUMENT |
810 | | fprintf(stderr, "\tdouble_buffr %ld %ld %ld\n", |
811 | | (long)to, (long)from, (long)nbytes); |
812 | | #endif |
813 | 0 | status = px_get(nciop, pxp, to, nbytes, RGN_WRITE, |
814 | 0 | &dest); |
815 | 0 | if(status != NC_NOERR) |
816 | 0 | return status; |
817 | | |
818 | 0 | if(pxp->slave == NULL) |
819 | 0 | { |
820 | 0 | pxp->slave = (ncio_px *) malloc(sizeof(ncio_px)); |
821 | 0 | if(pxp->slave == NULL) |
822 | 0 | return ENOMEM; |
823 | | |
824 | 0 | pxp->slave->blksz = pxp->blksz; |
825 | | /* pos done below */ |
826 | 0 | pxp->slave->bf_offset = pxp->bf_offset; |
827 | 0 | pxp->slave->bf_extent = pxp->bf_extent; |
828 | 0 | pxp->slave->bf_cnt = pxp->bf_cnt; |
829 | 0 | pxp->slave->bf_base = malloc(2 * pxp->blksz); |
830 | 0 | if(pxp->slave->bf_base == NULL) |
831 | 0 | return ENOMEM; |
832 | 0 | (void) memcpy(pxp->slave->bf_base, pxp->bf_base, |
833 | 0 | pxp->bf_extent); |
834 | 0 | pxp->slave->bf_rflags = 0; |
835 | 0 | pxp->slave->bf_refcount = 0; |
836 | 0 | pxp->slave->slave = NULL; |
837 | 0 | } |
838 | | |
839 | 0 | pxp->slave->pos = pxp->pos; |
840 | 0 | status = px_get(nciop, pxp->slave, from, nbytes, 0, |
841 | 0 | &src); |
842 | 0 | if(status != NC_NOERR) |
843 | 0 | return status; |
844 | 0 | if(pxp->pos != pxp->slave->pos) |
845 | 0 | { |
846 | | /* position changed, sync */ |
847 | 0 | pxp->pos = pxp->slave->pos; |
848 | 0 | } |
849 | |
|
850 | 0 | (void) memcpy(dest, src, nbytes); |
851 | |
|
852 | 0 | (void)px_rel(pxp->slave, from, 0); |
853 | 0 | (void)px_rel(pxp, to, RGN_MODIFIED); |
854 | |
|
855 | 0 | return status; |
856 | 0 | } |
857 | | |
858 | | /* Like memmove(), safely move possibly overlapping data. |
859 | | |
860 | | Copy one region to another without making anything available to |
861 | | higher layers. May be just implemented in terms of get() and rel(), |
862 | | or may be tricky to be efficient. Only used in by nc_enddef() |
863 | | after redefinition. |
864 | | |
865 | | nciop - pointer to ncio struct with file info. |
866 | | to - src for move? |
867 | | from - dest for move? |
868 | | nbytes - number of bytes to move. |
869 | | rflags - One of the RGN_* flags defined in ncio.h. The only |
870 | | reasonable flag value is RGN_NOLOCK. |
871 | | */ |
872 | | static int |
873 | | ncio_px_move(ncio *const nciop, off_t to, off_t from, |
874 | | size_t nbytes, int rflags) |
875 | 0 | { |
876 | 0 | ncio_px *const pxp = (ncio_px *)nciop->pvt; |
877 | 0 | int status = NC_NOERR; |
878 | 0 | off_t lower; |
879 | 0 | off_t upper; |
880 | 0 | char *base; |
881 | 0 | size_t diff; |
882 | 0 | size_t extent; |
883 | |
|
884 | 0 | if(to == from) |
885 | 0 | return NC_NOERR; /* NOOP */ |
886 | | |
887 | 0 | if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE)) |
888 | 0 | return EPERM; /* attempt to write readonly file */ |
889 | | |
890 | 0 | rflags &= RGN_NOLOCK; /* filter unwanted flags */ |
891 | |
|
892 | 0 | if(to > from) |
893 | 0 | { |
894 | | /* growing */ |
895 | 0 | lower = from; |
896 | 0 | upper = to; |
897 | 0 | } |
898 | 0 | else |
899 | 0 | { |
900 | | /* shrinking */ |
901 | 0 | lower = to; |
902 | 0 | upper = from; |
903 | 0 | } |
904 | 0 | diff = (size_t)(upper - lower); |
905 | 0 | extent = diff + nbytes; |
906 | |
|
907 | | #if INSTRUMENT |
908 | | fprintf(stderr, "ncio_px_move %ld %ld %ld %ld %ld\n", |
909 | | (long)to, (long)from, (long)nbytes, (long)lower, (long)extent); |
910 | | #endif |
911 | 0 | if(extent > pxp->blksz) |
912 | 0 | { |
913 | 0 | size_t remaining = nbytes; |
914 | |
|
915 | 0 | if(to > from) |
916 | 0 | { |
917 | 0 | off_t frm = from + nbytes; |
918 | 0 | off_t toh = to + nbytes; |
919 | 0 | for(;;) |
920 | 0 | { |
921 | 0 | size_t loopextent = MIN(remaining, pxp->blksz); |
922 | 0 | frm -= loopextent; |
923 | 0 | toh -= loopextent; |
924 | |
|
925 | 0 | status = px_double_buffer(nciop, toh, frm, |
926 | 0 | loopextent, rflags) ; |
927 | 0 | if(status != NC_NOERR) |
928 | 0 | return status; |
929 | 0 | remaining -= loopextent; |
930 | |
|
931 | 0 | if(remaining == 0) |
932 | 0 | break; /* normal loop exit */ |
933 | 0 | } |
934 | 0 | } |
935 | 0 | else |
936 | 0 | { |
937 | 0 | for(;;) |
938 | 0 | { |
939 | 0 | size_t loopextent = MIN(remaining, pxp->blksz); |
940 | |
|
941 | 0 | status = px_double_buffer(nciop, to, from, |
942 | 0 | loopextent, rflags) ; |
943 | 0 | if(status != NC_NOERR) |
944 | 0 | return status; |
945 | 0 | remaining -= loopextent; |
946 | |
|
947 | 0 | if(remaining == 0) |
948 | 0 | break; /* normal loop exit */ |
949 | 0 | to += loopextent; |
950 | 0 | from += loopextent; |
951 | 0 | } |
952 | 0 | } |
953 | 0 | return NC_NOERR; |
954 | 0 | } |
955 | | |
956 | | #if INSTRUMENT |
957 | | fprintf(stderr, "\tncio_px_move small\n"); |
958 | | #endif |
959 | 0 | status = px_get(nciop, pxp, lower, extent, RGN_WRITE|rflags, |
960 | 0 | (void **)&base); |
961 | |
|
962 | 0 | if(status != NC_NOERR) |
963 | 0 | return status; |
964 | | |
965 | 0 | if(to > from) |
966 | 0 | (void) memmove(base + diff, base, nbytes); |
967 | 0 | else |
968 | 0 | (void) memmove(base, base + diff, nbytes); |
969 | |
|
970 | 0 | (void) px_rel(pxp, lower, RGN_MODIFIED); |
971 | |
|
972 | 0 | return status; |
973 | 0 | } |
974 | | |
975 | | |
976 | | /* Flush any buffers to disk. May be a no-op on if I/O is unbuffered. |
977 | | This function is used when NC_SHARE is NOT used. |
978 | | */ |
979 | | static int |
980 | | ncio_px_sync(ncio *const nciop) |
981 | 0 | { |
982 | 0 | ncio_px *const pxp = (ncio_px *)nciop->pvt; |
983 | 0 | int status = NC_NOERR; |
984 | 0 | if(fIsSet(pxp->bf_rflags, RGN_MODIFIED)) |
985 | 0 | { |
986 | 0 | assert(pxp->bf_refcount <= 0); |
987 | 0 | status = px_pgout(nciop, pxp->bf_offset, |
988 | 0 | pxp->bf_cnt, |
989 | 0 | pxp->bf_base, &pxp->pos); |
990 | 0 | if(status != NC_NOERR) |
991 | 0 | return status; |
992 | 0 | pxp->bf_rflags = 0; |
993 | 0 | } |
994 | 0 | else if (!fIsSet(pxp->bf_rflags, RGN_WRITE)) |
995 | 0 | { |
996 | | /* |
997 | | * The dataset is readonly. Invalidate the buffers so |
998 | | * that the next ncio_px_get() will actually read data. |
999 | | */ |
1000 | 0 | pxp->bf_offset = OFF_NONE; |
1001 | 0 | pxp->bf_cnt = 0; |
1002 | 0 | } |
1003 | 0 | return status; |
1004 | 0 | } |
1005 | | |
1006 | | /* Internal function called at close to |
1007 | | free up anything hanging off pvt. |
1008 | | */ |
1009 | | static void |
1010 | | ncio_px_freepvt(void *const pvt) |
1011 | 0 | { |
1012 | 0 | ncio_px *const pxp = (ncio_px *)pvt; |
1013 | 0 | if(pxp == NULL) |
1014 | 0 | return; |
1015 | | |
1016 | 0 | if(pxp->slave != NULL) |
1017 | 0 | { |
1018 | 0 | if(pxp->slave->bf_base != NULL) |
1019 | 0 | { |
1020 | 0 | free(pxp->slave->bf_base); |
1021 | 0 | pxp->slave->bf_base = NULL; |
1022 | 0 | pxp->slave->bf_extent = 0; |
1023 | 0 | pxp->slave->bf_offset = OFF_NONE; |
1024 | 0 | } |
1025 | 0 | free(pxp->slave); |
1026 | 0 | pxp->slave = NULL; |
1027 | 0 | } |
1028 | |
|
1029 | 0 | if(pxp->bf_base != NULL) |
1030 | 0 | { |
1031 | 0 | free(pxp->bf_base); |
1032 | 0 | pxp->bf_base = NULL; |
1033 | 0 | pxp->bf_extent = 0; |
1034 | 0 | pxp->bf_offset = OFF_NONE; |
1035 | 0 | } |
1036 | 0 | } |
1037 | | |
1038 | | |
1039 | | /* This is the second half of the ncio initialization. This is called |
1040 | | after the file has actually been opened. |
1041 | | |
1042 | | The most important thing that happens is the allocation of a block |
1043 | | of memory at pxp->bf_base. This is going to be twice the size of |
1044 | | the chunksizehint (rounded up to the nearest sizeof(double)) passed |
1045 | | in from nc__create or nc__open. The rounded chunksizehint (passed |
1046 | | in here in sizehintp) is going to be stored as pxp->blksize. |
1047 | | |
1048 | | According to our "contract" we are not allowed to ask for an extent |
1049 | | larger than this chunksize/sizehint/blksize from the ncio get |
1050 | | function. |
1051 | | |
1052 | | nciop - pointer to the ncio struct |
1053 | | sizehintp - pointer to a size hint that will be rounded up and |
1054 | | passed back to the caller. |
1055 | | isNew - true if this is being called from ncio_create for a new |
1056 | | file. |
1057 | | */ |
1058 | | static int |
1059 | | ncio_px_init2(ncio *const nciop, size_t *sizehintp, int isNew) |
1060 | 0 | { |
1061 | 0 | ncio_px *const pxp = (ncio_px *)nciop->pvt; |
1062 | 0 | const size_t bufsz = 2 * *sizehintp; |
1063 | |
|
1064 | 0 | assert(nciop->fd >= 0); |
1065 | | |
1066 | 0 | pxp->blksz = *sizehintp; |
1067 | |
|
1068 | 0 | assert(pxp->bf_base == NULL); |
1069 | | |
1070 | | /* this is separate allocation because it may grow */ |
1071 | 0 | pxp->bf_base = malloc(bufsz); |
1072 | 0 | if(pxp->bf_base == NULL) |
1073 | 0 | return ENOMEM; |
1074 | | /* else */ |
1075 | 0 | pxp->bf_cnt = 0; |
1076 | 0 | if(isNew) |
1077 | 0 | { |
1078 | | /* save a read */ |
1079 | 0 | pxp->pos = 0; |
1080 | 0 | pxp->bf_offset = 0; |
1081 | 0 | pxp->bf_extent = bufsz; |
1082 | 0 | (void) memset(pxp->bf_base, 0, pxp->bf_extent); |
1083 | 0 | } |
1084 | 0 | return NC_NOERR; |
1085 | 0 | } |
1086 | | |
1087 | | |
1088 | | /* This is the first of a two-part initialization of the ncio struct. |
1089 | | Here the rel, get, move, sync, and free function pointers are set |
1090 | | to their POSIX non-NC_SHARE functions (ncio_px_*). |
1091 | | |
1092 | | The ncio_px struct is also partially initialized. |
1093 | | */ |
1094 | | static void |
1095 | | ncio_px_init(ncio *const nciop) |
1096 | 0 | { |
1097 | 0 | ncio_px *const pxp = (ncio_px *)nciop->pvt; |
1098 | |
|
1099 | 0 | *((ncio_relfunc **)&nciop->rel) = ncio_px_rel; /* cast away const */ |
1100 | 0 | *((ncio_getfunc **)&nciop->get) = ncio_px_get; /* cast away const */ |
1101 | 0 | *((ncio_movefunc **)&nciop->move) = ncio_px_move; /* cast away const */ |
1102 | 0 | *((ncio_syncfunc **)&nciop->sync) = ncio_px_sync; /* cast away const */ |
1103 | 0 | *((ncio_filesizefunc **)&nciop->filesize) = ncio_px_filesize; /* cast away const */ |
1104 | 0 | *((ncio_pad_lengthfunc **)&nciop->pad_length) = ncio_px_pad_length; /* cast away const */ |
1105 | 0 | *((ncio_closefunc **)&nciop->close) = ncio_px_close; /* cast away const */ |
1106 | |
|
1107 | 0 | pxp->blksz = 0; |
1108 | 0 | pxp->pos = -1; |
1109 | 0 | pxp->bf_offset = OFF_NONE; |
1110 | 0 | pxp->bf_extent = 0; |
1111 | 0 | pxp->bf_rflags = 0; |
1112 | 0 | pxp->bf_refcount = 0; |
1113 | 0 | pxp->bf_base = NULL; |
1114 | 0 | pxp->slave = NULL; |
1115 | |
|
1116 | 0 | } |
1117 | | |
1118 | | /* Begin spx */ |
1119 | | |
1120 | | /* This is the struct that gets hung of ncio->pvt(?) when the NC_SHARE |
1121 | | flag is used. |
1122 | | */ |
1123 | | typedef struct ncio_spx { |
1124 | | off_t pos; |
1125 | | /* buffer */ |
1126 | | off_t bf_offset; |
1127 | | size_t bf_extent; |
1128 | | size_t bf_cnt; |
1129 | | void *bf_base; |
1130 | | } ncio_spx; |
1131 | | |
1132 | | |
1133 | | /*ARGSUSED*/ |
1134 | | /* This function releases the region specified by offset. |
1135 | | |
1136 | | For POSIX system, with NC_SHARE, this becomes the rel function |
1137 | | pointed to by the ncio rel function pointer. It merely checks for |
1138 | | file write permission, then calls px_rel to do everything. |
1139 | | |
1140 | | nciop - pointer to ncio struct. |
1141 | | |
1142 | | offset - beginning of region. |
1143 | | |
1144 | | rflags - One of the RGN_* flags defined in ncio.h. If set to |
1145 | | RGN_MODIFIED it means that the data in this region were modified, |
1146 | | and it needs to be written out to the disk immediately (since we |
1147 | | are not buffering with NC_SHARE on). |
1148 | | |
1149 | | */ |
1150 | | static int |
1151 | | ncio_spx_rel(ncio *const nciop, off_t offset, int rflags) |
1152 | 0 | { |
1153 | 0 | ncio_spx *const pxp = (ncio_spx *)nciop->pvt; |
1154 | 0 | int status = NC_NOERR; |
1155 | |
|
1156 | 0 | assert(pxp->bf_offset <= offset); |
1157 | 0 | assert(pxp->bf_cnt != 0); |
1158 | 0 | assert(pxp->bf_cnt <= pxp->bf_extent); |
1159 | | #ifdef X_ALIGN |
1160 | | assert(offset < pxp->bf_offset + X_ALIGN); |
1161 | | assert(pxp->bf_cnt % X_ALIGN == 0 ); |
1162 | | #endif |
1163 | 0 | NC_UNUSED(offset); |
1164 | |
|
1165 | 0 | if(fIsSet(rflags, RGN_MODIFIED)) |
1166 | 0 | { |
1167 | 0 | if(!fIsSet(nciop->ioflags, NC_WRITE)) |
1168 | 0 | return EPERM; /* attempt to write readonly file */ |
1169 | | |
1170 | 0 | status = px_pgout(nciop, pxp->bf_offset, |
1171 | 0 | pxp->bf_cnt, |
1172 | 0 | pxp->bf_base, &pxp->pos); |
1173 | | /* if error, invalidate buffer anyway */ |
1174 | 0 | } |
1175 | 0 | pxp->bf_offset = OFF_NONE; |
1176 | 0 | pxp->bf_cnt = 0; |
1177 | 0 | return status; |
1178 | 0 | } |
1179 | | |
1180 | | |
1181 | | /* Request that the region (offset, extent) be made available through |
1182 | | *vpp. |
1183 | | |
1184 | | This function converts a file region specified by an offset and |
1185 | | extent to a memory pointer. The region may be locked until the |
1186 | | corresponding call to rel(). |
1187 | | |
1188 | | For POSIX systems, with NC_SHARE. |
1189 | | |
1190 | | nciop - pointer to ncio struct for this file. |
1191 | | offset - offset (from beginning of file?) to the data we want to |
1192 | | read. |
1193 | | extent - the number of bytes we want. |
1194 | | rflags - One of the RGN_* flags defined in ncio.h. May be RGN_NOLOCK. |
1195 | | vpp - handle to point at data when it's been read. |
1196 | | */ |
1197 | | static int |
1198 | | ncio_spx_get(ncio *const nciop, |
1199 | | off_t offset, size_t extent, |
1200 | | int rflags, |
1201 | | void **const vpp) |
1202 | 0 | { |
1203 | 0 | ncio_spx *const pxp = (ncio_spx *)nciop->pvt; |
1204 | 0 | int status = NC_NOERR; |
1205 | | #ifdef X_ALIGN |
1206 | | size_t rem; |
1207 | | #endif |
1208 | |
|
1209 | 0 | if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE)) |
1210 | 0 | return EPERM; /* attempt to write readonly file */ |
1211 | | |
1212 | 0 | assert(extent != 0); |
1213 | 0 | assert(extent < X_INT_MAX); /* sanity check */ |
1214 | | |
1215 | 0 | assert(pxp->bf_cnt == 0); |
1216 | | |
1217 | | #ifdef X_ALIGN |
1218 | | rem = (size_t)(offset % X_ALIGN); |
1219 | | if(rem != 0) |
1220 | | { |
1221 | | offset -= rem; |
1222 | | extent += rem; |
1223 | | } |
1224 | | |
1225 | | { |
1226 | | const size_t rndup = extent % X_ALIGN; |
1227 | | if(rndup != 0) |
1228 | | extent += X_ALIGN - rndup; |
1229 | | } |
1230 | | |
1231 | | assert(offset % X_ALIGN == 0); |
1232 | | assert(extent % X_ALIGN == 0); |
1233 | | #endif |
1234 | | |
1235 | 0 | if(pxp->bf_extent < extent) |
1236 | 0 | { |
1237 | 0 | if(pxp->bf_base != NULL) |
1238 | 0 | { |
1239 | 0 | free(pxp->bf_base); |
1240 | 0 | pxp->bf_base = NULL; |
1241 | 0 | pxp->bf_extent = 0; |
1242 | 0 | } |
1243 | 0 | assert(pxp->bf_extent == 0); |
1244 | 0 | pxp->bf_base = malloc(extent+1); |
1245 | 0 | if(pxp->bf_base == NULL) |
1246 | 0 | return ENOMEM; |
1247 | 0 | pxp->bf_extent = extent; |
1248 | 0 | } |
1249 | | |
1250 | 0 | status = px_pgin(nciop, offset, |
1251 | 0 | extent, |
1252 | 0 | pxp->bf_base, |
1253 | 0 | &pxp->bf_cnt, &pxp->pos); |
1254 | 0 | if(status != NC_NOERR) |
1255 | 0 | return status; |
1256 | | |
1257 | 0 | pxp->bf_offset = offset; |
1258 | |
|
1259 | 0 | if(pxp->bf_cnt < extent) |
1260 | 0 | pxp->bf_cnt = extent; |
1261 | |
|
1262 | | #ifdef X_ALIGN |
1263 | | *vpp = (char *)pxp->bf_base + rem; |
1264 | | #else |
1265 | 0 | *vpp = pxp->bf_base; |
1266 | 0 | #endif |
1267 | 0 | return NC_NOERR; |
1268 | 0 | } |
1269 | | |
1270 | | |
1271 | | #if 0 |
1272 | | /*ARGSUSED*/ |
1273 | | static int |
1274 | | strategy(ncio *const nciop, off_t to, off_t offset, |
1275 | | size_t extent, int rflags) |
1276 | | { |
1277 | | static ncio_spx pxp[1]; |
1278 | | int status = NC_NOERR; |
1279 | | #ifdef X_ALIGN |
1280 | | size_t rem; |
1281 | | #endif |
1282 | | |
1283 | | assert(extent != 0); |
1284 | | assert(extent < X_INT_MAX); /* sanity check */ |
1285 | | #if INSTRUMENT |
1286 | | fprintf(stderr, "strategy %ld at %ld to %ld\n", |
1287 | | (long)extent, (long)offset, (long)to); |
1288 | | #endif |
1289 | | |
1290 | | |
1291 | | #ifdef X_ALIGN |
1292 | | rem = (size_t)(offset % X_ALIGN); |
1293 | | if(rem != 0) |
1294 | | { |
1295 | | offset -= rem; |
1296 | | extent += rem; |
1297 | | } |
1298 | | |
1299 | | { |
1300 | | const size_t rndup = extent % X_ALIGN; |
1301 | | if(rndup != 0) |
1302 | | extent += X_ALIGN - rndup; |
1303 | | } |
1304 | | |
1305 | | assert(offset % X_ALIGN == 0); |
1306 | | assert(extent % X_ALIGN == 0); |
1307 | | #endif |
1308 | | |
1309 | | if(pxp->bf_extent < extent) |
1310 | | { |
1311 | | if(pxp->bf_base != NULL) |
1312 | | { |
1313 | | free(pxp->bf_base); |
1314 | | pxp->bf_base = NULL; |
1315 | | pxp->bf_extent = 0; |
1316 | | } |
1317 | | assert(pxp->bf_extent == 0); |
1318 | | pxp->bf_base = malloc(extent); |
1319 | | if(pxp->bf_base == NULL) |
1320 | | return ENOMEM; |
1321 | | pxp->bf_extent = extent; |
1322 | | } |
1323 | | |
1324 | | status = px_pgin(nciop, offset, |
1325 | | extent, |
1326 | | pxp->bf_base, |
1327 | | &pxp->bf_cnt, &pxp->pos); |
1328 | | if(status != NC_NOERR) |
1329 | | return status; |
1330 | | |
1331 | | pxp->bf_offset = to; /* TODO: XALIGN */ |
1332 | | |
1333 | | if(pxp->bf_cnt < extent) |
1334 | | pxp->bf_cnt = extent; |
1335 | | |
1336 | | status = px_pgout(nciop, pxp->bf_offset, |
1337 | | pxp->bf_cnt, |
1338 | | pxp->bf_base, &pxp->pos); |
1339 | | /* if error, invalidate buffer anyway */ |
1340 | | pxp->bf_offset = OFF_NONE; |
1341 | | pxp->bf_cnt = 0; |
1342 | | return status; |
1343 | | } |
1344 | | #endif |
1345 | | |
1346 | | /* Copy one region to another without making anything available to |
1347 | | higher layers. May be just implemented in terms of get() and rel(), |
1348 | | or may be tricky to be efficient. Only used in by nc_enddef() |
1349 | | after redefinition. |
1350 | | |
1351 | | nciop - pointer to ncio struct for this file. |
1352 | | to - dest for move? |
1353 | | from - src for move? |
1354 | | nbytes - number of bytes to move. |
1355 | | rflags - One of the RGN_* flags defined in ncio.h. |
1356 | | */ |
1357 | | static int |
1358 | | ncio_spx_move(ncio *const nciop, off_t to, off_t from, |
1359 | | size_t nbytes, int rflags) |
1360 | 0 | { |
1361 | 0 | int status = NC_NOERR; |
1362 | 0 | off_t lower = from; |
1363 | 0 | off_t upper = to; |
1364 | 0 | char *base; |
1365 | 0 | size_t diff; |
1366 | 0 | size_t extent; |
1367 | |
|
1368 | 0 | rflags &= RGN_NOLOCK; /* filter unwanted flags */ |
1369 | |
|
1370 | 0 | if(to == from) |
1371 | 0 | return NC_NOERR; /* NOOP */ |
1372 | | |
1373 | 0 | if(to > from) |
1374 | 0 | { |
1375 | | /* growing */ |
1376 | 0 | lower = from; |
1377 | 0 | upper = to; |
1378 | 0 | } |
1379 | 0 | else |
1380 | 0 | { |
1381 | | /* shrinking */ |
1382 | 0 | lower = to; |
1383 | 0 | upper = from; |
1384 | 0 | } |
1385 | |
|
1386 | 0 | diff = (size_t)(upper - lower); |
1387 | 0 | extent = diff + nbytes; |
1388 | |
|
1389 | 0 | status = ncio_spx_get(nciop, lower, extent, RGN_WRITE|rflags, |
1390 | 0 | (void **)&base); |
1391 | |
|
1392 | 0 | if(status != NC_NOERR) |
1393 | 0 | return status; |
1394 | | |
1395 | 0 | if(to > from) |
1396 | 0 | (void) memmove(base + diff, base, nbytes); |
1397 | 0 | else |
1398 | 0 | (void) memmove(base, base + diff, nbytes); |
1399 | |
|
1400 | 0 | (void) ncio_spx_rel(nciop, lower, RGN_MODIFIED); |
1401 | |
|
1402 | 0 | return status; |
1403 | 0 | } |
1404 | | |
1405 | | |
1406 | | /*ARGSUSED*/ |
1407 | | /* Flush any buffers to disk. May be a no-op on if I/O is unbuffered. |
1408 | | */ |
1409 | | static int |
1410 | | ncio_spx_sync(ncio *const nciop) |
1411 | 0 | { |
1412 | 0 | NC_UNUSED(nciop); |
1413 | | /* NOOP */ |
1414 | 0 | return NC_NOERR; |
1415 | 0 | } |
1416 | | |
1417 | | static void |
1418 | | ncio_spx_freepvt(void *const pvt) |
1419 | 0 | { |
1420 | 0 | ncio_spx *const pxp = (ncio_spx *)pvt; |
1421 | 0 | if(pxp == NULL) |
1422 | 0 | return; |
1423 | | |
1424 | 0 | if(pxp->bf_base != NULL) |
1425 | 0 | { |
1426 | 0 | free(pxp->bf_base); |
1427 | 0 | pxp->bf_base = NULL; |
1428 | 0 | pxp->bf_offset = OFF_NONE; |
1429 | 0 | pxp->bf_extent = 0; |
1430 | 0 | pxp->bf_cnt = 0; |
1431 | 0 | } |
1432 | 0 | } |
1433 | | |
1434 | | |
1435 | | /* This does the second half of the ncio_spx struct initialization for |
1436 | | POSIX systems, with NC_SHARE on. |
1437 | | |
1438 | | nciop - pointer to ncio struct for this file. File has been opened. |
1439 | | sizehintp - pointer to a size which will be rounded up to the |
1440 | | nearest 8-byt boundary and then used as the max size "chunk" (or |
1441 | | page) to read from the file. |
1442 | | */ |
1443 | | static int |
1444 | | ncio_spx_init2(ncio *const nciop, const size_t *const sizehintp) |
1445 | 0 | { |
1446 | 0 | ncio_spx *const pxp = (ncio_spx *)nciop->pvt; |
1447 | |
|
1448 | 0 | assert(nciop->fd >= 0); |
1449 | | |
1450 | 0 | pxp->bf_extent = *sizehintp; |
1451 | |
|
1452 | 0 | assert(pxp->bf_base == NULL); |
1453 | | |
1454 | | /* this is separate allocation because it may grow */ |
1455 | 0 | pxp->bf_base = malloc(pxp->bf_extent); |
1456 | 0 | if(pxp->bf_base == NULL) |
1457 | 0 | { |
1458 | 0 | pxp->bf_extent = 0; |
1459 | 0 | return ENOMEM; |
1460 | 0 | } |
1461 | | /* else */ |
1462 | 0 | return NC_NOERR; |
1463 | 0 | } |
1464 | | |
1465 | | |
1466 | | /* First half of init for ncio_spx struct, setting the rel, get, move, |
1467 | | sync, and free function pointers to the NC_SHARE versions of these |
1468 | | functions (i.e. the ncio_spx_* functions). |
1469 | | */ |
1470 | | static void |
1471 | | ncio_spx_init(ncio *const nciop) |
1472 | 0 | { |
1473 | 0 | ncio_spx *const pxp = (ncio_spx *)nciop->pvt; |
1474 | |
|
1475 | 0 | *((ncio_relfunc **)&nciop->rel) = ncio_spx_rel; /* cast away const */ |
1476 | 0 | *((ncio_getfunc **)&nciop->get) = ncio_spx_get; /* cast away const */ |
1477 | 0 | *((ncio_movefunc **)&nciop->move) = ncio_spx_move; /* cast away const */ |
1478 | 0 | *((ncio_syncfunc **)&nciop->sync) = ncio_spx_sync; /* cast away const */ |
1479 | | /* shared with _px_ */ |
1480 | 0 | *((ncio_filesizefunc **)&nciop->filesize) = ncio_px_filesize; /* cast away const */ |
1481 | 0 | *((ncio_pad_lengthfunc **)&nciop->pad_length) = ncio_px_pad_length; /* cast away const */ |
1482 | 0 | *((ncio_closefunc **)&nciop->close) = ncio_spx_close; /* cast away const */ |
1483 | |
|
1484 | 0 | pxp->pos = -1; |
1485 | 0 | pxp->bf_offset = OFF_NONE; |
1486 | 0 | pxp->bf_extent = 0; |
1487 | 0 | pxp->bf_cnt = 0; |
1488 | 0 | pxp->bf_base = NULL; |
1489 | 0 | } |
1490 | | |
1491 | | |
1492 | | /* */ |
1493 | | |
1494 | | /* This will call whatever free function is attached to the free |
1495 | | function pointer in ncio. It's called from ncio_close, and from |
1496 | | ncio_open and ncio_create when an error occurs that the file |
1497 | | metadata must be freed. |
1498 | | */ |
1499 | | static void |
1500 | | ncio_px_free(ncio *nciop) |
1501 | 0 | { |
1502 | 0 | if(nciop == NULL) |
1503 | 0 | return; |
1504 | 0 | if(nciop->pvt != NULL) |
1505 | 0 | ncio_px_freepvt(nciop->pvt); |
1506 | 0 | free(nciop); |
1507 | 0 | } |
1508 | | |
1509 | | static void |
1510 | | ncio_spx_free(ncio *nciop) |
1511 | 0 | { |
1512 | 0 | if(nciop == NULL) |
1513 | 0 | return; |
1514 | 0 | if(nciop->pvt != NULL) |
1515 | 0 | ncio_spx_freepvt(nciop->pvt); |
1516 | 0 | free(nciop); |
1517 | 0 | } |
1518 | | |
1519 | | |
1520 | | /* Create a new ncio struct to hold info about the file. This will |
1521 | | create and init the ncio_px or ncio_spx struct (the latter if |
1522 | | NC_SHARE is used.) |
1523 | | */ |
1524 | | static ncio * |
1525 | | ncio_px_new(const char *path, int ioflags) |
1526 | 0 | { |
1527 | 0 | size_t sz_ncio = M_RNDUP(sizeof(ncio)); |
1528 | 0 | size_t sz_path = M_RNDUP(strlen(path) +1); |
1529 | 0 | size_t sz_ncio_pvt; |
1530 | 0 | ncio *nciop; |
1531 | |
|
1532 | | #if ALWAYS_NC_SHARE /* DEBUG */ |
1533 | | fSet(ioflags, NC_SHARE); |
1534 | | #endif |
1535 | |
|
1536 | 0 | if(fIsSet(ioflags, NC_SHARE)) |
1537 | 0 | sz_ncio_pvt = sizeof(ncio_spx); |
1538 | 0 | else |
1539 | 0 | sz_ncio_pvt = sizeof(ncio_px); |
1540 | |
|
1541 | 0 | nciop = (ncio *) malloc(sz_ncio + sz_path + sz_ncio_pvt); |
1542 | 0 | if(nciop == NULL) |
1543 | 0 | return NULL; |
1544 | | |
1545 | 0 | nciop->ioflags = ioflags; |
1546 | 0 | *((int *)&nciop->fd) = -1; /* cast away const */ |
1547 | |
|
1548 | 0 | nciop->path = (char *) ((char *)nciop + sz_ncio); |
1549 | 0 | (void) strcpy((char *)nciop->path, path); /* cast away const */ |
1550 | | |
1551 | | /* cast away const */ |
1552 | 0 | *((void **)&nciop->pvt) = (void *)(nciop->path + sz_path); |
1553 | |
|
1554 | 0 | if(fIsSet(ioflags, NC_SHARE)) |
1555 | 0 | ncio_spx_init(nciop); |
1556 | 0 | else |
1557 | 0 | ncio_px_init(nciop); |
1558 | |
|
1559 | 0 | return nciop; |
1560 | 0 | } |
1561 | | |
1562 | | |
1563 | | /* Public below this point */ |
1564 | | #ifndef NCIO_MINBLOCKSIZE |
1565 | | #define NCIO_MINBLOCKSIZE 256 |
1566 | | #endif |
1567 | | #ifndef NCIO_MAXBLOCKSIZE |
1568 | 0 | #define NCIO_MAXBLOCKSIZE 268435456 /* sanity check, about X_SIZE_T_MAX/8 */ |
1569 | | #endif |
1570 | | |
1571 | | #ifdef S_IRUSR |
1572 | | #define NC_DEFAULT_CREAT_MODE \ |
1573 | | (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) /* 0666 */ |
1574 | | |
1575 | | #else |
1576 | | #define NC_DEFAULT_CREAT_MODE 0666 |
1577 | | #endif |
1578 | | |
1579 | | /* Create a file, and the ncio struct to go with it. This function is |
1580 | | only called from nc__create_mp. |
1581 | | |
1582 | | path - path of file to create. |
1583 | | ioflags - flags from nc_create |
1584 | | initialsz - From the netcdf man page: "The argument |
1585 | | Iinitialsize sets the initial size of the file at creation time." |
1586 | | igeto - |
1587 | | igetsz - |
1588 | | sizehintp - this eventually goes into pxp->blksz and is the size of |
1589 | | a page of data for buffered reads and writes. |
1590 | | nciopp - pointer to a pointer that will get location of newly |
1591 | | created and inited ncio struct. |
1592 | | igetvpp - pointer to pointer which will get the location of ? |
1593 | | */ |
1594 | | int |
1595 | | posixio_create(const char *path, int ioflags, |
1596 | | size_t initialsz, |
1597 | | off_t igeto, size_t igetsz, size_t *sizehintp, |
1598 | | void* parameters, |
1599 | | ncio **nciopp, void **const igetvpp) |
1600 | 0 | { |
1601 | 0 | ncio *nciop; |
1602 | 0 | int oflags = (O_RDWR|O_CREAT); |
1603 | 0 | int fd; |
1604 | 0 | int status; |
1605 | 0 | NC_UNUSED(parameters); |
1606 | |
|
1607 | 0 | if(initialsz < (size_t)igeto + igetsz) |
1608 | 0 | initialsz = (size_t)igeto + igetsz; |
1609 | |
|
1610 | 0 | fSet(ioflags, NC_WRITE); |
1611 | |
|
1612 | 0 | if(path == NULL || *path == 0) |
1613 | 0 | return EINVAL; |
1614 | | |
1615 | 0 | nciop = ncio_px_new(path, ioflags); |
1616 | 0 | if(nciop == NULL) |
1617 | 0 | return ENOMEM; |
1618 | | |
1619 | 0 | if(fIsSet(ioflags, NC_NOCLOBBER)) |
1620 | 0 | fSet(oflags, O_EXCL); |
1621 | 0 | else |
1622 | 0 | fSet(oflags, O_TRUNC); |
1623 | | #ifdef O_BINARY |
1624 | | fSet(oflags, O_BINARY); |
1625 | | #endif |
1626 | | #ifdef vms |
1627 | | fd = NCopen3(path, oflags, NC_DEFAULT_CREAT_MODE, "ctx=stm"); |
1628 | | #else |
1629 | | /* Should we mess with the mode based on NC_SHARE ?? */ |
1630 | 0 | fd = NCopen3(path, oflags, NC_DEFAULT_CREAT_MODE); |
1631 | 0 | #endif |
1632 | | #if 0 |
1633 | | (void) fprintf(stderr, "ncio_create(): path=\"%s\"\n", path); |
1634 | | (void) fprintf(stderr, "ncio_create(): oflags=0x%x\n", oflags); |
1635 | | #endif |
1636 | 0 | if(fd < 0) |
1637 | 0 | { |
1638 | 0 | status = errno ? errno : ENOENT; |
1639 | 0 | goto unwind_new; |
1640 | 0 | } |
1641 | 0 | *((int *)&nciop->fd) = fd; /* cast away const */ |
1642 | |
|
1643 | 0 | if(*sizehintp < NCIO_MINBLOCKSIZE) |
1644 | 0 | { |
1645 | | /* Use default */ |
1646 | 0 | *sizehintp = blksize(fd); |
1647 | 0 | } |
1648 | 0 | else if(*sizehintp >= NCIO_MAXBLOCKSIZE) |
1649 | 0 | { |
1650 | | /* Use maximum allowed value */ |
1651 | 0 | *sizehintp = NCIO_MAXBLOCKSIZE; |
1652 | 0 | } |
1653 | 0 | else |
1654 | 0 | { |
1655 | 0 | *sizehintp = M_RNDUP(*sizehintp); |
1656 | 0 | } |
1657 | |
|
1658 | 0 | if(fIsSet(nciop->ioflags, NC_SHARE)) |
1659 | 0 | status = ncio_spx_init2(nciop, sizehintp); |
1660 | 0 | else |
1661 | 0 | status = ncio_px_init2(nciop, sizehintp, 1); |
1662 | |
|
1663 | 0 | if(status != NC_NOERR) |
1664 | 0 | goto unwind_open; |
1665 | | |
1666 | 0 | if(initialsz != 0) |
1667 | 0 | { |
1668 | 0 | status = fgrow(fd, (off_t)initialsz); |
1669 | 0 | if(status != NC_NOERR) |
1670 | 0 | goto unwind_open; |
1671 | 0 | } |
1672 | | |
1673 | 0 | if(igetsz != 0) |
1674 | 0 | { |
1675 | 0 | status = nciop->get(nciop, |
1676 | 0 | igeto, igetsz, |
1677 | 0 | RGN_WRITE, |
1678 | 0 | igetvpp); |
1679 | 0 | if(status != NC_NOERR) |
1680 | 0 | goto unwind_open; |
1681 | 0 | } |
1682 | | |
1683 | 0 | *nciopp = nciop; |
1684 | 0 | return NC_NOERR; |
1685 | | |
1686 | 0 | unwind_open: |
1687 | 0 | (void) close(fd); |
1688 | | /* ?? unlink */ |
1689 | | /*FALLTHRU*/ |
1690 | 0 | unwind_new: |
1691 | 0 | ncio_close(nciop,!fIsSet(ioflags, NC_NOCLOBBER)); |
1692 | 0 | return status; |
1693 | 0 | } |
1694 | | |
1695 | | |
1696 | | /* This function opens the data file. It is only called from nc.c, |
1697 | | from nc__open_mp and nc_delete_mp. |
1698 | | |
1699 | | path - path of data file. |
1700 | | |
1701 | | ioflags - flags passed into nc_open. |
1702 | | |
1703 | | igeto - looks like this function can do an initial page get, and |
1704 | | igeto is going to be the offset for that. But it appears to be |
1705 | | unused |
1706 | | |
1707 | | igetsz - the size in bytes of initial page get (a.k.a. extent). Not |
1708 | | ever used in the library. |
1709 | | |
1710 | | sizehintp - pointer to sizehint parameter from nc__open or |
1711 | | nc__create. This is used to set pxp->blksz. |
1712 | | |
1713 | | Here's what the man page has to say: |
1714 | | |
1715 | | "The argument referenced by chunksize controls a space versus time |
1716 | | tradeoff, memory allocated in the netcdf library versus number of |
1717 | | system calls. |
1718 | | |
1719 | | Because of internal requirements, the value may not be set to |
1720 | | exactly the value requested. The actual value chosen is returned by reference. |
1721 | | |
1722 | | Using the value NC_SIZEHINT_DEFAULT causes the library to choose a |
1723 | | default. How the system choses the default depends on the |
1724 | | system. On many systems, the "preferred I/O block size" is |
1725 | | available from the stat() system call, struct stat member |
1726 | | st_blksize. If this is available it is used. Lacking that, twice |
1727 | | the system pagesize is used. Lacking a call to discover the system |
1728 | | pagesize, we just set default chunksize to 8192. |
1729 | | |
1730 | | The chunksize is a property of a given open netcdf descriptor ncid, |
1731 | | it is not a persistent property of the netcdf dataset." |
1732 | | |
1733 | | nciopp - pointer to pointer that will get address of newly created |
1734 | | and inited ncio struct. |
1735 | | |
1736 | | igetvpp - handle to pass back pointer to data from initial page |
1737 | | read, if this were ever used, which it isn't. |
1738 | | */ |
1739 | | int |
1740 | | posixio_open(const char *path, |
1741 | | int ioflags, |
1742 | | off_t igeto, size_t igetsz, size_t *sizehintp, |
1743 | | void* parameters, |
1744 | | ncio **nciopp, void **const igetvpp) |
1745 | 0 | { |
1746 | 0 | ncio *nciop; |
1747 | 0 | int oflags = fIsSet(ioflags, NC_WRITE) ? O_RDWR : O_RDONLY; |
1748 | 0 | int fd = -1; |
1749 | 0 | int status = 0; |
1750 | 0 | NC_UNUSED(parameters); |
1751 | |
|
1752 | 0 | if(path == NULL || *path == 0) |
1753 | 0 | return EINVAL; |
1754 | | |
1755 | 0 | nciop = ncio_px_new(path, ioflags); |
1756 | 0 | if(nciop == NULL) |
1757 | 0 | return ENOMEM; |
1758 | | |
1759 | | #ifdef O_BINARY |
1760 | | /*#if _MSC_VER*/ |
1761 | | fSet(oflags, O_BINARY); |
1762 | | #endif |
1763 | | |
1764 | | #ifdef vms |
1765 | | fd = NCopen3(path, oflags, 0, "ctx=stm"); |
1766 | | #else |
1767 | 0 | fd = NCopen3(path, oflags, 0); |
1768 | 0 | #endif |
1769 | 0 | if(fd < 0) |
1770 | 0 | { |
1771 | 0 | status = errno ? errno : ENOENT; |
1772 | 0 | goto unwind_new; |
1773 | 0 | } |
1774 | 0 | *((int *)&nciop->fd) = fd; /* cast away const */ |
1775 | |
|
1776 | 0 | if(*sizehintp < NCIO_MINBLOCKSIZE) |
1777 | 0 | { |
1778 | | /* Use default */ |
1779 | 0 | *sizehintp = blksize(fd); |
1780 | 0 | } |
1781 | 0 | else if(*sizehintp >= NCIO_MAXBLOCKSIZE) |
1782 | 0 | { |
1783 | | /* Use maximum allowed value */ |
1784 | 0 | *sizehintp = NCIO_MAXBLOCKSIZE; |
1785 | 0 | } |
1786 | 0 | else |
1787 | 0 | { |
1788 | 0 | *sizehintp = M_RNDUP(*sizehintp); |
1789 | 0 | } |
1790 | |
|
1791 | 0 | if(fIsSet(nciop->ioflags, NC_SHARE)) |
1792 | 0 | status = ncio_spx_init2(nciop, sizehintp); |
1793 | 0 | else |
1794 | 0 | status = ncio_px_init2(nciop, sizehintp, 0); |
1795 | |
|
1796 | 0 | if(status != NC_NOERR) |
1797 | 0 | goto unwind_open; |
1798 | | |
1799 | 0 | if(igetsz != 0) |
1800 | 0 | { |
1801 | 0 | status = nciop->get(nciop, |
1802 | 0 | igeto, igetsz, |
1803 | 0 | 0, |
1804 | 0 | igetvpp); |
1805 | 0 | if(status != NC_NOERR) |
1806 | 0 | goto unwind_open; |
1807 | 0 | } |
1808 | | |
1809 | 0 | *nciopp = nciop; |
1810 | 0 | return NC_NOERR; |
1811 | | |
1812 | 0 | unwind_open: |
1813 | 0 | (void) close(fd); /* assert fd >= 0 */ |
1814 | | /*FALLTHRU*/ |
1815 | 0 | unwind_new: |
1816 | 0 | ncio_close(nciop,0); |
1817 | 0 | return status; |
1818 | 0 | } |
1819 | | |
1820 | | /* |
1821 | | * Get file size in bytes. |
1822 | | */ |
1823 | | static int |
1824 | | ncio_px_filesize(ncio *nciop, off_t *filesizep) |
1825 | 0 | { |
1826 | | |
1827 | | |
1828 | | /* There is a problem with fstat on Windows based systems |
1829 | | which manifests (so far) when Config RELEASE is built. |
1830 | | Use _filelengthi64 isntead. */ |
1831 | | #ifdef HAVE_FILE_LENGTH_I64 |
1832 | | |
1833 | | int64_t file_len = 0; |
1834 | | if( (file_len = _filelengthi64(nciop->fd)) < 0) { |
1835 | | return errno; |
1836 | | } |
1837 | | |
1838 | | *filesizep = file_len; |
1839 | | |
1840 | | #else |
1841 | 0 | struct stat sb; |
1842 | 0 | assert(nciop != NULL); |
1843 | 0 | if (fstat(nciop->fd, &sb) < 0) |
1844 | 0 | return errno; |
1845 | 0 | *filesizep = sb.st_size; |
1846 | 0 | #endif |
1847 | 0 | return NC_NOERR; |
1848 | 0 | } |
1849 | | |
1850 | | /* |
1851 | | * Sync any changes to disk, then truncate or extend file so its size |
1852 | | * is length. This is only intended to be called before close, if the |
1853 | | * file is open for writing and the actual size does not match the |
1854 | | * calculated size, perhaps as the result of having been previously |
1855 | | * written in NOFILL mode. |
1856 | | */ |
1857 | | static int |
1858 | | ncio_px_pad_length(ncio *nciop, off_t length) |
1859 | 0 | { |
1860 | |
|
1861 | 0 | int status = NC_NOERR; |
1862 | |
|
1863 | 0 | if(nciop == NULL) |
1864 | 0 | return EINVAL; |
1865 | | |
1866 | 0 | if(!fIsSet(nciop->ioflags, NC_WRITE)) |
1867 | 0 | return EPERM; /* attempt to write readonly file */ |
1868 | | |
1869 | 0 | status = nciop->sync(nciop); |
1870 | 0 | if(status != NC_NOERR) |
1871 | 0 | return status; |
1872 | | |
1873 | 0 | status = fgrow2(nciop->fd, length); |
1874 | 0 | if(status != NC_NOERR) |
1875 | 0 | return status; |
1876 | 0 | return NC_NOERR; |
1877 | 0 | } |
1878 | | |
1879 | | |
1880 | | /* Write out any dirty buffers to disk and |
1881 | | ensure that next read will get data from disk. |
1882 | | |
1883 | | Sync any changes, then close the open file associated with the ncio |
1884 | | struct, and free its memory. |
1885 | | |
1886 | | nciop - pointer to ncio to close. |
1887 | | |
1888 | | doUnlink - if true, unlink file |
1889 | | */ |
1890 | | static int |
1891 | | ncio_px_close(ncio *nciop, int doUnlink) |
1892 | 0 | { |
1893 | 0 | int status = NC_NOERR; |
1894 | 0 | if(nciop == NULL) |
1895 | 0 | return EINVAL; |
1896 | 0 | if(nciop->fd > 0) { |
1897 | 0 | status = nciop->sync(nciop); |
1898 | 0 | (void) close(nciop->fd); |
1899 | 0 | } |
1900 | 0 | if(doUnlink) |
1901 | 0 | (void) unlink(nciop->path); |
1902 | 0 | ncio_px_free(nciop); |
1903 | 0 | return status; |
1904 | 0 | } |
1905 | | |
1906 | | static int |
1907 | | ncio_spx_close(ncio *nciop, int doUnlink) |
1908 | 0 | { |
1909 | 0 | int status = NC_NOERR; |
1910 | 0 | if(nciop == NULL) |
1911 | 0 | return EINVAL; |
1912 | 0 | if(nciop->fd > 0) { |
1913 | 0 | status = nciop->sync(nciop); |
1914 | 0 | (void) close(nciop->fd); |
1915 | 0 | } |
1916 | 0 | if(doUnlink) |
1917 | 0 | (void) unlink(nciop->path); |
1918 | 0 | ncio_spx_free(nciop); |
1919 | 0 | return status; |
1920 | 0 | } |