Coverage Report

Created: 2023-05-28 06:42

/src/netcdf-c/libsrc/nc3internal.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Copyright 2018, Unuiversity Corporation for Atmospheric Research
3
 *      See netcdf/COPYRIGHT file for copying and redistribution conditions.
4
 */
5
6
#if HAVE_CONFIG_H
7
#include <config.h>
8
#endif
9
10
#include <stdlib.h>
11
#include <string.h>
12
#include <assert.h>
13
#ifdef HAVE_UNISTD_H
14
#include <unistd.h>
15
#endif
16
17
#include "nc3internal.h"
18
#include "netcdf_mem.h"
19
#include "rnd.h"
20
#include "ncx.h"
21
#include "ncrc.h"
22
23
/* These have to do with version numbers. */
24
#define MAGIC_NUM_LEN 4
25
#define VER_CLASSIC 1
26
#define VER_64BIT_OFFSET 2
27
#define VER_HDF5 3
28
29
0
#define NC_NUMRECS_OFFSET 4
30
31
/* For netcdf classic */
32
0
#define NC_NUMRECS_EXTENT3 4
33
/* For cdf5 */
34
0
#define NC_NUMRECS_EXTENT5 8
35
36
/* Internal function; breaks ncio abstraction */
37
extern int memio_extract(ncio* const nciop, size_t* sizep, void** memoryp);
38
39
static void
40
free_NC3INFO(NC3_INFO *nc3)
41
104
{
42
104
  if(nc3 == NULL)
43
0
    return;
44
104
  free_NC_dimarrayV(&nc3->dims);
45
104
  free_NC_attrarrayV(&nc3->attrs);
46
104
  free_NC_vararrayV(&nc3->vars);
47
104
  free(nc3);
48
104
}
49
50
static NC3_INFO *
51
new_NC3INFO(const size_t *chunkp)
52
104
{
53
104
  NC3_INFO *ncp;
54
104
  ncp = (NC3_INFO*)calloc(1,sizeof(NC3_INFO));
55
104
  if(ncp == NULL) return ncp;
56
104
        ncp->chunk = chunkp != NULL ? *chunkp : NC_SIZEHINT_DEFAULT;
57
  /* Note that ncp->xsz is not set yet because we do not know the file format */
58
104
  return ncp;
59
104
}
60
61
static NC3_INFO *
62
dup_NC3INFO(const NC3_INFO *ref)
63
0
{
64
0
  NC3_INFO *ncp;
65
0
  ncp = (NC3_INFO*)calloc(1,sizeof(NC3_INFO));
66
0
  if(ncp == NULL) return ncp;
67
68
0
  if(dup_NC_dimarrayV(&ncp->dims, &ref->dims) != NC_NOERR)
69
0
    goto err;
70
0
  if(dup_NC_attrarrayV(&ncp->attrs, &ref->attrs) != NC_NOERR)
71
0
    goto err;
72
0
  if(dup_NC_vararrayV(&ncp->vars, &ref->vars) != NC_NOERR)
73
0
    goto err;
74
75
0
  ncp->xsz = ref->xsz;
76
0
  ncp->begin_var = ref->begin_var;
77
0
  ncp->begin_rec = ref->begin_rec;
78
0
  ncp->recsize = ref->recsize;
79
0
  NC_set_numrecs(ncp, NC_get_numrecs(ref));
80
0
  return ncp;
81
0
err:
82
0
  free_NC3INFO(ncp);
83
0
  return NULL;
84
0
}
85
86
87
/*
88
 *  Verify that this is a user nc_type
89
 * Formerly NCcktype()
90
 * Sense of the return is changed.
91
 */
92
int
93
nc3_cktype(int mode, nc_type type)
94
0
{
95
0
#ifdef ENABLE_CDF5
96
0
    if (mode & NC_CDF5) { /* CDF-5 format */
97
0
        if (type >= NC_BYTE && type < NC_STRING) return NC_NOERR;
98
0
    } else
99
0
#endif
100
0
      if (mode & NC_64BIT_OFFSET) { /* CDF-2 format */
101
0
        if (type >= NC_BYTE && type <= NC_DOUBLE) return NC_NOERR;
102
0
    } else if ((mode & NC_64BIT_OFFSET) == 0) { /* CDF-1 format */
103
0
        if (type >= NC_BYTE && type <= NC_DOUBLE) return NC_NOERR;
104
0
    }
105
0
    return(NC_EBADTYPE);
106
0
}
107
108
109
/*
110
 * How many objects of 'type'
111
 * will fit into xbufsize?
112
 */
113
size_t
114
ncx_howmany(nc_type type, size_t xbufsize)
115
0
{
116
0
  switch(type){
117
0
  case NC_BYTE:
118
0
  case NC_CHAR:
119
0
    return xbufsize;
120
0
  case NC_SHORT:
121
0
    return xbufsize/X_SIZEOF_SHORT;
122
0
  case NC_INT:
123
0
    return xbufsize/X_SIZEOF_INT;
124
0
  case NC_FLOAT:
125
0
    return xbufsize/X_SIZEOF_FLOAT;
126
0
  case NC_DOUBLE:
127
0
    return xbufsize/X_SIZEOF_DOUBLE;
128
0
  case NC_UBYTE:
129
0
    return xbufsize;
130
0
  case NC_USHORT:
131
0
    return xbufsize/X_SIZEOF_USHORT;
132
0
  case NC_UINT:
133
0
    return xbufsize/X_SIZEOF_UINT;
134
0
  case NC_INT64:
135
0
    return xbufsize/X_SIZEOF_LONGLONG;
136
0
  case NC_UINT64:
137
0
    return xbufsize/X_SIZEOF_ULONGLONG;
138
0
  default:
139
0
          assert("ncx_howmany: Bad type" == 0);
140
0
    return(0);
141
0
  }
142
0
}
143
144
0
#define D_RNDUP(x, align) _RNDUP(x, (off_t)(align))
145
146
/*
147
 * Compute each variable's 'begin' offset,
148
 * update 'begin_rec' as well.
149
 */
150
static int
151
NC_begins(NC3_INFO* ncp,
152
  size_t h_minfree, size_t v_align,
153
  size_t v_minfree, size_t r_align)
