Coverage Report

Created: 2025-10-28 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/netcdf-c/libsrc/posixio.c
Line
Count
Source
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
0
#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-(off_t)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 -= (size_t)partial;
317
0
  }
318
0
  if(partial == -1)
319
0
      return errno;
320
0
  *posp += (off_t)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, (size_t)((ssize_t)extent - nread));
394
0
    }
395
396
0
    *nreadp = (size_t)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 = offset - blkoffset;
537
0
  size_t blkextent = _RNDUP((size_t)diff + extent, pxp->blksz);
538
539
0
  if(!(extent != 0 && extent < X_INT_MAX && offset >= 0)) /* sanity check */
540
0
      return NC_ENOTNC;
541
542
0
  if(2 * pxp->blksz < blkextent)
543
0
    return E2BIG; /* TODO: temporary kludge */
544
0
  if(pxp->bf_offset == OFF_NONE)
545
0
  {
546
    /* Uninitialized */
547
0
    if(pxp->bf_base == NULL)
548
0
    {
549
0
      assert(pxp->bf_extent == 0);
550
0
      assert(blkextent <= 2 * pxp->blksz);
551
0
      pxp->bf_base = malloc(2 * pxp->blksz);
552
0
      if(pxp->bf_base == NULL)
553
0
        return ENOMEM;
554
0
    }
555
0
    goto pgin;
556
0
  }
557
  /* else */
558
0
  assert(blkextent <= 2 * pxp->blksz);
559
560
0
  if(blkoffset == pxp->bf_offset)
561
0
  {
562
    /* hit */
563
0
    if(blkextent > pxp->bf_extent)
564
0
    {
565
      /* page in upper */
566
0
      void *const middle =
567
0
        (void *)((char *)pxp->bf_base + pxp->blksz);
568
0
      assert(pxp->bf_extent == pxp->blksz);
569
0
      status = px_pgin(nciop,
570
0
         pxp->bf_offset + (off_t)pxp->blksz,
571
0
         pxp->blksz,
572
0
         middle,
573
0
         &pxp->bf_cnt,
574
0
         &pxp->pos);
575
0
      if(status != NC_NOERR)
576
0
        return status;
577
0
      pxp->bf_extent = 2 * pxp->blksz;
578
0
      pxp->bf_cnt += pxp->blksz;
579
0
    }
580
0
    goto done;
581
0
  }
582
  /* else */
583
584
0
  if(pxp->bf_extent > pxp->blksz
585
0
     && blkoffset == pxp->bf_offset + (off_t)pxp->blksz)
586
0
  {
587
    /* hit in upper half */
588
0
    if(blkextent == pxp->blksz)
589
0
    {
590
      /* all in upper half, no fault needed */
591
0
      diff += (off_t)pxp->blksz;
592
0
      goto done;
593
0
    }
594
    /* else */
595
0
    if(pxp->bf_cnt > pxp->blksz)
596
0
    {
597
      /* data in upper half */
598
0
      void *const middle =
599
0
        (void *)((char *)pxp->bf_base + pxp->blksz);
600
0
      assert(pxp->bf_extent == 2 * pxp->blksz);
601
0
      if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
602
0
      {
603
        /* page out lower half */
604
0
        assert(pxp->bf_refcount <= 0);
605
0
        status = px_pgout(nciop,
606
0
          pxp->bf_offset,
607
0
          pxp->blksz,
608
0
          pxp->bf_base,
609
0
          &pxp->pos);
610
0
        if(status != NC_NOERR)
611
0
          return status;
612
0
      }
613
0
      pxp->bf_cnt -= pxp->blksz;
614
      /* copy upper half into lower half */
615
0
      (void) memcpy(pxp->bf_base, middle, pxp->bf_cnt);
616
0
    }
617
0
    else    /* added to fix nofill bug */
618
0
    {
619
0
      assert(pxp->bf_extent == 2 * pxp->blksz);
620
      /* still have to page out lower half, if modified */
621
0
      if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
622
0
      {
623
0
        assert(pxp->bf_refcount <= 0);
624
0
        status = px_pgout(nciop,
625
0
          pxp->bf_offset,
626
0
          pxp->blksz,
627
0
          pxp->bf_base,
628
0
          &pxp->pos);
629
0
        if(status != NC_NOERR)
630
0
          return status;
631
0
      }
632
0
    }
633
0
    pxp->bf_offset = blkoffset;
634
    /* pxp->bf_extent = pxp->blksz; */
635
636
0
    assert(blkextent == 2 * pxp->blksz);
637
0
    {
638
      /* page in upper */
639
0
      void *const middle =
640
0
        (void *)((char *)pxp->bf_base + pxp->blksz);
641
0
      status = px_pgin(nciop,
642
0
         pxp->bf_offset + (off_t)pxp->blksz,
643
0
         pxp->blksz,
644
0
         middle,
645
0
         &pxp->bf_cnt,
646
0
         &pxp->pos);
647
0
      if(status != NC_NOERR)
648
0
        return status;
649
0
      pxp->bf_extent = 2 * pxp->blksz;
650
0
      pxp->bf_cnt += pxp->blksz;
651
0
    }
652
0
    goto done;
653
0
  }
654
  /* else */
655
656
0
  if(blkoffset == pxp->bf_offset - (off_t)pxp->blksz)
657
0
  {
658
    /* wants the page below */
659
0
    void *const middle =
660
0
      (void *)((char *)pxp->bf_base + pxp->blksz);
661
0
    size_t upper_cnt = 0;
662
0
    if(pxp->bf_cnt > pxp->blksz)
663
0
    {
664
      /* data in upper half */
665
0
      assert(pxp->bf_extent == 2 * pxp->blksz);
666
0
      if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
667
0
      {
668
        /* page out upper half */
669
0
        assert(pxp->bf_refcount <= 0);
670
0
        status = px_pgout(nciop,
671
0
          pxp->bf_offset + (off_t)pxp->blksz,
672
0
          pxp->bf_cnt - pxp->blksz,
673
0
          middle,
674
0
          &pxp->pos);
675
0
        if(status != NC_NOERR)
676
0
          return status;
677
0
      }
678
0
      pxp->bf_cnt = pxp->blksz;
679
0
      pxp->bf_extent = pxp->blksz;
680
0
    }
681
0
    if(pxp->bf_cnt > 0)
682
0
    {
683
      /* copy lower half into upper half */
684
0
      (void) memcpy(middle, pxp->bf_base, pxp->blksz);
685
0
      upper_cnt = pxp->bf_cnt;
686
0
    }
687
    /* read page below into lower half */
688
0
    status = px_pgin(nciop,
689
0
       blkoffset,
690
0
       pxp->blksz,
691
0
       pxp->bf_base,
692
0
       &pxp->bf_cnt,
693
0
       &pxp->pos);
694
0
    if(status != NC_NOERR)
695
0
      return status;
696
0
    pxp->bf_offset = blkoffset;
697
0
    if(upper_cnt != 0)
698
0
    {
699
0
      pxp->bf_extent = 2 * pxp->blksz;
700
0
      pxp->bf_cnt = pxp->blksz + upper_cnt;
701
0
    }
702
0
    else
703
0
    {
704
0
      pxp->bf_extent = pxp->blksz;
705
0
    }
706
0
    goto done;
707
0
  }
