PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/other/netcdf_write_matrix/src/libsrc/posixio.c

http://github.com/jbeezley/wrf-fire
C | 1698 lines | 1085 code | 207 blank | 406 comment | 257 complexity | fcb144d4ceadf97292621d52fbf9be5f MD5 | raw file
Possible License(s): AGPL-1.0
  1. /*
  2. * Copyright 1996, University Corporation for Atmospheric Research
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. */
  5. /* $Id: posixio.c,v 1.79 2006/01/03 04:58:07 russ Exp $ */
  6. #include <config.h>
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #ifndef ENOERR
  11. #define ENOERR 0
  12. #endif
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <string.h>
  17. #ifdef _MSC_VER /* Microsoft Compilers */
  18. #include <io.h>
  19. #else
  20. #include <unistd.h>
  21. #endif
  22. #ifndef SEEK_SET
  23. #define SEEK_SET 0
  24. #define SEEK_CUR 1
  25. #define SEEK_END 2
  26. #endif
  27. #include "ncio.h"
  28. #include "fbits.h"
  29. #include "rnd.h"
  30. /* #define INSTRUMENT 1 */
  31. #if INSTRUMENT /* debugging */
  32. #undef NDEBUG
  33. #include <stdio.h>
  34. #include "instr.h"
  35. #endif
  36. #undef MIN /* system may define MIN somewhere and complain */
  37. #define MIN(mm,nn) (((mm) < (nn)) ? (mm) : (nn))
  38. #if !defined(NDEBUG) && !defined(X_INT_MAX)
  39. #define X_INT_MAX 2147483647
  40. #endif
  41. #if 0 /* !defined(NDEBUG) && !defined(X_ALIGN) */
  42. #define X_ALIGN 4
  43. #else
  44. #undef X_ALIGN
  45. #endif
  46. /*
  47. * Define the following for debugging.
  48. */
  49. /* #define ALWAYS_NC_SHARE 1 */
  50. /* Begin OS */
  51. #ifndef POSIXIO_DEFAULT_PAGESIZE
  52. #define POSIXIO_DEFAULT_PAGESIZE 4096
  53. #endif
  54. /*
  55. * What is the system pagesize?
  56. */
  57. static size_t
  58. pagesize(void)
  59. {
  60. /* Hmm, aren't standards great? */
  61. #if defined(_SC_PAGE_SIZE) && !defined(_SC_PAGESIZE)
  62. #define _SC_PAGESIZE _SC_PAGE_SIZE
  63. #endif
  64. #ifdef _SC_PAGESIZE
  65. {
  66. const long pgsz = sysconf(_SC_PAGESIZE);
  67. if(pgsz > 0)
  68. return (size_t) pgsz;
  69. /* else, silent in the face of error */
  70. }
  71. #elif defined(HAVE_GETPAGESIZE)
  72. return (size_t) getpagesize();
  73. #endif
  74. return (size_t) POSIXIO_DEFAULT_PAGESIZE;
  75. }
  76. /*
  77. * What is the preferred I/O block size?
  78. */
  79. static size_t
  80. blksize(int fd)
  81. {
  82. #if defined(HAVE_ST_BLKSIZE)
  83. struct stat sb;
  84. if (fstat(fd, &sb) > -1)
  85. {
  86. if(sb.st_blksize >= 8192)
  87. return (size_t) sb.st_blksize;
  88. return 8192;
  89. }
  90. /* else, silent in the face of error */
  91. #endif
  92. return (size_t) 2 * pagesize();
  93. }
  94. /*
  95. * Sortof like ftruncate, except won't make the
  96. * file shorter.
  97. */
  98. static int
  99. fgrow(const int fd, const off_t len)
  100. {
  101. struct stat sb;
  102. if (fstat(fd, &sb) < 0)
  103. return errno;
  104. if (len < sb.st_size)
  105. return ENOERR;
  106. {
  107. const long dumb = 0;
  108. /* we don't use ftruncate() due to problem with FAT32 file systems */
  109. /* cache current position */
  110. const off_t pos = lseek(fd, 0, SEEK_CUR);
  111. if(pos < 0)
  112. return errno;
  113. if (lseek(fd, len-sizeof(dumb), SEEK_SET) < 0)
  114. return errno;
  115. if(write(fd, &dumb, sizeof(dumb)) < 0)
  116. return errno;
  117. if (lseek(fd, pos, SEEK_SET) < 0)
  118. return errno;
  119. }
  120. return ENOERR;
  121. }
  122. /*
  123. * Sortof like ftruncate, except won't make the file shorter. Differs
  124. * from fgrow by only writing one byte at designated seek position, if
  125. * needed.
  126. */
  127. static int
  128. fgrow2(const int fd, const off_t len)
  129. {
  130. struct stat sb;
  131. if (fstat(fd, &sb) < 0)
  132. return errno;
  133. if (len <= sb.st_size)
  134. return ENOERR;
  135. {
  136. const char dumb = 0;
  137. /* we don't use ftruncate() due to problem with FAT32 file systems */
  138. /* cache current position */
  139. const off_t pos = lseek(fd, 0, SEEK_CUR);
  140. if(pos < 0)
  141. return errno;
  142. if (lseek(fd, len-1, SEEK_SET) < 0)
  143. return errno;
  144. if(write(fd, &dumb, sizeof(dumb)) < 0)
  145. return errno;
  146. if (lseek(fd, pos, SEEK_SET) < 0)
  147. return errno;
  148. }
  149. return ENOERR;
  150. }
  151. /* End OS */
  152. /* Begin px */
  153. /* The px_ functions are for posix systems, when NC_SHARE is not in
  154. effect. */
  155. /* Write out a "page" of data to the file. The size of the page
  156. (i.e. the extent) varies.
  157. nciop - pointer to the file metadata.
  158. offset - where in the file should this page be written.
  159. extent - how many bytes should be written.
  160. vp - pointer to the data to write.
  161. posp - pointer to current position in file, updated after write.
  162. */
  163. static int
  164. px_pgout(ncio *const nciop,
  165. off_t const offset, const size_t extent,
  166. void *const vp, off_t *posp)
  167. {
  168. #ifdef X_ALIGN
  169. assert(offset % X_ALIGN == 0);
  170. #endif
  171. assert(*posp == OFF_NONE || *posp == lseek(nciop->fd, 0, SEEK_CUR));
  172. if(*posp != offset)
  173. {
  174. if(lseek(nciop->fd, offset, SEEK_SET) != offset)
  175. {
  176. return errno;
  177. }
  178. *posp = offset;
  179. }
  180. if(write(nciop->fd, vp, extent) != (ssize_t) extent)
  181. {
  182. return errno;
  183. }
  184. *posp += extent;
  185. return ENOERR;
  186. }
  187. /* Read in a page of data.
  188. nciop - a pointer to the ncio struct for this file.
  189. offset - byte offset in file where read starts.
  190. extent - the size of the page that will be read.
  191. vp - a pointer to where the data will end up.
  192. nreadp - returned number of bytes actually read (may be less than extent).
  193. posp - pointer to current position in file, updated after read.
  194. */
  195. static int
  196. px_pgin(ncio *const nciop,
  197. off_t const offset, const size_t extent,
  198. void *const vp, size_t *nreadp, off_t *posp)
  199. {
  200. int status;
  201. ssize_t nread;
  202. #ifdef X_ALIGN
  203. assert(offset % X_ALIGN == 0);
  204. assert(extent % X_ALIGN == 0);
  205. #endif
  206. assert(*posp == OFF_NONE || *posp == lseek(nciop->fd, 0, SEEK_CUR));
  207. if(*posp != offset)
  208. {
  209. if(lseek(nciop->fd, offset, SEEK_SET) != offset)
  210. {
  211. status = errno;
  212. return status;
  213. }
  214. *posp = offset;
  215. }
  216. errno = 0;
  217. nread = read(nciop->fd, vp, extent);
  218. if(nread != (ssize_t) extent)
  219. {
  220. status = errno;
  221. if(nread == -1 || status != ENOERR)
  222. return status;
  223. /* else it's okay we read less than asked for */
  224. (void) memset((char *)vp + nread, 0, (ssize_t)extent - nread);
  225. }
  226. *nreadp = nread;
  227. *posp += nread;
  228. return ENOERR;
  229. }
  230. /* This struct is for POSIX systems, with NC_SHARE not in effect. If
  231. NC_SHARE is used, see ncio_spx.
  232. blksz - block size for reads and writes to file.
  233. pos - current read/write position in file.
  234. bf_offset - file offset corresponding to start of memory buffer
  235. bf_extent - number of bytes in I/O request
  236. bf_cnt - number of bytes available in buffer
  237. bf_base - pointer to beginning of buffer.
  238. bf_rflags - buffer region flags (defined in ncio.h) tell the lock
  239. status, read/write permissions, and modification status of regions
  240. of data in the buffer.
  241. bf_refcount - buffer reference count.
  242. slave - used in moves.
  243. */
  244. typedef struct ncio_px {
  245. size_t blksz;
  246. off_t pos;
  247. /* buffer */
  248. off_t bf_offset;
  249. size_t bf_extent;
  250. size_t bf_cnt;
  251. void *bf_base;
  252. int bf_rflags;
  253. int bf_refcount;
  254. /* chain for double buffering in px_move */
  255. struct ncio_px *slave;
  256. } ncio_px;
  257. /*ARGSUSED*/
  258. /* This function indicates the file region starting at offset may be
  259. released.
  260. This is for POSIX, without NC_SHARE. If called with RGN_MODIFIED
  261. flag, sets the modified flag in pxp->bf_rflags and decrements the
  262. reference count.
  263. pxp - pointer to posix non-share ncio_px struct.
  264. offset - file offset for beginning of to region to be
  265. released.
  266. rflags - only RGN_MODIFIED is relevent to this function, others ignored
  267. */
  268. static int
  269. px_rel(ncio_px *const pxp, off_t offset, int rflags)
  270. {
  271. assert(pxp->bf_offset <= offset
  272. && offset < pxp->bf_offset + (off_t) pxp->bf_extent);
  273. assert(pIf(fIsSet(rflags, RGN_MODIFIED),
  274. fIsSet(pxp->bf_rflags, RGN_WRITE)));
  275. if(fIsSet(rflags, RGN_MODIFIED))
  276. {
  277. fSet(pxp->bf_rflags, RGN_MODIFIED);
  278. }
  279. pxp->bf_refcount--;
  280. return ENOERR;
  281. }
  282. /* This function indicates the file region starting at offset may be
  283. released. Each read or write to the file is bracketed by a call to
  284. the "get" region function and a call to the "rel" region function.
  285. If you only read from the memory region, release it with a flag of
  286. 0, if you modify the region, release it with a flag of
  287. RGN_MODIFIED.
  288. For POSIX system, without NC_SHARE, this becomes the rel function
  289. pointed to by the ncio rel function pointer. It mearly checks for
  290. file write permission, then calls px_rel to do everything.
  291. nciop - pointer to ncio struct.
  292. offset - num bytes from beginning of buffer to region to be
  293. released.
  294. rflags - only RGN_MODIFIED is relevent to this function, others ignored
  295. */
  296. static int
  297. ncio_px_rel(ncio *const nciop, off_t offset, int rflags)
  298. {
  299. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  300. if(fIsSet(rflags, RGN_MODIFIED) && !fIsSet(nciop->ioflags, NC_WRITE))
  301. return EPERM; /* attempt to write readonly file */
  302. return px_rel(pxp, offset, rflags);
  303. }
  304. /* POSIX get. This will "make a region available." Since we're using
  305. buffered IO, this means that if needed, we'll fetch a new page from
  306. the file, otherwise, just return a pointer to what's in memory
  307. already.
  308. nciop - pointer to ncio struct, containing file info.
  309. pxp - pointer to ncio_px struct, which contains special metadate
  310. for posix files without NC_SHARE.
  311. offset - start byte of region to get.
  312. extent - how many bytes to read.
  313. rflags - One of the RGN_* flags defined in ncio.h.
  314. vpp - pointer to pointer that will recieve data.
  315. NOTES:
  316. * For blkoffset round offset down to the nearest pxp->blksz. This
  317. provides the offset (in bytes) to the beginning of the block that
  318. holds the current offset.
  319. * diff tells how far into the current block we are.
  320. * For blkextent round up to the number of bytes at the beginning of
  321. the next block, after the one that holds our current position, plus
  322. whatever extra (i.e. the extent) that we are about to grab.
  323. * The blkextent can't be more than twice the pxp->blksz. That's
  324. because the pxp->blksize is the sizehint, and in ncio_px_init2 the
  325. buffer (pointed to by pxp->bf-base) is allocated with 2 *
  326. *sizehintp. This is checked (unneccesarily) more than once in
  327. asserts.
  328. * If this is called on a newly opened file, pxp->bf_offset will be
  329. OFF_NONE and we'll jump to label pgin to immediately read in a
  330. page.
  331. */
  332. static int
  333. px_get(ncio *const nciop, ncio_px *const pxp,
  334. off_t offset, size_t extent,
  335. int rflags,
  336. void **const vpp)
  337. {
  338. int status = ENOERR;
  339. const off_t blkoffset = _RNDDOWN(offset, (off_t)pxp->blksz);
  340. size_t diff = (size_t)(offset - blkoffset);
  341. size_t blkextent = _RNDUP(diff + extent, pxp->blksz);
  342. assert(extent != 0);
  343. assert(extent < X_INT_MAX); /* sanity check */
  344. assert(offset >= 0); /* sanity check */
  345. if(2 * pxp->blksz < blkextent)
  346. return E2BIG; /* TODO: temporary kludge */
  347. if(pxp->bf_offset == OFF_NONE)
  348. {
  349. /* Uninitialized */
  350. if(pxp->bf_base == NULL)
  351. {
  352. assert(pxp->bf_extent == 0);
  353. assert(blkextent <= 2 * pxp->blksz);
  354. pxp->bf_base = malloc(2 * pxp->blksz);
  355. if(pxp->bf_base == NULL)
  356. return ENOMEM;
  357. }
  358. goto pgin;
  359. }
  360. /* else */
  361. assert(blkextent <= 2 * pxp->blksz);
  362. if(blkoffset == pxp->bf_offset)
  363. {
  364. /* hit */
  365. if(blkextent > pxp->bf_extent)
  366. {
  367. /* page in upper */
  368. void *const middle =
  369. (void *)((char *)pxp->bf_base + pxp->blksz);
  370. assert(pxp->bf_extent == pxp->blksz);
  371. status = px_pgin(nciop,
  372. pxp->bf_offset + (off_t)pxp->blksz,
  373. pxp->blksz,
  374. middle,
  375. &pxp->bf_cnt,
  376. &pxp->pos);
  377. if(status != ENOERR)
  378. return status;
  379. pxp->bf_extent = 2 * pxp->blksz;
  380. pxp->bf_cnt += pxp->blksz;
  381. }
  382. goto done;
  383. }
  384. /* else */
  385. if(pxp->bf_extent > pxp->blksz
  386. && blkoffset == pxp->bf_offset + (off_t)pxp->blksz)
  387. {
  388. /* hit in upper half */
  389. if(blkextent == pxp->blksz)
  390. {
  391. /* all in upper half, no fault needed */
  392. diff += pxp->blksz;
  393. goto done;
  394. }
  395. /* else */
  396. if(pxp->bf_cnt > pxp->blksz)
  397. {
  398. /* data in upper half */
  399. void *const middle =
  400. (void *)((char *)pxp->bf_base + pxp->blksz);
  401. assert(pxp->bf_extent == 2 * pxp->blksz);
  402. if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
  403. {
  404. /* page out lower half */
  405. assert(pxp->bf_refcount <= 0);
  406. status = px_pgout(nciop,
  407. pxp->bf_offset,
  408. pxp->blksz,
  409. pxp->bf_base,
  410. &pxp->pos);
  411. if(status != ENOERR)
  412. return status;
  413. }
  414. pxp->bf_cnt -= pxp->blksz;
  415. /* copy upper half into lower half */
  416. (void) memcpy(pxp->bf_base, middle, pxp->bf_cnt);
  417. }
  418. pxp->bf_offset = blkoffset;
  419. /* pxp->bf_extent = pxp->blksz; */
  420. assert(blkextent == 2 * pxp->blksz);
  421. {
  422. /* page in upper */
  423. void *const middle =
  424. (void *)((char *)pxp->bf_base + pxp->blksz);
  425. status = px_pgin(nciop,
  426. pxp->bf_offset + (off_t)pxp->blksz,
  427. pxp->blksz,
  428. middle,
  429. &pxp->bf_cnt,
  430. &pxp->pos);
  431. if(status != ENOERR)
  432. return status;
  433. pxp->bf_extent = 2 * pxp->blksz;
  434. pxp->bf_cnt += pxp->blksz;
  435. }
  436. goto done;
  437. }
  438. /* else */
  439. if(blkoffset == pxp->bf_offset - (off_t)pxp->blksz)
  440. {
  441. /* wants the page below */
  442. void *const middle =
  443. (void *)((char *)pxp->bf_base + pxp->blksz);
  444. size_t upper_cnt = 0;
  445. if(pxp->bf_cnt > pxp->blksz)
  446. {
  447. /* data in upper half */
  448. assert(pxp->bf_extent == 2 * pxp->blksz);
  449. if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
  450. {
  451. /* page out upper half */
  452. assert(pxp->bf_refcount <= 0);
  453. status = px_pgout(nciop,
  454. pxp->bf_offset + (off_t)pxp->blksz,
  455. pxp->bf_cnt - pxp->blksz,
  456. middle,
  457. &pxp->pos);
  458. if(status != ENOERR)
  459. return status;
  460. }
  461. pxp->bf_cnt = pxp->blksz;
  462. pxp->bf_extent = pxp->blksz;
  463. }
  464. if(pxp->bf_cnt > 0)
  465. {
  466. /* copy lower half into upper half */
  467. (void) memcpy(middle, pxp->bf_base, pxp->blksz);
  468. upper_cnt = pxp->bf_cnt;
  469. }
  470. /* read page below into lower half */
  471. status = px_pgin(nciop,
  472. blkoffset,
  473. pxp->blksz,
  474. pxp->bf_base,
  475. &pxp->bf_cnt,
  476. &pxp->pos);
  477. if(status != ENOERR)
  478. return status;
  479. pxp->bf_offset = blkoffset;
  480. if(upper_cnt != 0)
  481. {
  482. pxp->bf_extent = 2 * pxp->blksz;
  483. pxp->bf_cnt = pxp->blksz + upper_cnt;
  484. }
  485. else
  486. {
  487. pxp->bf_extent = pxp->blksz;
  488. }
  489. goto done;
  490. }
  491. /* else */
  492. /* no overlap */
  493. if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
  494. {
  495. assert(pxp->bf_refcount <= 0);
  496. status = px_pgout(nciop,
  497. pxp->bf_offset,
  498. pxp->bf_cnt,
  499. pxp->bf_base,
  500. &pxp->pos);
  501. if(status != ENOERR)
  502. return status;
  503. pxp->bf_rflags = 0;
  504. }
  505. pgin:
  506. status = px_pgin(nciop,
  507. blkoffset,
  508. blkextent,
  509. pxp->bf_base,
  510. &pxp->bf_cnt,
  511. &pxp->pos);
  512. if(status != ENOERR)
  513. return status;
  514. pxp->bf_offset = blkoffset;
  515. pxp->bf_extent = blkextent;
  516. done:
  517. extent += diff;
  518. if(pxp->bf_cnt < extent)
  519. pxp->bf_cnt = extent;
  520. assert(pxp->bf_cnt <= pxp->bf_extent);
  521. pxp->bf_rflags |= rflags;
  522. pxp->bf_refcount++;
  523. *vpp = (char *)pxp->bf_base + diff;
  524. return ENOERR;
  525. }
  526. /* Request that the region (offset, extent) be made available through
  527. *vpp.
  528. This function converts a file region specified by an offset and
  529. extent to a memory pointer. The region may be locked until the
  530. corresponding call to rel().
  531. For POSIX systems, without NC_SHARE. This function gets a page of
  532. size extent?
  533. This is a wrapper for the function px_get, which does all the heavy
  534. lifting.
  535. nciop - pointer to ncio struct for this file.
  536. offset - offset (from beginning of file?) to the data we want to
  537. read.
  538. extent - the number of bytes to read from the file.
  539. rflags - One of the RGN_* flags defined in ncio.h.
  540. vpp - handle to point at data when it's been read.
  541. */
  542. static int
  543. ncio_px_get(ncio *const nciop,
  544. off_t offset, size_t extent,
  545. int rflags,
  546. void **const vpp)
  547. {
  548. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  549. if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
  550. return EPERM; /* attempt to write readonly file */
  551. /* reclaim space used in move */
  552. if(pxp->slave != NULL)
  553. {
  554. if(pxp->slave->bf_base != NULL)
  555. {
  556. free(pxp->slave->bf_base);
  557. pxp->slave->bf_base = NULL;
  558. pxp->slave->bf_extent = 0;
  559. pxp->slave->bf_offset = OFF_NONE;
  560. }
  561. free(pxp->slave);
  562. pxp->slave = NULL;
  563. }
  564. return px_get(nciop, pxp, offset, extent, rflags, vpp);
  565. }
  566. /* ARGSUSED */
  567. static int
  568. px_double_buffer(ncio *const nciop, off_t to, off_t from,
  569. size_t nbytes, int rflags)
  570. {
  571. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  572. int status = ENOERR;
  573. void *src;
  574. void *dest;
  575. #if INSTRUMENT
  576. fprintf(stderr, "\tdouble_buffr %ld %ld %ld\n",
  577. (long)to, (long)from, (long)nbytes);
  578. #endif
  579. status = px_get(nciop, pxp, to, nbytes, RGN_WRITE,
  580. &dest);
  581. if(status != ENOERR)
  582. return status;
  583. if(pxp->slave == NULL)
  584. {
  585. pxp->slave = (ncio_px *) malloc(sizeof(ncio_px));
  586. if(pxp->slave == NULL)
  587. return ENOMEM;
  588. pxp->slave->blksz = pxp->blksz;
  589. /* pos done below */
  590. pxp->slave->bf_offset = pxp->bf_offset;
  591. pxp->slave->bf_extent = pxp->bf_extent;
  592. pxp->slave->bf_cnt = pxp->bf_cnt;
  593. pxp->slave->bf_base = malloc(2 * pxp->blksz);
  594. if(pxp->slave->bf_base == NULL)
  595. return ENOMEM;
  596. (void) memcpy(pxp->slave->bf_base, pxp->bf_base,
  597. pxp->bf_extent);
  598. pxp->slave->bf_rflags = 0;
  599. pxp->slave->bf_refcount = 0;
  600. pxp->slave->slave = NULL;
  601. }
  602. pxp->slave->pos = pxp->pos;
  603. status = px_get(nciop, pxp->slave, from, nbytes, 0,
  604. &src);
  605. if(status != ENOERR)
  606. return status;
  607. if(pxp->pos != pxp->slave->pos)
  608. {
  609. /* position changed, sync */
  610. pxp->pos = pxp->slave->pos;
  611. }
  612. (void) memcpy(dest, src, nbytes);
  613. (void)px_rel(pxp->slave, from, 0);
  614. (void)px_rel(pxp, to, RGN_MODIFIED);
  615. return status;
  616. }
  617. /* Like memmove(), safely move possibly overlapping data.
  618. Copy one region to another without making anything available to
  619. higher layers. May be just implemented in terms of get() and rel(),
  620. or may be tricky to be efficient. Only used in by nc_enddef()
  621. after redefinition.
  622. nciop - pointer to ncio struct with file info.
  623. to - src for move?
  624. from - dest for move?
  625. nbytes - number of bytes to move.
  626. rflags - One of the RGN_* flags defined in ncio.h. The only
  627. reasonable flag value is RGN_NOLOCK.
  628. */
  629. static int
  630. ncio_px_move(ncio *const nciop, off_t to, off_t from,
  631. size_t nbytes, int rflags)
  632. {
  633. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  634. int status = ENOERR;
  635. off_t lower;
  636. off_t upper;
  637. char *base;
  638. size_t diff;
  639. size_t extent;
  640. if(to == from)
  641. return ENOERR; /* NOOP */
  642. if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
  643. return EPERM; /* attempt to write readonly file */
  644. rflags &= RGN_NOLOCK; /* filter unwanted flags */
  645. if(to > from)
  646. {
  647. /* growing */
  648. lower = from;
  649. upper = to;
  650. }
  651. else
  652. {
  653. /* shrinking */
  654. lower = to;
  655. upper = from;
  656. }
  657. diff = (size_t)(upper - lower);
  658. extent = diff + nbytes;
  659. #if INSTRUMENT
  660. fprintf(stderr, "ncio_px_move %ld %ld %ld %ld %ld\n",
  661. (long)to, (long)from, (long)nbytes, (long)lower, (long)extent);
  662. #endif
  663. if(extent > pxp->blksz)
  664. {
  665. size_t remaining = nbytes;
  666. if(to > from)
  667. {
  668. off_t frm = from + nbytes;
  669. off_t toh = to + nbytes;
  670. for(;;)
  671. {
  672. size_t loopextent = MIN(remaining, pxp->blksz);
  673. frm -= loopextent;
  674. toh -= loopextent;
  675. status = px_double_buffer(nciop, toh, frm,
  676. loopextent, rflags) ;
  677. if(status != ENOERR)
  678. return status;
  679. remaining -= loopextent;
  680. if(remaining == 0)
  681. break; /* normal loop exit */
  682. }
  683. }
  684. else
  685. {
  686. for(;;)
  687. {
  688. size_t loopextent = MIN(remaining, pxp->blksz);
  689. status = px_double_buffer(nciop, to, from,
  690. loopextent, rflags) ;
  691. if(status != ENOERR)
  692. return status;
  693. remaining -= loopextent;
  694. if(remaining == 0)
  695. break; /* normal loop exit */
  696. to += loopextent;
  697. from += loopextent;
  698. }
  699. }
  700. return ENOERR;
  701. }
  702. #if INSTRUMENT
  703. fprintf(stderr, "\tncio_px_move small\n");
  704. #endif
  705. status = px_get(nciop, pxp, lower, extent, RGN_WRITE|rflags,
  706. (void **)&base);
  707. if(status != ENOERR)
  708. return status;
  709. if(to > from)
  710. (void) memmove(base + diff, base, nbytes);
  711. else
  712. (void) memmove(base, base + diff, nbytes);
  713. (void) px_rel(pxp, lower, RGN_MODIFIED);
  714. return status;
  715. }
  716. /* Flush any buffers to disk. May be a no-op on if I/O is unbuffered.
  717. This function is used when NC_SHARE is NOT used.
  718. */
  719. static int
  720. ncio_px_sync(ncio *const nciop)
  721. {
  722. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  723. int status = ENOERR;
  724. if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
  725. {
  726. assert(pxp->bf_refcount <= 0);
  727. status = px_pgout(nciop, pxp->bf_offset,
  728. pxp->bf_cnt,
  729. pxp->bf_base, &pxp->pos);
  730. if(status != ENOERR)
  731. return status;
  732. pxp->bf_rflags = 0;
  733. }
  734. else if (!fIsSet(pxp->bf_rflags, RGN_WRITE))
  735. {
  736. /*
  737. * The dataset is readonly. Invalidate the buffers so
  738. * that the next ncio_px_get() will actually read data.
  739. */
  740. pxp->bf_offset = OFF_NONE;
  741. pxp->bf_cnt = 0;
  742. }
  743. return status;
  744. }
  745. /* Internal function called at close to
  746. free up anything hanging off pvt.
  747. */
  748. static void
  749. ncio_px_free(void *const pvt)
  750. {
  751. ncio_px *const pxp = (ncio_px *)pvt;
  752. if(pxp == NULL)
  753. return;
  754. if(pxp->slave != NULL)
  755. {
  756. if(pxp->slave->bf_base != NULL)
  757. {
  758. free(pxp->slave->bf_base);
  759. pxp->slave->bf_base = NULL;
  760. pxp->slave->bf_extent = 0;
  761. pxp->slave->bf_offset = OFF_NONE;
  762. }
  763. free(pxp->slave);
  764. pxp->slave = NULL;
  765. }
  766. if(pxp->bf_base != NULL)
  767. {
  768. free(pxp->bf_base);
  769. pxp->bf_base = NULL;
  770. pxp->bf_extent = 0;
  771. pxp->bf_offset = OFF_NONE;
  772. }
  773. }
  774. /* This is the second half of the ncio initialization. This is called
  775. after the file has actually been opened.
  776. The most important thing that happens is the allocation of a block
  777. of memory at pxp->bf_base. This is going to be twice the size of
  778. the chunksizehint (rounded up to the nearest sizeof(double)) passed
  779. in from nc__create or nc__open. The rounded chunksizehint (passed
  780. in here in sizehintp) is going to be stored as pxp->blksize.
  781. According to our "contract" we are not allowed to ask for an extent
  782. larger than this chunksize/sizehint/blksize from the ncio get
  783. function.
  784. nciop - pointer to the ncio struct
  785. sizehintp - pointer to a size hint that will be rounded up and
  786. passed back to the caller.
  787. isNew - true if this is being called from ncio_create for a new
  788. file.
  789. */
  790. static int
  791. ncio_px_init2(ncio *const nciop, size_t *sizehintp, int isNew)
  792. {
  793. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  794. const size_t bufsz = 2 * *sizehintp;
  795. assert(nciop->fd >= 0);
  796. pxp->blksz = *sizehintp;
  797. assert(pxp->bf_base == NULL);
  798. /* this is separate allocation because it may grow */
  799. pxp->bf_base = malloc(bufsz);
  800. if(pxp->bf_base == NULL)
  801. return ENOMEM;
  802. /* else */
  803. pxp->bf_cnt = 0;
  804. if(isNew)
  805. {
  806. /* save a read */
  807. pxp->pos = 0;
  808. pxp->bf_offset = 0;
  809. pxp->bf_extent = bufsz;
  810. (void) memset(pxp->bf_base, 0, pxp->bf_extent);
  811. }
  812. return ENOERR;
  813. }
  814. /* This is the first of a two-part initialization of the ncio struct.
  815. Here the rel, get, move, sync, and free function pointers are set
  816. to their POSIX non-NC_SHARE functions (ncio_px_*).
  817. The ncio_px struct is also partially initialized.
  818. */
  819. static void
  820. ncio_px_init(ncio *const nciop)
  821. {
  822. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  823. *((ncio_relfunc **)&nciop->rel) = ncio_px_rel; /* cast away const */
  824. *((ncio_getfunc **)&nciop->get) = ncio_px_get; /* cast away const */
  825. *((ncio_movefunc **)&nciop->move) = ncio_px_move; /* cast away const */
  826. *((ncio_syncfunc **)&nciop->sync) = ncio_px_sync; /* cast away const */
  827. *((ncio_freefunc **)&nciop->free) = ncio_px_free; /* cast away const */
  828. pxp->blksz = 0;
  829. pxp->pos = -1;
  830. pxp->bf_offset = OFF_NONE;
  831. pxp->bf_extent = 0;
  832. pxp->bf_rflags = 0;
  833. pxp->bf_refcount = 0;
  834. pxp->bf_base = NULL;
  835. pxp->slave = NULL;
  836. }
  837. /* Begin spx */
  838. /* This is the struct that gets hung of ncio->pvt(?) when the NC_SHARE
  839. flag is used.
  840. */
  841. typedef struct ncio_spx {
  842. off_t pos;
  843. /* buffer */
  844. off_t bf_offset;
  845. size_t bf_extent;
  846. size_t bf_cnt;
  847. void *bf_base;
  848. } ncio_spx;
  849. /*ARGSUSED*/
  850. /* This function releases the region specified by offset.
  851. For POSIX system, with NC_SHARE, this becomes the rel function
  852. pointed to by the ncio rel function pointer. It mearly checks for
  853. file write permission, then calls px_rel to do everything.
  854. nciop - pointer to ncio struct.
  855. offset - beginning of region.
  856. rflags - One of the RGN_* flags defined in ncio.h. If set to
  857. RGN_MODIFIED it means that the data in this region were modified,
  858. and it needs to be written out to the disk immediately (since we
  859. are not buffering with NC_SHARE on).
  860. */
  861. static int
  862. ncio_spx_rel(ncio *const nciop, off_t offset, int rflags)
  863. {
  864. ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
  865. int status = ENOERR;
  866. assert(pxp->bf_offset <= offset);
  867. assert(pxp->bf_cnt != 0);
  868. assert(pxp->bf_cnt <= pxp->bf_extent);
  869. #ifdef X_ALIGN
  870. assert(offset < pxp->bf_offset + X_ALIGN);
  871. assert(pxp->bf_cnt % X_ALIGN == 0 );
  872. #endif
  873. if(fIsSet(rflags, RGN_MODIFIED))
  874. {
  875. if(!fIsSet(nciop->ioflags, NC_WRITE))
  876. return EPERM; /* attempt to write readonly file */
  877. status = px_pgout(nciop, pxp->bf_offset,
  878. pxp->bf_cnt,
  879. pxp->bf_base, &pxp->pos);
  880. /* if error, invalidate buffer anyway */
  881. }
  882. pxp->bf_offset = OFF_NONE;
  883. pxp->bf_cnt = 0;
  884. return status;
  885. }
  886. /* Request that the region (offset, extent) be made available through
  887. *vpp.
  888. This function converts a file region specified by an offset and
  889. extent to a memory pointer. The region may be locked until the
  890. corresponding call to rel().
  891. For POSIX systems, with NC_SHARE.
  892. nciop - pointer to ncio struct for this file.
  893. offset - offset (from beginning of file?) to the data we want to
  894. read.
  895. extent - the number of bytes we want.
  896. rflags - One of the RGN_* flags defined in ncio.h. May be RGN_NOLOCK.
  897. vpp - handle to point at data when it's been read.
  898. */
  899. static int
  900. ncio_spx_get(ncio *const nciop,
  901. off_t offset, size_t extent,
  902. int rflags,
  903. void **const vpp)
  904. {
  905. ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
  906. int status = ENOERR;
  907. #ifdef X_ALIGN
  908. size_t rem;
  909. #endif
  910. if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
  911. return EPERM; /* attempt to write readonly file */
  912. assert(extent != 0);
  913. assert(extent < X_INT_MAX); /* sanity check */
  914. assert(pxp->bf_cnt == 0);
  915. #ifdef X_ALIGN
  916. rem = (size_t)(offset % X_ALIGN);
  917. if(rem != 0)
  918. {
  919. offset -= rem;
  920. extent += rem;
  921. }
  922. {
  923. const size_t rndup = extent % X_ALIGN;
  924. if(rndup != 0)
  925. extent += X_ALIGN - rndup;
  926. }
  927. assert(offset % X_ALIGN == 0);
  928. assert(extent % X_ALIGN == 0);
  929. #endif
  930. if(pxp->bf_extent < extent)
  931. {
  932. if(pxp->bf_base != NULL)
  933. {
  934. free(pxp->bf_base);
  935. pxp->bf_base = NULL;
  936. pxp->bf_extent = 0;
  937. }
  938. assert(pxp->bf_extent == 0);
  939. pxp->bf_base = malloc(extent);
  940. if(pxp->bf_base == NULL)
  941. return ENOMEM;
  942. pxp->bf_extent = extent;
  943. }
  944. status = px_pgin(nciop, offset,
  945. extent,
  946. pxp->bf_base,
  947. &pxp->bf_cnt, &pxp->pos);
  948. if(status != ENOERR)
  949. return status;
  950. pxp->bf_offset = offset;
  951. if(pxp->bf_cnt < extent)
  952. pxp->bf_cnt = extent;
  953. #ifdef X_ALIGN
  954. *vpp = (char *)pxp->bf_base + rem;
  955. #else
  956. *vpp = pxp->bf_base;
  957. #endif
  958. return ENOERR;
  959. }
  960. #if 0
  961. /*ARGSUSED*/
  962. static int
  963. strategy(ncio *const nciop, off_t to, off_t offset,
  964. size_t extent, int rflags)
  965. {
  966. static ncio_spx pxp[1];
  967. int status = ENOERR;
  968. #ifdef X_ALIGN
  969. size_t rem;
  970. #endif
  971. assert(extent != 0);
  972. assert(extent < X_INT_MAX); /* sanity check */
  973. #if INSTRUMENT
  974. fprintf(stderr, "strategy %ld at %ld to %ld\n",
  975. (long)extent, (long)offset, (long)to);
  976. #endif
  977. #ifdef X_ALIGN
  978. rem = (size_t)(offset % X_ALIGN);
  979. if(rem != 0)
  980. {
  981. offset -= rem;
  982. extent += rem;
  983. }
  984. {
  985. const size_t rndup = extent % X_ALIGN;
  986. if(rndup != 0)
  987. extent += X_ALIGN - rndup;
  988. }
  989. assert(offset % X_ALIGN == 0);
  990. assert(extent % X_ALIGN == 0);
  991. #endif
  992. if(pxp->bf_extent < extent)
  993. {
  994. if(pxp->bf_base != NULL)
  995. {
  996. free(pxp->bf_base);
  997. pxp->bf_base = NULL;
  998. pxp->bf_extent = 0;
  999. }
  1000. assert(pxp->bf_extent == 0);
  1001. pxp->bf_base = malloc(extent);
  1002. if(pxp->bf_base == NULL)
  1003. return ENOMEM;
  1004. pxp->bf_extent = extent;
  1005. }
  1006. status = px_pgin(nciop, offset,
  1007. extent,
  1008. pxp->bf_base,
  1009. &pxp->bf_cnt, &pxp->pos);
  1010. if(status != ENOERR)
  1011. return status;
  1012. pxp->bf_offset = to; /* TODO: XALIGN */
  1013. if(pxp->bf_cnt < extent)
  1014. pxp->bf_cnt = extent;
  1015. status = px_pgout(nciop, pxp->bf_offset,
  1016. pxp->bf_cnt,
  1017. pxp->bf_base, &pxp->pos);
  1018. /* if error, invalidate buffer anyway */
  1019. pxp->bf_offset = OFF_NONE;
  1020. pxp->bf_cnt = 0;
  1021. return status;
  1022. }
  1023. #endif
  1024. /* Copy one region to another without making anything available to
  1025. higher layers. May be just implemented in terms of get() and rel(),
  1026. or may be tricky to be efficient. Only used in by nc_enddef()
  1027. after redefinition.
  1028. nciop - pointer to ncio struct for this file.
  1029. to - dest for move?
  1030. from - src for move?
  1031. nbytes - number of bytes to move.
  1032. rflags - One of the RGN_* flags defined in ncio.h.
  1033. */
  1034. static int
  1035. ncio_spx_move(ncio *const nciop, off_t to, off_t from,
  1036. size_t nbytes, int rflags)
  1037. {
  1038. int status = ENOERR;
  1039. off_t lower = from;
  1040. off_t upper = to;
  1041. char *base;
  1042. size_t diff = (size_t)(upper - lower);
  1043. size_t extent = diff + nbytes;
  1044. rflags &= RGN_NOLOCK; /* filter unwanted flags */
  1045. if(to == from)
  1046. return ENOERR; /* NOOP */
  1047. if(to > from)
  1048. {
  1049. /* growing */
  1050. lower = from;
  1051. upper = to;
  1052. }
  1053. else
  1054. {
  1055. /* shrinking */
  1056. lower = to;
  1057. upper = from;
  1058. }
  1059. diff = (size_t)(upper - lower);
  1060. extent = diff + nbytes;
  1061. status = ncio_spx_get(nciop, lower, extent, RGN_WRITE|rflags,
  1062. (void **)&base);
  1063. if(status != ENOERR)
  1064. return status;
  1065. if(to > from)
  1066. (void) memmove(base + diff, base, nbytes);
  1067. else
  1068. (void) memmove(base, base + diff, nbytes);
  1069. (void) ncio_spx_rel(nciop, lower, RGN_MODIFIED);
  1070. return status;
  1071. }
  1072. /*ARGSUSED*/
  1073. /* Flush any buffers to disk. May be a no-op on if I/O is unbuffered.
  1074. */
  1075. static int
  1076. ncio_spx_sync(ncio *const nciop)
  1077. {
  1078. /* NOOP */
  1079. return ENOERR;
  1080. }
  1081. static void
  1082. ncio_spx_free(void *const pvt)
  1083. {
  1084. ncio_spx *const pxp = (ncio_spx *)pvt;
  1085. if(pxp == NULL)
  1086. return;
  1087. if(pxp->bf_base != NULL)
  1088. {
  1089. free(pxp->bf_base);
  1090. pxp->bf_base = NULL;
  1091. pxp->bf_offset = OFF_NONE;
  1092. pxp->bf_extent = 0;
  1093. pxp->bf_cnt = 0;
  1094. }
  1095. }
  1096. /* This does the second half of the ncio_spx struct initialization for
  1097. POSIX systems, with NC_SHARE on.
  1098. nciop - pointer to ncio struct for this file. File has been opened.
  1099. sizehintp - pointer to a size which will be rounded up to the
  1100. nearest 8-byt boundary and then used as the max size "chunk" (or
  1101. page) to read from the file.
  1102. */
  1103. static int
  1104. ncio_spx_init2(ncio *const nciop, const size_t *const sizehintp)
  1105. {
  1106. ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
  1107. assert(nciop->fd >= 0);
  1108. pxp->bf_extent = *sizehintp;
  1109. assert(pxp->bf_base == NULL);
  1110. /* this is separate allocation because it may grow */
  1111. pxp->bf_base = malloc(pxp->bf_extent);
  1112. if(pxp->bf_base == NULL)
  1113. {
  1114. pxp->bf_extent = 0;
  1115. return ENOMEM;
  1116. }
  1117. /* else */
  1118. return ENOERR;
  1119. }
  1120. /* First half of init for ncio_spx struct, setting the rel, get, move,
  1121. snyc, and free function pointers to the NC_SHARE versions of these
  1122. functions (i.e. the ncio_spx_* functions).
  1123. */
  1124. static void
  1125. ncio_spx_init(ncio *const nciop)
  1126. {
  1127. ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
  1128. *((ncio_relfunc **)&nciop->rel) = ncio_spx_rel; /* cast away const */
  1129. *((ncio_getfunc **)&nciop->get) = ncio_spx_get; /* cast away const */
  1130. *((ncio_movefunc **)&nciop->move) = ncio_spx_move; /* cast away const */
  1131. *((ncio_syncfunc **)&nciop->sync) = ncio_spx_sync; /* cast away const */
  1132. *((ncio_freefunc **)&nciop->free) = ncio_spx_free; /* cast away const */
  1133. pxp->pos = -1;
  1134. pxp->bf_offset = OFF_NONE;
  1135. pxp->bf_extent = 0;
  1136. pxp->bf_cnt = 0;
  1137. pxp->bf_base = NULL;
  1138. }
  1139. /* */
  1140. /* This will call whatever free function is attached to the free
  1141. function pointer in ncio. It's called from ncio_close, and from
  1142. ncio_open and ncio_create when an error occurs that the file
  1143. metadata must be freed.
  1144. */
  1145. static void
  1146. ncio_free(ncio *nciop)
  1147. {
  1148. if(nciop == NULL)
  1149. return;
  1150. if(nciop->free != NULL)
  1151. nciop->free(nciop->pvt);
  1152. free(nciop);
  1153. }
  1154. /* Create a new ncio struct to hold info about the file. This will
  1155. create and init the ncio_px or ncio_spx struct (the latter if
  1156. NC_SHARE is used.)
  1157. */
  1158. static ncio *
  1159. ncio_new(const char *path, int ioflags)
  1160. {
  1161. size_t sz_ncio = M_RNDUP(sizeof(ncio));
  1162. size_t sz_path = M_RNDUP(strlen(path) +1);
  1163. size_t sz_ncio_pvt;
  1164. ncio *nciop;
  1165. #if ALWAYS_NC_SHARE /* DEBUG */
  1166. fSet(ioflags, NC_SHARE);
  1167. #endif
  1168. if(fIsSet(ioflags, NC_SHARE))
  1169. sz_ncio_pvt = sizeof(ncio_spx);
  1170. else
  1171. sz_ncio_pvt = sizeof(ncio_px);
  1172. nciop = (ncio *) malloc(sz_ncio + sz_path + sz_ncio_pvt);
  1173. if(nciop == NULL)
  1174. return NULL;
  1175. nciop->ioflags = ioflags;
  1176. *((int *)&nciop->fd) = -1; /* cast away const */
  1177. nciop->path = (char *) ((char *)nciop + sz_ncio);
  1178. (void) strcpy((char *)nciop->path, path); /* cast away const */
  1179. /* cast away const */
  1180. *((void **)&nciop->pvt) = (void *)(nciop->path + sz_path);
  1181. if(fIsSet(ioflags, NC_SHARE))
  1182. ncio_spx_init(nciop);
  1183. else
  1184. ncio_px_init(nciop);
  1185. return nciop;
  1186. }
  1187. /* Public below this point */
  1188. #define NCIO_MINBLOCKSIZE 256
  1189. #define NCIO_MAXBLOCKSIZE 268435456 /* sanity check, about X_SIZE_T_MAX/8 */
  1190. #ifdef S_IRUSR
  1191. #define NC_DEFAULT_CREAT_MODE \
  1192. (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) /* 0666 */
  1193. #else
  1194. #define NC_DEFAULT_CREAT_MODE 0666
  1195. #endif
  1196. /* Create a file, and the ncio struct to go with it. This funtion is
  1197. only called from nc__create_mp.
  1198. path - path of file to create.
  1199. ioflags - flags from nc_create
  1200. initialsz - From the netcdf man page: "The argument
  1201. Iinitialsize sets the initial size of the file at creation time."
  1202. igeto -
  1203. igetsz -
  1204. sizehintp - this eventually goes into pxp->blksz and is the size of
  1205. a page of data for buffered reads and writes.
  1206. nciopp - pointer to a pointer that will get location of newly
  1207. created and inited ncio struct.
  1208. igetvpp - pointer to pointer which will get the location of ?
  1209. */
  1210. int
  1211. ncio_create(const char *path, int ioflags,
  1212. size_t initialsz,
  1213. off_t igeto, size_t igetsz, size_t *sizehintp,
  1214. ncio **nciopp, void **const igetvpp)
  1215. {
  1216. ncio *nciop;
  1217. int oflags = (O_RDWR|O_CREAT);
  1218. int fd;
  1219. int status;
  1220. if(initialsz < (size_t)igeto + igetsz)
  1221. initialsz = (size_t)igeto + igetsz;
  1222. fSet(ioflags, NC_WRITE);
  1223. if(path == NULL || *path == 0)
  1224. return EINVAL;
  1225. nciop = ncio_new(path, ioflags);
  1226. if(nciop == NULL)
  1227. return ENOMEM;
  1228. if(fIsSet(ioflags, NC_NOCLOBBER))
  1229. fSet(oflags, O_EXCL);
  1230. else
  1231. fSet(oflags, O_TRUNC);
  1232. #ifdef O_BINARY
  1233. fSet(oflags, O_BINARY);
  1234. #endif
  1235. #ifdef vms
  1236. fd = open(path, oflags, NC_DEFAULT_CREAT_MODE, "ctx=stm");
  1237. #else
  1238. /* Should we mess with the mode based on NC_SHARE ?? */
  1239. fd = open(path, oflags, NC_DEFAULT_CREAT_MODE);
  1240. #endif
  1241. #if 0
  1242. (void) fprintf(stderr, "ncio_create(): path=\"%s\"\n", path);
  1243. (void) fprintf(stderr, "ncio_create(): oflags=0x%x\n", oflags);
  1244. #endif
  1245. if(fd < 0)
  1246. {
  1247. status = errno;
  1248. goto unwind_new;
  1249. }
  1250. *((int *)&nciop->fd) = fd; /* cast away const */
  1251. if(*sizehintp < NCIO_MINBLOCKSIZE || *sizehintp > NCIO_MAXBLOCKSIZE)
  1252. {
  1253. /* Use default */
  1254. *sizehintp = blksize(fd);
  1255. }
  1256. else
  1257. {
  1258. *sizehintp = M_RNDUP(*sizehintp);
  1259. }
  1260. if(fIsSet(nciop->ioflags, NC_SHARE))
  1261. status = ncio_spx_init2(nciop, sizehintp);
  1262. else
  1263. status = ncio_px_init2(nciop, sizehintp, 1);
  1264. if(status != ENOERR)
  1265. goto unwind_open;
  1266. if(initialsz != 0)
  1267. {
  1268. status = fgrow(fd, (off_t)initialsz);
  1269. if(status != ENOERR)
  1270. goto unwind_open;
  1271. }
  1272. if(igetsz != 0)
  1273. {
  1274. status = nciop->get(nciop,
  1275. igeto, igetsz,
  1276. RGN_WRITE,
  1277. igetvpp);
  1278. if(status != ENOERR)
  1279. goto unwind_open;
  1280. }
  1281. *nciopp = nciop;
  1282. return ENOERR;
  1283. unwind_open:
  1284. (void) close(fd);
  1285. /* ?? unlink */
  1286. /*FALLTHRU*/
  1287. unwind_new:
  1288. ncio_free(nciop);
  1289. return status;
  1290. }
  1291. /* This function opens the data file. It is only called from nc.c,
  1292. from nc__open_mp and nc_delete_mp.
  1293. path - path of data file.
  1294. ioflags - flags passed into nc_open.
  1295. igeto - looks like this function can do an initial page get, and
  1296. igeto is going to be the offset for that. But it appears to be
  1297. unused
  1298. igetsz - the size in bytes of initial page get (a.k.a. extent). Not
  1299. ever used in the library.
  1300. sizehintp - pointer to sizehint parameter from nc__open or
  1301. nc__create. This is used to set pxp->blksz.
  1302. Here's what the man page has to say:
  1303. "The argument referenced by chunksize controls a space versus time
  1304. tradeoff, memory allocated in the netcdf library versus number of
  1305. system calls.
  1306. Because of internal requirements, the value may not be set to
  1307. exactly the value requested. The actual value chosen is returned by reference.
  1308. Using the value NC_SIZEHINT_DEFAULT causes the library to choose a
  1309. default. How the system choses the default depends on the
  1310. system. On many systems, the "preferred I/O block size" is
  1311. available from the stat() system call, struct stat member
  1312. st_blksize. If this is available it is used. Lacking that, twice
  1313. the system pagesize is used. Lacking a call to discover the system
  1314. pagesize, we just set default chunksize to 8192.
  1315. The chunksize is a property of a given open netcdf descriptor ncid,
  1316. it is not a persistent property of the netcdf dataset."
  1317. nciopp - pointer to pointer that will get address of newly created
  1318. and inited ncio struct.
  1319. igetvpp - handle to pass back pointer to data from inital page
  1320. read, if this were ever used, which it isn't.
  1321. */
  1322. int
  1323. ncio_open(const char *path,
  1324. int ioflags,
  1325. off_t igeto, size_t igetsz, size_t *sizehintp,
  1326. ncio **nciopp, void **const igetvpp)
  1327. {
  1328. ncio *nciop;
  1329. int oflags = fIsSet(ioflags, NC_WRITE) ? O_RDWR : O_RDONLY;
  1330. int fd;
  1331. int status;
  1332. if(path == NULL || *path == 0)
  1333. return EINVAL;
  1334. nciop = ncio_new(path, ioflags);
  1335. if(nciop == NULL)
  1336. return ENOMEM;
  1337. #ifdef O_BINARY
  1338. fSet(oflags, O_BINARY);
  1339. #endif
  1340. #ifdef vms
  1341. fd = open(path, oflags, 0, "ctx=stm");
  1342. #else
  1343. fd = open(path, oflags, 0);
  1344. #endif
  1345. if(fd < 0)
  1346. {
  1347. status = errno;
  1348. goto unwind_new;
  1349. }
  1350. *((int *)&nciop->fd) = fd; /* cast away const */
  1351. if(*sizehintp < NCIO_MINBLOCKSIZE || *sizehintp > NCIO_MAXBLOCKSIZE)
  1352. {
  1353. /* Use default */
  1354. *sizehintp = blksize(fd);
  1355. }
  1356. else
  1357. {
  1358. *sizehintp = M_RNDUP(*sizehintp);
  1359. }
  1360. if(fIsSet(nciop->ioflags, NC_SHARE))
  1361. status = ncio_spx_init2(nciop, sizehintp);
  1362. else
  1363. status = ncio_px_init2(nciop, sizehintp, 0);
  1364. if(status != ENOERR)
  1365. goto unwind_open;
  1366. if(igetsz != 0)
  1367. {
  1368. status = nciop->get(nciop,
  1369. igeto, igetsz,
  1370. 0,
  1371. igetvpp);
  1372. if(status != ENOERR)
  1373. goto unwind_open;
  1374. }
  1375. *nciopp = nciop;
  1376. return ENOERR;
  1377. unwind_open:
  1378. (void) close(fd);
  1379. /*FALLTHRU*/
  1380. unwind_new:
  1381. ncio_free(nciop);
  1382. return status;
  1383. }
  1384. /*
  1385. * Get file size in bytes.
  1386. */
  1387. int
  1388. ncio_filesize(ncio *nciop, off_t *filesizep)
  1389. {
  1390. struct stat sb;
  1391. assert(nciop != NULL);
  1392. if (fstat(nciop->fd, &sb) < 0)
  1393. return errno;
  1394. *filesizep = sb.st_size;
  1395. return ENOERR;
  1396. }
  1397. /*
  1398. * Sync any changes to disk, then truncate or extend file so its size
  1399. * is length. This is only intended to be called before close, if the
  1400. * file is open for writing and the actual size does not match the
  1401. * calculated size, perhaps as the result of having been previously
  1402. * written in NOFILL mode.
  1403. */
  1404. int
  1405. ncio_pad_length(ncio *nciop, off_t length)
  1406. {
  1407. int status = ENOERR;
  1408. if(nciop == NULL)
  1409. return EINVAL;
  1410. if(!fIsSet(nciop->ioflags, NC_WRITE))
  1411. return EPERM; /* attempt to write readonly file */
  1412. status = nciop->sync(nciop);
  1413. if(status != ENOERR)
  1414. return status;
  1415. status = fgrow2(nciop->fd, length);
  1416. if(status != NC_NOERR)
  1417. return status;
  1418. return ENOERR;
  1419. }
  1420. /* Write out any dirty buffers to disk and
  1421. ensure that next read will get data from disk.
  1422. Sync any changes, then close the open file associated with the ncio
  1423. struct, and free its memory.
  1424. nciop - pointer to ncio to close.
  1425. doUnlink - if true, unlink file
  1426. */
  1427. int
  1428. ncio_close(ncio *nciop, int doUnlink)
  1429. {
  1430. int status = ENOERR;
  1431. if(nciop == NULL)
  1432. return EINVAL;
  1433. status = nciop->sync(nciop);
  1434. (void) close(nciop->fd);
  1435. if(doUnlink)
  1436. (void) unlink(nciop->path);
  1437. ncio_free(nciop);
  1438. return status;
  1439. }