close_nat.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*!
  2. \file lib/vector/Vlib/close_nat.c
  3. \brief Vector library - Close map (native format)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2015 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. */
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include <grass/vector.h>
  15. #include <grass/glocale.h>
  16. #include "local_proto.h"
  17. /*!
  18. \brief Close vector map
  19. \param Map vector map to be closed
  20. \return 0 on success
  21. \return non-zero on error
  22. */
  23. int V1_close_nat(struct Map_info *Map)
  24. {
  25. struct Coor_info CInfo;
  26. G_debug(1, "V1_close_nat(): name = %s mapset= %s", Map->name,
  27. Map->mapset);
  28. if (!VECT_OPEN(Map))
  29. return 1;
  30. if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW) {
  31. Vect_coor_info(Map, &CInfo);
  32. Map->head.size = CInfo.size;
  33. dig__write_head(Map);
  34. Vect__write_head(Map);
  35. Vect_write_dblinks(Map);
  36. }
  37. /* close coor file */
  38. fclose(Map->dig_fp.file);
  39. dig_file_free(&(Map->dig_fp));
  40. /* delete temporary map ? */
  41. if (Map->temporary) {
  42. int delete;
  43. char *env = getenv("GRASS_VECTOR_TEMPORARY");
  44. delete = TRUE;
  45. if (Map->temporary == TEMPORARY_MAP_ENV && env) {
  46. if (G_strcasecmp(env, "move") == 0) {
  47. /* copy temporary vector map to the current mapset */
  48. char path_tmp[GPATH_MAX], path_map[GPATH_MAX];
  49. G_debug(1, "V1_close_nat(): temporary map <%s> TO BE MOVED TO"
  50. " CURRENT MAPSET",
  51. Map->name);
  52. Vect__get_element_path(path_tmp, Map, NULL);
  53. G_file_name(path_map, GV_DIRECTORY, NULL, Map->mapset);
  54. if (access(path_map, 0) != 0 && G_mkdir(path_map) != 0)
  55. G_fatal_error(_("Unable to create '%s': %s"),
  56. path_map, strerror(errno));
  57. G_file_name(path_map, GV_DIRECTORY, Map->name, Map->mapset);
  58. G_debug(1, "V1_close_nat(): %s -> %s", path_tmp, path_map);
  59. if (0 != G_recursive_copy(path_tmp, path_map))
  60. G_fatal_error(_("Unable to copy '%s': %s"), path_tmp, strerror(errno));
  61. #ifdef TEMPORARY_MAP_DB
  62. int i, ndblinks;
  63. int tmp;
  64. struct field_info *fi;
  65. dbConnection connection;
  66. struct dblinks *dblinks;
  67. G_debug(1, "V1_close_nat(): copying attributes");
  68. /* copy also attributes */
  69. dblinks = Vect_new_dblinks_struct();
  70. db_get_connection(&connection);
  71. ndblinks = Vect_get_num_dblinks(Map);
  72. for (i = 0; i < ndblinks; i++) {
  73. fi = Vect_get_dblink(Map, i);
  74. if (DB_OK != db_copy_table(fi->driver, fi->database, fi->table,
  75. connection.driverName,
  76. connection.databaseName,
  77. fi->table)) {
  78. G_warning(_("Unable to copy table <%s>"), fi->table);
  79. continue;
  80. }
  81. Vect_add_dblink(dblinks, fi->number, fi->name,
  82. fi->table, fi->key, connection.databaseName,
  83. connection.driverName);
  84. G_free(fi);
  85. }
  86. G_free(Map->dblnk);
  87. Map->dblnk = dblinks;
  88. tmp = Map->temporary;
  89. Map->temporary = TEMPORARY_MAP_DISABLED;
  90. Vect_write_dblinks(Map);
  91. Map->temporary = tmp;
  92. #endif
  93. }
  94. else if (G_strcasecmp(env, "delete") == 0) {
  95. /* delete temporary vector map */
  96. G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED", Map->name);
  97. }
  98. else {
  99. /* do not delete temporary vector map */
  100. G_debug(1, "V1_close_nat(): temporary map <%s> IS NOT DELETED",
  101. Map->name);
  102. delete = FALSE;
  103. }
  104. }
  105. else if (Map->temporary == TEMPORARY_MAP) {
  106. G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED", Map->name);
  107. delete = TRUE;
  108. }
  109. if (delete) {
  110. char path_tmp[GPATH_MAX];
  111. /* delete vector directory */
  112. Vect__get_element_path(path_tmp, Map, NULL);
  113. G_recursive_remove(path_tmp);
  114. #ifndef TEMPORARY_MAP_DB
  115. if (G_strcasecmp(env, "move") != 0) {
  116. int i, ndblinks;
  117. dbDriver *driver;
  118. dbString table_name;
  119. struct field_info *fi;
  120. db_init_string(&table_name);
  121. /* drop also attribute table */
  122. ndblinks = Vect_get_num_dblinks(Map);
  123. for (i = 0; i < ndblinks; i++) {
  124. fi = Vect_get_dblink(Map, i);
  125. driver = db_start_driver_open_database(fi->driver, fi->database);
  126. if (driver == NULL) {
  127. G_warning(_("Unable to open database <%s> by driver <%s>"),
  128. fi->database, fi->driver);
  129. continue;
  130. }
  131. db_set_string(&table_name, fi->table);
  132. if (DB_OK != db_drop_table(driver, &table_name)) {
  133. G_warning(_("Unable to drop table <%s>"), fi->table);
  134. continue;
  135. }
  136. }
  137. }
  138. #endif
  139. }
  140. }
  141. return 0;
  142. }