708
  /* else */
709
710
  /* no overlap */
711
0
  if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
712
0
  {
713
0
    assert(pxp->bf_refcount <= 0);
714
0
    status = px_pgout(nciop,
715
0
      pxp->bf_offset,
716
0
      pxp->bf_cnt,
717
0
      pxp->bf_base,
718
0
      &pxp->pos);
719
0
    if(status != NC_NOERR)
720
0
      return status;
721
0
    pxp->bf_rflags = 0;
722
0
  }
723
724
0
pgin:
725
0
  status = px_pgin(nciop,
726
0
     blkoffset,
727
0
     blkextent,
728
0
     pxp->bf_base,
729
0
     &pxp->bf_cnt,
730
0
     &pxp->pos);
731
0
  if(status != NC_NOERR)
732
0
    return status;
733
0
        pxp->bf_offset = blkoffset;
734
0
        pxp->bf_extent = blkextent;
735
736
0
done:
737
0
  extent += (size_t)diff;
738
0
  if(pxp->bf_cnt < extent)
739
0
    pxp->bf_cnt = extent;
740
0
  assert(pxp->bf_cnt <= pxp->bf_extent);
741
742
0
  pxp->bf_rflags |= rflags;
743
0
  pxp->bf_refcount++;
744
745
0
    *vpp = (void *)((signed char*)pxp->bf_base + diff);
746
0
  return NC_NOERR;
747
0
}
748
749
/* Request that the region (offset, extent) be made available through
750
   *vpp.
751
752
   This function converts a file region specified by an offset and
753
   extent to a memory pointer. The region may be locked until the
754
   corresponding call to rel().
755
756
   For POSIX systems, without NC_SHARE. This function gets a page of
757
   size extent?
758
759
   This is a wrapper for the function px_get, which does all the heavy
760
   lifting.
761
762
   nciop - pointer to ncio struct for this file.
763
   offset - offset (from beginning of file?) to the data we want to
764
   read.
765
   extent - the number of bytes to read from the file.
766
   rflags - One of the RGN_* flags defined in ncio.h.
767
   vpp - handle to point at data when it's been read.
768
*/
769
static int
770
ncio_px_get(ncio *const nciop,
771
    off_t offset, size_t extent,
772
    int rflags,
773
    void **const vpp)
774
0
{
775
0
  ncio_px *const pxp = (ncio_px *)nciop->pvt;
776
777
0
  if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
778
0
    return EPERM; /* attempt to write readonly file */
779
780
  /* reclaim space used in move */
781
0
  if(pxp->slave != NULL)
782
0
  {
783
0
    if(pxp->slave->bf_base != NULL)
784
0
    {
785
0
      free(pxp->slave->bf_base);
786
0
      pxp->slave->bf_base = NULL;
787
0
      pxp->slave->bf_extent = 0;
788
0
      pxp->slave->bf_offset = OFF_NONE;
789
0
    }
790
0
    free(pxp->slave);
791
0
    pxp->slave = NULL;
792
0
  }
793
0
  return px_get(nciop, pxp, offset, extent, rflags, vpp);
794
0
}
795
796
797
/* ARGSUSED */
798
static int
799
px_double_buffer(ncio *const nciop, off_t to, off_t from,
800
      size_t nbytes, int rflags)
801
0
{
802
0
  ncio_px *const pxp = (ncio_px *)nciop->pvt;
803
0
  int status = NC_NOERR;
804
0
  void *src;
805
0
  void *dest;
806
0
  NC_UNUSED(rflags);
807
808
#if INSTRUMENT
809
fprintf(stderr, "\tdouble_buffr %ld %ld %ld\n",
810
     (long)to, (long)from, (long)nbytes);
811
#endif
812
0
  status = px_get(nciop, pxp, to, nbytes, RGN_WRITE,
813
0
      &dest);
814
0
  if(status != NC_NOERR)
815
0
    return status;
816
817
0
  if(pxp->slave == NULL)
818
0
  {
819
0
    pxp->slave = (ncio_px *) malloc(sizeof(ncio_px));
820
0
    if(pxp->slave == NULL)
821
0
      return ENOMEM;
822
823
0
    pxp->slave->blksz = pxp->blksz;
824
    /* pos done below */
825
0
    pxp->slave->bf_offset = pxp->bf_offset;
826
0
    pxp->slave->bf_extent = pxp->bf_extent;
827
0
    pxp->slave->bf_cnt = pxp->bf_cnt;
828
0
    pxp->slave->bf_base = malloc(2 * pxp->blksz);
829
0
    if(pxp->slave->bf_base == NULL)
830
0
      return ENOMEM;
831
0
    (void) memcpy(pxp->slave->bf_base, pxp->bf_base,
832
0
       pxp->bf_extent);
833
0
    pxp->slave->bf_rflags = 0;
834
0
    pxp->slave->bf_refcount = 0;
835
0
    pxp->slave->slave = NULL;
836
0
  }
837
838
0
  pxp->slave->pos = pxp->pos;
839
0
  status = px_get(nciop, pxp->slave, from, nbytes, 0,
840
0
      &src);
841
0
  if(status != NC_NOERR)
842
0
    return status;
843
0
  if(pxp->pos != pxp->slave->pos)
844
0
  {
845
    /* position changed, sync */
846
0
    pxp->pos = pxp->slave->pos;
847
0
  }
848
849
0
  (void) memcpy(dest, src, nbytes);
850
851
0
  (void)px_rel(pxp->slave, from, 0);
852
0
  (void)px_rel(pxp, to, RGN_MODIFIED);
853
854
0
  return status;
855
0
}
856
857
/* Like memmove(), safely move possibly overlapping data.
858
859
   Copy one region to another without making anything available to
860
   higher layers. May be just implemented in terms of get() and rel(),
861
   or may be tricky to be efficient. Only used in by nc_enddef()
862
   after redefinition.
863
864
   nciop - pointer to ncio struct with file info.
865
   to - src for move?
866
   from - dest for move?
867
   nbytes - number of bytes to move.
868
   rflags - One of the RGN_* flags defined in ncio.h. The only
869
   reasonable flag value is RGN_NOLOCK.
870
*/
871
static int
872
ncio_px_move(ncio *const nciop, off_t to, off_t from,
873
      size_t nbytes, int rflags)