154
0
{
155
0
  size_t ii, j;
156
0
  int sizeof_off_t;
157
0
  off_t index = 0;
158
0
  off_t old_ncp_begin_var;
159
0
  NC_var **vpp;
160
0
  NC_var *last = NULL;
161
0
  NC_var *first_var = NULL;       /* first "non-record" var */
162
163
164
0
  if(v_align == NC_ALIGN_CHUNK)
165
0
    v_align = ncp->chunk;
166
0
  if(r_align == NC_ALIGN_CHUNK)
167
0
    r_align = ncp->chunk;
168
169
0
  if (fIsSet(ncp->flags, NC_64BIT_OFFSET) || fIsSet(ncp->flags, NC_64BIT_DATA)) {
170
0
    sizeof_off_t = 8;
171
0
  } else {
172
0
    sizeof_off_t = 4;
173
0
  }
174
175
0
  ncp->xsz = ncx_len_NC(ncp,sizeof_off_t);
176
177
0
  if(ncp->vars.nelems == 0)
178
0
    return NC_NOERR;
179
180
0
        old_ncp_begin_var = ncp->begin_var;
181
182
  /* only (re)calculate begin_var if there is not sufficient space in header
183
     or start of non-record variables is not aligned as requested by valign */
184
0
  if (ncp->begin_var < ncp->xsz + h_minfree ||
185
0
      ncp->begin_var != D_RNDUP(ncp->begin_var, v_align) )
186
0
  {
187
0
    index = (off_t) ncp->xsz;
188
0
    ncp->begin_var = D_RNDUP(index, v_align);
189
0
    if(ncp->begin_var < index + h_minfree)
190
0
    {
191
0
      ncp->begin_var = D_RNDUP(index + (off_t)h_minfree, v_align);
192
0
    }
193
0
  }
194
195
0
  if (ncp->old != NULL) {
196
            /* check whether the new begin_var is smaller */
197
0
            if (ncp->begin_var < ncp->old->begin_var)
198
0
                ncp->begin_var = ncp->old->begin_var;
199
0
  }
200
201
0
  index = ncp->begin_var;
202
203
  /* loop thru vars, first pass is for the 'non-record' vars */
204
0
  j = 0;
205
0
  vpp = ncp->vars.value;
206
0
  for(ii = 0; ii < ncp->vars.nelems ; ii++, vpp++)
207
0
  {
208
0
    if( IS_RECVAR(*vpp) )
209
0
    {
210
      /* skip record variables on this pass */
211
0
      continue;
212
0
    }
213
0
    if (first_var == NULL) first_var = *vpp;
214
215
#if 0
216
fprintf(stderr, "    VAR %d %s: %ld\n", ii, (*vpp)->name->cp, (long)index);
217
#endif
218
0
                if( sizeof_off_t == 4 && (index > X_OFF_MAX || index < 0) )
219
0
    {
220
0
                    ncp->begin_var = old_ncp_begin_var;
221
0
        return NC_EVARSIZE;
222
0
                }
223
0
    (*vpp)->begin = index;
224
225
0
    if (ncp->old != NULL) {
226
          /* move to the next fixed variable */
227
0
          for (; j<ncp->old->vars.nelems; j++) {
228
0
            if (!IS_RECVAR(ncp->old->vars.value[j]))
229
0
              break;
230
0
          }
231
232
0
          if (j < ncp->old->vars.nelems) {
233
0
            if ((*vpp)->begin < ncp->old->vars.value[j]->begin) {
234
              /* the first ncp->vars.nelems fixed variables
235
                 should be the same. If the new begin is smaller,
236
                 reuse the old begin */
237
0
              (*vpp)->begin = ncp->old->vars.value[j]->begin;
238
0
              index = (*vpp)->begin;
239
0
            }
240
0
            j++;
241
0
          }
242
0
    }
243
244
0
    index += (*vpp)->len;
245
0
  }
246
247
0
  if (ncp->old != NULL) {
248
      /* check whether the new begin_rec is smaller */
249
0
      if (ncp->begin_rec < ncp->old->begin_rec)
250
0
          ncp->begin_rec = ncp->old->begin_rec;
251
0
  }
252
253
  /* only (re)calculate begin_rec if there is not sufficient
254
     space at end of non-record variables or if start of record
255
     variables is not aligned as requested by r_align */
256
0
  if (ncp->begin_rec < index + v_minfree ||
257
0
      ncp->begin_rec != D_RNDUP(ncp->begin_rec, r_align) )
258
0
  {
259
0
    ncp->begin_rec = D_RNDUP(index, r_align);
260
0
    if(ncp->begin_rec < index + v_minfree)
261
0
    {
262
0
      ncp->begin_rec = D_RNDUP(index + (off_t)v_minfree, r_align);
263
0
    }
264
0
  }
265
266
0
  if (first_var != NULL)
267
0
      ncp->begin_var = first_var->begin;
268
0
  else
269
0
      ncp->begin_var = ncp->begin_rec;
270
271
0
  index = ncp->begin_rec;
272
273
0
  ncp->recsize = 0;
274
275
  /* loop thru vars, second pass is for the 'record' vars */
276
0
  j = 0;
277
0
  vpp = (NC_var **)ncp->vars.value;
278
0
  for(ii = 0; ii < ncp->vars.nelems; ii++, vpp++)
279
0
  {
280
0
    if( !IS_RECVAR(*vpp) )
281
0
    {
282
      /* skip non-record variables on this pass */
283
0
      continue;
284
0
    }
285
286
#if 0
287
fprintf(stderr, "    REC %d %s: %ld\n", ii, (*vpp)->name->cp, (long)index);
288
#endif
289
0
                if( sizeof_off_t == 4 && (index > X_OFF_MAX || index < 0) )
290
0
    {
291
0
                    ncp->begin_var = old_ncp_begin_var;
292
0
        return NC_EVARSIZE;
293
0
                }
294
0
    (*vpp)->begin = index;
295
296
0
                if (ncp->old != NULL) {
297
                    /* move to the next record variable */
298
0
                    for (; j<ncp->old->vars.nelems; j++)
299
0
                        if (IS_RECVAR(ncp->old->vars.value[j]))
300
0
                            break;
301
0
                    if (j < ncp->old->vars.nelems) {
302
0
                        if ((*vpp)->begin < ncp->old->vars.value[j]->begin)
303
                            /* if the new begin is smaller, use the old begin */
304
0
                            (*vpp)->begin = ncp->old->vars.value[j]->begin;
305
0
                        j++;
306
0
                    }
307
0
                }
308
309
0
    index += (*vpp)->len;
310
    /* check if record size must fit in 32-bits */
311
#if SIZEOF_OFF_T == SIZEOF_SIZE_T && SIZEOF_SIZE_T == 4
312
    if( ncp->recsize > X_UINT_MAX - (*vpp)->len )
313
    {
314
                    ncp->begin_var = old_ncp_begin_var;
315
        return NC_EVARSIZE;
316
    }
317
#endif
318
0
    ncp->recsize += (*vpp)->len;
319
0
    last = (*vpp);
320
0
  }
321
322
    /*
323
     * for special case (Check CDF-1 and CDF-2 file format specifications.)
324
     * "A special case: Where there is exactly one record variable, we drop the
325
     * requirement that each record be four-byte aligned, so in this case there
326
     * is no record padding."
327
     */
328
0
    if (last != NULL) {
329
0
        if (ncp->recsize == last->len) {
330
            /* exactly one record variable, pack value */
331
0
            ncp->recsize = *last->dsizes * last->xsz;
332
0
        }
333
0
    }
334
335
0
  if(NC_IsNew(ncp))
336
0
    NC_set_numrecs(ncp, 0);
337
0
  return NC_NOERR;
338
0
}
339
340
341
/*
342
 * Read just the numrecs member.
343
 * (A relatively expensive way to do things.)
344
 */
345
int
346
read_numrecs(NC3_INFO *ncp)
347
0
{
348
0
  int status = NC_NOERR;
349
0
  const void *xp = NULL;
350
0
  size_t new_nrecs = 0;
351
0
  size_t  old_nrecs = NC_get_numrecs(ncp);
352
0
  size_t nc_numrecs_extent = NC_NUMRECS_EXTENT3; /* CDF-1 and CDF-2 */
353
354
0
  assert(!NC_indef(ncp));
355
356
0
  if (fIsSet(ncp->flags, NC_64BIT_DATA))
357
0
    nc_numrecs_extent = NC_NUMRECS_EXTENT5; /* CDF-5 */
358
359
0
  status = ncio_get(ncp->nciop,
360
0
     NC_NUMRECS_OFFSET, nc_numrecs_extent, 0, (void **)&xp);/* cast away const */
361
0
  if(status != NC_NOERR)
362
0
    return status;
363
364
0
  if (fIsSet(ncp->flags, NC_64BIT_DATA)) {
365
0
      unsigned long long tmp=0;
366
0
      status = ncx_get_uint64(&xp, &tmp);
367
0
      new_nrecs = (size_t)tmp;
368
0
        } else
369
0
      status = ncx_get_size_t(&xp, &new_nrecs);
370
371
0
  (void) ncio_rel(ncp->nciop, NC_NUMRECS_OFFSET, 0);
372
373
0
  if(status == NC_NOERR && old_nrecs != new_nrecs)
374
0
  {
375
0
    NC_set_numrecs(ncp, new_nrecs);
376
0
    fClr(ncp->flags, NC_NDIRTY);
377
0
  }
378
379
0
  return status;
380
0
}
381
382
383
/*
384
 * Write out just the numrecs member.
385
 * (A relatively expensive way to do things.)
386
 */
387
int
388
write_numrecs(NC3_INFO *ncp)
389
0
{
390
0
  int status = NC_NOERR;
391
0
  void *xp = NULL;
392
0
  size_t nc_numrecs_extent = NC_NUMRECS_EXTENT3; /* CDF-1 and CDF-2 */
393
394
0
  assert(!NC_readonly(ncp));
395
0
  assert(!NC_indef(ncp));
396
397
0
  if (fIsSet(ncp->flags, NC_64BIT_DATA))
398
0
      nc_numrecs_extent = NC_NUMRECS_EXTENT5; /* CDF-5 */
399
400
0
  status = ncio_get(ncp->nciop,
401
0
     NC_NUMRECS_OFFSET, nc_numrecs_extent, RGN_WRITE, &xp);
402
0
  if(status != NC_NOERR)
403
0
    return status;
404
405
0
  {
406
0
    const size_t nrecs = NC_get_numrecs(ncp);
407
0
    if (fIsSet(ncp->flags, NC_64BIT_DATA))
408
0
        status = ncx_put_uint64(&xp, (unsigned long long)nrecs);
409
0
    else
410
0
        status = ncx_put_size_t(&xp, &nrecs);
411
0
  }
412
413
0
  (void) ncio_rel(ncp->nciop, NC_NUMRECS_OFFSET, RGN_MODIFIED);
414
415
0
  if(status == NC_NOERR)
416
0
    fClr(ncp->state, NC_NDIRTY);
417
418
0
  return status;
419
0
}
420
421
422
/*
423
 * Read in the header
424
 * It is expensive.
425
 */
