close.c 15 KB

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