874
0
{
875
0
  ncio_px *const pxp = (ncio_px *)nciop->pvt;
876
0
  int status = NC_NOERR;
877
0
  off_t lower;
878
0
  off_t upper;
879
0
  char *base;
880
0
  size_t diff;
881
0
  size_t extent;
882
883
0
  if(to == from)
884
0
    return NC_NOERR; /* NOOP */
885
886
0
  if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
887
0
    return EPERM; /* attempt to write readonly file */
888
889
0
  rflags &= RGN_NOLOCK; /* filter unwanted flags */
890
891
0
  if(to > from)
892
0
  {
893
    /* growing */
894
0
    lower = from;
895
0
    upper = to;
896
0
  }
897
0
  else
898
0
  {
899
    /* shrinking */
900
0
    lower = to;
901
0
    upper = from;
902
0
  }
903
0
  diff = (size_t)(upper - lower);
904
0
  extent = diff + nbytes;
905
906
#if INSTRUMENT
907
fprintf(stderr, "ncio_px_move %ld %ld %ld %ld %ld\n",
908
     (long)to, (long)from, (long)nbytes, (long)lower, (long)extent);
909
#endif
910
0
  if(extent > pxp->blksz)
911
0
  {
912
0
    size_t remaining = nbytes;
913
914
0
if(to > from)
915
0
{
916
0
    off_t frm = from + (off_t)nbytes;
917
0
    off_t toh = to + (off_t)nbytes;
918
0
    for(;;)
919
0
    {
920
0
      size_t loopextent = MIN(remaining, pxp->blksz);
921
0
      frm -= (off_t)loopextent;
922
0
                        toh -= (off_t)loopextent;
923
924
0
      status = px_double_buffer(nciop, toh, frm,
925
0
          loopextent, rflags) ;
926
0
      if(status != NC_NOERR)
927
0
        return status;
928
0
      remaining -= loopextent;
929
930
0
      if(remaining == 0)
931
0
        break; /* normal loop exit */
932
0
    }
933
0
}
934
0
else
935
0
{
936
0
    for(;;)
937
0
    {
938
0
      size_t loopextent = MIN(remaining, pxp->blksz);
939
940
0
      status = px_double_buffer(nciop, to, from,
941
0
          loopextent, rflags) ;
942
0
      if(status != NC_NOERR)
943
0
        return status;
944
0
      remaining -= loopextent;
945
946
0
      if(remaining == 0)
947
0
        break; /* normal loop exit */
948
0
      to += (off_t)loopextent;
949
0
                        from += (off_t)loopextent;
950
0
    }
951
0
}
952
0
    return NC_NOERR;
953
0
  }
954
955
#if INSTRUMENT
956
fprintf(stderr, "\tncio_px_move small\n");
957
#endif
958
0
  status = px_get(nciop, pxp, lower, extent, RGN_WRITE|rflags,
959
0
      (void **)&base);
960
961
0
  if(status != NC_NOERR)
962
0
    return status;
963
964
0
  if(to > from)
965
0
    (void) memmove(base + diff, base, nbytes);
966
0
  else
967
0
    (void) memmove(base, base + diff, nbytes);
968
969
0
  (void) px_rel(pxp, lower, RGN_MODIFIED);
970
971
0
  return status;
972
0
}
973
974
975
/* Flush any buffers to disk. May be a no-op on if I/O is unbuffered.
976
   This function is used when NC_SHARE is NOT used.
977
*/
978
static int
979
ncio_px_sync(ncio *const nciop)
980
0
{
981
0
  ncio_px *const pxp = (ncio_px *)nciop->pvt;
982
0
  int status = NC_NOERR;
983
0
  if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
984
0
  {
985
0
    assert(pxp->bf_refcount <= 0);
986
0
    status = px_pgout(nciop, pxp->bf_offset,
987
0
      pxp->bf_cnt,
988
0
      pxp->bf_base, &pxp->pos);
989
0
    if(status != NC_NOERR)
990
0
      return status;
991
0
    pxp->bf_rflags = 0;
992
0
  }
993
0
  else if (!fIsSet(pxp->bf_rflags, RGN_WRITE))
994
0
  {
995
      /*
996
       * The dataset is readonly.  Invalidate the buffers so
997
       * that the next ncio_px_get() will actually read data.
998
       */
999
0
      pxp->bf_offset = OFF_NONE;
1000
0
      pxp->bf_cnt = 0;
1001
0
  }
1002
0
  return status;
1003
0
}
1004
1005
/* Internal function called at close to
1006
   free up anything hanging off pvt.
1007
*/
1008
static void
1009
ncio_px_freepvt(void *const pvt)
1010
0
{
1011
0
  ncio_px *const pxp = (ncio_px *)pvt;
1012
0
  if(pxp == NULL)
1013
0
    return;
1014
1015
0
  if(pxp->slave != NULL)
1016
0
  {
1017
0
    if(pxp->slave->bf_base != NULL)
1018
0
    {
1019
0
      free(pxp->slave->bf_base);
1020
0
      pxp->slave->bf_base = NULL;
1021
0
      pxp->slave->bf_extent = 0;
1022
0
      pxp->slave->bf_offset = OFF_NONE;
1023
0
    }
1024
0
    free(pxp->slave);
1025
0
    pxp->slave = NULL;
1026
0
  }
1027
1028
0
  if(pxp->bf_base != NULL)
1029
0
  {
1030
0
    free(pxp->bf_base);
1031
0
    pxp->bf_base = NULL;
1032
0
    pxp->bf_extent = 0;
1033
0
    pxp->bf_offset = OFF_NONE;
1034
0
  }
1035
0
}
1036
1037
1038
/* This is the second half of the ncio initialization. This is called
1039
   after the file has actually been opened.
1040
1041
   The most important thing that happens is the allocation of a block
1042
   of memory at pxp->bf_base. This is going to be twice the size of
1043
   the chunksizehint (rounded up to the nearest sizeof(double)) passed
1044
   in from nc__create or nc__open. The rounded chunksizehint (passed
1045
   in here in sizehintp) is going to be stored as pxp->blksize.
1046
1047
   According to our "contract" we are not allowed to ask for an extent
1048
   larger than this chunksize/sizehint/blksize from the ncio get
1049
   function.
1050
1051
   nciop - pointer to the ncio struct
1052
   sizehintp - pointer to a size hint that will be rounded up and
1053
   passed back to the caller.
1054
   isNew - true if this is being called from ncio_create for a new
1055
   file.
1056
*/
1057
static int
1058
ncio_px_init2(ncio *const nciop, size_t *sizehintp, int isNew)
1059
0
{
1060
0
  ncio_px *const pxp = (ncio_px *)nciop->pvt;
1061
0
  const size_t bufsz = 2 * *sizehintp;
1062
1063
0
  assert(nciop->fd >= 0);
1064
1065
0
  pxp->blksz = *sizehintp;
1066
1067
0
  assert(pxp->bf_base == NULL);
1068
1069
  /* this is separate allocation because it may grow */
1070
0
  pxp->bf_base = malloc(bufsz);
1071
0
  if(pxp->bf_base == NULL)
1072
0
    return ENOMEM;
1073
  /* else */
1074
0
  pxp->bf_cnt = 0;
1075
0
  if(isNew)
1076
0
  {
1077
    /* save a read */
1078
0
    pxp->pos = 0;
1079
0
    pxp->bf_offset = 0;
1080
0
    pxp->bf_extent = bufsz;
1081
0
    (void) memset(pxp->bf_base, 0, pxp->bf_extent);
1082
0
  }
1083
0
  return NC_NOERR;
1084
0
}
1085
1086
1087
/* This is the first of a two-part initialization of the ncio struct.
1088
   Here the rel, get, move, sync, and free function pointers are set
1089
   to their POSIX non-NC_SHARE functions (ncio_px_*).
1090
1091
   The ncio_px struct is also partially initialized.
1092
*/
1093
static void
1094
ncio_px_init(ncio *const nciop)
1095
0
{
1096
0
  ncio_px *const pxp = (ncio_px *)nciop->pvt;
1097
1098
0
  *((ncio_relfunc **)&nciop->rel) = ncio_px_rel; /* cast away const */
1099
0
  *((ncio_getfunc **)&nciop->get) = ncio_px_get; /* cast away const */
1100
0
  *((ncio_movefunc **)&nciop->move) = ncio_px_move; /* cast away const */
1101
0
  *((ncio_syncfunc **)&nciop->sync) = ncio_px_sync; /* cast away const */
1102
0
  *((ncio_filesizefunc **)&nciop->filesize) = ncio_px_filesize; /* cast away const */
1103
0
  *((ncio_pad_lengthfunc **)&nciop->pad_length) = ncio_px_pad_length; /* cast away const */
1104
0
  *((ncio_closefunc **)&nciop->close) = ncio_px_close; /* cast away const */
1105
1106
0
  pxp->blksz = 0;
1107
0
  pxp->pos = -1;
1108
0
  pxp->bf_offset = OFF_NONE;
1109
0
  pxp->bf_extent = 0;
1110
0
  pxp->bf_rflags = 0;
1111
0
  pxp->bf_refcount = 0;
1112
0
  pxp->bf_base = NULL;
1113
0
  pxp->slave = NULL;
1114
1115
0
}
1116
1117
/* Begin spx */
1118
1119
/* This is the struct that gets hung of ncio->pvt(?) when the NC_SHARE
1120
   flag is used.
1121
*/
1122
typedef struct ncio_spx {
1123
  off_t pos;
1124
  /* buffer */
1125
  off_t bf_offset;
1126
  size_t  bf_extent;
1127
  size_t  bf_cnt;
1128
  void  *bf_base;
1129
} ncio_spx;
1130
1131
1132
/*ARGSUSED*/
1133
/* This function releases the region specified by offset.
1134
1135
   For POSIX system, with NC_SHARE, this becomes the rel function
1136
   pointed to by the ncio rel function pointer. It merely checks for
1137
   file write permission, then calls px_rel to do everything.
1138
1139
   nciop - pointer to ncio struct.
1140
1141
   offset - beginning of region.
1142
1143
   rflags - One of the RGN_* flags defined in ncio.h. If set to
1144
   RGN_MODIFIED it means that the data in this region were modified,
1145
   and it needs to be written out to the disk immediately (since we
1146
   are not buffering with NC_SHARE on).
1147
1148
*/
1149
static int
1150
ncio_spx_rel(ncio *const nciop, off_t offset, int rflags)
1151
0
{
1152
0
  ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
1153
0
  int status = NC_NOERR;
1154
1155
0
  assert(pxp->bf_offset <= offset);
1156
0
  assert(pxp->bf_cnt != 0);
1157
0
  assert(pxp->bf_cnt <= pxp->bf_extent);
1158
#ifdef X_ALIGN
1159
  assert(offset < pxp->bf_offset + X_ALIGN);
1160
  assert(pxp->bf_cnt % X_ALIGN == 0 );
1161
#endif
1162
0
  NC_UNUSED(offset);
1163
1164
0
  if(fIsSet(rflags, RGN_MODIFIED))
1165
0
  {
1166
0
    if(!fIsSet(nciop->ioflags, NC_WRITE))
1167
0
      return EPERM; /* attempt to write readonly file */
1168
1169
0
    status = px_pgout(nciop, pxp->bf_offset,
1170
0
      pxp->bf_cnt,
1171
0
      pxp->bf_base, &pxp->pos);
1172
    /* if error, invalidate buffer anyway */
1173
0
  }
1174
0
  pxp->bf_offset = OFF_NONE;
1175
0
  pxp->bf_cnt = 0;
1176
0
  return status;
1177
0
}
1178
1179
1180
/* Request that the region (offset, extent) be made available through
1181
   *vpp.
1182
1183
   This function converts a file region specified by an offset and
1184
   extent to a memory pointer. The region may be locked until the
1185
   corresponding call to rel().
1186
1187
   For POSIX systems, with NC_SHARE.
1188
1189
   nciop - pointer to ncio struct for this file.
1190
   offset - offset (from beginning of file?) to the data we want to
1191
   read.
1192
   extent - the number of bytes we want.
1193
   rflags - One of the RGN_* flags defined in ncio.h. May be RGN_NOLOCK.
1194
   vpp - handle to point at data when it's been read.
1195
*/
1196
static int
1197
ncio_spx_get(ncio *const nciop,
1198
    off_t offset, size_t extent,
1199
    int rflags,
1200
    void **const vpp)
