nc4attr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. This file is part of netcdf-4, a netCDF-like interface for HDF5, or a
  3. HDF5 backend for netCDF, depending on your point of view.
  4. This file handles the nc4 attribute functions.
  5. Remember that with atts, type conversion can take place when writing
  6. them, and when reading them.
  7. Copyright 2003-2011, University Corporation for Atmospheric
  8. Research. See COPYRIGHT file for copying and redistribution
  9. conditions.
  10. */
  11. #include "nc4internal.h"
  12. #include "nc.h"
  13. #include "nc4dispatch.h"
  14. #include "ncdispatch.h"
  15. #ifdef USE_PNETCDF
  16. #include <pnetcdf.h>
  17. #endif
  18. int nc4typelen(nc_type type);
  19. /* Get or put attribute metadata from our linked list of file
  20. info. Always locate the attribute by name, never by attnum.
  21. The mem_type is ignored if data=NULL. */
  22. int
  23. nc4_get_att(int ncid, NC_FILE_INFO_T *nc, int varid, const char *name,
  24. nc_type *xtype, nc_type mem_type, size_t *lenp,
  25. int *attnum, int is_long, void *data)
  26. {
  27. NC_GRP_INFO_T *grp;
  28. NC_HDF5_FILE_INFO_T *h5;
  29. NC_ATT_INFO_T *att;
  30. int my_attnum = -1;
  31. int need_to_convert = 0;
  32. int range_error = NC_NOERR;
  33. void *bufr = NULL;
  34. size_t type_size;
  35. char norm_name[NC_MAX_NAME + 1];
  36. int i;
  37. int retval = NC_NOERR;
  38. if (attnum)
  39. my_attnum = *attnum;
  40. assert(nc && nc->nc4_info);
  41. LOG((3, "nc4_get_att: ncid 0x%x varid %d name %s attnum %d mem_type %d",
  42. ncid, varid, name, my_attnum, mem_type));
  43. /* Find info for this file and group, and set pointer to each. */
  44. h5 = nc->nc4_info;
  45. if (!(grp = nc4_rec_find_grp(h5->root_grp, (ncid & GRP_ID_MASK))))
  46. return NC_EBADGRPID;
  47. /* Normalize name. */
  48. if ((retval = nc4_normalize_name(name, norm_name)))
  49. return retval;
  50. /* Find the attribute, if it exists. If we don't find it, we are
  51. major failures. */
  52. if ((retval = nc4_find_grp_att(grp, varid, norm_name, my_attnum, &att)))
  53. return retval;
  54. /* If mem_type is NC_NAT, it means we want to use the attribute's
  55. * file type as the mem type as well. */
  56. if (mem_type == NC_NAT)
  57. mem_type = att->xtype;
  58. /* If the attribute is NC_CHAR, and the mem_type isn't, or vice
  59. * versa, that's a freakish attempt to convert text to
  60. * numbers. Some pervert out there is trying to pull a fast one!
  61. * Send him an NC_ECHAR error...*/
  62. if (data && att->len &&
  63. ((att->xtype == NC_CHAR && mem_type != NC_CHAR) ||
  64. (att->xtype != NC_CHAR && mem_type == NC_CHAR)))
  65. return NC_ECHAR; /* take that, you freak! */
  66. /* Copy the info. */
  67. if (lenp)
  68. *lenp = att->len;
  69. if (xtype)
  70. *xtype = att->xtype;
  71. if (attnum)
  72. *attnum = att->attnum;
  73. /* Zero len attributes are easy to read! */
  74. if (!att->len)
  75. return NC_NOERR;
  76. /* Later on, we will need to know the size of this type. */
  77. if ((retval = nc4_get_typelen_mem(h5, mem_type, is_long, &type_size)))
  78. return retval;
  79. /* We may have to convert data. Treat NC_CHAR the same as
  80. * NC_UBYTE. If the mem_type is NAT, don't try any conversion - use
  81. * the attribute's type. */
  82. if (data && att->len && mem_type != att->xtype &&
  83. mem_type != NC_NAT &&
  84. !(mem_type == NC_CHAR &&
  85. (att->xtype == NC_UBYTE || att->xtype == NC_BYTE)))
  86. {
  87. need_to_convert++;
  88. if (!(bufr = malloc((size_t)(att->len * type_size))))
  89. return NC_ENOMEM;
  90. if ((retval = nc4_convert_type(att->data, bufr, att->xtype,
  91. mem_type, (size_t)att->len, &range_error,
  92. NULL, (h5->cmode & NC_CLASSIC_MODEL), 0, is_long)))
  93. BAIL(retval);
  94. /* For strict netcdf-3 rules, ignore erange errors between UBYTE
  95. * and BYTE types. */
  96. if ((h5->cmode & NC_CLASSIC_MODEL) &&
  97. (att->xtype == NC_UBYTE || att->xtype == NC_BYTE) &&
  98. (mem_type == NC_UBYTE || mem_type == NC_BYTE) &&
  99. range_error)
  100. range_error = 0;
  101. }
  102. else
  103. {
  104. bufr = att->data;
  105. }
  106. /* If the caller wants data, copy it for him. If he hasn't
  107. allocated enough memory for it, he will burn in segmantation
  108. fault hell, writhing with the agony of undiscovered memory
  109. bugs! */
  110. if (data)
  111. {
  112. if (att->vldata)
  113. {
  114. size_t base_typelen = type_size;
  115. hvl_t *vldest = data;
  116. NC_TYPE_INFO_T *type;
  117. if ((retval = nc4_find_type(h5, att->xtype, &type)))
  118. return retval;
  119. for (i = 0; i < att->len; i++)
  120. {
  121. vldest[i].len = att->vldata[i].len;
  122. if (!(vldest[i].p = malloc(vldest[i].len * base_typelen)))
  123. BAIL(NC_ENOMEM);
  124. memcpy(vldest[i].p, att->vldata[i].p, vldest[i].len * base_typelen);
  125. }
  126. }
  127. else if (att->stdata)
  128. {
  129. for (i = 0; i < att->len; i++)
  130. {
  131. if (!(((char **)data)[i] = malloc(strlen(att->stdata[i]) + 1)))
  132. BAIL(NC_ENOMEM);
  133. strcpy(((char **)data)[i], att->stdata[i]);
  134. }
  135. }
  136. else
  137. {
  138. /* For long types, we need to handle this special... */
  139. if (is_long && att->xtype == NC_INT)
  140. {
  141. long *lp = data;
  142. int *ip = bufr;
  143. for (i = 0; i < att->len; i++)
  144. *lp++ = *ip++;
  145. }
  146. else
  147. memcpy(data, bufr, (size_t)(att->len * type_size));
  148. }
  149. }
  150. exit:
  151. if (need_to_convert) free(bufr);
  152. if (retval)
  153. return retval;
  154. if (range_error)
  155. return NC_ERANGE;
  156. return NC_NOERR;
  157. }
  158. /* Put attribute metadata into our global metadata. */
  159. int
  160. nc4_put_att(int ncid, NC_FILE_INFO_T *nc, int varid, const char *name,
  161. nc_type file_type, nc_type mem_type, size_t len, int is_long,
  162. const void *data)
  163. {
  164. NC_GRP_INFO_T *grp;
  165. NC_HDF5_FILE_INFO_T *h5;
  166. NC_VAR_INFO_T *var = NULL;
  167. NC_ATT_INFO_T *att, **attlist = NULL, *varatt;
  168. NC_TYPE_INFO_T *type = NULL;
  169. char norm_name[NC_MAX_NAME + 1];
  170. int new_att = 0;
  171. int retval = NC_NOERR, range_error = 0;
  172. size_t type_size;
  173. int i;
  174. int res;
  175. if (!name)
  176. return NC_EBADNAME;
  177. assert(nc && nc->nc4_info);
  178. LOG((1, "nc4_put_att: ncid 0x%x varid %d name %s "
  179. "file_type %d mem_type %d len %d", ncid, varid,
  180. name, file_type, mem_type, len));
  181. /* If len is not zero, then there must be some data. */
  182. if (len && !data)
  183. return NC_EINVAL;
  184. /* Find info for this file and group, and set pointer to each. */
  185. h5 = nc->nc4_info;
  186. if (!(grp = nc4_rec_find_grp(h5->root_grp, (ncid & GRP_ID_MASK))))
  187. return NC_EBADGRPID;
  188. /* If the file is read-only, return an error. */
  189. if (h5->no_write)
  190. return NC_EPERM;
  191. /* Check and normalize the name. */
  192. if ((retval = nc4_check_name(name, norm_name)))
  193. return retval;
  194. /* Find att, if it exists. */
  195. if (varid == NC_GLOBAL)
  196. attlist = &grp->att;
  197. else
  198. {
  199. for (var = grp->var; var; var = var->next)
  200. if (var->varid == varid)
  201. {
  202. attlist = &var->att;
  203. break;
  204. }
  205. if (!var)
  206. return NC_ENOTVAR;
  207. }
  208. for (att = *attlist; att; att = att->next)
  209. if (!strcmp(att->name, norm_name))
  210. break;
  211. if (!att)
  212. {
  213. /* If this is a new att, require define mode. */
  214. if (!(h5->flags & NC_INDEF))
  215. {
  216. if (h5->cmode & NC_CLASSIC_MODEL)
  217. return NC_EINDEFINE;
  218. if ((retval = NC4_redef(ncid)))
  219. BAIL(retval);
  220. }
  221. new_att++;
  222. }
  223. else
  224. {
  225. /* For an existing att, if we're not in define mode, the len
  226. must not be greater than the existing len for classic model. */
  227. if (!(h5->flags & NC_INDEF) &&
  228. len * nc4typelen(file_type) > (size_t)att->len * nc4typelen(att->xtype))
  229. {
  230. if (h5->cmode & NC_CLASSIC_MODEL)
  231. return NC_EINDEFINE;
  232. if ((retval = NC4_redef(ncid)))
  233. BAIL(retval);
  234. }
  235. }
  236. /* We must have two valid types to continue. */
  237. if (file_type == NC_NAT || mem_type == NC_NAT)
  238. return NC_EBADTYPE;
  239. /* Get information about this type. */
  240. if ((retval = nc4_find_type(h5, file_type, &type)))
  241. return retval;
  242. if ((retval = nc4_get_typelen_mem(h5, file_type, is_long, &type_size)))
  243. return retval;
  244. /* No character conversions are allowed. */
  245. if (file_type != mem_type &&
  246. (file_type == NC_CHAR || mem_type == NC_CHAR ||
  247. file_type == NC_STRING || mem_type == NC_STRING))
  248. return NC_ECHAR;
  249. /* For classic mode file, only allow atts with classic types to be
  250. * created. */
  251. if (h5->cmode & NC_CLASSIC_MODEL && file_type > NC_DOUBLE)
  252. return NC_ESTRICTNC3;
  253. /* Add to the end of the attribute list, if this att doesn't
  254. already exist. */
  255. if (new_att)
  256. {
  257. LOG((3, "adding attribute %s to the list...", norm_name));
  258. if ((res = nc4_att_list_add(attlist)))
  259. BAIL (res);
  260. /* Find this att's entry in the list (the last one). */
  261. for (att=*attlist; att->next; att=att->next)
  262. ;
  263. }
  264. /* Now fill in the metadata. */
  265. att->dirty++;
  266. if (att->name)
  267. free(att->name);
  268. if (!(att->name = malloc((strlen(norm_name) + 1) * sizeof(char))))
  269. return NC_ENOMEM;
  270. strcpy(att->name, norm_name);
  271. att->xtype = file_type;
  272. att->len = len;
  273. if (att->prev)
  274. att->attnum = att->prev->attnum + 1;
  275. else
  276. att->attnum = 0;
  277. if (type)
  278. att->class = type->class;
  279. /* If this is the _FillValue attribute, then we will also have to
  280. * copy the value to the fll_vlue pointer of the NC_VAR_INFO_T
  281. * struct for this var. (But ignore a global _FillValue
  282. * attribute). */
  283. if (!strcmp(att->name, _FillValue) && varid != NC_GLOBAL)
  284. {
  285. NC_TYPE_INFO_T *type_info;
  286. int size;
  287. /* Fill value must be same type. */
  288. if (att->xtype != var->xtype)
  289. return NC_EINVAL;
  290. /* If we already wrote to the dataset, then return an error. */
  291. if (var->written_to)
  292. return NC_ELATEFILL;
  293. /* If fill value hasn't been set, allocate space. Of course,
  294. * vlens have to be differnt... */
  295. if ((retval = nc4_get_typelen_mem(grp->file->nc4_info, var->xtype, 0,
  296. &type_size)))
  297. return retval;
  298. if ((retval = nc4_find_type(grp->file->nc4_info, var->xtype, &type_info)))
  299. BAIL(retval);
  300. /* Already set a fill value? Now I'll have to free the old
  301. * one. Make up your damn mind, would you? */
  302. if (var->fill_value)
  303. {
  304. if (type_info && type_info->class == NC_VLEN)
  305. if ((retval = nc_free_vlen(var->fill_value)))
  306. return retval;
  307. free(var->fill_value);
  308. }
  309. /* Allocate space for the fill value. */
  310. if (type_info && type_info->class == NC_VLEN)
  311. size = sizeof(hvl_t);
  312. else if (var->xtype == NC_STRING)
  313. size = sizeof(char *);
  314. else
  315. size = type_size;
  316. /* size = strlen(*(char **)data) + 1; */
  317. if (!(var->fill_value = malloc(size)))
  318. return NC_ENOMEM;
  319. /* Copy the fill_value. */
  320. LOG((4, "Copying fill value into metadata for variable %s", var->name));
  321. if (type_info && type_info->class == NC_VLEN)
  322. {
  323. nc_vlen_t *in_vlen = (nc_vlen_t *)data, *fv_vlen = (nc_vlen_t *)(var->fill_value);
  324. fv_vlen->len = in_vlen->len;
  325. if (!(fv_vlen->p = malloc(size * in_vlen->len)))
  326. return NC_ENOMEM;
  327. memcpy(fv_vlen->p, in_vlen->p, in_vlen->len * size);
  328. }
  329. else if (var->xtype == NC_STRING)
  330. {
  331. if (!(*(char **)var->fill_value = malloc(strlen(*(char **)data) + 1)))
  332. return NC_ENOMEM;
  333. strcpy(*(char **)(var->fill_value), *(char **)data);
  334. }
  335. else
  336. memcpy(var->fill_value, data, type_size);
  337. /* Mark the var and all its atts as dirty, so they get
  338. * rewritten. */
  339. var->dirty++;
  340. for (varatt = var->att; varatt; varatt = varatt->next)
  341. varatt->dirty++;
  342. }
  343. /* Copy the attribute data, if there is any. VLENs and string
  344. * arrays have to be handled specially. */
  345. if (type && type->class == NC_VLEN && data && att->len)
  346. {
  347. const hvl_t *vldata1;
  348. vldata1 = data;
  349. if (!(att->vldata = malloc(att->len * sizeof(hvl_t))))
  350. BAIL(NC_ENOMEM);
  351. for (i = 0; i < att->len; i++)
  352. {
  353. att->vldata[i].len = vldata1[i].len;
  354. if (!(att->vldata[i].p = malloc(type_size * att->vldata[i].len)))
  355. BAIL(NC_ENOMEM);
  356. memcpy(att->vldata[i].p, vldata1[i].p, type_size * att->vldata[i].len);
  357. }
  358. }
  359. else if (file_type == NC_STRING && data && att->len)
  360. {
  361. LOG((4, "copying array of NC_STRING"));
  362. if (!(att->stdata = malloc(sizeof(char *) * att->len)))
  363. BAIL(NC_ENOMEM);
  364. for (i = 0; i < att->len; i++)
  365. {
  366. LOG((5, "copying string %d of size %d", i, strlen(((char **)data)[i]) + 1));
  367. if (!(att->stdata[i] = malloc(strlen(((char **)data)[i]) + 1)))
  368. BAIL(NC_ENOMEM);
  369. strcpy(att->stdata[i], ((char **)data)[i]);
  370. }
  371. }
  372. else
  373. {
  374. /* Data types are like religions, in that one can convert. */
  375. if (att->len)
  376. {
  377. if (!new_att)
  378. free (att->data);
  379. if (!(att->data = malloc(att->len * type_size)))
  380. BAIL(NC_ENOMEM);
  381. if (type)
  382. {
  383. /* Just copy the data... */
  384. if (type->class == NC_OPAQUE || type->class == NC_COMPOUND || type->class == NC_ENUM)
  385. memcpy(att->data, data, len * type_size);
  386. else
  387. LOG((0, "nc4_put_att: unknown type."));
  388. }
  389. else
  390. {
  391. if ((retval = nc4_convert_type(data, att->data, mem_type, file_type,
  392. len, &range_error, NULL,
  393. (h5->cmode & NC_CLASSIC_MODEL), is_long, 0)))
  394. BAIL(retval);
  395. }
  396. }
  397. }
  398. att->dirty = 1;
  399. att->created = 0;
  400. exit:
  401. /* If there was an error return it, otherwise return any potential
  402. range error value. If none, return NC_NOERR as usual.*/
  403. if (retval)
  404. return retval;
  405. if (range_error)
  406. return NC_ERANGE;
  407. return NC_NOERR;
  408. }
  409. /* Learn about an att. All the nc4 nc_inq_ functions just call
  410. * add_meta_get to get the metadata on an attribute. */
  411. int
  412. NC4_inq_att(int ncid, int varid, const char *name, nc_type *xtypep, size_t *lenp)
  413. {
  414. NC_FILE_INFO_T *nc;
  415. LOG((2, "nc_inq_att: ncid 0x%x varid %d name %s", ncid, varid, name));
  416. /* Find metadata. */
  417. if (!(nc = nc4_find_nc_file(ncid)))
  418. return NC_EBADID;
  419. #ifdef USE_PNETCDF
  420. /* Take care of files created/opened with parallel-netcdf library. */
  421. if (nc->pnetcdf_file)
  422. {
  423. MPI_Offset mpi_len;
  424. int ret;
  425. if ((ret = ncmpi_inq_att(nc->int_ncid, varid, name, xtypep, &mpi_len)))
  426. return ret;
  427. if (lenp)
  428. *lenp = mpi_len;
  429. }
  430. #endif /* USE_PNETCDF */
  431. /* Handle netcdf-3 files. */
  432. assert(nc->nc4_info);
  433. /* Handle netcdf-4 files. */
  434. return nc4_get_att(ncid, nc, varid, name, xtypep, NC_UBYTE, lenp, NULL, 0, NULL);
  435. }
  436. /* Learn an attnum, given a name. */
  437. int
  438. NC4_inq_attid(int ncid, int varid, const char *name, int *attnump)
  439. {
  440. NC_FILE_INFO_T *nc;
  441. LOG((2, "nc_inq_attid: ncid 0x%x varid %d name %s", ncid, varid, name));
  442. /* Find metadata. */
  443. if (!(nc = nc4_find_nc_file(ncid)))
  444. return NC_EBADID;
  445. #ifdef USE_PNETCDF
  446. /* Take care of files created/opened with parallel-netcdf library. */
  447. if (nc->pnetcdf_file)
  448. return ncmpi_inq_attid(nc->int_ncid, varid, name, attnump);
  449. #endif /* USE_PNETCDF */
  450. /* Handle netcdf-3 files. */
  451. assert(nc->nc4_info);
  452. /* Handle netcdf-4 files. */
  453. return nc4_get_att(ncid, nc, varid, name, NULL, NC_UBYTE,
  454. NULL, attnump, 0, NULL);
  455. }
  456. /* Given an attnum, find the att's name. */
  457. int
  458. NC4_inq_attname(int ncid, int varid, int attnum, char *name)
  459. {
  460. NC_FILE_INFO_T *nc;
  461. NC_ATT_INFO_T *att;
  462. int retval = NC_NOERR;
  463. LOG((2, "nc_inq_attname: ncid 0x%x varid %d attnum %d",
  464. ncid, varid, attnum));
  465. /* Find metadata. */
  466. if (!(nc = nc4_find_nc_file(ncid)))
  467. return NC_EBADID;
  468. #ifdef USE_PNETCDF
  469. /* Take care of files created/opened with parallel-netcdf library. */
  470. if (nc->pnetcdf_file)
  471. return ncmpi_inq_attname(nc->int_ncid, varid, attnum, name);
  472. #endif /* USE_PNETCDF */
  473. /* Handle netcdf-3 files. */
  474. assert(nc->nc4_info);
  475. /* Handle netcdf-4 files. */
  476. if ((retval = nc4_find_nc_att(ncid, varid, NULL, attnum, &att)))
  477. return retval;
  478. /* Get the name. */
  479. if (name)
  480. strcpy(name, att->name);
  481. return NC_NOERR;
  482. }
  483. /* I think all atts should be named the exact same thing, to avoid
  484. confusion! */
  485. int
  486. NC4_rename_att(int ncid, int varid, const char *name,
  487. const char *newname)
  488. {
  489. NC_FILE_INFO_T *nc;
  490. NC_GRP_INFO_T *grp;
  491. NC_HDF5_FILE_INFO_T *h5;
  492. NC_VAR_INFO_T *var;
  493. NC_ATT_INFO_T *att, *list;
  494. char norm_newname[NC_MAX_NAME + 1], norm_name[NC_MAX_NAME + 1];
  495. hid_t datasetid = 0;
  496. int retval = NC_NOERR;
  497. if (!name || !newname)
  498. return NC_EINVAL;
  499. LOG((2, "nc_rename_att: ncid 0x%x varid %d name %s newname %s",
  500. ncid, varid, name, newname));
  501. /* If the new name is too long, that's an error. */
  502. if (strlen(newname) > NC_MAX_NAME)
  503. return NC_EMAXNAME;
  504. /* Find metadata for this file. */
  505. if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
  506. return retval;
  507. #ifdef USE_PNETCDF
  508. /* Take care of files created/opened with parallel-netcdf library. */
  509. if (nc->pnetcdf_file)
  510. return ncmpi_rename_att(nc->int_ncid, varid, name, newname);
  511. #endif /* USE_PNETCDF */
  512. /* Handle netcdf-3 files. */
  513. assert(h5);
  514. /* If the file is read-only, return an error. */
  515. if (h5->no_write)
  516. return NC_EPERM;
  517. /* Check and normalize the name. */
  518. if ((retval = nc4_check_name(newname, norm_newname)))
  519. return retval;
  520. /* Is norm_newname in use? */
  521. if (varid == NC_GLOBAL)
  522. {
  523. list = grp->att;
  524. }
  525. else
  526. {
  527. for (var = grp->var; var; var = var->next)
  528. if (var->varid == varid)
  529. {
  530. list = var->att;
  531. break;
  532. }
  533. if (!var)
  534. return NC_ENOTVAR;
  535. }
  536. for (att = list; att; att = att->next)
  537. if (!strncmp(att->name, norm_newname, NC_MAX_NAME))
  538. return NC_ENAMEINUSE;
  539. /* Normalize name and find the attribute. */
  540. if ((retval = nc4_normalize_name(name, norm_name)))
  541. return retval;
  542. for (att = list; att; att = att->next)
  543. if (!strncmp(att->name, norm_name, NC_MAX_NAME))
  544. break;
  545. if (!att)
  546. return NC_ENOTATT;
  547. /* If we're not in define mode, new name must be of equal or
  548. less size, if complying with strict NC3 rules. */
  549. if (!(h5->flags & NC_INDEF) && strlen(norm_newname) > strlen(att->name) &&
  550. (h5->cmode & NC_CLASSIC_MODEL))
  551. return NC_ENOTINDEFINE;
  552. /* Delete the original attribute, if it exists in the HDF5 file. */
  553. if (att->created)
  554. {
  555. if (varid == NC_GLOBAL)
  556. {
  557. if (H5Adelete(grp->hdf_grpid, att->name) < 0)
  558. return NC_EHDFERR;
  559. }
  560. else
  561. {
  562. if ((retval = nc4_open_var_grp2(grp, varid, &datasetid)))
  563. return retval;
  564. if (H5Adelete(datasetid, att->name) < 0)
  565. return NC_EHDFERR;
  566. }
  567. att->created = 0;
  568. }
  569. /* Copy the new name into our metadata. */
  570. free(att->name);
  571. if (!(att->name = malloc((strlen(norm_newname) + 1) * sizeof(char))))
  572. return NC_ENOMEM;
  573. strcpy(att->name, norm_newname);
  574. att->dirty = 1;
  575. return retval;
  576. }
  577. /* Delete an att. Rub it out. Push the button on it. Liquidate
  578. it. Bump it off. Take it for a one-way ride. Terminate it. Drop the
  579. bomb on it. You get the idea.
  580. Ed Hartnett, 10/1/3
  581. */
  582. int
  583. NC4_del_att(int ncid, int varid, const char *name)
  584. {
  585. NC_FILE_INFO_T *nc;
  586. NC_GRP_INFO_T *grp;
  587. NC_HDF5_FILE_INFO_T *h5;
  588. NC_ATT_INFO_T *att, *natt;
  589. NC_VAR_INFO_T *var;
  590. NC_ATT_INFO_T **attlist = NULL;
  591. hid_t locid = 0, datasetid = 0;
  592. int retval = NC_NOERR;
  593. if (!name)
  594. return NC_EINVAL;
  595. LOG((2, "nc_del_att: ncid 0x%x varid %d name %s",
  596. ncid, varid, name));
  597. /* Find metadata for this file. */
  598. if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
  599. return retval;
  600. #ifdef USE_PNETCDF
  601. /* Take care of files created/opened with parallel-netcdf library. */
  602. if (nc->pnetcdf_file)
  603. return ncmpi_del_att(nc->int_ncid, varid, name);
  604. #endif /* USE_PNETCDF */
  605. /* Handle netcdf-3 files. */
  606. assert(h5);
  607. assert(h5 && grp);
  608. /* If the file is read-only, return an error. */
  609. if (h5->no_write)
  610. return NC_EPERM;
  611. /* If it's not in define mode, forget it. */
  612. if (!(h5->flags & NC_INDEF))
  613. {
  614. if (h5->cmode & NC_CLASSIC_MODEL)
  615. return NC_ENOTINDEFINE;
  616. if ((retval = NC4_redef(ncid)))
  617. BAIL(retval);
  618. }
  619. /* Get either the global or a variable attribute list. Also figure
  620. out the HDF5 location it's attached to. */
  621. if (varid == NC_GLOBAL)
  622. {
  623. attlist = &grp->att;
  624. locid = grp->hdf_grpid;
  625. }
  626. else
  627. {
  628. for(var = grp->var; var; var = var->next)
  629. {
  630. if (var->varid == varid)
  631. {
  632. attlist = &var->att;
  633. break;
  634. }
  635. }
  636. if (!var)
  637. return NC_ENOTVAR;
  638. if (var->created)
  639. {
  640. locid = var->hdf_datasetid;
  641. }
  642. }
  643. /* Now find the attribute by name or number. */
  644. for (att = *attlist; att; att = att->next)
  645. if (!strcmp(att->name, name))
  646. break;
  647. /* If att is NULL, we couldn't find the attribute. */
  648. if (!att)
  649. BAIL_QUIET(NC_ENOTATT);
  650. /* Delete it from the HDF5 file, if it's been created. */
  651. if (att->created)
  652. if(H5Adelete(locid, att->name) < 0)
  653. BAIL(NC_EATTMETA);
  654. /* Renumber all following attributes. */
  655. for (natt = att->next; natt; natt = natt->next)
  656. natt->attnum--;
  657. /* Delete this attribute from this list. */
  658. if ((retval = nc4_att_list_del(attlist, att)))
  659. BAIL(retval);
  660. exit:
  661. if (datasetid > 0) H5Dclose(datasetid);
  662. return retval;
  663. }
  664. /* Write an attribute with type conversion. */
  665. int
  666. nc4_put_att_tc(int ncid, int varid, const char *name, nc_type file_type,
  667. nc_type mem_type, int mem_type_is_long, size_t len,
  668. const void *op)
  669. {
  670. NC_FILE_INFO_T *nc;
  671. if (!name || strlen(name) > NC_MAX_NAME)
  672. return NC_EBADNAME;
  673. LOG((3, "nc4_put_att_tc: ncid 0x%x varid %d name %s file_type %d "
  674. "mem_type %d len %d", ncid, varid, name, file_type, mem_type, len));
  675. /* The length needs to be positive (cast needed for braindead
  676. systems with signed size_t). */
  677. if((unsigned long) len > X_INT_MAX)
  678. return NC_EINVAL;
  679. /* Find metadata. */
  680. if (!(nc = nc4_find_nc_file(ncid)))
  681. return NC_EBADID;
  682. #ifdef USE_PNETCDF
  683. /* Take care of files created/opened with parallel-netcdf library. */
  684. if (nc->pnetcdf_file)
  685. {
  686. if (mem_type == NC_UBYTE)
  687. mem_type = NC_BYTE;
  688. switch(mem_type)
  689. {
  690. case NC_BYTE:
  691. return ncmpi_put_att_schar(nc->int_ncid, varid, name,
  692. file_type, len, op);
  693. case NC_CHAR:
  694. return ncmpi_put_att_text(nc->int_ncid, varid, name,
  695. len, op);
  696. case NC_SHORT:
  697. return ncmpi_put_att_short(nc->int_ncid, varid, name,
  698. file_type, len, op);
  699. case NC_INT:
  700. if (mem_type_is_long)
  701. return ncmpi_put_att_long(nc->int_ncid, varid, name,
  702. file_type, len, op);
  703. else
  704. return ncmpi_put_att_int(nc->int_ncid, varid, name,
  705. file_type, len, op);
  706. case NC_FLOAT:
  707. return ncmpi_put_att_float(nc->int_ncid, varid, name,
  708. file_type, len, op);
  709. case NC_DOUBLE:
  710. return ncmpi_put_att_double(nc->int_ncid, varid, name,
  711. file_type, len, op);
  712. case NC_NAT:
  713. default:
  714. return NC_EBADTYPE;
  715. }
  716. }
  717. #endif /* USE_PNETCDF */
  718. /* Handle netcdf-3 files. */
  719. assert(nc->nc4_info);
  720. /* Otherwise, handle things the netcdf-4 way. */
  721. return nc4_put_att(ncid, nc, varid, name, file_type, mem_type, len,
  722. mem_type_is_long, op);
  723. }
  724. /* Read an attribute of any type, with type conversion. This may be
  725. * called by any of the nc_get_att_* functions. */
  726. int
  727. nc4_get_att_tc(int ncid, int varid, const char *name, nc_type mem_type,
  728. int mem_type_is_long, void *ip)
  729. {
  730. NC_FILE_INFO_T *nc;
  731. LOG((3, "nc4_get_att_tc: ncid 0x%x varid %d name %s mem_type %d",
  732. ncid, varid, name, mem_type));
  733. /* Find metadata. */
  734. if (!(nc = nc4_find_nc_file(ncid)))
  735. return NC_EBADID;
  736. #ifdef USE_PNETCDF
  737. /* Take care of files created/opened with parallel-netcdf library. */
  738. if (nc->pnetcdf_file)
  739. {
  740. if (mem_type == NC_UBYTE)
  741. mem_type = NC_BYTE;
  742. switch(mem_type)
  743. {
  744. case NC_BYTE:
  745. return ncmpi_get_att_schar(nc->int_ncid, varid, name, ip);
  746. case NC_CHAR:
  747. return ncmpi_get_att_text(nc->int_ncid, varid, name, ip);
  748. case NC_SHORT:
  749. return ncmpi_get_att_short(nc->int_ncid, varid, name, ip);
  750. case NC_INT:
  751. if (mem_type_is_long)
  752. return ncmpi_get_att_long(nc->int_ncid, varid, name, ip);
  753. else
  754. return ncmpi_get_att_int(nc->int_ncid, varid, name, ip);
  755. case NC_FLOAT:
  756. return ncmpi_get_att_float(nc->int_ncid, varid, name, ip);
  757. case NC_DOUBLE:
  758. return ncmpi_get_att_double(nc->int_ncid, varid, name, ip);
  759. case NC_NAT:
  760. default:
  761. return NC_EBADTYPE;
  762. }
  763. }
  764. #endif /* USE_PNETCDF */
  765. /* Handle netcdf-3 files. */
  766. assert(nc->nc4_info);
  767. return nc4_get_att(ncid, nc, varid, name, NULL, mem_type,
  768. NULL, NULL, mem_type_is_long, ip);
  769. }
  770. int
  771. NC4_put_att(int ncid, int varid, const char *name, nc_type xtype,
  772. size_t nelems, const void *value, nc_type memtype)
  773. {
  774. return nc4_put_att_tc(ncid, varid, name, xtype, memtype, 0, nelems, value);
  775. }
  776. int
  777. NC4_get_att(int ncid, int varid, const char *name, void *value, nc_type memtype)
  778. {
  779. return nc4_get_att_tc(ncid, varid, name, memtype, 0, value);
  780. }