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);
170 sprintf(buf,
"%s/%s", in, files[i]);
172 sprintf(buf,
"%s/%s", out, files[i]);
175 if (access(old_path, F_OK) == 0) {
176 G_debug(2,
"copy %s to %s", old_path, new_path);
177 if (copy_file(old_path, new_path)) {
178 G_warning(
_(
"Unable to copy vector map <%s> to <%s>"),
241 G_debug(2,
"Rename vector '%s' to '%s'", in, out);
248 G_warning(
_(
"Vector map <%s> already exists and will be overwritten"),
262 G_warning(
_(
"Vector map <%s> not found"), in);
265 else if (ret == -1) {
266 G_warning(
_(
"Unable to copy vector map <%s> to <%s>"), in, out);
287 fields = (
int *)
G_malloc(n *
sizeof(
int));
289 for (i = 0; i < n; i++) {
294 for (i = 0; i < n; i++) {
295 G_debug(3,
"field[%d] = %d", i, fields[i]);
299 G_warning(
_(
"Database connection not defined for layer %d"),
306 G_debug(3,
"Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
339 if (driver ==
NULL) {
340 G_warning(
_(
"Unable to open database <%s> by driver <%s>"),
345 G_warning(
_(
"Unable to create index for table <%s>, key <%s>"),
387 const char *tmp, *mapset, *env;
394 G_debug(3,
"Delete vector '%s' (is_tmp = %d)", map, is_tmp);
400 if (strcmp(mapset, xmapset) != 0)
401 G_warning(
_(
"Ignoring invalid mapset: %s"), xmapset);
405 if (map ==
NULL || strlen(map) == 0) {
406 G_warning(
_(
"Invalid vector map name <%s>"), map ? map :
"null");
416 G_warning(
_(
"Unable to open header file for vector map <%s>"),
423 G_debug(1,
"dbln file: %s", path);
425 if (access(path, F_OK) == 0) {
432 for (i = 0; i < n; i++) {
435 G_warning(
_(
"Database connection not defined for layer %d"),
448 G_warning(
_(
"Unable to find table <%s> linked to vector map <%s>"),
471 G_warning(
_(
"Table <%s> linked to vector map <%s> does not exist"),
481 G_debug(3,
"opendir '%s'", path);
484 G_warning(
_(
"Unable to open directory '%s'"), path);
489 G_debug(3,
"file = '%s'", ent->d_name);
490 if ((strcmp(ent->d_name,
".") == 0) ||
491 (strcmp(ent->d_name,
"..") == 0))
494 ret = snprintf(path_buf,
GPATH_MAX,
"%s/%s", path, ent->d_name);
496 G_warning(
_(
"Filepath '%s/%s' exceeds max length"), path,
501 G_debug(3,
"delete file '%s'", path_buf);
502 ret = unlink(path_buf);
504 G_warning(
_(
"Unable to delete file '%s'"), path_buf);
511 env =
getenv(
"GRASS_VECTOR_TMPDIR_MAPSET");
512 if (env && strcmp(env,
"0") == 0) {
520 G_debug(3,
"rename '%s' to '%s'", path, tmp);
522 ret = rename(path, tmp);
524 G_warning(
_(
"Unable to rename directory '%s' to '%s'"), path, tmp);
529 G_debug(3,
"remove directory '%s'", tmp);
533 G_warning(
_(
"Unable to remove directory '%s': %s"),
534 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.
int G_make_mapset_dir_object(const char *, const char *)
Create directory for an object of a given type.
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 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')