1201
0
{
1202
0
  ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
1203
0
  int status = NC_NOERR;
1204
#ifdef X_ALIGN
1205
  size_t rem;
1206
#endif
1207
1208
0
  if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
1209
0
    return EPERM; /* attempt to write readonly file */
1210
1211
0
  assert(extent != 0);
1212
0
  assert(extent < X_INT_MAX); /* sanity check */
1213
1214
0
  assert(pxp->bf_cnt == 0);
1215
1216
#ifdef X_ALIGN
1217
  rem = (size_t)(offset % X_ALIGN);
1218
  if(rem != 0)
1219
  {
1220
    offset -= rem;
1221
    extent += rem;
1222
  }
1223
1224
  {
1225
      const size_t rndup = extent % X_ALIGN;
1226
      if(rndup != 0)
1227
        extent += X_ALIGN - rndup;
1228
  }
1229
1230
  assert(offset % X_ALIGN == 0);
1231
  assert(extent % X_ALIGN == 0);
1232
#endif
1233
1234
0
  if(pxp->bf_extent < extent)
1235
0
  {
1236
0
    if(pxp->bf_base != NULL)
1237
0
    {
1238
0
      free(pxp->bf_base);
1239
0
      pxp->bf_base = NULL;
1240
0
      pxp->bf_extent = 0;
1241
0
    }
1242
0
    assert(pxp->bf_extent == 0);
1243
0
    pxp->bf_base = malloc(extent+1);
1244
0
    if(pxp->bf_base == NULL)
1245
0
      return ENOMEM;
1246
0
    pxp->bf_extent = extent;
1247
0
  }
1248
1249
0
  status = px_pgin(nciop, offset,
1250
0
     extent,
1251
0
     pxp->bf_base,
1252
0
     &pxp->bf_cnt, &pxp->pos);
1253
0
  if(status != NC_NOERR)
1254
0
    return status;
1255
1256
0
  pxp->bf_offset = offset;
1257
1258
0
  if(pxp->bf_cnt < extent)
1259
0
    pxp->bf_cnt = extent;
1260
1261
#ifdef X_ALIGN
1262
  *vpp = (char *)pxp->bf_base + rem;
1263
#else
1264
0
  *vpp = pxp->bf_base;
1265
0
#endif
1266
0
  return NC_NOERR;
1267
0
}
1268
1269
1270
#if 0
1271
/*ARGSUSED*/
1272
static int
1273
strategy(ncio *const nciop, off_t to, off_t offset,
1274
      size_t extent, int rflags)
