close.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*!
  2. * \file lib/raster/close.c
  3. *
  4. * \brief Raster Library - Close raster file
  5. *
  6. * (C) 1999-2009 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. * \author USACERL and many others
  13. */
  14. #ifdef __MINGW32__
  15. # include <windows.h>
  16. #endif
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <signal.h>
  23. #include <errno.h>
  24. #include <grass/gis.h>
  25. #include <grass/raster.h>
  26. #include <grass/glocale.h>
  27. #include "R.h"
  28. #define FORMAT_FILE "f_format"
  29. #define QUANT_FILE "f_quant"
  30. #define NULL_FILE "null"
  31. /* cmpressed null file */
  32. #define NULLC_FILE "nullcmpr"
  33. static int close_old(int);
  34. static int close_new(int, int);
  35. static void sync_and_close(int fd, char *element, char *name)
  36. {
  37. /* from man 2 write:
  38. * A successful return from write() does not make any guarantee
  39. * that data has been committed to disk. On some filesystems,
  40. * including NFS, it does not even guarantee that space has
  41. * successfully been reserved for the data. In this case, some
  42. * errors might be delayed until a future write(2), fsync(2), or
  43. * even close(2). The only way to be sure is to call fsync(2)
  44. * after you are done writing all your data.
  45. */
  46. #ifndef __MINGW32__
  47. if (fsync(fd)) {
  48. G_warning(_("Unable to flush file %s for raster map %s: %s"),
  49. element, name, strerror(errno));
  50. }
  51. /* for MS Windows, try fdopen(int, char *) + fflush(FILE *) + fclose(FILE *)
  52. * flcose() closes the underlying file descriptor, thus no need to
  53. * call close(fd) afterwards */
  54. #endif
  55. if (close(fd)) {
  56. G_warning(_("Unable to close file %s for raster map %s: %s"),
  57. element, name, strerror(errno));
  58. }
  59. }
  60. static void write_fp_format(int fd);
  61. /*!
  62. * \brief Close a raster map
  63. *
  64. * The raster map opened on file descriptor <i>fd</i> is
  65. * closed. Memory allocated for raster processing is freed. If open
  66. * for writing, skeletal support files for the new raster map are
  67. * created as well.
  68. *
  69. * <b>Note:</b> If a module wants to explicitly write support files
  70. * (e.g., a specific color table) for a raster map it creates, it must
  71. * do so after the raster map is closed. Otherwise the close will
  72. * overwrite the support files. See \ref
  73. * Raster_Map_Layer_Support_Routines for routines which write raster
  74. * support files.
  75. *
  76. * If the map is a new floating point, move the <tt>.tmp</tt> file
  77. * into the <tt>fcell</tt> element, create an empty file in the
  78. * <tt>cell</tt> directory; write the floating-point range file; write
  79. * a default quantization file quantization file is set here to round
  80. * fp numbers (this is a default for now). create an empty category
  81. * file, with max cat = max value (for backwards compatibility). Move
  82. * the <tt>.tmp</tt> NULL-value bitmap file to the <tt>cell_misc</tt>
  83. * directory.
  84. *
  85. * \param fd file descriptor
  86. *
  87. * \return void
  88. */
  89. void Rast_close(int fd)
  90. {
  91. struct fileinfo *fcb = &R__.fileinfo[fd];
  92. if (fd < 0 || fd >= R__.fileinfo_count || fcb->open_mode <= 0)
  93. G_fatal_error(_("Invalid descriptor: %d"), fd);
  94. if (fcb->open_mode == OPEN_OLD)
  95. close_old(fd);
  96. else
  97. close_new(fd, 1);
  98. }
  99. /*!
  100. * \brief Unopen a raster map
  101. *
  102. * The raster map opened on file descriptor <i>fd</i> is
  103. * closed. Memory allocated for raster processing is freed. If open
  104. * for writing, the raster map is not created and the temporary file
  105. * created when the raster map was opened is removed (see \ref
  106. * Creating_and_Opening_New_Raster_Files). This routine is useful when
  107. * errors are detected and it is desired to not create the new raster
  108. * map. While it is true that the raster map will not be created if
  109. * the module exits without closing the file, the temporary file will
  110. * not be removed at module exit. GRASS database management will
  111. * eventually remove the temporary file, but the file can be quite
  112. * large and will take up disk space until GRASS does remove it. Use
  113. * this routine as a courtesy to the user.
  114. *
  115. * \param fd file descriptor
  116. *
  117. * \return void
  118. */
  119. void Rast_unopen(int fd)
  120. {
  121. struct fileinfo *fcb = &R__.fileinfo[fd];
  122. if (fd < 0 || fd >= R__.fileinfo_count || fcb->open_mode <= 0)
  123. G_fatal_error(_("Invalid descriptor: %d"), fd);
  124. if (fcb->open_mode == OPEN_OLD)
  125. close_old(fd);
  126. else
  127. close_new(fd, 0);
  128. }
  129. /*!
  130. * \brief Unopen all raster maps
  131. *
  132. * Unopen all raster maps opened for write. Memory allocated for
  133. * raster processing is freed, and the temporary file created when the
  134. * raster map was opened is removed (see \ref
  135. * Creating_and_Opening_New_Raster_Files). This routine is useful when
  136. * errors are detected and it is desired to remove temporary files.
  137. *
  138. * \return void
  139. */
  140. void Rast__unopen_all(void)
  141. {
  142. int i;
  143. for (i = 0; i < R__.fileinfo_count; i++) {
  144. struct fileinfo *fcb = &R__.fileinfo[i];
  145. if (fcb->open_mode == OPEN_NEW_COMPRESSED ||
  146. fcb->open_mode == OPEN_NEW_UNCOMPRESSED)
  147. close_new(i, 0);
  148. }
  149. }
  150. static int close_old(int fd)
  151. {
  152. struct fileinfo *fcb = &R__.fileinfo[fd];
  153. /* if R__.auto_mask was only allocated for reading map rows to create
  154. non-existant null rows, and not for actuall mask, free R__.mask_row
  155. if(R__.auto_mask <=0)
  156. G_free (R__.mask_buf);
  157. This is obsolete since now the mask_bus is always allocated
  158. */
  159. if (fcb->gdal)
  160. Rast_close_gdal_link(fcb->gdal);
  161. if (fcb->vrt)
  162. Rast_close_vrt(fcb->vrt);
  163. if (fcb->null_bits)
  164. G_free(fcb->null_bits);
  165. if (fcb->null_row_ptr)
  166. G_free(fcb->null_row_ptr);
  167. if (fcb->null_fd >= 0)
  168. close(fcb->null_fd);
  169. fcb->null_fd = -1;
  170. if (fcb->cellhd.compressed)
  171. G_free(fcb->row_ptr);
  172. G_free(fcb->col_map);
  173. G_free(fcb->mapset);
  174. G_free(fcb->data);
  175. G_free(fcb->name);
  176. if (fcb->reclass_flag)
  177. Rast_free_reclass(&fcb->reclass);
  178. fcb->open_mode = -1;
  179. if (fcb->map_type != CELL_TYPE) {
  180. Rast_quant_free(&fcb->quant);
  181. }
  182. if (fcb->data_fd >= 0)
  183. close(fcb->data_fd);
  184. return 1;
  185. }
  186. static void write_support_files(int fd)
  187. {
  188. struct fileinfo *fcb = &R__.fileinfo[fd];
  189. struct Categories cats;
  190. struct History hist;
  191. CELL cell_min, cell_max;
  192. char path[GPATH_MAX];
  193. /* remove color table */
  194. Rast_remove_colors(fcb->name, "");
  195. /* create a history file */
  196. Rast_short_history(fcb->name, "raster", &hist);
  197. Rast_write_history(fcb->name, &hist);
  198. /* write the range */
  199. if (fcb->map_type == CELL_TYPE) {
  200. Rast_write_range(fcb->name, &fcb->range);
  201. Rast__remove_fp_range(fcb->name);
  202. }
  203. /*NOTE: int range for floating point maps is not written out */
  204. else { /* if(fcb->map_type != CELL_TYPE) */
  205. Rast_write_fp_range(fcb->name, &fcb->fp_range);
  206. Rast_construct_default_range(&fcb->range);
  207. /* this range will be used to add default rule to quant structure */
  208. }
  209. if (fcb->map_type != CELL_TYPE)
  210. fcb->cellhd.format = -1;
  211. else /* CELL map */
  212. fcb->cellhd.format = fcb->nbytes - 1;
  213. /* write header file */
  214. Rast_put_cellhd(fcb->name, &fcb->cellhd);
  215. /* if map is floating point write the quant rules, otherwise remove f_quant */
  216. if (fcb->map_type != CELL_TYPE) {
  217. /* DEFAULT RANGE QUANT
  218. Rast_get_fp_range_min_max(&fcb->fp_range, &dcell_min, &dcell_max);
  219. if(!Rast_is_d_null_value(&dcell_min) && !Rast_is_d_null_value(&dcell_max))
  220. {
  221. Rast_get_range_min_max(&fcb->range, &cell_min, &cell_max);
  222. Rast_quant_add_rule(&fcb->quant, dcell_min, dcell_max,
  223. cell_min, cell_max);
  224. }
  225. */
  226. Rast_quant_round(&fcb->quant);
  227. Rast_write_quant(fcb->name, fcb->mapset, &fcb->quant);
  228. }
  229. else {
  230. /* remove cell_misc/name/f_quant */
  231. G_file_name_misc(path, "cell_misc", QUANT_FILE, fcb->name,
  232. fcb->mapset);
  233. remove(path);
  234. }
  235. /* create empty cats file */
  236. Rast_get_range_min_max(&fcb->range, &cell_min, &cell_max);
  237. if (Rast_is_c_null_value(&cell_max))
  238. cell_max = 0;
  239. Rast_init_cats((char *)NULL, &cats);
  240. Rast_write_cats(fcb->name, &cats);
  241. Rast_free_cats(&cats);
  242. /* write the histogram */
  243. /* only works for integer maps */
  244. if ((fcb->map_type == CELL_TYPE)
  245. && (fcb->want_histogram)) {
  246. Rast_write_histogram_cs(fcb->name, &fcb->statf);
  247. Rast_free_cell_stats(&fcb->statf);
  248. }
  249. else {
  250. Rast_remove_histogram(fcb->name);
  251. }
  252. }
  253. static int close_new_gdal(int fd, int ok)
  254. {
  255. struct fileinfo *fcb = &R__.fileinfo[fd];
  256. char path[GPATH_MAX];
  257. int stat = 1;
  258. if (ok) {
  259. int cell_fd;
  260. G_debug(1, "close %s GDAL", fcb->name);
  261. if (fcb->cur_row < fcb->cellhd.rows) {
  262. int row;
  263. Rast_zero_output_buf(fcb->data, fcb->map_type);
  264. for (row = fcb->cur_row; row < fcb->cellhd.rows; row++)
  265. Rast_put_row(fd, fcb->data, fcb->map_type);
  266. G_free(fcb->data);
  267. fcb->data = NULL;
  268. }
  269. /* create path : full null file name */
  270. G__make_mapset_element_misc("cell_misc", fcb->name);
  271. G_file_name_misc(path, "cell_misc", NULL_FILE, fcb->name,
  272. G_mapset());
  273. remove(path);
  274. G_file_name_misc(path, "cell_misc", NULLC_FILE, fcb->name,
  275. G_mapset());
  276. remove(path);
  277. /* write 0-length cell file */
  278. G_make_mapset_element("cell");
  279. G_file_name(path, "cell", fcb->name, fcb->mapset);
  280. cell_fd = creat(path, 0666);
  281. close(cell_fd);
  282. if (fcb->map_type != CELL_TYPE) { /* floating point map */
  283. write_fp_format(fd);
  284. /* write 0-length fcell file */
  285. G_make_mapset_element("fcell");
  286. G_file_name(path, "fcell", fcb->name, fcb->mapset);
  287. cell_fd = creat(path, 0666);
  288. close(cell_fd);
  289. }
  290. else {
  291. /* remove fcell/name file */
  292. G_file_name(path, "fcell", fcb->name, fcb->mapset);
  293. remove(path);
  294. /* remove cell_misc/name/f_format */
  295. G_file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name,
  296. fcb->mapset);
  297. remove(path);
  298. }
  299. if (Rast_close_gdal_write_link(fcb->gdal) < 0)
  300. stat = -1;
  301. }
  302. else {
  303. remove(fcb->gdal->filename);
  304. Rast_close_gdal_link(fcb->gdal);
  305. }
  306. fcb->open_mode = -1;
  307. if (fcb->data != NULL)
  308. G_free(fcb->data);
  309. if (ok)
  310. write_support_files(fd);
  311. G_free(fcb->name);
  312. G_free(fcb->mapset);
  313. if (fcb->map_type != CELL_TYPE)
  314. Rast_quant_free(&fcb->quant);
  315. return stat;
  316. }
  317. static int close_new(int fd, int ok)
  318. {
  319. struct fileinfo *fcb = &R__.fileinfo[fd];
  320. int stat;
  321. char path[GPATH_MAX];
  322. int row;
  323. const char *CELL_DIR;
  324. if (fcb->gdal)
  325. return close_new_gdal(fd, ok);
  326. if (ok) {
  327. switch (fcb->open_mode) {
  328. case OPEN_NEW_COMPRESSED:
  329. G_debug(1, "close %s compressed", fcb->name);
  330. break;
  331. case OPEN_NEW_UNCOMPRESSED:
  332. G_debug(1, "close %s uncompressed", fcb->name);
  333. break;
  334. }
  335. if (fcb->cur_row < fcb->cellhd.rows) {
  336. Rast_zero_output_buf(fcb->data, fcb->map_type);
  337. for (row = fcb->cur_row; row < fcb->cellhd.rows; row++)
  338. Rast_put_row(fd, fcb->data, fcb->map_type);
  339. G_free(fcb->data);
  340. fcb->data = NULL;
  341. }
  342. if (fcb->null_row_ptr) { /* compressed nulls */
  343. fcb->null_row_ptr[fcb->cellhd.rows] = lseek(fcb->null_fd, 0L, SEEK_CUR);
  344. Rast__write_null_row_ptrs(fd, fcb->null_fd);
  345. }
  346. if (fcb->null_fd >= 0) {
  347. sync_and_close(fcb->null_fd,
  348. (fcb->null_row_ptr ? NULLC_FILE : NULL_FILE),
  349. fcb->name);
  350. }
  351. fcb->null_fd = -1;
  352. /* create path : full null file name */
  353. G__make_mapset_element_misc("cell_misc", fcb->name);
  354. G_file_name_misc(path, "cell_misc", NULL_FILE, fcb->name, G_mapset());
  355. remove(path);
  356. G_file_name_misc(path, "cell_misc", NULLC_FILE, fcb->name, G_mapset());
  357. remove(path);
  358. G_file_name_misc(path, "cell_misc",
  359. fcb->null_row_ptr ? NULLC_FILE : NULL_FILE,
  360. fcb->name, G_mapset());
  361. if (fcb->null_cur_row > 0) {
  362. /* if temporary NULL file exists, write it into cell_misc/name/null */
  363. if (rename(fcb->null_temp_name, path)) {
  364. G_warning(_("Unable to rename null file '%s' to '%s': %s"),
  365. fcb->null_temp_name, path, strerror(errno));
  366. stat = -1;
  367. }
  368. /* if rename() was successful what is left to remove() ? */
  369. else {
  370. remove(fcb->null_temp_name);
  371. }
  372. }
  373. else {
  374. remove(fcb->null_temp_name);
  375. remove(path); /* again ? */
  376. } /* null_cur_row > 0 */
  377. if (fcb->open_mode == OPEN_NEW_COMPRESSED) { /* auto compression */
  378. fcb->row_ptr[fcb->cellhd.rows] = lseek(fcb->data_fd, 0L, SEEK_CUR);
  379. Rast__write_row_ptrs(fd);
  380. }
  381. if (fcb->map_type != CELL_TYPE) { /* floating point map */
  382. int cell_fd;
  383. write_fp_format(fd);
  384. /* now write 0-length cell file */
  385. G_make_mapset_element("cell");
  386. cell_fd =
  387. creat(G_file_name(path, "cell", fcb->name, fcb->mapset),
  388. 0666);
  389. close(cell_fd);
  390. CELL_DIR = "fcell";
  391. }
  392. else {
  393. /* remove fcell/name file */
  394. G_file_name(path, "fcell", fcb->name, fcb->mapset);
  395. remove(path);
  396. /* remove cell_misc/name/f_format */
  397. G_file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name,
  398. fcb->mapset);
  399. remove(path);
  400. CELL_DIR = "cell";
  401. }
  402. } /* ok */
  403. /* NOW CLOSE THE FILE DESCRIPTOR */
  404. sync_and_close(fcb->data_fd,
  405. (fcb->map_type == CELL_TYPE ? "cell" : "fcell"),
  406. fcb->name);
  407. fcb->open_mode = -1;
  408. if (fcb->null_fd >= 0) {
  409. sync_and_close(fcb->null_fd,
  410. (fcb->null_row_ptr ? NULLC_FILE : NULL_FILE),
  411. fcb->name);
  412. }
  413. fcb->null_fd = -1;
  414. if (fcb->data != NULL)
  415. G_free(fcb->data);
  416. if (fcb->null_temp_name != NULL) {
  417. G_free(fcb->null_temp_name);
  418. fcb->null_temp_name = NULL;
  419. }
  420. /* if the cell file was written to a temporary file
  421. * move this temporary file into the cell file
  422. * if the move fails, tell the user, but go ahead and create
  423. * the support files
  424. */
  425. stat = 1;
  426. if (ok && (fcb->temp_name != NULL)) {
  427. G_file_name(path, CELL_DIR, fcb->name, fcb->mapset);
  428. remove(path);
  429. if (rename(fcb->temp_name, path)) {
  430. G_warning(_("Unable to rename cell file '%s' to '%s': %s"),
  431. fcb->temp_name, path, strerror(errno));
  432. stat = -1;
  433. }
  434. /* if rename() was successful what is left to remove() ? */
  435. else {
  436. remove(fcb->temp_name);
  437. }
  438. }
  439. if (fcb->temp_name != NULL) {
  440. G_free(fcb->temp_name);
  441. }
  442. if (ok)
  443. write_support_files(fd);
  444. G_free(fcb->name);
  445. G_free(fcb->mapset);
  446. G_free(fcb->null_bits);
  447. if (fcb->null_row_ptr)
  448. G_free(fcb->null_row_ptr);
  449. if (fcb->map_type != CELL_TYPE)
  450. Rast_quant_free(&fcb->quant);
  451. return stat;
  452. }
  453. void Rast__close_null(int fd)
  454. {
  455. struct fileinfo *fcb = &R__.fileinfo[fd];
  456. char path[GPATH_MAX];
  457. if (fcb->null_row_ptr) { /* compressed nulls */
  458. fcb->null_row_ptr[fcb->cellhd.rows] = lseek(fcb->null_fd, 0L, SEEK_CUR);
  459. Rast__write_null_row_ptrs(fd, fcb->null_fd);
  460. G_free(fcb->null_row_ptr);
  461. }
  462. if (fcb->null_fd >= 0)
  463. close(fcb->null_fd);
  464. fcb->null_fd = -1;
  465. /* create path : full null file name */
  466. G__make_mapset_element_misc("cell_misc", fcb->name);
  467. G_file_name_misc(path, "cell_misc", NULL_FILE, fcb->name, G_mapset());
  468. remove(path);
  469. G_file_name_misc(path, "cell_misc", NULLC_FILE, fcb->name, G_mapset());
  470. remove(path);
  471. G_file_name_misc(path, "cell_misc",
  472. fcb->null_row_ptr ? NULLC_FILE : NULL_FILE,
  473. fcb->name, G_mapset());
  474. if (rename(fcb->null_temp_name, path))
  475. G_warning(_("Unable to rename null file '%s' to '%s': %s"),
  476. fcb->null_temp_name, path, strerror(errno));
  477. remove(fcb->null_temp_name);
  478. G_free(fcb->null_temp_name);
  479. G_free(fcb->name);
  480. G_free(fcb->mapset);
  481. G_free(fcb->null_bits);
  482. fcb->open_mode = -1;
  483. }
  484. /* returns 0 on success, 1 on failure */
  485. static void write_fp_format(int fd)
  486. {
  487. struct fileinfo *fcb = &R__.fileinfo[fd];
  488. struct Key_Value *format_kv;
  489. char path[GPATH_MAX];
  490. if (fcb->map_type == CELL_TYPE) {
  491. G_warning(_("unable to write f_format file for CELL maps"));
  492. return;
  493. }
  494. format_kv = G_create_key_value();
  495. if (fcb->map_type == FCELL_TYPE)
  496. G_set_key_value("type", "float", format_kv);
  497. else
  498. G_set_key_value("type", "double", format_kv);
  499. G_set_key_value("byte_order", "xdr", format_kv);
  500. if (fcb->open_mode == OPEN_NEW_COMPRESSED)
  501. G_set_key_value("lzw_compression_bits", "-1", format_kv);
  502. G__make_mapset_element_misc("cell_misc", fcb->name);
  503. G_file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name, fcb->mapset);
  504. G_write_key_value_file(path, format_kv);
  505. G_free_key_value(format_kv);
  506. }