426
static int
427
read_NC(NC3_INFO *ncp)
428
0
{
429
0
  int status = NC_NOERR;
430
431
0
  free_NC_dimarrayV(&ncp->dims);
432
0
  free_NC_attrarrayV(&ncp->attrs);
433
0
  free_NC_vararrayV(&ncp->vars);
434
435
0
  status = nc_get_NC(ncp);
436
437
0
  if(status == NC_NOERR)
438
0
    fClr(ncp->state, NC_NDIRTY | NC_HDIRTY);
439
440
0
  return status;
441
0
}
442
443
444
/*
445
 * Write out the header
446
 */
447
static int
448
write_NC(NC3_INFO *ncp)
449
0
{
450
0
  int status = NC_NOERR;
451
452
0
  assert(!NC_readonly(ncp));
453
454
0
  status = ncx_put_NC(ncp, NULL, 0, 0);
455
456
0
  if(status == NC_NOERR)
457
0
    fClr(ncp->state, NC_NDIRTY | NC_HDIRTY);
458
459
0
  return status;
460
0
}
461
462
463
/*
464
 * Write the header or the numrecs if necessary.
465
 */
466
int
467
NC_sync(NC3_INFO *ncp)
468
0
{
469
0
  assert(!NC_readonly(ncp));
470
471
0
  if(NC_hdirty(ncp))
472
0
  {
473
0
    return write_NC(ncp);
474
0
  }
475
  /* else */
476
477
0
  if(NC_ndirty(ncp))
478
0
  {
479
0
    return write_numrecs(ncp);
480
0
  }
481
  /* else */
482
483
0
  return NC_NOERR;
484
0
}
485
486
487
/*
488
 * Initialize the 'non-record' variables.
489
 */
490
static int
491
fillerup(NC3_INFO *ncp)
492
0
{
493
0
  int status = NC_NOERR;
494
0
  size_t ii;
495
0
  NC_var **varpp;
496
497
0
  assert(!NC_readonly(ncp));
498
499
  /* loop thru vars */
500
0
  varpp = ncp->vars.value;
501
0
  for(ii = 0; ii < ncp->vars.nelems; ii++, varpp++)
502
0
  {
503
0
    if ((*varpp)->no_fill) continue;
504
505
0
    if(IS_RECVAR(*varpp))
506
0
    {
507
      /* skip record variables */
508
0
      continue;
509
0
    }
510
511
0
    status = fill_NC_var(ncp, *varpp, (*varpp)->len, 0);
512
0
    if(status != NC_NOERR)
513
0
      break;
514
0
  }
515
0
  return status;
516
0
}
517
518
/* Begin endef */
519
520
/*
521
 */
522
static int
523
fill_added_recs(NC3_INFO *gnu, NC3_INFO *old)
524
0
{
525
0
  NC_var ** const gnu_varpp = (NC_var **)gnu->vars.value;
526
527
0
  const int old_nrecs = (int) NC_get_numrecs(old);
528
0
  int recno = 0;
529
0
  NC_var **vpp = gnu_varpp;
530
0
  NC_var *const *const end = &vpp[gnu->vars.nelems];
531
0
  int numrecvars = 0;
532
533
  /* Determine if there is only one record variable.  If so, we
534
     must treat as a special case because there's no record padding */
535
0
  for(; vpp < end; vpp++) {
536
0
      if(IS_RECVAR(*vpp)) {
537
0
    numrecvars++;
538
0
      }
539
0
  }
540
541
0
  for(; recno < old_nrecs; recno++)
542
0
      {
543
0
    int varid = (int)old->vars.nelems;
544
0
    for(; varid < (int)gnu->vars.nelems; varid++)
545
0
        {
546
0
      const NC_var *const gnu_varp = *(gnu_varpp + varid);
547
548
0
      if (gnu_varp->no_fill) continue;
549
550
0
      if(!IS_RECVAR(gnu_varp))
551
0
          {
552
        /* skip non-record variables */
553
0
        continue;
554
0
          }
555
      /* else */
556
0
      {
557
0
          size_t varsize = numrecvars == 1 ? gnu->recsize :  gnu_varp->len;
558
0
          const int status = fill_NC_var(gnu, gnu_varp, varsize, recno);
559
0
          if(status != NC_NOERR)
560
0
        return status;
561
0
      }
562
0
        }
563
0
      }
564
0
  return NC_NOERR;
565
0
}
566
567
/*
568
 */
569
static int
570
fill_added(NC3_INFO *gnu, NC3_INFO *old)
571
0
{
572
0
  NC_var ** const gnu_varpp = (NC_var **)gnu->vars.value;
573
0
  int varid = (int)old->vars.nelems;
574
575
0
  for(; varid < (int)gnu->vars.nelems; varid++)
576
0
  {
577
0
    const NC_var *const gnu_varp = *(gnu_varpp + varid);
578
579
0
    if (gnu_varp->no_fill) continue;
580
581
0
    if(IS_RECVAR(gnu_varp))
582
0
    {
583
      /* skip record variables */
584
0
      continue;
585
0
    }
586
    /* else */
587
0
    {
588
0
    const int status = fill_NC_var(gnu, gnu_varp, gnu_varp->len, 0);
589
0
    if(status != NC_NOERR)
590
0
      return status;
591
0
    }
592
0
  }
593
594
0
  return NC_NOERR;
595
0
}
596
597
598
/*
599
 * Move the records "out".
600
 * Fill as needed.
601
 */
602
static int
603
move_recs_r(NC3_INFO *gnu, NC3_INFO *old)
604
0
{
605
0
  int status;
606
0
  int recno;
607
0
  int varid;
608
0
  NC_var **gnu_varpp = (NC_var **)gnu->vars.value;
609
0
  NC_var **old_varpp = (NC_var **)old->vars.value;
610
0
  NC_var *gnu_varp;
611
0
  NC_var *old_varp;
612
0
  off_t gnu_off;
613
0
  off_t old_off;
614
0
  const size_t old_nrecs = NC_get_numrecs(old);
615
616
  /* Don't parallelize this loop */
617
0
  for(recno = (int)old_nrecs -1; recno >= 0; recno--)
618
0
  {
619
  /* Don't parallelize this loop */
620
0
  for(varid = (int)old->vars.nelems -1; varid >= 0; varid--)
621
0
  {
622
0
    gnu_varp = *(gnu_varpp + varid);
623
0
    if(!IS_RECVAR(gnu_varp))
624
0
    {
625
      /* skip non-record variables on this pass */
626
0
      continue;
627
0
    }
628
    /* else */
629
630
    /* else, a pre-existing variable */
631
0
    old_varp = *(old_varpp + varid);
632
0
    gnu_off = gnu_varp->begin + (off_t)(gnu->recsize * recno);
633
0
    old_off = old_varp->begin + (off_t)(old->recsize * recno);
634
635
0
    if(gnu_off == old_off)
636
0
      continue;   /* nothing to do */
637
638
0
    assert(gnu_off > old_off);
639
640
0
    status = ncio_move(gnu->nciop, gnu_off, old_off,
641
0
       old_varp->len, 0);
642
643
0
    if(status != NC_NOERR)
644
0
      return status;
645
646
0
  }
647
0
  }
648
649
0
  NC_set_numrecs(gnu, old_nrecs);
650
651
0
  return NC_NOERR;
652
0
}
653
654
655
/*
656
 * Move the "non record" variables "out".
657
 * Fill as needed.
658
 */
659
static int
660
move_vars_r(NC3_INFO *gnu, NC3_INFO *old)
661
0
{
662
0
  int err, status=NC_NOERR;
663
0
  int varid;
664
0
  NC_var **gnu_varpp = (NC_var **)gnu->vars.value;
665
0
  NC_var **old_varpp = (NC_var **)old->vars.value;
666
0
  NC_var *gnu_varp;
667
0
  NC_var *old_varp;
668
0
  off_t gnu_off;
669
0
  off_t old_off;
670
671
  /* Don't parallelize this loop */
672
0
  for(varid = (int)old->vars.nelems -1;
673
0
     varid >= 0; varid--)
674
0
  {
675
0
    gnu_varp = *(gnu_varpp + varid);
676
0
    if(IS_RECVAR(gnu_varp))
677
0
    {
678
      /* skip record variables on this pass */
679
0
      continue;
680
0
    }
681
    /* else */
682
683
0
    old_varp = *(old_varpp + varid);
684
0
    gnu_off = gnu_varp->begin;
685
0
    old_off = old_varp->begin;
686
687
0
    if (gnu_off > old_off) {
688
0
        err = ncio_move(gnu->nciop, gnu_off, old_off,
689
0
                     old_varp->len, 0);
690
0
        if (status == NC_NOERR) status = err;
691
0
    }
692
0
  }
693
0
  return status;
694
0
}
695
696
697
/*
698
 * Given a valid ncp, return NC_EVARSIZE if any variable has a bad len
699
 * (product of non-rec dim sizes too large), else return NC_NOERR.
700
 */