1275
{
1276
  static ncio_spx pxp[1];
1277
  int status = NC_NOERR;
1278
#ifdef X_ALIGN
1279
  size_t rem;
1280
#endif
1281
1282
  assert(extent != 0);
1283
  assert(extent < X_INT_MAX); /* sanity check */
1284
#if INSTRUMENT
1285
fprintf(stderr, "strategy %ld at %ld to %ld\n",
1286
   (long)extent, (long)offset, (long)to);
1287
#endif
1288
1289
1290
#ifdef X_ALIGN
1291
  rem = (size_t)(offset % X_ALIGN);
1292
  if(rem != 0)
1293
  {
1294
    offset -= rem;
1295
    extent += rem;
1296
  }
1297
1298
  {
1299
    const size_t rndup = extent % X_ALIGN;
1300
    if(rndup != 0)
1301
      extent += X_ALIGN - rndup;
1302
  }
1303
1304
  assert(offset % X_ALIGN == 0);
1305
  assert(extent % X_ALIGN == 0);
1306
#endif
1307
1308
  if(pxp->bf_extent < extent)
1309
  {
1310
    if(pxp->bf_base != NULL)
1311
    {
1312
      free(pxp->bf_base);
1313
      pxp->bf_base = NULL;
1314
      pxp->bf_extent = 0;
1315
    }
1316
    assert(pxp->bf_extent == 0);
1317
    pxp->bf_base = malloc(extent);
1318
    if(pxp->bf_base == NULL)
1319
      return ENOMEM;
1320
    pxp->bf_extent = extent;
1321
  }
1322
1323
  status = px_pgin(nciop, offset,
1324
     extent,
1325
     pxp->bf_base,
1326
     &pxp->bf_cnt, &pxp->pos);
1327
  if(status != NC_NOERR)
1328
    return status;
1329
1330
  pxp->bf_offset = to; /* TODO: XALIGN */
1331
1332
  if(pxp->bf_cnt < extent)
1333
    pxp->bf_cnt = extent;
1334
1335
  status = px_pgout(nciop, pxp->bf_offset,
1336
    pxp->bf_cnt,
1337
    pxp->bf_base, &pxp->pos);
1338
  /* if error, invalidate buffer anyway */
1339
  pxp->bf_offset = OFF_NONE;
1340
  pxp->bf_cnt = 0;
1341
  return status;
1342
}
1343
#endif
1344
1345
/* Copy one region to another without making anything available to
1346
   higher layers. May be just implemented in terms of get() and rel(),
1347
   or may be tricky to be efficient.  Only used in by nc_enddef()
1348
   after redefinition.
1349
1350
   nciop - pointer to ncio struct for this file.
1351
   to - dest for move?
1352
   from - src for move?
1353
   nbytes - number of bytes to move.
1354
   rflags - One of the RGN_* flags defined in ncio.h.
1355
*/
1356
static int
1357
ncio_spx_move(ncio *const nciop, off_t to, off_t from,
1358
      size_t nbytes, int rflags)
