GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
close_nat.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/close_nat.c
3 
4  \brief Vector library - Close map (native format)
5 
6  Higher level functions for reading/writing/manipulating vectors.
7 
8  (C) 2001-2015 by the GRASS Development Team
9 
10  This program is free software under the GNU General Public License
11  (>=v2). Read the file COPYING that comes with GRASS for details.
12 
13  \author Original author CERL, probably Dave Gerdes or Mike Higgins.
14  \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
15  */
16 
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <errno.h>
20 
21 #include <grass/vector.h>
22 #include <grass/glocale.h>
23 
24 #include "local_proto.h"
25 
26 /*!
27  \brief Close vector map
28 
29  \param Map vector map to be closed
30 
31  \return 0 on success
32  \return non-zero on error
33  */
34 int V1_close_nat(struct Map_info *Map)
35 {
36  struct Coor_info CInfo;
37 
38  G_debug(1, "V1_close_nat(): name = %s mapset= %s", Map->name, Map->mapset);
39  if (!VECT_OPEN(Map))
40  return 1;
41 
42  if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW) {
43  Vect_coor_info(Map, &CInfo);
44  Map->head.size = CInfo.size;
45  dig__write_head(Map);
46 
47  Vect__write_head(Map);
48  Vect_write_dblinks(Map);
49  }
50 
51  /* close coor file */
52  fclose(Map->dig_fp.file);
53  dig_file_free(&(Map->dig_fp));
54 
55  /* delete temporary map ? */
56  if (Map->temporary) {
57  int delete;
58  char *env = getenv("GRASS_VECTOR_TEMPORARY");
59 
60  delete = TRUE;
61  if (Map->temporary == TEMPORARY_MAP_ENV && env) {
62  if (G_strcasecmp(env, "move") == 0) {
63  /* copy temporary vector map to the current mapset */
64  char path_tmp[GPATH_MAX], path_map[GPATH_MAX];
65 
66  G_debug(1,
67  "V1_close_nat(): temporary map <%s> TO BE MOVED TO"
68  " CURRENT MAPSET",
69  Map->name);
70  Vect__get_element_path(path_tmp, Map, NULL);
71 
72  G_file_name(path_map, GV_DIRECTORY, NULL, Map->mapset);
73  if (access(path_map, 0) != 0 && G_mkdir(path_map) != 0)
74  G_fatal_error(_("Unable to create '%s': %s"), path_map,
75  strerror(errno));
76 
77  G_file_name(path_map, GV_DIRECTORY, Map->name, Map->mapset);
78 
79  G_debug(1, "V1_close_nat(): %s -> %s", path_tmp, path_map);
80  if (0 != G_recursive_copy(path_tmp, path_map))
81  G_fatal_error(_("Unable to copy '%s': %s"), path_tmp,
82  strerror(errno));
83 
84 #ifdef TEMPORARY_MAP_DB
85  int i, ndblinks;
86  int tmp;
87 
88  struct field_info *fi;
89  dbConnection connection;
90  struct dblinks *dblinks;
91 
92  G_debug(1, "V1_close_nat(): copying attributes");
93  /* copy also attributes */
95  db_get_connection(&connection);
96  ndblinks = Vect_get_num_dblinks(Map);
97  for (i = 0; i < ndblinks; i++) {
98  fi = Vect_get_dblink(Map, i);
99  if (DB_OK != db_copy_table(fi->driver, fi->database,
100  fi->table, connection.driverName,
101  connection.databaseName,
102  fi->table)) {
103  G_warning(_("Unable to copy table <%s>"), fi->table);
104  continue;
105  }
106 
107  Vect_add_dblink(dblinks, fi->number, fi->name, fi->table,
108  fi->key, connection.databaseName,
109  connection.driverName);
110  G_free(fi);
111  }
112  G_free(Map->dblnk);
113  Map->dblnk = dblinks;
114  tmp = Map->temporary;
115  Map->temporary = TEMPORARY_MAP_DISABLED;
116  Vect_write_dblinks(Map);
117  Map->temporary = tmp;
118 #endif
119  }
120  else if (G_strcasecmp(env, "delete") == 0) {
121  /* delete temporary vector map */
122  G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED",
123  Map->name);
124  }
125  else {
126  /* do not delete temporary vector map */
127  G_debug(1, "V1_close_nat(): temporary map <%s> IS NOT DELETED",
128  Map->name);
129  delete = FALSE;
130  }
131  }
132  else if (Map->temporary == TEMPORARY_MAP) {
133  G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED",
134  Map->name);
135  delete = TRUE;
136  }
137 
138  if (delete) {
139  char path_tmp[GPATH_MAX];
140 
141  /* delete vector directory */
142  Vect__get_element_path(path_tmp, Map, NULL);
143  G_recursive_remove(path_tmp);
144 
145 #ifndef TEMPORARY_MAP_DB
146  if (G_strcasecmp(env, "move") != 0) {
147  int i, ndblinks;
148 
149  dbDriver *driver;
150  dbString table_name;
151 
152  struct field_info *fi;
153 
154  db_init_string(&table_name);
155 
156  /* drop also attribute table */
157  ndblinks = Vect_get_num_dblinks(Map);
158  for (i = 0; i < ndblinks; i++) {
159  fi = Vect_get_dblink(Map, i);
160 
161  driver =
163  if (driver == NULL) {
164  G_warning(
165  _("Unable to open database <%s> by driver <%s>"),
166  fi->database, fi->driver);
167  continue;
168  }
169 
170  db_set_string(&table_name, fi->table);
171  if (DB_OK != db_drop_table(driver, &table_name)) {
172  G_warning(_("Unable to drop table <%s>"), fi->table);
173  continue;
174  }
175  }
176  }
177 #endif
178  }
179  }
180 
181  return 0;
182 }
#define NULL
Definition: ccmath.h:32
int V1_close_nat(struct Map_info *Map)
Close vector map.
Definition: close_nat.c:34
#define DB_OK
Definition: dbmi.h:71
dbDriver * db_start_driver_open_database(const char *, const char *)
Open driver/database connection.
Definition: db.c:28
int db_drop_table(dbDriver *, dbString *)
Drop table.
Definition: c_drop_tab.c:28
int db_get_connection(dbConnection *)
Get default DB connection settings for the current mapset.
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition: string.c:41
void db_init_string(dbString *)
Initialize dbString.
Definition: string.c:25
int db_copy_table(const char *, const char *, const char *, const char *, const char *, const char *)
Copy a table.
Definition: copy_tab.c:443
int G_recursive_copy(const char *, const char *)
Copy recursively source directory to destination directory.
Definition: copy_dir.c:70
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition: file_name.c:61
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition: strings.c:47
int G_recursive_remove(const char *)
Recursively remove all files in given directory.
Definition: remove.c:113
int G_mkdir(const char *)
Creates a new directory.
Definition: paths.c:27
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_add_dblink(struct dblinks *, int, const char *, const char *, const char *, const char *, const char *)
Add new DB connection to dblinks structure.
Definition: field.c:285
int Vect_coor_info(struct Map_info *, struct Coor_info *)
Update Coor_info structure.
struct dblinks * Vect_new_dblinks_struct(void)
Create and init new dblinks structure.
Definition: field.c:52
int Vect_write_dblinks(struct Map_info *)
Write dblinks to file.
Definition: field.c:878
int Vect_get_num_dblinks(struct Map_info *)
Get number of defined dblinks.
Definition: level_two.c:159
struct field_info * Vect_get_dblink(struct Map_info *, int)
Get information about link to database.
Definition: field.c:475
int Vect__write_head(struct Map_info *)
Writes head information to text file (GV_HEAD_ELEMENT)
#define VECT_OPEN(Map)
Check if vector map is open.
Definition: dig_defines.h:137
#define GV_DIRECTORY
Name of vector directory.
Definition: dig_defines.h:8
#define GV_MODE_WRITE
Write vector map open mode.
Definition: dig_defines.h:106
#define GV_MODE_RW
Read-write vector map open mode.
Definition: dig_defines.h:108
int dig__write_head(struct Map_info *)
Definition: head.c:23
void dig_file_free(struct gvfile *file)
Free struct gvfile.
Definition: file.c:268
const struct driver * driver
Definition: driver/init.c:25
#define GPATH_MAX
Definition: gis.h:194
#define TRUE
Definition: gis.h:79
#define FALSE
Definition: gis.h:83
#define _(str)
Definition: glocale.h:10
Coor file info.
Definition: dig_structs.h:371
off_t size
Total size (in bytes)
Definition: dig_structs.h:375
Vector map info.
Definition: dig_structs.h:1243
char * mapset
Mapset name.
Definition: dig_structs.h:1320
int temporary
Temporary map flag.
Definition: dig_structs.h:1260
struct gvfile dig_fp
GV file pointer (native format only)
Definition: dig_structs.h:1395
struct dig_head head
Header info.
Definition: dig_structs.h:1388
char * name
Map name (for 4.0)
Definition: dig_structs.h:1316
struct dblinks * dblnk
Array of DB links.
Definition: dig_structs.h:1265
char * databaseName
Definition: dbmi.h:280
char * driverName
Definition: dbmi.h:278
off_t size
Coor file size.
Definition: dig_structs.h:344
Definition: driver.h:21
Layer (old: field) information.
Definition: dig_structs.h:131
char * table
Name of DB table.
Definition: dig_structs.h:151
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
Definition: dig_structs.h:143
char * name
Layer name (optional)
Definition: dig_structs.h:139
char * database
Definition: dig_structs.h:147
char * key
Name of key column (usually 'cat')
Definition: dig_structs.h:155
int number
Layer number.
Definition: dig_structs.h:135
FILE * file
File descriptor.
Definition: dig_structs.h:98
char * Vect__get_element_path(char *file_path, struct Map_info *Map, const char *element)
Get map element full path (internal use only)