Skip to content

Commit b9330d2

Browse files
authored
Merge pull request #1491 from Unidata/gh1487.wif
#1487 + Release Note entry
2 parents 2e64fdb + 1d42cab commit b9330d2

File tree

7 files changed

+128
-51
lines changed

7 files changed

+128
-51
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This file contains a high-level description of this package's evolution. Release
77

88
## 4.7.2 - TBD
99

10+
* [Enhancement] Added a function for changing the ncid of an open file. This function should only be used if you know what you are doing, and is meant to be used primarily with PIO integration. See [GitHub #1483](https://github.com/Unidata/netcdf-c/pull/1483) and [GitHub #1487](https://github.com/Unidata/netcdf-c/pull/1487) for more information.
11+
1012
## 4.7.1 - August 27, 2019
1113

1214
* [Enhancement] Added unit_test directory, which contains unit tests for the libdispatch and libsrc4 code (and any other directories that want to put unit tests there). Use --disable-unit-tests to run without unit tests (ex. for code coverage analysis). See [GitHub #1458](https://github.com/Unidata/netcdf-c/issues/1458).
@@ -26,7 +28,7 @@ read-only access to, for example, Amazon S3 objects and also Thredds Server
2628
datasets via the HTTPService access method.
2729
See [GitHub #1251](https://github.com/Unidata/netcdf-c/issues/1251).
2830

29-
* Update the license from the home-brewed NetCDF license to the standard 3-Clause BSD License. This change does not result in any new restrictions; it is merely the adoption of a standard, well-known and well-understood license in place of the historic NetCDF license written at Unidata. This is part of a broader push by Unidata to adopt modern, standardized licensing.
31+
* Update the license from the home-brewed NetCDF license to the standard 3-Clause BSD License. This change does not result in any new restrictions; it is merely the adoption of a standard, well-known and well-understood license in place of the historic NetCDF license written at Unidata. This is part of a broader push by Unidata to adopt modern, standardized licensing.
3032

3133
## 4.6.3 - February 28, 2019
3234

@@ -35,7 +37,7 @@ See [GitHub #1251](https://github.com/Unidata/netcdf-c/issues/1251).
3537
* Some fixes for rename, including fix for renumbering of varids after a rename (#1307), renaming var to dim without coordinate var. See [Github #1297](https://github.com/Unidata/netcdf-c/issues/1297).
3638
* Fix of NULL parameter causing segfaults in put_vars functions. See [Github #1265](https://github.com/Unidata/netcdf-c/issues/1265) for more information.
3739
* Fix of --enable-benchmark benchmark tests [Github #1211](https://github.com/Unidata/netcdf-c/issues/1211)
38-
* Update the license from the home-brewed NetCDF license to the standard 3-Clause BSD License. This change does not result in any new restrictions; it is merely the adoption of a standard, well-known and well-understood license in place of the historic NetCDF license written at Unidata. This is part of a broader push by Unidata to adopt modern, standardized licensing.
40+
* Update the license from the home-brewed NetCDF license to the standard 3-Clause BSD License. This change does not result in any new restrictions; it is merely the adoption of a standard, well-known and well-understood license in place of the historic NetCDF license written at Unidata. This is part of a broader push by Unidata to adopt modern, standardized licensing.
3941
* [BugFix] Corrected DAP-releated issues on big-endian machines. See [Github #1321](https://github.com/Unidata/netcdf-c/issues/1321), [Github #1302](https://github.com/Unidata/netcdf-c/issues/1302) for more information.
4042
* [BugFix][Enhancement] Various and sundry bugfixes and performance enhancements, thanks to \@edhartnett, \@gsjaardema, \@t-b, \@wkliao, and all of our other contributors.
4143
* [Enhancement] Extended `nccopy -F` syntax to support multiple variables with a single invocation. See [Github #1311](https://github.com/Unidata/netcdf-c/issues/1311) for more information.

include/nc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ extern int add_to_NCList(NC*);
7272
extern void del_from_NCList(NC*);/* does not free object */
7373
extern NC* find_in_NCList(int ext_ncid);
7474
extern NC* find_in_NCList_by_name(const char*);
75+
extern int move_in_NCList(NC *ncp, int new_id);
7576
extern void free_NCList(void);/* reclaim whole list */
7677
extern int count_NCList(void); /* return # of entries in NClist */
7778
extern int iterate_NCList(int i,NC**); /* Walk from 0 ...; ERANGE return => stop */

include/nc4internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ int nc4_file_list_add(int ncid, const char *path, int mode,
388388
int nc4_file_list_get(int ncid, char **path, int *mode,
389389
void **dispatchdata);
390390
int nc4_file_list_del(int ncid);
391+
int nc4_file_change_ncid(int ncid, unsigned short new_ncid_index);
391392
int nc4_var_list_add(NC_GRP_INFO_T* grp, const char* name, int ndims,
392393
NC_VAR_INFO_T **var);
393394
int nc4_var_list_add2(NC_GRP_INFO_T* grp, const char* name,

libdispatch/nclistmgr.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,35 @@ add_to_NCList(NC* ncp)
100100
return NC_NOERR;
101101
}
102102

103+
/**
104+
* Move an NC in the nc_filelist. This is required by PIO.
105+
*
106+
* @param ncp Pointer to already-allocated and initialized NC struct.
107+
* @param new_id New index in the nc_filelist for this file.
108+
*
109+
* @return ::NC_NOERR No error.
110+
* @return ::NC_EINVAL Invalid input.
111+
* @author Ed Hartnett
112+
*/
113+
int
114+
move_in_NCList(NC *ncp, int new_id)
115+
{
116+
/* If no files in list, error. */
117+
if (!nc_filelist)
118+
return NC_EINVAL;
119+
120+
/* If new slot is already taken, error. */
121+
if (nc_filelist[new_id])
122+
return NC_EINVAL;
123+
124+
/* Move the file. */
125+
nc_filelist[ncp->ext_ncid >> ID_SHIFT] = NULL;
126+
nc_filelist[new_id] = ncp;
127+
ncp->ext_ncid = (new_id << ID_SHIFT);
128+
129+
return NC_NOERR;
130+
}
131+
103132
/**
104133
* Delete an NC struct from the list. This happens when the file is
105134
* closed. Relies on all memory in the NC being deallocated after this

libsrc4/nc4internal.c

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,30 +125,42 @@ nc4_file_list_add(int ncid, const char *path, int mode, void **dispatchdata)
125125
return NC_NOERR;
126126
}
127127

128-
/* /\** */
129-
/* * @internal Change the ncid of an open file. This is needed for PIO */
130-
/* * integration. */
131-
/* * */
132-
/* * @param ncid The ncid of the file (aka ext_ncid). */
133-
/* * @param new_ncid The new ncid to use. */
134-
/* * */
135-
/* * @return ::NC_NOERR No error. */
136-
/* * @return ::NC_EBADID No NC struct with this ext_ncid. */
137-
/* * @return ::NC_ENOMEM Out of memory. */
138-
/* * @author Ed Hartnett */
139-
/* *\/ */
140-
/* int */
141-
/* nc4_file_change_ncid(int ncid, int new_ncid) */
142-
/* { */
143-
/* NC *nc; */
144-
/* int ret; */
145-
146-
/* /\* Find NC pointer for this file. *\/ */
147-
/* if ((ret = NC_check_id(ncid, &nc))) */
148-
/* return ret; */
149-
150-
/* return NC_NOERR; */
151-
/* } */
128+
/**
129+
* @internal Change the ncid of an open file. This is needed for PIO
130+
* integration.
131+
*
132+
* @param ncid The ncid of the file (aka ext_ncid).
133+
* @param new_ncid The new ncid index to use (i.e. the first two bytes
134+
* of the ncid).
135+
*
136+
* @return ::NC_NOERR No error.
137+
* @return ::NC_EBADID No NC struct with this ext_ncid.
138+
* @return ::NC_ENOMEM Out of memory.
139+
* @author Ed Hartnett
140+
*/
141+
int
142+
nc4_file_change_ncid(int ncid, unsigned short new_ncid_index)
143+
{
144+
NC *nc;
145+
int ret;
146+
147+
LOG((2, "%s: ncid %d new_ncid_index %d", __func__, ncid, new_ncid_index));
148+
149+
/* Find NC pointer for this file. */
150+
if ((ret = NC_check_id(ncid, &nc)))
151+
return ret;
152+
153+
/* Move it in the list. It will faile if list spot is already
154+
* occupied. */
155+
LOG((3, "moving nc->ext_ncid %d nc->ext_ncid >> ID_SHIFT %d",
156+
nc->ext_ncid, nc->ext_ncid >> ID_SHIFT));
157+
if (move_in_NCList(nc, new_ncid_index))
158+
return NC_EIO;
159+
LOG((3, "moved to new_ncid_index %d new nc->ext_ncid %d", new_ncid_index,
160+
nc->ext_ncid));
161+
162+
return NC_NOERR;
163+
}
152164

153165
/**
154166
* @internal Get info about a file on the list of libsrc4 open

unit_test/tst_nc4internal.c

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -265,36 +265,37 @@ main(int argc, char **argv)
265265
free_NC(ncp);
266266
}
267267
SUMMARIZE_ERR;
268-
/* printf("Testing changing ncid..."); */
269-
/* { */
270-
/* NC *ncp, *ncp2; */
271-
/* int mode = 0; */
272-
/* NCmodel model; */
273-
/* int ret; */
274-
275-
/* /\* Create the NC* instance and insert its dispatcher and model. *\/ */
276-
/* if ((ret = new_NC(NULL, FILE_NAME, mode, &model, &ncp))) ERR; */
268+
printf("Testing changing ncid...");
269+
{
270+
NC *ncp;
271+
NCmodel model;
272+
NC_GRP_INFO_T *grp;
273+
NC_FILE_INFO_T *h5;
274+
int old_ncid;
277275

278-
/* /\* Add to list of known open files and define ext_ncid. *\/ */
279-
/* add_to_NCList(ncp); */
276+
/* Create the NC, add it to nc_filelist array, add and init
277+
* NC_FILE_INFO_T. */
278+
if (new_NC(NC3_dispatch_table, FILE_NAME, 0, &model, &ncp)) ERR;
279+
add_to_NCList(ncp);
280+
if (nc4_file_list_add(ncp->ext_ncid, FILE_NAME, 0, NULL)) ERR;
281+
if (nc4_find_nc_grp_h5(ncp->ext_ncid, NULL, &grp, &h5)) ERR;
280282

281-
/* /\* Find it in the list. *\/ */
282-
/* if (!(ncp2 = find_in_NCList(ncp->ext_ncid))) ERR; */
283-
/* if (!(ncp2 = find_in_NCList_by_name(FILE_NAME))) ERR; */
284-
/* if ((ret = iterate_NCList(1, &ncp2))) ERR; */
285-
/* if (count_NCList() != 1) ERR; */
283+
/* Change the ncid. */
284+
old_ncid = ncp->ext_ncid;
285+
if (nc4_file_change_ncid(ncp->ext_ncid, TEST_VAL_42)) ERR;
286286

287-
/* /\* Change the ncid. *\/ */
288-
/* if (nc4_file_change_ncid(ncp->ext_ncid, TEST_VAL_42)) ERR; */
287+
/* Can't find old ncid. */
288+
if (nc4_find_nc_grp_h5(old_ncid, NULL, NULL, NULL) != NC_EBADID) ERR;
289289

290-
/* /\* Delete it. *\/ */
291-
/* del_from_NCList(ncp); /\* Will free empty list. *\/ */
292-
/* free_NC(ncp); */
290+
/* Delete it. */
291+
if (nc4_file_list_del(ncp->ext_ncid)) ERR;
292+
del_from_NCList(ncp); /* Will free empty list. */
293+
free_NC(ncp);
293294

294-
/* /\* Ensure it is no longer in list. *\/ */
295-
/* /\* if (find_in_NCList(ncp->ext_ncid)) ERR; *\/ */
295+
/* Ensure it is no longer in list. */
296+
/* if (find_in_NCList(ncp->ext_ncid)) ERR; */
296297

297-
/* } */
298-
/* SUMMARIZE_ERR; */
298+
}
299+
SUMMARIZE_ERR;
299300
FINAL_RESULTS;
300301
}

unit_test/tst_nclist.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,37 @@ main(int argc, char **argv)
7777
if (find_in_NCList(ncid)) ERR;
7878
}
7979
SUMMARIZE_ERR;
80+
printf("Testing moving in NC list (needed for PIO)...");
81+
{
82+
int ncid;
83+
NC *ncp, *ncp2;
84+
int mode = 0;
85+
NCmodel model;
86+
int ret;
87+
88+
/* Create the NC* instance and add it to list. */
89+
if ((ret = new_NC(NULL, FILE_NAME, mode, &model, &ncp))) ERR;
90+
add_to_NCList(ncp);
91+
92+
/* Find it in the list. */
93+
if (!(ncp2 = find_in_NCList(ncp->ext_ncid))) ERR;
94+
95+
/* Move it. */
96+
ncid = ncp->ext_ncid;
97+
if (move_in_NCList(ncp, TEST_VAL_42)) ERR;
98+
99+
/* Now we won't find old ncid in the list. */
100+
if (find_in_NCList(ncid)) ERR;
101+
102+
/* Delete it. */
103+
ncid = ncp->ext_ncid;
104+
del_from_NCList(ncp); /* Will free empty list. */
105+
free_NC(ncp);
106+
107+
/* Ensure it is no longer in list. */
108+
if (find_in_NCList(ncid)) ERR;
109+
}
110+
SUMMARIZE_ERR;
80111
#ifdef LARGE_FILE_TESTS
81112
/* This test is slow, only run it on large file test builds. */
82113
printf("Testing maxing out NC list...");

0 commit comments

Comments
 (0)