701
int
702
NC_check_vlens(NC3_INFO *ncp)
703
2
{
704
2
    NC_var **vpp;
705
    /* maximum permitted variable size (or size of one record's worth
706
       of a record variable) in bytes.  This is different for format 1
707
       and format 2. */
708
2
    long long vlen_max;
709
2
    size_t ii;
710
2
    size_t large_vars_count;
711
2
    size_t rec_vars_count;
712
2
    int last = 0;
713
714
2
    if(ncp->vars.nelems == 0)
715
2
  return NC_NOERR;
716
717
0
    if (fIsSet(ncp->flags,NC_64BIT_DATA)) /* CDF-5 */
718
0
  vlen_max = X_INT64_MAX - 3; /* "- 3" handles rounded-up size */
719
0
    else if (fIsSet(ncp->flags,NC_64BIT_OFFSET) && sizeof(off_t) > 4)
720
  /* CDF2 format and LFS */
721
0
  vlen_max = X_UINT_MAX - 3; /* "- 3" handles rounded-up size */
722
0
    else /* CDF1 format */
723
0
  vlen_max = X_INT_MAX - 3;
724
725
    /* Loop through vars, first pass is for non-record variables.   */
726
0
    large_vars_count = 0;
727
0
    rec_vars_count = 0;
728
0
    vpp = ncp->vars.value;
729
0
    for (ii = 0; ii < ncp->vars.nelems; ii++, vpp++) {
730
0
  assert(vpp != NULL && *vpp != NULL);
731
0
  if( !IS_RECVAR(*vpp) ) {
732
0
      last = 0;
733
0
      if( NC_check_vlen(*vpp, vlen_max) == 0 ) {
734
0
                if (fIsSet(ncp->flags,NC_64BIT_DATA)) /* too big for CDF-5 */
735
0
                    return NC_EVARSIZE;
736
0
    large_vars_count++;
737
0
    last = 1;
738
0
      }
739
0
  } else {
740
0
    rec_vars_count++;
741
0
  }
742
0
    }
743
    /* OK if last non-record variable size too large, since not used to
744
       compute an offset */
745
0
    if( large_vars_count > 1) { /* only one "too-large" variable allowed */
746
0
      return NC_EVARSIZE;
747
0
    }
748
    /* and it has to be the last one */
749
0
    if( large_vars_count == 1 && last == 0) {
750
0
      return NC_EVARSIZE;
751
0
    }
752
0
    if( rec_vars_count > 0 ) {
753
  /* and if it's the last one, there can't be any record variables */
754
0
  if( large_vars_count == 1 && last == 1) {
755
0
      return NC_EVARSIZE;
756
0
  }
757
  /* Loop through vars, second pass is for record variables.   */
758
0
  large_vars_count = 0;
759
0
  vpp = ncp->vars.value;
760
0
  for (ii = 0; ii < ncp->vars.nelems; ii++, vpp++) {
761
0
      if( IS_RECVAR(*vpp) ) {
762
0
    last = 0;
763
0
    if( NC_check_vlen(*vpp, vlen_max) == 0 ) {
764
0
                    if (fIsSet(ncp->flags,NC_64BIT_DATA)) /* too big for CDF-5 */
765
0
                        return NC_EVARSIZE;
766
0
        large_vars_count++;
767
0
        last = 1;
768
0
    }
769
0
      }
770
0
  }
771
  /* OK if last record variable size too large, since not used to
772
     compute an offset */
773
0
  if( large_vars_count > 1) { /* only one "too-large" variable allowed */
774
0
      return NC_EVARSIZE;
775
0
  }
776
  /* and it has to be the last one */
777
0
  if( large_vars_count == 1 && last == 0) {
778
0
      return NC_EVARSIZE;
779
0
  }
780
0
    }
781
0
    return NC_NOERR;
782
0
}
783
784
/*----< NC_check_voffs() >---------------------------------------------------*/
785
/*
786
 * Given a valid ncp, check whether the file starting offsets (begin) of all
787
 * variables follows the same increasing order as they were defined.
788
 */
789
int
790
NC_check_voffs(NC3_INFO *ncp)
791
2
{
792
2
    size_t i;
793
2
    off_t prev_off;
794
2
    NC_var *varp;
795
796
2
    if (ncp->vars.nelems == 0) return NC_NOERR;
797
798
    /* Loop through vars, first pass is for non-record variables */
799
0
    prev_off = ncp->begin_var;
800
0
    for (i=0; i<ncp->vars.nelems; i++) {
801
0
        varp = ncp->vars.value[i];
802
0
        if (IS_RECVAR(varp)) continue;
803
804
0
        if (varp->begin < prev_off) {
805
#if 0
806
            fprintf(stderr,"Variable \"%s\" begin offset (%lld) is less than previous variable end offset (%lld)\n", varp->name->cp, varp->begin, prev_off);
807
#endif
808
0
            return NC_ENOTNC;
809
0
        }
810
0
        prev_off = varp->begin + varp->len;
811
0
    }
812
813
0
    if (ncp->begin_rec < prev_off) {
814
#if 0
815
        fprintf(stderr,"Record variable section begin offset (%lld) is less than fix-sized variable section end offset (%lld)\n", varp->begin, prev_off);
816
#endif
817
0
        return NC_ENOTNC;
818
0
    }
819
820
    /* Loop through vars, second pass is for record variables */
821
0
    prev_off = ncp->begin_rec;
822
0
    for (i=0; i<ncp->vars.nelems; i++) {
823
0
        varp = ncp->vars.value[i];
824
0
        if (!IS_RECVAR(varp)) continue;
825
826
0
        if (varp->begin < prev_off) {
827
#if 0
828
            fprintf(stderr,"Variable \"%s\" begin offset (%lld) is less than previous variable end offset (%lld)\n", varp->name->cp, varp->begin, prev_off);
829
#endif
830
0
            return NC_ENOTNC;
831
0
        }
832
0
        prev_off = varp->begin + varp->len;
833
0
    }
834
835
0
    return NC_NOERR;
836
0
}
837
838
/*
839
 *  End define mode.
840
 *  Common code for ncendef, ncclose(endef)
841
 *  Flushes I/O buffers.
842
 */
843
static int
844
NC_endef(NC3_INFO *ncp,
845
  size_t h_minfree, size_t v_align,
846
  size_t v_minfree, size_t r_align)
847
0
{
848
0
  int status = NC_NOERR;
849
850
0
  assert(!NC_readonly(ncp));
851
0
  assert(NC_indef(ncp));
852
853
0
  status = NC_check_vlens(ncp);
854
0
  if(status != NC_NOERR)
855
0
      return status;
856
0
  status = NC_begins(ncp, h_minfree, v_align, v_minfree, r_align);
857
0
  if(status != NC_NOERR)
858
0
      return status;
859
0
  status = NC_check_voffs(ncp);
860
0
  if(status != NC_NOERR)
861
0
      return status;
862
863
0
  if(ncp->old != NULL)
864
0
  {
865
    /* a plain redef, not a create */
866
0
    assert(!NC_IsNew(ncp));
867
0
    assert(fIsSet(ncp->state, NC_INDEF));
868
0
    assert(ncp->begin_rec >= ncp->old->begin_rec);
869
0
    assert(ncp->begin_var >= ncp->old->begin_var);
870
871
0
    if(ncp->vars.nelems != 0)
872
0
    {
873
0
    if(ncp->begin_rec > ncp->old->begin_rec)
874
0
    {
875
0
      status = move_recs_r(ncp, ncp->old);
876
0
      if(status != NC_NOERR)
877
0
        return status;
878
0
      if(ncp->begin_var > ncp->old->begin_var)
879
0
      {
880
0
        status = move_vars_r(ncp, ncp->old);
881
0
        if(status != NC_NOERR)
882
0
          return status;
883
0
      }
884
      /* else if (ncp->begin_var == ncp->old->begin_var) { NOOP } */
885
0
    }
886
0
    else
887
0
                {
888
      /* due to fixed variable alignment, it is possible that header
889
                           grows but begin_rec did not change */
890
0
      if(ncp->begin_var > ncp->old->begin_var)
891
0
      {
892
0
        status = move_vars_r(ncp, ncp->old);
893
0
        if(status != NC_NOERR)
894
0
          return status;
895
0
      }
896
      /* Even if (ncp->begin_rec == ncp->old->begin_rec)
897
         and     (ncp->begin_var == ncp->old->begin_var)
898
         might still have added a new record variable */
899
0
            if(ncp->recsize > ncp->old->recsize)
900
0
      {
901
0
              status = move_recs_r(ncp, ncp->old);
902
0
        if(status != NC_NOERR)
903
0
              return status;
904
0
      }
905
0
    }
906
0
    }
907
0
  }
908
909
0
  status = write_NC(ncp);
910
0
  if(status != NC_NOERR)
911
0
    return status;
912
913
  /* fill mode is now per variable */
914
0
  {
915
0
    if(NC_IsNew(ncp))
916
0
    {
917
0
      status = fillerup(ncp);
918
0
      if(status != NC_NOERR)
919
0
        return status;
920
921
0
    }
922
0
    else if(ncp->old == NULL ? 0
923
0
                                         : (ncp->vars.nelems > ncp->old->vars.nelems))
924
0
          {
925
0
            status = fill_added(ncp, ncp->old);
926
0
            if(status != NC_NOERR)
927
0
              return status;
928
0
            status = fill_added_recs(ncp, ncp->old);
929
0
            if(status != NC_NOERR)
930
0
              return status;
931
0
          }
932
0
  }
933
934
0
  if(ncp->old != NULL)
935
0
  {
936
0
    free_NC3INFO(ncp->old);
937
0
    ncp->old = NULL;
938
0
  }
939
940
0
  fClr(ncp->state, NC_CREAT | NC_INDEF);
941
942
0
  return ncio_sync(ncp->nciop);
943
0
}
944
945
946
/*
947
 * Compute the expected size of the file.
948
 */