1359
0
{
1360
0
  int status = NC_NOERR;
1361
0
  off_t lower = from;
1362
0
  off_t upper = to;
1363
0
  char *base;
1364
0
  size_t diff;
1365
0
  size_t extent;
1366
1367
0
  rflags &= RGN_NOLOCK; /* filter unwanted flags */
1368
1369
0
  if(to == from)
1370
0
    return NC_NOERR; /* NOOP */
1371
1372
0
  if(to > from)
1373
0
  {
1374
    /* growing */
1375
0
    lower = from;
1376
0
    upper = to;
1377
0
  }
1378
0
  else
1379
0
  {
1380
    /* shrinking */
1381
0
    lower = to;
1382
0
    upper = from;
1383
0
  }
1384
1385
0
  diff = (size_t)(upper - lower);
1386
0
  extent = diff + nbytes;
1387
1388
0
  status = ncio_spx_get(nciop, lower, extent, RGN_WRITE|rflags,
1389
0
      (void **)&base);
1390
1391
0
  if(status != NC_NOERR)
1392
0
    return status;
1393
1394
0
  if(to > from)
1395
0
    (void) memmove(base + diff, base, nbytes);
1396
0
  else
1397
0
    (void) memmove(base, base + diff, nbytes);
1398
1399
0
  (void) ncio_spx_rel(nciop, lower, RGN_MODIFIED);
1400
1401
0
  return status;
1402
0
}
1403
1404
1405
/*ARGSUSED*/
1406
/* Flush any buffers to disk. May be a no-op on if I/O is unbuffered.
1407
*/
1408
static int
1409
ncio_spx_sync(ncio *const nciop)
1410
0
{
1411
0
  NC_UNUSED(nciop);
1412
  /* NOOP */
1413
0
  return NC_NOERR;
1414
0
}
1415
1416
static void
1417
ncio_spx_freepvt(void *const pvt)
1418
0
{
1419
0
  ncio_spx *const pxp = (ncio_spx *)pvt;
1420
0
  if(pxp == NULL)
1421
0
    return;
1422
1423
0
  if(pxp->bf_base != NULL)
1424
0
  {
1425
0
    free(pxp->bf_base);
1426
0
    pxp->bf_base = NULL;
1427
0
    pxp->bf_offset = OFF_NONE;
1428
0
    pxp->bf_extent = 0;
1429
0
    pxp->bf_cnt = 0;
1430
0
  }
1431
0
}
1432
1433
1434
/* This does the second half of the ncio_spx struct initialization for
1435
   POSIX systems, with NC_SHARE on.
1436
1437
   nciop - pointer to ncio struct for this file. File has been opened.
1438
   sizehintp - pointer to a size which will be rounded up to the
1439
   nearest 8-byt boundary and then used as the max size "chunk" (or
1440
   page) to read from the file.
1441
*/
1442
static int
1443
ncio_spx_init2(ncio *const nciop, const size_t *const sizehintp)
1444
0
{
1445
0
  ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
1446
1447
0
  assert(nciop->fd >= 0);
1448
1449
0
  pxp->bf_extent = *sizehintp;
1450
1451
0
  assert(pxp->bf_base == NULL);
1452
1453
  /* this is separate allocation because it may grow */
1454
0
  pxp->bf_base = malloc(pxp->bf_extent);
1455
0
  if(pxp->bf_base == NULL)
1456
0
  {
1457
0
    pxp->bf_extent = 0;
1458
0
    return ENOMEM;
1459
0
  }
1460
  /* else */
1461
0
  return NC_NOERR;
1462
0
}
1463
1464
1465
/* First half of init for ncio_spx struct, setting the rel, get, move,
1466
   sync, and free function pointers to the NC_SHARE versions of these
1467
   functions (i.e. the ncio_spx_* functions).
1468
*/
1469
static void
1470
ncio_spx_init(ncio *const nciop)
1471
0
{
1472
0
  ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
1473
1474
0
  *((ncio_relfunc **)&nciop->rel) = ncio_spx_rel; /* cast away const */
1475
0
  *((ncio_getfunc **)&nciop->get) = ncio_spx_get; /* cast away const */
1476
0
  *((ncio_movefunc **)&nciop->move) = ncio_spx_move; /* cast away const */
1477
0
  *((ncio_syncfunc **)&nciop->sync) = ncio_spx_sync; /* cast away const */
1478
  /* shared with _px_ */
1479
0
  *((ncio_filesizefunc **)&nciop->filesize) = ncio_px_filesize; /* cast away const */
1480
0
  *((ncio_pad_lengthfunc **)&nciop->pad_length) = ncio_px_pad_length; /* cast away const */
1481
0
  *((ncio_closefunc **)&nciop->close) = ncio_spx_close; /* cast away const */
1482
1483
0
  pxp->pos = -1;
1484
0
  pxp->bf_offset = OFF_NONE;
1485
0
  pxp->bf_extent = 0;
1486
0
  pxp->bf_cnt = 0;
1487
0
  pxp->bf_base = NULL;
1488
0
}
1489
1490
1491
/* */
1492
1493
/* This will call whatever free function is attached to the free
1494
   function pointer in ncio. It's called from ncio_close, and from
1495
   ncio_open and ncio_create when an error occurs that the file
1496
   metadata must be freed.
1497
*/
1498
static void
1499
ncio_px_free(ncio *nciop)
1500
0
{
1501
0
  if(nciop == NULL)
1502
0
    return;
1503
0
  if(nciop->pvt != NULL)
1504
0
    ncio_px_freepvt(nciop->pvt);
1505
0
  free(nciop);
1506
0
}
1507
1508
static void
1509
ncio_spx_free(ncio *nciop)
1510
0
{
1511
0
  if(nciop == NULL)
1512
0
    return;
1513
0
  if(nciop->pvt != NULL)
1514
0
    ncio_spx_freepvt(nciop->pvt);
1515
0
  free(nciop);
1516
0
}
1517
1518
1519
/* Create a new ncio struct to hold info about the file. This will
1520
   create and init the ncio_px or ncio_spx struct (the latter if
1521
   NC_SHARE is used.)
1522
*/
1523
static ncio *
1524
ncio_px_new(const char *path, int ioflags)
1525
0
{
1526
0
  size_t sz_ncio = M_RNDUP(sizeof(ncio));
1527
0
  size_t sz_path = M_RNDUP(strlen(path) +1);
1528
0
  size_t sz_ncio_pvt;
1529
0
  ncio *nciop;
1530
1531
#if ALWAYS_NC_SHARE /* DEBUG */
1532
  fSet(ioflags, NC_SHARE);
1533
#endif
1534
1535
0
  if(fIsSet(ioflags, NC_SHARE))
1536
0
    sz_ncio_pvt = sizeof(ncio_spx);
1537
0
  else
1538
0
    sz_ncio_pvt = sizeof(ncio_px);
1539
1540
0
  nciop = (ncio *) malloc(sz_ncio + sz_path + sz_ncio_pvt);
1541
0
  if(nciop == NULL)
1542
0
    return NULL;
1543
1544
0
  nciop->ioflags = ioflags;
1545
0
  *((int *)&nciop->fd) = -1; /* cast away const */
1546
1547
0
  nciop->path = (char *) ((char *)nciop + sz_ncio);
1548
0
  (void) strcpy((char *)nciop->path, path); /* cast away const */
1549
1550
        /* cast away const */
1551
0
  *((void **)&nciop->pvt) = (void *)(nciop->path + sz_path);
1552
1553
0
  if(fIsSet(ioflags, NC_SHARE))
1554
0
    ncio_spx_init(nciop);
1555
0
  else
1556
0
    ncio_px_init(nciop);
1557
1558
0
  return nciop;
1559
0
}
1560
1561
1562
/* Public below this point */
1563
#ifndef NCIO_MINBLOCKSIZE
1564
#define NCIO_MINBLOCKSIZE 256
1565
#endif
1566
#ifndef NCIO_MAXBLOCKSIZE
1567
0
#define NCIO_MAXBLOCKSIZE 268435456 /* sanity check, about X_SIZE_T_MAX/8 */
1568
#endif
1569
1570
#ifdef S_IRUSR
1571
#define NC_DEFAULT_CREAT_MODE \
1572
        (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) /* 0666 */
