22 #include <sys/types.h> 32 #include "local_proto.h" 78 static int copy_file(
const char *src,
const char *
dst)
85 if ((fd =
open(src, O_RDONLY)) < 0)
89 if ((f2 = fopen(dst,
"w")) ==
NULL) {
97 while ((len = read(fd, buf, 4096)) > 0) {
98 while (len && (len2 = write(fd2, buf, len)) >= 0)
106 if (len == -1 || len2 == -1)
134 const char *inmapset;
137 G_debug(2,
"Copy vector '%s' in '%s' to '%s'", in, mapset, out);
144 G_warning(
_(
"Unable to find vector map <%s> in <%s>"), in, mapset);
156 G_warning(
_(
"Vector map <%s> already exists and will be overwritten"),
160 G_warning(
_(
"Unable to delete vector map <%s>"), out);
172 sprintf(buf,
"%s/%s", in, files[i]);
174 sprintf(buf,
"%s/%s", out, files[i]);
177 if (access(old_path, F_OK) == 0) {
178 G_debug(2,
"copy %s to %s", old_path, new_path);
179 if (copy_file(old_path, new_path)) {
180 G_warning(
_(
"Unable to copy vector map <%s> to <%s>"),
243 G_debug(2,
"Rename vector '%s' to '%s'", in, out);
250 G_warning(
_(
"Vector map <%s> already exists and will be overwritten"),
264 G_warning(
_(
"Vector map <%s> not found"), in);
267 else if (ret == -1) {
268 G_warning(
_(
"Unable to copy vector map <%s> to <%s>"), in, out);
289 fields = (
int *)
G_malloc(n *
sizeof(
int));
291 for (i = 0; i < n; i++) {
296 for (i = 0; i < n; i++) {
297 G_debug(3,
"field[%d] = %d", i, fields[i]);
301 G_warning(
_(
"Database connection not defined for layer %d"),
308 G_debug(3,
"Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
341 if (driver ==
NULL) {
342 G_warning(
_(
"Unable to open database <%s> by driver <%s>"),
347 G_warning(
_(
"Unable to create index for table <%s>, key <%s>"),
389 const char *tmp, *mapset, *env;
396 G_debug(3,
"Delete vector '%s' (is_tmp = %d)", map, is_tmp);
402 if (strcmp(mapset, xmapset) != 0)
403 G_warning(
_(
"Ignoring invalid mapset: %s"), xmapset);
407 if (map ==
NULL || strlen(map) == 0) {
408 G_warning(
_(
"Invalid vector map name <%s>"), map ? map :
"null");
418 G_warning(
_(
"Unable to open header file for vector map <%s>"),
425 G_debug(1,
"dbln file: %s", path);
427 if (access(path, F_OK) == 0) {
434 for (i = 0; i < n; i++) {
437 G_warning(
_(
"Database connection not defined for layer %d"),
450 G_warning(
_(
"Unable to find table <%s> linked to vector map <%s>"),
473 G_warning(
_(
"Table <%s> linked to vector map <%s> does not exist"),
483 G_debug(3,
"opendir '%s'", path);
486 G_warning(
_(
"Unable to open directory '%s'"), path);
491 G_debug(3,
"file = '%s'", ent->d_name);
492 if ((strcmp(ent->d_name,
".") == 0) ||
493 (strcmp(ent->d_name,
"..") == 0))
496 sprintf(path_buf,
"%s/%s", path, ent->d_name);
497 G_debug(3,
"delete file '%s'", path_buf);
498 ret = unlink(path_buf);
500 G_warning(
_(
"Unable to delete file '%s'"), path_buf);
507 env =
getenv(
"GRASS_VECTOR_TMPDIR_MAPSET");
508 if (env && strcmp(env,
"0") == 0) {
516 G_debug(3,
"rename '%s' to '%s'", path, tmp);
518 ret = rename(path, tmp);
520 G_warning(
_(
"Unable to rename directory '%s' to '%s'"), path, tmp);
525 G_debug(3,
"remove directory '%s'", tmp);
529 G_warning(
_(
"Unable to remove directory '%s': %s"),
530 tmp, strerror(errno));
int Vect_rename(const char *in, const char *out)
Rename existing vector map (in the current mapset).
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
char * Vect__get_element_path(char *file_path, const struct Map_info *Map, const char *element)
Get map element full path (internal use only)
#define GV_DIRECTORY
Name of vector directory.
#define GV_MTABLE
More tables linked to vector map.
int Vect_open_update_head(struct Map_info *, const char *, const char *)
Open header file of existing vector map for updating (mostly for database link updates) ...
#define GV_FRMT_ELEMENT
Format description, data location (OGR)
int Vect__open_old(struct Map_info *Map, const char *name, const char *mapset, const char *layer, int update, int head_only, int is_tmp)
Open existing vector map for reading (internal use only)
const char * G_find_vector2(const char *, const char *)
Find a vector map (look but don't touch)
#define GV_FORMAT_NATIVE
Geometry data formats supported by lib Don't change GV_FORMAT_* values, this order is hardcoded in li...
#define GV_DBLN_ELEMENT
Native format, link to database.
int Vect_map_add_dblink(struct Map_info *, int, const char *, const char *, const char *, const char *, const char *)
Add new db connection to Map_info structure.
int release_support
Release memory occupied by support structures (topo, spatial, category)
struct field_info * field
Pointer to the first field_info structure.
char * table
Name of DB table.
void Vect_set_category_index_update(struct Map_info *Map)
Set category index to be updated when vector is changed.
void G_free(void *)
Free allocated memory.
#define GV_TOPO_ELEMENT
Native format, topology file.
#define GV_HEAD_ELEMENT
Native format, header information.
dbDriver * db_start_driver_open_database(const char *, const char *)
Open driver/database connection.
int db_delete_table(const char *, const char *, const char *)
Delete table.
int Vect_close(struct Map_info *)
Close vector map.
int Vect__delete(const char *map, int is_tmp)
Delete vector map (internal use only)
int type
Feature type constraint.
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
struct field_info * Vect_get_field(const struct Map_info *, int)
Get information about link to database (by layer number)
int Vect_get_num_dblinks(const struct Map_info *)
Get number of defined dblinks.
Layer (old: field) information.
char * name
Layer name (optional)
#define GV_CIDX_ELEMENT
Native format, category index.
char * Vect_subst_var(const char *, const struct Map_info *)
Substitute variable in string.
char * G_tempfile(void)
Returns a temporary file name.
struct field_info * Vect_get_dblink(const struct Map_info *, int)
Get information about link to database.
struct Map_info * Vect_new_map_struct(void)
Creates and initializes Map_info structure.
#define GV_HIST_ELEMENT
Native format, history file.
const struct driver * driver
struct Plus_head plus
Plus info (topology, version, ...)
int Vect_map_del_dblink(struct Map_info *, int)
Delete db connection from Map_info structure.
int Vect_delete(const char *map)
Delete vector map including attribute tables.
#define GV_1TABLE
One table linked to vector map.
int Vect_legal_filename(const char *)
Check if output is legal vector name.
int G_rename(const char *, const char *, const char *)
Rename a database file.
char * mapset
Mapset name.
int Vect_set_open_level(int)
Predetermine level at which a vector map will be opened for reading.
int Vect_open_old_head(struct Map_info *, const char *, const char *)
Reads only info about vector map (headers)
int db_create_index2(dbDriver *, const char *, const char *)
Create unique index.
struct dblinks * dblnk
Array of DB links.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
struct field_info * Vect_default_field_info(struct Map_info *, int, const char *, int)
Get default information about link to database for new dblink.
int db_copy_table(const char *, const char *, const char *, const char *, const char *, const char *)
Copy a table.
const char * G_mapset(void)
Get current mapset name.
int Vect_copy(const char *in, const char *mapset, const char *out)
Copy vector map including attribute tables.
int Vect_copy_tables(const struct Map_info *, struct Map_info *, int)
Copy attribute tables linked to vector map.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_warning(const char *,...) __attribute__((format(printf
void Vect_destroy_map_struct(struct Map_info *p)
Frees all memory associated with a Map_info structure, including the structure itself.
int G_make_mapset_element(const char *)
Create element in the current mapset.
int format
Map format (native, ogr, postgis)
int db_table_exists(const char *, const char *, const char *)
Check if table exists.
#define GV_SIDX_ELEMENT
Native format, spatial index.
#define GV_COOR_ELEMENT
Native format, coordinates.
int update_cidx
Update category index if vector is modified.
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
void Vect_set_release_support(struct Map_info *Map)
Set spatial index to be realease when vector is closed.
int G_debug(int, const char *,...) __attribute__((format(printf
char * key
Name of key column (usually 'cat')