949
int
950
NC_calcsize(const NC3_INFO *ncp, off_t *calcsizep)
951
2
{
952
2
  NC_var **vpp = (NC_var **)ncp->vars.value;
953
2
  NC_var *const *const end = &vpp[ncp->vars.nelems];
954
2
  NC_var *last_fix = NULL;  /* last "non-record" var */
955
2
  int numrecvars = 0; /* number of record variables */
956
957
2
  if(ncp->vars.nelems == 0) { /* no non-record variables and
958
               no record variables */
959
2
      *calcsizep = ncp->xsz; /* size of header */
960
2
      return NC_NOERR;
961
2
  }
962
963
0
  for( /*NADA*/; vpp < end; vpp++) {
964
0
      if(IS_RECVAR(*vpp)) {
965
0
    numrecvars++;
966
0
      } else {
967
0
    last_fix = *vpp;
968
0
      }
969
0
  }
970
971
0
  if(numrecvars == 0) {
972
0
      off_t varsize;
973
0
      assert(last_fix != NULL);
974
0
      varsize = last_fix->len;
975
0
      if(last_fix->len == X_UINT_MAX) { /* huge last fixed var */
976
0
    int i;
977
0
    varsize = 1;
978
0
            for(i = 0; i < last_fix->ndims; i++ ) {
979
0
                varsize *= (last_fix->shape ? last_fix->shape[i] : 1);
980
0
        }
981
0
      }
982
0
      *calcsizep = last_fix->begin + varsize;
983
      /*last_var = last_fix;*/
984
0
  } else {       /* we have at least one record variable */
985
0
      *calcsizep = ncp->begin_rec + ncp->numrecs * ncp->recsize;
986
0
  }
987
988
0
  return NC_NOERR;
989
0
}
990
991
/* Public */
992
993
#if 0 /* no longer needed */
994
int NC3_new_nc(NC3_INFO** ncpp)
995
{
996
  NC *nc;
997
  NC3_INFO* nc3;
998
999
  ncp = (NC *) malloc(sizeof(NC));
1000
  if(ncp == NULL)
1001
    return NC_ENOMEM;
1002
  (void) memset(ncp, 0, sizeof(NC));
1003
1004
  ncp->xsz = MIN_NC_XSZ;
1005
  assert(ncp->xsz == ncx_len_NC(ncp,0));
1006
1007
        if(ncpp) *ncpp = ncp;
1008
        return NC_NOERR;
1009
1010
}
1011
#endif
1012
1013
/* WARNING: SIGNATURE CHANGE */
1014
int
1015
NC3_create(const char *path, int ioflags, size_t initialsz, int basepe,
1016
           size_t *chunksizehintp, void *parameters,
1017
           const NC_Dispatch *dispatch, int ncid)
1018
0
{
1019
0
  int status = NC_NOERR;
1020
0
  void *xp = NULL;
1021
0
  int sizeof_off_t = 0;
1022
0
        NC *nc;
1023
0
  NC3_INFO* nc3 = NULL;
1024
1025
        /* Find NC struct for this file. */
1026
0
        if ((status = NC_check_id(ncid, &nc)))
1027
0
            return status;
1028
1029
  /* Create our specific NC3_INFO instance */
1030
0
  nc3 = new_NC3INFO(chunksizehintp);
1031
1032
#if ALWAYS_NC_SHARE /* DEBUG */
1033
  fSet(ioflags, NC_SHARE);
1034
#endif
1035
1036
  /*
1037
   * Only pe 0 is valid
1038
   */
1039
0
  if(basepe != 0) {
1040
0
            if(nc3) free(nc3);
1041
0
            return NC_EINVAL;
1042
0
        }
1043
0
  assert(nc3->flags == 0);
1044
1045
  /* Now we can set min size */
1046
0
  if (fIsSet(ioflags, NC_64BIT_DATA))
1047
0
      nc3->xsz = MIN_NC5_XSZ; /* CDF-5 has minimum 16 extra bytes */
1048
0
  else
1049
0
      nc3->xsz = MIN_NC3_XSZ;
1050
1051
0
  if (fIsSet(ioflags, NC_64BIT_OFFSET)) {
1052
0
      fSet(nc3->flags, NC_64BIT_OFFSET);
1053
0
      sizeof_off_t = 8;
1054
0
  } else if (fIsSet(ioflags, NC_64BIT_DATA)) {
1055
0
      fSet(nc3->flags, NC_64BIT_DATA);
1056
0
      sizeof_off_t = 8;
1057
0
  } else {
1058
0
    sizeof_off_t = 4;
1059
0
  }
1060
1061
0
  assert(nc3->xsz == ncx_len_NC(nc3,sizeof_off_t));
1062
1063
0
        status =  ncio_create(path, ioflags, initialsz,
1064
0
            0, nc3->xsz, &nc3->chunk, NULL,
1065
0
            &nc3->nciop, &xp);
1066
0
  if(status != NC_NOERR)
1067
0
  {
1068
    /* translate error status */
1069
0
    if(status == EEXIST)
1070
0
      status = NC_EEXIST;
1071
0
    goto unwind_alloc;
1072
0
  }
1073
1074
0
  fSet(nc3->state, NC_CREAT);
1075
1076
0
  if(fIsSet(nc3->nciop->ioflags, NC_SHARE))
1077
0
  {
1078
    /*
1079
     * NC_SHARE implies sync up the number of records as well.
1080
     * (File format version one.)
1081
     * Note that other header changes are not shared
1082
     * automatically.  Some sort of IPC (external to this package)
1083
     * would be used to trigger a call to nc_sync().
1084
     */
1085
0
    fSet(nc3->state, NC_NSYNC);
1086
0
  }
1087
1088
0
  status = ncx_put_NC(nc3, &xp, sizeof_off_t, nc3->xsz);
1089
0
  if(status != NC_NOERR)
1090
0
    goto unwind_ioc;
1091
1092
0
  if(chunksizehintp != NULL)
1093
0
    *chunksizehintp = nc3->chunk;
1094
1095
  /* Link nc3 and nc */
1096
0
        NC3_DATA_SET(nc,nc3);
1097
0
  nc->int_ncid = nc3->nciop->fd;
1098
1099
0
  return NC_NOERR;
1100
1101
0
unwind_ioc:
1102
0
  if(nc3 != NULL) {
1103
0
      (void) ncio_close(nc3->nciop, 1); /* N.B.: unlink */
1104
0
      nc3->nciop = NULL;
1105
0
  }
1106
  /*FALLTHRU*/
1107
0
unwind_alloc:
1108
0
  free_NC3INFO(nc3);
1109
0
  if(nc)
1110
0
            NC3_DATA_SET(nc,NULL);
1111
0
  return status;
1112
0
}
1113
1114
#if 0
1115
/* This function sets a default create flag that will be logically
1116
   or'd to whatever flags are passed into nc_create for all future
1117
   calls to nc_create.
1118
   Valid default create flags are NC_64BIT_OFFSET, NC_CDF5, NC_CLOBBER,
1119
   NC_LOCK, NC_SHARE. */