1573
1574
#else
1575
#define NC_DEFAULT_CREAT_MODE 0666
1576
#endif
1577
1578
/* Create a file, and the ncio struct to go with it. This function is
1579
   only called from nc__create_mp.
1580
1581
   path - path of file to create.
1582
   ioflags - flags from nc_create
1583
   initialsz - From the netcdf man page: "The argument
1584
   Iinitialsize sets the initial size of the file at creation time."
1585
   igeto -
1586
   igetsz -
1587
   sizehintp - this eventually goes into pxp->blksz and is the size of
1588
   a page of data for buffered reads and writes.
1589
   nciopp - pointer to a pointer that will get location of newly
1590
   created and inited ncio struct.
1591
   igetvpp - pointer to pointer which will get the location of ?
1592
*/
1593
int
1594
posixio_create(const char *path, int ioflags,
1595
  size_t initialsz,
1596
  off_t igeto, size_t igetsz, size_t *sizehintp,
1597
  void* parameters,
1598
  ncio **nciopp, void **const igetvpp)
1599
0
{
1600
0
  ncio *nciop;
1601
0
  int oflags = (O_RDWR|O_CREAT);
1602
0
  int fd;
1603
0
  int status;
1604
0
  NC_UNUSED(parameters);
1605
1606
0
  if(initialsz < (size_t)igeto + igetsz)
1607
0
    initialsz = (size_t)igeto + igetsz;
1608
1609
0
  fSet(ioflags, NC_WRITE);
1610
1611
0
  if(path == NULL || *path == 0)
1612
0
    return EINVAL;
1613
1614
0
  nciop = ncio_px_new(path, ioflags);
1615
0
  if(nciop == NULL)
1616
0
    return ENOMEM;
1617
1618
0
  if(fIsSet(ioflags, NC_NOCLOBBER))
1619
0
    fSet(oflags, O_EXCL);
1620
0
  else
1621
0
    fSet(oflags, O_TRUNC);
1622
#ifdef O_BINARY
1623
  fSet(oflags, O_BINARY);
1624
#endif
1625
#ifdef vms
1626
  fd = NCopen3(path, oflags, NC_DEFAULT_CREAT_MODE, "ctx=stm");
1627
#else
1628
  /* Should we mess with the mode based on NC_SHARE ?? */
1629
0
  fd = NCopen3(path, oflags, NC_DEFAULT_CREAT_MODE);
1630
0
#endif
1631
#if 0
1632
  (void) fprintf(stderr, "ncio_create(): path=\"%s\"\n", path);
1633
  (void) fprintf(stderr, "ncio_create(): oflags=0x%x\n", oflags);
1634
#endif
1635
0
  if(fd < 0)
1636
0
  {
1637
0
    status = errno ? errno : ENOENT;
1638
0
    goto unwind_new;
1639
0
  }
1640
0
  *((int *)&nciop->fd) = fd; /* cast away const */
1641
1642
0
  if(*sizehintp < NCIO_MINBLOCKSIZE)
1643
0
  {
1644
    /* Use default */
1645
0
    *sizehintp = blksize(fd);
1646
0
  }
1647
0
  else if(*sizehintp >= NCIO_MAXBLOCKSIZE)
1648
0
  {
1649
    /* Use maximum allowed value */
1650
0
    *sizehintp = NCIO_MAXBLOCKSIZE;
1651
0
  }
1652
0
  else
1653
0
  {
1654
0
    *sizehintp = M_RNDUP(*sizehintp);
1655
0
  }
1656
1657
0
  if(fIsSet(nciop->ioflags, NC_SHARE))
1658
0
    status = ncio_spx_init2(nciop, sizehintp);
1659
0
  else
1660
0
    status = ncio_px_init2(nciop, sizehintp, 1);
1661
1662
0
  if(status != NC_NOERR)
1663
0
    goto unwind_open;
1664
1665
0
  if(initialsz != 0)
1666
0
  {
1667
0
    status = fgrow(fd, (off_t)initialsz);
1668
0
    if(status != NC_NOERR)
1669
0
      goto unwind_open;
1670
0
  }
1671
1672
0
  if(igetsz != 0)
1673
0
  {
1674
0
    status = nciop->get(nciop,
1675
0
        igeto, igetsz,
1676
0
                          RGN_WRITE,
1677
0
                          igetvpp);
1678
0
    if(status != NC_NOERR)
1679
0
      goto unwind_open;
1680
0
  }
1681
1682
0
  *nciopp = nciop;
1683
0
  return NC_NOERR;
1684
1685
0
unwind_open:
1686
0
  (void) close(fd);
1687
  /* ?? unlink */
1688
  /*FALLTHRU*/
1689
0
unwind_new:
1690
0
  ncio_close(nciop,!fIsSet(ioflags, NC_NOCLOBBER));
1691
0
  return status;
1692
0
}
1693
1694
1695
/* This function opens the data file. It is only called from nc.c,
1696
   from nc__open_mp and nc_delete_mp.
1697
1698
   path - path of data file.
1699
1700
   ioflags - flags passed into nc_open.
1701
1702
   igeto - looks like this function can do an initial page get, and
1703
   igeto is going to be the offset for that. But it appears to be
1704
   unused
1705
1706
   igetsz - the size in bytes of initial page get (a.k.a. extent). Not
1707
   ever used in the library.
1708
1709
   sizehintp - pointer to sizehint parameter from nc__open or
1710
   nc__create. This is used to set pxp->blksz.
1711
1712
   Here's what the man page has to say:
1713
1714
   "The argument referenced by chunksize controls a space versus time
1715
   tradeoff, memory allocated in the netcdf library versus number of
1716
   system calls.
1717
1718
   Because of internal requirements, the value may not be set to
1719
   exactly the value requested. The actual value chosen is returned by reference.
1720
1721
   Using the value NC_SIZEHINT_DEFAULT causes the library to choose a
1722
   default. How the system choses the default depends on the
1723
   system. On many systems, the "preferred I/O block size" is
1724
   available from the stat() system call, struct stat member
1725
   st_blksize. If this is available it is used. Lacking that, twice
1726
   the system pagesize is used. Lacking a call to discover the system
1727
   pagesize, we just set default chunksize to 8192.
1728
1729
   The chunksize is a property of a given open netcdf descriptor ncid,
1730
   it is not a persistent property of the netcdf dataset."
1731
1732
   nciopp - pointer to pointer that will get address of newly created
1733
   and inited ncio struct.
1734
1735
   igetvpp - handle to pass back pointer to data from initial page
1736
   read, if this were ever used, which it isn't.
1737
*/
1738
int
1739
posixio_open(const char *path,
1740
  int ioflags,
1741
  off_t igeto, size_t igetsz, size_t *sizehintp,
1742
        void* parameters,
1743
  ncio **nciopp, void **const igetvpp)