1120
int
1121
nc_set_default_format(int format, int *old_formatp)
1122
{
1123
    /* Return existing format if desired. */
1124
    if (old_formatp)
1125
      *old_formatp = default_create_format;
1126
1127
    /* Make sure only valid format is set. */
1128
#ifdef USE_NETCDF4
1129
    if (format != NC_FORMAT_CLASSIC && format != NC_FORMAT_64BIT_OFFSET &&
1130
  format != NC_FORMAT_NETCDF4 && format != NC_FORMAT_NETCDF4_CLASSIC)
1131
      return NC_EINVAL;
1132
#else
1133
    if (format != NC_FORMAT_CLASSIC && format != NC_FORMAT_64BIT_OFFSET
1134
#ifdef ENABLE_CDF5
1135
        && format != NC_FORMAT_CDF5
1136
#endif
1137
        )
1138
      return NC_EINVAL;
1139
#endif
1140
    default_create_format = format;
1141
    return NC_NOERR;
1142
}
1143
#endif
1144
1145
int
1146
NC3_open(const char *path, int ioflags, int basepe, size_t *chunksizehintp,
1147
         void *parameters, const NC_Dispatch *dispatch, int ncid)
1148
104
{
1149
104
  int status;
1150
104
  NC3_INFO* nc3 = NULL;
1151
104
        NC *nc;
1152
1153
        /* Find NC struct for this file. */
1154
104
        if ((status = NC_check_id(ncid, &nc)))
1155
0
            return status;
1156
1157
  /* Create our specific NC3_INFO instance */
1158
104
  nc3 = new_NC3INFO(chunksizehintp);
1159
1160
#if ALWAYS_NC_SHARE /* DEBUG */
1161
  fSet(ioflags, NC_SHARE);
1162
#endif
1163
1164
  /*
1165
   * Only pe 0 is valid.
1166
   */
1167
104
  if(basepe != 0) {
1168
0
            if(nc3) {
1169
0
                free(nc3);
1170
0
                nc3 = NULL;
1171
0
            }
1172
0
            status = NC_EINVAL;
1173
0
            goto unwind_alloc;
1174
0
        }
1175
1176
104
        status = ncio_open(path, ioflags, 0, 0, &nc3->chunk, parameters,
1177
104
             &nc3->nciop, NULL);
1178
104
  if(status)
1179
0
    goto unwind_alloc;
1180
1181
104
  assert(nc3->state == 0);
1182
1183
104
  if(fIsSet(nc3->nciop->ioflags, NC_SHARE))
1184
0
  {
1185
    /*
1186
     * NC_SHARE implies sync up the number of records as well.
1187
     * (File format version one.)
1188
     * Note that other header changes are not shared
1189
     * automatically.  Some sort of IPC (external to this package)
1190
     * would be used to trigger a call to nc_sync().
1191
     */
1192
0
    fSet(nc3->state, NC_NSYNC);
1193
0
  }
1194
1195
104
  status = nc_get_NC(nc3);
1196
104
  if(status != NC_NOERR)
1197
102
    goto unwind_ioc;
1198
1199
2
  if(chunksizehintp != NULL)
1200
0
    *chunksizehintp = nc3->chunk;
1201
1202
  /* Link nc3 and nc */
1203
2
        NC3_DATA_SET(nc,nc3);
1204
2
  nc->int_ncid = nc3->nciop->fd;
1205
1206
2
  return NC_NOERR;
1207
1208
102
unwind_ioc:
1209
102
  if(nc3) {
1210
102
          (void) ncio_close(nc3->nciop, 0);
1211
102
      nc3->nciop = NULL;
1212
102
  }
1213
  /*FALLTHRU*/
1214
102
unwind_alloc:
1215
102
  free_NC3INFO(nc3);
1216
102
  if(nc)
1217
102
            NC3_DATA_SET(nc,NULL);
1218
102
  return status;
1219
102
}
1220
1221
int
1222
NC3__enddef(int ncid,
1223
  size_t h_minfree, size_t v_align,
1224
  size_t v_minfree, size_t r_align)
1225
0
{
1226
0
  int status;
1227
0
  NC *nc;
1228
0
  NC3_INFO* nc3;
1229
1230
0
  status = NC_check_id(ncid, &nc);
1231
0
  if(status != NC_NOERR)
1232
0
    return status;
1233
0
  nc3 = NC3_DATA(nc);
1234
1235
0
  if(!NC_indef(nc3))
1236
0
    return(NC_ENOTINDEFINE);
1237
1238
0
  return (NC_endef(nc3, h_minfree, v_align, v_minfree, r_align));
1239
0
}
1240
1241
/*
1242
 * In data mode, same as ncclose.
1243
 * In define mode, restore previous definition.
1244
 * In create, remove the file.
1245
 */
1246
int
1247
NC3_abort(int ncid)
1248
0
{
1249
0
  int status;
1250
0
  NC *nc;
1251
0
  NC3_INFO* nc3;
1252
0
  int doUnlink = 0;
1253
1254
0
  status = NC_check_id(ncid, &nc);
1255
0
  if(status != NC_NOERR)
1256
0
      return status;
1257
0
  nc3 = NC3_DATA(nc);
1258
1259
0
  doUnlink = NC_IsNew(nc3);
1260
1261
0
  if(nc3->old != NULL)
1262
0
  {
1263
    /* a plain redef, not a create */
1264
0
    assert(!NC_IsNew(nc3));
1265
0
    assert(fIsSet(nc3->state, NC_INDEF));
1266
0
    free_NC3INFO(nc3->old);
1267
0
    nc3->old = NULL;
1268
0
    fClr(nc3->state, NC_INDEF);
1269
0
  }
1270
0
  else if(!NC_readonly(nc3))
1271
0
  {
1272
0
    status = NC_sync(nc3);
1273
0
    if(status != NC_NOERR)
1274
0
      return status;
1275
0
  }
1276
1277
1278
0
  (void) ncio_close(nc3->nciop, doUnlink);
1279
0
  nc3->nciop = NULL;
1280
1281
0
  free_NC3INFO(nc3);
1282
0
  if(nc)
1283
0
            NC3_DATA_SET(nc,NULL);
1284
1285
0
  return NC_NOERR;
1286
0
}
1287
1288
int
1289
NC3_close(int ncid, void* params)
1290
2
{
1291
2
  int status = NC_NOERR;
1292
2
  NC *nc;
1293
2
  NC3_INFO* nc3;
1294
1295
2
  status = NC_check_id(ncid, &nc);
1296
2
  if(status != NC_NOERR)
1297
0
      return status;
1298
2
  nc3 = NC3_DATA(nc);
1299
1300
2
  if(NC_indef(nc3))
1301
0
  {
1302
0
    status = NC_endef(nc3, 0, 1, 0, 1); /* TODO: defaults */
1303
0
    if(status != NC_NOERR )
1304
0
    {
1305
0
      (void) NC3_abort(ncid);
1306
0
      return status;
1307
0
    }
1308
0
  }
1309
2
  else if(!NC_readonly(nc3))
1310
0
  {
1311
0
    status = NC_sync(nc3);
1312
    /* flush buffers before any filesize comparisons */
1313
0
    (void) ncio_sync(nc3->nciop);
1314
0
  }
1315
1316
  /*
1317
   * If file opened for writing and filesize is less than
1318
   * what it should be (due to previous use of NOFILL mode),
1319
   * pad it to correct size, as reported by NC_calcsize().
1320
   */
1321
2
  if (status == NC_NOERR) {
1322
2
      off_t filesize;   /* current size of open file */
1323
2
      off_t calcsize; /* calculated file size, from header */
1324
2
      status = ncio_filesize(nc3->nciop, &filesize);
1325
2
      if(status != NC_NOERR)
1326
0
    return status;
1327
2
      status = NC_calcsize(nc3, &calcsize);
1328
2
      if(status != NC_NOERR)
1329
0
    return status;
1330
2
      if(filesize < calcsize && !NC_readonly(nc3)) {
1331
0
    status = ncio_pad_length(nc3->nciop, calcsize);
1332
0
    if(status != NC_NOERR)
1333
0
        return status;
1334
0
      }
1335
2
  }
1336
1337
2
  if(params != NULL && (nc->mode & NC_INMEMORY) != 0) {
1338
0
      NC_memio* memio = (NC_memio*)params;
1339
            /* Extract the final memory size &/or contents */
1340
0
            status = memio_extract(nc3->nciop,&memio->size,&memio->memory);
1341
0
        }
1342
1343
2
  (void) ncio_close(nc3->nciop, 0);
1344
2
  nc3->nciop = NULL;
1345
1346
2
  free_NC3INFO(nc3);
1347
2
        NC3_DATA_SET(nc,NULL);
1348
1349
2
  return status;
1350
2
}
1351
1352
int
1353
NC3_redef(int ncid)
1354
0
{
1355
0
  int status;
1356
0
  NC *nc;
1357
0
  NC3_INFO* nc3;
1358
1359
0
  status = NC_check_id(ncid, &nc);
1360
0
  if(status != NC_NOERR)
1361
0
    return status;
1362
0
  nc3 = NC3_DATA(nc);
1363
1364
0
  if(NC_readonly(nc3))
1365
0
    return NC_EPERM;
1366
1367
0
  if(NC_indef(nc3))
1368
0
    return NC_EINDEFINE;
1369
1370
1371
0
  if(fIsSet(nc3->nciop->ioflags, NC_SHARE))
1372
0
  {
1373
    /* read in from disk */
1374
0
    status = read_NC(nc3);
1375
0
    if(status != NC_NOERR)
1376
0
      return status;
1377
0
  }
1378
1379
0
  nc3->old = dup_NC3INFO(nc3);
1380
0
  if(nc3->old == NULL)
1381
0
    return NC_ENOMEM;
1382
1383
0
  fSet(nc3->state, NC_INDEF);
1384
1385
0
  return NC_NOERR;
1386
0
}
1387
1388
1389
int
1390
NC3_inq(int ncid,
1391
  int *ndimsp,
1392
  int *nvarsp,
1393
  int *nattsp,
1394
  int *xtendimp)
1395
0
{
1396
0
  int status;
1397
0
  NC *nc;
1398
0
  NC3_INFO* nc3;
1399
1400
0
  status = NC_check_id(ncid, &nc);
1401
0
  if(status != NC_NOERR)
1402
0
    return status;
1403
0
  nc3 = NC3_DATA(nc);
1404
1405
0
  if(ndimsp != NULL)
1406
0
    *ndimsp = (int) nc3->dims.nelems;
1407
0
  if(nvarsp != NULL)
1408
0
    *nvarsp = (int) nc3->vars.nelems;
1409
0
  if(nattsp != NULL)
1410
0
    *nattsp = (int) nc3->attrs.nelems;
1411
0
  if(xtendimp != NULL)
1412
0
    *xtendimp = find_NC_Udim(&nc3->dims, NULL);
1413
1414
0
  return NC_NOERR;
1415
0
}
1416
1417
int
1418
NC3_inq_unlimdim(int ncid, int *xtendimp)
1419
0
{
1420
0
  int status;
1421
0
  NC *nc;
1422
0
  NC3_INFO* nc3;
1423
1424
0
  status = NC_check_id(ncid, &nc);
1425
0
  if(status != NC_NOERR)
1426
0
    return status;
1427
0
  nc3 = NC3_DATA(nc);
1428
1429
0
  if(xtendimp != NULL)
1430
0
    *xtendimp = find_NC_Udim(&nc3->dims, NULL);
1431
1432
0
  return NC_NOERR;
1433
0
}
1434
1435
int
1436
NC3_sync(int ncid)
1437
0
{
1438
0
  int status;
1439
0
  NC *nc;
1440
0
  NC3_INFO* nc3;
1441
1442
0
  status = NC_check_id(ncid, &nc);
1443
0
  if(status != NC_NOERR)
1444
0
    return status;
1445
0
  nc3 = NC3_DATA(nc);
1446
1447
0
  if(NC_indef(nc3))
1448
0
    return NC_EINDEFINE;
1449
1450
0
  if(NC_readonly(nc3))
1451
0
  {
1452
0
    return read_NC(nc3);
1453
0
  }
1454
  /* else, read/write */
1455
1456
0
  status = NC_sync(nc3);
1457
0
  if(status != NC_NOERR)
1458
0
    return status;
1459
1460
0
  status = ncio_sync(nc3->nciop);
1461
0
  if(status != NC_NOERR)
1462
0
    return status;
1463
1464
#ifdef USE_FSYNC
1465
  /* may improve concurrent access, but slows performance if
1466
   * called frequently */
1467
#ifndef _WIN32
1468
  status = fsync(nc3->nciop->fd);
1469
#else
1470
  status = _commit(nc3->nciop->fd);
1471
#endif  /* _WIN32 */
1472
#endif  /* USE_FSYNC */
1473
1474
0
  return status;
1475
0
}
1476
1477
1478
int
1479
NC3_set_fill(int ncid,
1480
  int fillmode, int *old_mode_ptr)
1481
0
{
1482
0
  int i, status;
1483
0
  NC *nc;
1484
0
  NC3_INFO* nc3;
1485
0
  int oldmode;
1486
1487
0
  status = NC_check_id(ncid, &nc);
1488
0
  if(status != NC_NOERR)
1489
0
    return status;
1490
0
  nc3 = NC3_DATA(nc);
1491
1492
0
  if(NC_readonly(nc3))
1493
0
    return NC_EPERM;
1494
1495
0
  oldmode = fIsSet(nc3->state, NC_NOFILL) ? NC_NOFILL : NC_FILL;
1496
1497
0
  if(fillmode == NC_NOFILL)
1498
0
  {
1499
0
    fSet(nc3->state, NC_NOFILL);
1500
0
  }
1501
0
  else if(fillmode == NC_FILL)
1502
0
  {
1503
0
    if(fIsSet(nc3->state, NC_NOFILL))
1504
0
    {
1505
      /*
1506
       * We are changing back to fill mode
1507
       * so do a sync
1508
       */
1509
0
      status = NC_sync(nc3);
1510
0
      if(status != NC_NOERR)
1511
0
        return status;
1512
0
    }
1513
0
    fClr(nc3->state, NC_NOFILL);
1514
0
  }
1515
0
  else
1516
0
  {
1517
0
    return NC_EINVAL; /* Invalid fillmode */
1518
0
  }
1519
1520
0
  if(old_mode_ptr != NULL)
1521
0
    *old_mode_ptr = oldmode;
1522
1523
  /* loop thru all variables to set/overwrite its fill mode */
1524
0
  for (i=0; i<nc3->vars.nelems; i++)
1525
0
    nc3->vars.value[i]->no_fill = (fillmode == NC_NOFILL);
1526
1527
  /* once the file's fill mode is set, any new variables defined after
1528
   * this call will check NC_dofill(nc3) and set their no_fill accordingly.
1529
   * See NC3_def_var() */
1530
1531
0
  return NC_NOERR;
1532
0
}
1533
1534
/**
1535
 * Return the file format.
1536
 *
1537
 * \param ncid the ID of the open file.
1538
1539
 * \param formatp a pointer that gets the format. Ignored if NULL.
1540
 *
1541
 * \returns NC_NOERR No error.
1542
 * \returns NC_EBADID Bad ncid.
1543
 * \internal
1544
 * \author Ed Hartnett, Dennis Heimbigner
1545
 */
1546
int
1547
NC3_inq_format(int ncid, int *formatp)
1548
0
{
1549
0
   int status;
1550
0
   NC *nc;
1551
0
   NC3_INFO* nc3;
1552
1553
0
   status = NC_check_id(ncid, &nc);
1554
0
   if(status != NC_NOERR)
1555
0
      return status;
1556
0
   nc3 = NC3_DATA(nc);
1557
1558
   /* Why even call this function with no format pointer? */
1559
0
   if (!formatp)
1560
0
      return NC_NOERR;
1561
1562
   /* only need to check for netCDF-3 variants, since this is never called for netCDF-4 files */
1563
0
#ifdef ENABLE_CDF5
1564
0
   if (fIsSet(nc3->flags, NC_64BIT_DATA))
1565
0
      *formatp = NC_FORMAT_CDF5;
1566
0
   else
1567
0
#endif
1568
0
      if (fIsSet(nc3->flags, NC_64BIT_OFFSET))
1569
0
         *formatp = NC_FORMAT_64BIT_OFFSET;
1570
0
      else
1571
0
         *formatp = NC_FORMAT_CLASSIC;
1572
0
   return NC_NOERR;
1573
0
}
1574
1575
/**
1576
 * Return the extended format (i.e. the dispatch model), plus the mode
1577
 * associated with an open file.
1578
 *
1579
 * \param ncid the ID of the open file.
1580
 * \param formatp a pointer that gets the extended format. Note that
1581
 * this is not the same as the format provided by nc_inq_format(). The
1582
 * extended format indicates the dispatch layer model. Classic, 64-bit
1583
 * offset, and CDF5 files all have an extended format of
1584
 * ::NC_FORMATX_NC3. Ignored if NULL.
1585
 * \param modep a pointer that gets the open/create mode associated with
1586
 * this file. Ignored if NULL.
1587
 *
1588
 * \returns NC_NOERR No error.
1589
 * \returns NC_EBADID Bad ncid.
1590
 * \internal
1591
 * \author Dennis Heimbigner
1592
 */