1744
0
{
1745
0
  ncio *nciop;
1746
0
  int oflags = fIsSet(ioflags, NC_WRITE) ? O_RDWR : O_RDONLY;
1747
0
  int fd = -1;
1748
0
  int status = 0;
1749
0
  NC_UNUSED(parameters);
1750
1751
0
  if(path == NULL || *path == 0)
1752
0
    return EINVAL;
1753
1754
0
  nciop = ncio_px_new(path, ioflags);
1755
0
  if(nciop == NULL)
1756
0
    return ENOMEM;
1757
1758
#ifdef O_BINARY
1759
  /*#if _MSC_VER*/
1760
  fSet(oflags, O_BINARY);
1761
#endif
1762
1763
#ifdef vms
1764
  fd = NCopen3(path, oflags, 0, "ctx=stm");
1765
#else
1766
0
  fd = NCopen3(path, oflags, 0);
1767
0
#endif
1768
0
  if(fd < 0)
1769
0
  {
1770
0
    status = errno ? errno : ENOENT;
1771
0
    goto unwind_new;
1772
0
  }
1773
0
  *((int *)&nciop->fd) = fd; /* cast away const */
1774
1775
0
  if(*sizehintp < NCIO_MINBLOCKSIZE)
1776
0
  {
1777
    /* Use default */
1778
0
    *sizehintp = blksize(fd);
1779
0
  }
1780
0
  else if(*sizehintp >= NCIO_MAXBLOCKSIZE)
1781
0
  {
1782
    /* Use maximum allowed value */
1783
0
    *sizehintp = NCIO_MAXBLOCKSIZE;
1784
0
  }
1785
0
  else
1786
0
  {
1787
0
    *sizehintp = M_RNDUP(*sizehintp);
1788
0
  }
1789
1790
0
  if(fIsSet(nciop->ioflags, NC_SHARE))
1791
0
    status = ncio_spx_init2(nciop, sizehintp);
1792
0
  else
1793
0
    status = ncio_px_init2(nciop, sizehintp, 0);
1794
1795
0
  if(status != NC_NOERR)
1796
0
    goto unwind_open;
1797
1798
0
  if(igetsz != 0)
1799
0
  {
1800
0
    status = nciop->get(nciop,
1801
0
        igeto, igetsz,
1802
0
                          0,
1803
0
                          igetvpp);
1804
0
    if(status != NC_NOERR)
1805
0
      goto unwind_open;
1806
0
  }
1807
1808
0
  *nciopp = nciop;
1809
0
  return NC_NOERR;
1810
1811
0
unwind_open:
1812
0
  (void) close(fd); /* assert fd >= 0 */
1813
  /*FALLTHRU*/
1814
0
unwind_new:
1815
0
  ncio_close(nciop,0);
1816
0
  return status;
1817
0
}
1818
1819
/*
1820
 * Get file size in bytes.
1821
 */
1822
static int
1823
ncio_px_filesize(ncio *nciop, off_t *filesizep)
1824
0
{
1825
1826
1827
  /* There is a problem with fstat on Windows based systems
1828
    which manifests (so far) when Config RELEASE is built.
1829
    Use _filelengthi64 isntead. */
1830
#ifdef HAVE_FILE_LENGTH_I64
1831
1832
  int64_t file_len = 0;
1833
  if( (file_len = _filelengthi64(nciop->fd)) < 0) {
1834
    return errno;
1835
  }
1836
1837
  *filesizep = file_len;
1838
1839
#else
1840
0
    struct stat sb;
1841
0
    assert(nciop != NULL);
1842
0
    if (fstat(nciop->fd, &sb) < 0)
1843
0
  return errno;
1844
0
    *filesizep = sb.st_size;
1845
0
#endif
1846
0
  return NC_NOERR;
1847
0
}
1848
1849
/*
1850
 * Sync any changes to disk, then truncate or extend file so its size
1851
 * is length.  This is only intended to be called before close, if the
1852
 * file is open for writing and the actual size does not match the
1853
 * calculated size, perhaps as the result of having been previously
1854
 * written in NOFILL mode.
1855
 */
1856
static int
1857
ncio_px_pad_length(ncio *nciop, off_t length)
1858
0
{
1859
1860
0
  int status = NC_NOERR;
1861
1862
0
  if(nciop == NULL)
1863
0
    return EINVAL;
1864
1865
0
  if(!fIsSet(nciop->ioflags, NC_WRITE))
1866
0
          return EPERM; /* attempt to write readonly file */
1867
1868
0
  status = nciop->sync(nciop);
1869
0
  if(status != NC_NOERR)
1870
0
          return status;
1871
1872
0
  status = fgrow2(nciop->fd, length);
1873
0
  if(status != NC_NOERR)
1874
0
          return status;
1875
0
  return NC_NOERR;
1876
0
}
1877
1878
1879
/* Write out any dirty buffers to disk and
1880
   ensure that next read will get data from disk.
1881
1882
   Sync any changes, then close the open file associated with the ncio
1883
   struct, and free its memory.
1884
1885
   nciop - pointer to ncio to close.
1886
1887
   doUnlink - if true, unlink file
1888
*/
1889
static int
1890
ncio_px_close(ncio *nciop, int doUnlink)
1891
0
{
1892
0
  int status = NC_NOERR;
1893
0
  if(nciop == NULL)
1894
0
    return EINVAL;
1895
0
  if(nciop->fd > 0) {
1896
0
      status = nciop->sync(nciop);
1897
0
      (void) close(nciop->fd);
1898
0
  }
1899
0
  if(doUnlink)
1900
0
    (void) unlink(nciop->path);
1901
0
  ncio_px_free(nciop);
1902
0
  return status;
1903
0
}
1904
1905
static int
1906
ncio_spx_close(ncio *nciop, int doUnlink)
1907
0
{
1908
0
  int status = NC_NOERR;
1909
0
  if(nciop == NULL)
1910
0
    return EINVAL;
1911
0
  if(nciop->fd > 0) {
1912
0
      status = nciop->sync(nciop);
1913
0
      (void) close(nciop->fd);
1914
0
  }
1915
0
  if(doUnlink)
1916
0
    (void) unlink(nciop->path);
1917
0
  ncio_spx_free(nciop);
1918
0
  return status;
1919
0
}