1593
int
1594
NC3_inq_format_extended(int ncid, int *formatp, int *modep)
1595
0
{
1596
0
   int status;
1597
0
   NC *nc;
1598
1599
0
   status = NC_check_id(ncid, &nc);
1600
0
   if(status != NC_NOERR)
1601
0
      return status;
1602
0
   if(formatp) *formatp = NC_FORMATX_NC3;
1603
0
   if(modep) *modep = nc->mode;
1604
0
   return NC_NOERR;
1605
0
}
1606
1607
/**
1608
 * Determine name and size of netCDF type. This netCDF-4 function
1609
 * proved so popular that a netCDF-classic version is provided. You're
1610
 * welcome.
1611
 *
1612
 * \param ncid The ID of an open file.
1613
 * \param typeid The ID of a netCDF type.
1614
 * \param name Pointer that will get the name of the type. Maximum
1615
 * size will be NC_MAX_NAME. Ignored if NULL.
1616
 * \param size Pointer that will get size of type in bytes. Ignored if
1617
 * null.
1618
 *
1619
 * \returns NC_NOERR No error.
1620
 * \returns NC_EBADID Bad ncid.
1621
 * \returns NC_EBADTYPE Bad typeid.
1622
 * \internal
1623
 * \author Ed Hartnett
1624
 */
1625
int
1626
NC3_inq_type(int ncid, nc_type typeid, char *name, size_t *size)
1627
0
{
1628
0
   NC *ncp;
1629
0
   int stat = NC_check_id(ncid, &ncp);
1630
0
   if (stat != NC_NOERR)
1631
0
      return stat;
1632
1633
0
   if(typeid < NC_BYTE || typeid > NC_STRING)
1634
0
      return NC_EBADTYPE;
1635
1636
   /* Give the user the values they want. */
1637
0
   if (name)
1638
0
      strcpy(name, NC_atomictypename(typeid));
1639
0
   if (size)
1640
0
      *size = NC_atomictypelen(typeid);
1641
1642
0
   return NC_NOERR;
1643
0
}
1644
1645
/**
1646
 * This is an obsolete form of nc_delete(), supported for backwards
1647
 * compatibility.
1648
 *
1649
 * @param path Filename to delete.
1650
 * @param basepe Must be 0.
1651
 *
1652
 * @return ::NC_NOERR No error.
1653
 * @return ::NC_EIO Couldn't delete file.
1654
 * @return ::NC_EINVAL Invaliod basepe. Must be 0.
1655
 * @author Glenn Davis, Ed Hartnett
1656
 */
1657
int
1658
nc_delete_mp(const char * path, int basepe)
1659
0
{
1660
0
  NC *nc;
1661
0
  int status;
1662
0
  int ncid;
1663
1664
0
  status = nc_open(path,NC_NOWRITE,&ncid);
1665
0
        if(status) return status;
1666
1667
0
  status = NC_check_id(ncid,&nc);
1668
0
        if(status) return status;
1669
1670
  /*
1671
   * Only pe 0 is valid.
1672
   */
1673
0
  if(basepe != 0)
1674
0
    return NC_EINVAL;
1675
1676
0
  (void) nc_close(ncid);
1677
0
  if(unlink(path) == -1) {
1678
0
      return NC_EIO; /* No more specific error code is appropriate */
1679
0
  }
1680
0
  return NC_NOERR;
1681
0
}
1682
1683
int
1684
nc_delete(const char * path)
1685
0
{
1686
0
        return nc_delete_mp(path, 0);
1687
0
}
1688
1689
/*----< NC3_inq_default_fill_value() >---------------------------------------*/
1690
/* copy the default fill value to the memory space pointed by fillp */
1691
int
1692
NC3_inq_default_fill_value(int xtype, void *fillp)
1693
0
{
1694
0
    if (fillp == NULL) return NC_NOERR;
1695
1696
0
    switch(xtype) {
1697
0
        case NC_CHAR   :               *(char*)fillp = NC_FILL_CHAR;   break;
1698
0
        case NC_BYTE   :        *(signed char*)fillp = NC_FILL_BYTE;   break;
1699
0
        case NC_SHORT  :              *(short*)fillp = NC_FILL_SHORT;  break;
1700
0
        case NC_INT    :                *(int*)fillp = NC_FILL_INT;    break;
1701
0
        case NC_FLOAT  :              *(float*)fillp = NC_FILL_FLOAT;  break;
1702
0
        case NC_DOUBLE :             *(double*)fillp = NC_FILL_DOUBLE; break;
1703
0
        case NC_UBYTE  :      *(unsigned char*)fillp = NC_FILL_UBYTE;  break;
1704
0
        case NC_USHORT :     *(unsigned short*)fillp = NC_FILL_USHORT; break;
1705
0
        case NC_UINT   :       *(unsigned int*)fillp = NC_FILL_UINT;   break;
1706
0
        case NC_INT64  :          *(long long*)fillp = NC_FILL_INT64;  break;
1707
0
        case NC_UINT64 : *(unsigned long long*)fillp = NC_FILL_UINT64; break;
1708
0
        default : return NC_EBADTYPE;
1709
0
    }
1710
0
    return NC_NOERR;
1711
0
}
1712
1713
1714
/*----< NC3_inq_var_fill() >-------------------------------------------------*/
1715
/* inquire the fill value of a variable */
1716
int
1717
NC3_inq_var_fill(const NC_var *varp, void *fill_value)
1718
0
{
1719
0
    NC_attr **attrpp = NULL;
1720
1721
0
    if (fill_value == NULL) return NC_EINVAL;
1722
1723
    /*
1724
     * find fill value
1725
     */
1726
0
    attrpp = NC_findattr(&varp->attrs, _FillValue);
1727
0
    if ( attrpp != NULL ) {
1728
0
        const void *xp;
1729
        /* User defined fill value */
1730
0
        if ( (*attrpp)->type != varp->type || (*attrpp)->nelems != 1 )
1731
0
            return NC_EBADTYPE;
1732
1733
0
        xp = (*attrpp)->xvalue;
1734
        /* value stored in xvalue is in external representation, may need byte-swap */
1735
0
        switch(varp->type) {
1736
0
            case NC_CHAR:   return ncx_getn_text               (&xp, 1,               (char*)fill_value);
1737
0
            case NC_BYTE:   return ncx_getn_schar_schar        (&xp, 1,        (signed char*)fill_value);
1738
0
            case NC_UBYTE:  return ncx_getn_uchar_uchar        (&xp, 1,      (unsigned char*)fill_value);
1739
0
            case NC_SHORT:  return ncx_getn_short_short        (&xp, 1,              (short*)fill_value);
1740
0
            case NC_USHORT: return ncx_getn_ushort_ushort      (&xp, 1,     (unsigned short*)fill_value);
1741
0
            case NC_INT:    return ncx_getn_int_int            (&xp, 1,                (int*)fill_value);
1742
0
            case NC_UINT:   return ncx_getn_uint_uint          (&xp, 1,       (unsigned int*)fill_value);
1743
0
            case NC_FLOAT:  return ncx_getn_float_float        (&xp, 1,              (float*)fill_value);
1744
0
            case NC_DOUBLE: return ncx_getn_double_double      (&xp, 1,             (double*)fill_value);
1745
0
            case NC_INT64:  return ncx_getn_longlong_longlong  (&xp, 1,          (long long*)fill_value);
1746
0
            case NC_UINT64: return ncx_getn_ulonglong_ulonglong(&xp, 1, (unsigned long long*)fill_value);
1747
0
            default: return NC_EBADTYPE;
1748
0
        }
1749
0
    }
1750
0
    else {
1751
        /* use the default */
1752
0
        switch(varp->type){
1753
0
            case NC_CHAR:                *(char *)fill_value = NC_FILL_CHAR;
1754
0
                 break;
1755
0
            case NC_BYTE:          *(signed char *)fill_value = NC_FILL_BYTE;
1756
0
                 break;
1757
0
            case NC_SHORT:               *(short *)fill_value = NC_FILL_SHORT;
1758
0
                 break;
1759
0
            case NC_INT:                   *(int *)fill_value = NC_FILL_INT;
1760
0
                 break;
1761
0
            case NC_UBYTE:       *(unsigned char *)fill_value = NC_FILL_UBYTE;
1762
0
                 break;
1763
0
            case NC_USHORT:     *(unsigned short *)fill_value = NC_FILL_USHORT;
1764
0
                 break;
1765
0
            case NC_UINT:         *(unsigned int *)fill_value = NC_FILL_UINT;
1766
0
                 break;
1767
0
            case NC_INT64:           *(long long *)fill_value = NC_FILL_INT64;
1768
0
                 break;
1769
0
            case NC_UINT64: *(unsigned long long *)fill_value = NC_FILL_UINT64;
1770
0
                 break;
1771
0
            case NC_FLOAT:               *(float *)fill_value = NC_FILL_FLOAT;
1772
0
                 break;
1773
0
            case NC_DOUBLE:             *(double *)fill_value = NC_FILL_DOUBLE;
1774
0
                 break;
1775
0
            default:
1776
0
                 return NC_EINVAL;
1777
0
        }
1778
0
    }
1779
0
    return NC_NOERR;
1780
0
}