Changeset 65348
- Timestamp:
- Jun 1, 2015, 7:57:03 AM (9 years ago)
- Location:
- grass/trunk
- Files:
-
- 23 edited
-
include/defs/gis.h (modified) (3 diffs)
-
lib/gis/file_name.c (modified) (4 diffs)
-
lib/gis/mapset_msc.c (modified) (3 diffs)
-
lib/gis/open.c (modified) (4 diffs)
-
lib/gis/remove.c (modified) (5 diffs)
-
lib/gis/rename.c (modified) (3 diffs)
-
lib/gis/tempfile.c (modified) (5 diffs)
-
lib/init/clean_temp.c (modified) (1 diff)
-
lib/init/grass.py (modified) (3 diffs)
-
lib/init/variables.html (modified) (2 diffs)
-
lib/raster/close.c (modified) (2 diffs)
-
lib/vector/Vlib/build.c (modified) (4 diffs)
-
lib/vector/Vlib/build_ogr.c (modified) (2 diffs)
-
lib/vector/Vlib/cindex.c (modified) (5 diffs)
-
lib/vector/Vlib/close.c (modified) (1 diff)
-
lib/vector/Vlib/close_nat.c (modified) (3 diffs)
-
lib/vector/Vlib/close_pg.c (modified) (1 diff)
-
lib/vector/Vlib/field.c (modified) (5 diffs)
-
lib/vector/Vlib/header.c (modified) (3 diffs)
-
lib/vector/Vlib/local_proto.h (modified) (2 diffs)
-
lib/vector/Vlib/map.c (modified) (6 diffs)
-
lib/vector/Vlib/open.c (modified) (16 diffs)
-
lib/vector/Vlib/open_nat.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
grass/trunk/include/defs/gis.h
r63960 r65348 248 248 char *G_file_name_misc(char *, const char *, const char *, const char *, 249 249 const char *); 250 char *G_file_name_tmp(char *, const char *, const char *, const char *); 250 251 251 252 /* find_file.c */ … … 438 439 /* mapset_msc.c */ 439 440 int G_make_mapset_element(const char *); 441 int G_make_mapset_element_tmp(const char *); 440 442 int G__make_mapset_element_misc(const char *, const char *); 441 443 int G_mapset_permissions(const char *); … … 597 599 int G_remove(const char *, const char *); 598 600 int G_remove_misc(const char *, const char *, const char *); 601 int G_recursive_remove(const char *); 599 602 600 603 /* rename.c */ -
grass/trunk/lib/gis/file_name.c
r55607 r65348 4 4 \brief GIS library - Determine GRASS data base file name 5 5 6 (C) 2001-20 08, 2013by the GRASS Development Team6 (C) 2001-2015 by the GRASS Development Team 7 7 8 8 This program is free software under the GNU General Public License 9 (>=v2). Read the file COPYING that comes with GRASS for details.9 (>=v2). Read the file COPYING that comes with GRASS for details. 10 10 11 11 \author Original author CERL … … 13 13 14 14 #include <string.h> 15 #include <stdlib.h> 15 16 #include <grass/gis.h> 16 17 17 18 #include "gis_local_proto.h" 18 19 20 char *file_name(char *, const char *, const char *, 21 const char *, const char *, const char *); 22 19 23 /*! 20 24 \brief Builds full path names to GIS data files 21 25 22 If name is of the form "nnn@ppp" then path is set as if name had23 been nnn and mapset had been ppp (mapset parameter itself is ignored24 i n this case).26 If <i>name</i> is of the form "nnn@ppp" then path is set as if name 27 had been "nnn" and mapset had been "ppp" (mapset parameter itself is 28 ignored in this case). 25 29 26 30 \param[out] path buffer to hold resultant full path to file 27 \param element database element (eg, "cell", "cellhd", etc)31 \param element database element (eg, "cell", "cellhd", "vector", etc) 28 32 \param name name of file to build path to (fully qualified names allowed) 29 33 \param mapset mapset name … … 34 38 const char *element, const char *name, const char *mapset) 35 39 { 36 char xname[GNAME_MAX]; 37 char xmapset[GMAPSET_MAX]; 40 return file_name(path, NULL, element, name, mapset, NULL); 41 } 42 43 /*! 44 \brief Builds full path names to GIS misc data files 45 46 \param[out] path buffer to hold resultant full path to file 47 \param dir misc directory 48 \param element database element (eg, "cell", "cellhd", "vector", etc) 49 \param name name of file to build path to (fully qualified names allowed) 50 \param mapset mapset name 51 52 \return pointer to <i>path</i> buffer 53 */ 54 char *G_file_name_misc(char *path, 55 const char *dir, 56 const char *element, 57 const char *name, const char *mapset) 58 { 59 return file_name(path, dir, element, name, mapset, NULL); 60 } 61 62 /*! 63 \brief Builds full path names to GIS data files in temporary directory 64 65 By default temporary directory is located 66 $LOCATION/$MAPSET/.tmp/$HOSTNAME. If GRASS_TMPDIR_MAPSET is set to 67 "0", the temporary directory is located in TMPDIR (environmental 68 variable defined by the user or GRASS initialization script if not 69 given). 70 71 \param[out] path buffer to hold resultant full path to file 72 \param element database element (eg, "cell", "cellhd", "vector", etc) 73 \param name name of file to build path to (fully qualified names allowed) 74 \param mapset mapset name 75 76 \return pointer to <i>path</i> buffer 77 */ 78 char *G_file_name_tmp(char *path, 79 const char *element, 80 const char *name, const char *mapset) 81 { 82 const char *env, *tmp_path; 83 84 tmp_path = NULL; 85 env = getenv("GRASS_TMPDIR_MAPSET"); 86 if (env && strcmp(env, "0") == 0) { 87 tmp_path = getenv("TMPDIR"); 88 } 89 90 return file_name(path, NULL, element, name, mapset, tmp_path); 91 } 92 93 char *file_name(char *path, 94 const char *dir, const char *element, const char *name, 95 const char *mapset, const char *base) 96 { 38 97 const char *pname = name; 39 char *location = G__location_path(); 40 41 /* 42 * if a name is given, build a file name 43 * must split the name into name, mapset if it is 44 * in the name@mapset format 45 */ 46 if (name && *name && G_name_is_fully_qualified(name, xname, xmapset)) { 47 pname = xname; 48 sprintf(path, "%s/%s", location, xmapset); 98 99 if (base && *base) { 100 sprintf(path, "%s", base); 49 101 } 50 else if (mapset && *mapset) 51 sprintf(path, "%s/%s", location, mapset); 52 else 53 sprintf(path, "%s/%s", location, G_mapset()); 54 55 G_free(location); 56 57 if (!element && !pname) 58 return path; 59 60 if (element && *element) { 61 strcat(path, "/"); 62 strcat(path, element); 102 else { 103 char xname[GNAME_MAX]; 104 char xmapset[GMAPSET_MAX]; 105 char *location = G__location_path(); 106 107 /* 108 * if a name is given, build a file name 109 * must split the name into name, mapset if it is 110 * in the name@mapset format 111 */ 112 if (name && *name && G_name_is_fully_qualified(name, xname, xmapset)) { 113 pname = xname; 114 sprintf(path, "%s/%s", location, xmapset); 115 } 116 else if (mapset && *mapset) 117 sprintf(path, "%s/%s", location, mapset); 118 else 119 sprintf(path, "%s/%s", location, G_mapset()); 120 G_free(location); 63 121 } 64 122 65 if ( pname && *pname) {123 if (dir && *dir) { /* misc element */ 66 124 strcat(path, "/"); 67 strcat(path, pname); 125 strcat(path, dir); 126 127 if (pname && *pname) { 128 strcat(path, "/"); 129 strcat(path, pname); 130 } 131 132 if (element && *element) { 133 strcat(path, "/"); 134 strcat(path, element); 135 } 136 } 137 else { 138 if (element && *element) { 139 strcat(path, "/"); 140 strcat(path, element); 141 } 142 143 if (pname && *pname) { 144 strcat(path, "/"); 145 strcat(path, pname); 146 } 68 147 } 69 148 … … 72 151 return path; 73 152 } 74 75 char *G_file_name_misc(char *path,76 const char *dir,77 const char *element,78 const char *name, const char *mapset)79 {80 char xname[GNAME_MAX];81 char xmapset[GMAPSET_MAX];82 const char *pname = name;83 char *location = G__location_path();84 85 /*86 * if a name is given, build a file name87 * must split the name into name, mapset if it is88 * in the name@mapset format89 */90 if (name && *name && G_name_is_fully_qualified(name, xname, xmapset)) {91 pname = xname;92 sprintf(path, "%s/%s", location, xmapset);93 }94 else if (mapset && *mapset)95 sprintf(path, "%s/%s", location, mapset);96 else97 sprintf(path, "%s/%s", location, G_mapset());98 99 G_free(location);100 101 if (dir && *dir) {102 strcat(path, "/");103 strcat(path, dir);104 }105 106 if (pname && *pname) {107 strcat(path, "/");108 strcat(path, pname);109 }110 111 if (element && *element) {112 strcat(path, "/");113 strcat(path, element);114 }115 116 return path;117 } -
grass/trunk/lib/gis/mapset_msc.c
r63846 r65348 20 20 #include <grass/glocale.h> 21 21 22 static int make_mapset_element(const char *, const char *); 23 22 24 /*! 23 25 \brief Create element in the current mapset. 24 26 25 Make the specified element in the current mapset 26 will check for the existence of the element and 27 do nothing if it is found so this routine 28 can be called even if the element already exists. 27 Make the specified element in the current mapset will check for the 28 existence of the element and do nothing if it is found so this 29 routine can be called even if the element already exists. 30 31 Calls G_fatal_error() on failure. 32 33 \param p_element element to be created in mapset 29 34 30 \param element element to be created in mapset 31 32 \return 0 ? 33 \return ? 35 \return 0 no element defined 36 \return 1 on success 34 37 */ 35 38 int G_make_mapset_element(const char *p_element) 36 39 { 37 40 char path[GPATH_MAX]; 38 char *p; 41 42 G_file_name(path, NULL, NULL, G_mapset()); 43 return make_mapset_element(path, p_element); 44 } 45 46 /*! 47 \brief Create element in the temporary directory. 48 49 See G_file_name_tmp() for details. 50 51 \param p_element element to be created in mapset 52 53 \return 0 no element defined 54 \return 1 on success 55 */ 56 int G_make_mapset_element_tmp(const char *p_element) 57 { 58 char path[GPATH_MAX]; 59 60 G_file_name_tmp(path, NULL, NULL, G_mapset()); 61 return make_mapset_element(path, p_element); 62 } 63 64 int make_mapset_element(const char *p_path, const char *p_element) 65 { 66 char path[GPATH_MAX], *p; 39 67 const char *element; 40 68 … … 43 71 return 0; 44 72 45 G_file_name(p = path, NULL, NULL, G_mapset()); 73 strncpy(path, p_path, GPATH_MAX); 74 p = path; 46 75 while (*p) 47 76 p++; … … 76 105 77 106 \param dir directory path 78 \param name element name107 \param name element to be created in mapset 79 108 80 \return 0 ?81 \return ?109 \return 0 no element defined 110 \return 1 on success 82 111 */ 83 112 int G__make_mapset_element_misc(const char *dir, const char *name) -
grass/trunk/lib/gis/open.c
r65195 r65348 4 4 * \brief GIS Library - Open file functions 5 5 * 6 * (C) 1999-20 09by the GRASS Development Team6 * (C) 1999-2015 by the GRASS Development Team 7 7 * 8 8 * This program is free software under the GNU General Public … … 54 54 { 55 55 int fd; 56 int is_tmp; 56 57 char path[GPATH_MAX]; 57 58 char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; 58 59 59 60 60 G__check_gisinit(); 61 62 is_tmp = (element && strncmp(element, ".tmp", 3) == 0); 63 64 if (is_tmp) 65 G_file_name_tmp(path, element, name, mapset); 66 else 67 G_file_name(path, element, name, mapset); 61 68 62 69 /* READ */ … … 72 79 } 73 80 74 mapset = G_find_file2(element, name, mapset); 75 76 if (!mapset) 77 return -1; 78 79 G_file_name(path, element, name, mapset); 80 81 if (!is_tmp) { 82 mapset = G_find_file2(element, name, mapset); 83 84 if (!mapset) 85 return -1; 86 } 87 81 88 if ((fd = open(path, 0)) < 0) 82 89 G_warning(_("G__open(read): Unable to open '%s': %s"), … … 99 106 return -1; 100 107 101 G_file_name(path, element, name, mapset);102 103 108 if (mode == 1 || access(path, 0) != 0) { 104 G_make_mapset_element(element); 109 if (is_tmp) 110 G_make_mapset_element_tmp(element); 111 else 112 G_make_mapset_element(element); 105 113 close(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666)); 106 114 } -
grass/trunk/lib/gis/remove.c
r63640 r65348 22 22 #include <grass/gis.h> 23 23 24 static int recursive_remove(const char *path);25 24 static int G__remove(int misc, const char *dir, const char *element, 26 25 const char *name); … … 96 95 return 0; 97 96 98 if ( recursive_remove(path) == 0)97 if (G_recursive_remove(path) == 0) 99 98 return 1; 100 99 … … 102 101 } 103 102 104 /* equivalent to rm -rf path */ 105 static int recursive_remove(const char *path) 103 /*! 104 \brief Recursively remove all files in given directory 105 106 Equivalent to rm -rf path. 107 108 \param path path to the directory which should be removed 109 110 \return 0 on success 111 \return -1 on error 112 */ 113 int G_recursive_remove(const char *path) 106 114 { 107 115 DIR *dirp; … … 111 119 112 120 if (G_lstat(path, &sb)) 113 return 1;121 return -1; 114 122 if (!S_ISDIR(sb.st_mode)) 115 return remove(path) == 0 ? 0 : 1;123 return remove(path) == 0 ? 0 : -1; 116 124 117 125 if ((dirp = opendir(path)) == NULL) 118 return 1;126 return -1; 119 127 while ((dp = readdir(dirp)) != NULL) { 120 128 if (dp->d_name[0] == '.') … … 123 131 continue; 124 132 sprintf(path2, "%s/%s", path, dp->d_name); 125 recursive_remove(path2);133 G_recursive_remove(path2); 126 134 } 127 135 closedir(dirp); 128 136 129 return rmdir(path) == 0 ? 0 : 1;137 return rmdir(path) == 0 ? 0 : -1; 130 138 } -
grass/trunk/lib/gis/rename.c
r63640 r65348 4 4 * \brief GIS Library - Rename file functions. 5 5 * 6 * (C) 2001-20 09by the GRASS Development Team6 * (C) 2001-2015 by the GRASS Development Team 7 7 * 8 8 * This program is free software under the GNU General Public License … … 20 20 21 21 /*! 22 \brief Rename a file in the filesystem.22 \brief Rename a file or a directory in the filesystem. 23 23 24 24 The file or directory <i>oldname</i> is renamed to <i>newname</i>. … … 32 32 int G_rename_file(const char *oldname, const char *newname) 33 33 { 34 int ret; 34 35 35 36 #ifdef __MINGW32__ 36 37 remove(newname); 37 38 #endif 39 40 ret = rename(oldname, newname); 38 41 39 return rename(oldname, newname); 42 if (ret == -1) { 43 /* if fails, try to copy file and then remove */ 44 if (1 == G_copy_file(oldname, newname)) { 45 if (remove(oldname) != -1) 46 ret = 0; 47 } 48 } 49 50 return ret; 40 51 } 41 52 -
grass/trunk/lib/gis/tempfile.c
r65195 r65348 4 4 * \brief GIS Library - Temporary file functions. 5 5 * 6 * (C) 2001-20 09by the GRASS Development Team6 * (C) 2001-2015 by the GRASS Development Team 7 7 * 8 8 * This program is free software under the GNU General Public License … … 15 15 #include <unistd.h> 16 16 #include <sys/stat.h> 17 #include <stdlib.h> 18 17 19 #include <grass/gis.h> 18 20 … … 84 86 int uniq = G_counter_next(&unique); 85 87 sprintf(name, "%d.%d", pid, uniq); 86 G_file_name (path, element, name, G_mapset());88 G_file_name_tmp(path, element, name, G_mapset()); 87 89 } 88 90 while (access(path, F_OK) == 0); 89 91 92 G_debug(2, "G_tempfile_pid(): %s", path); 93 90 94 return G_store(path); 91 95 } … … 98 102 void G_temp_element(char *element) 99 103 { 100 const char *machine ;104 const char *machine, *env; 101 105 102 106 strcpy(element, ".tmp"); … … 106 110 strcat(element, machine); 107 111 } 108 G_make_mapset_element(element); 112 113 env = getenv("GRASS_TMPDIR_MAPSET"); 114 if (!env || strcmp(env, "0") != 0) 115 G_make_mapset_element(element); 116 else 117 G_make_mapset_element_tmp(element); 118 119 G_debug(2, "G_temp_element(): %s (GRASS_TMPDIR_MAPSET=%s)", 120 element, env ? env : ""); 109 121 } -
grass/trunk/lib/init/clean_temp.c
r63843 r65348 136 136 /* Get the mapset temp directory */ 137 137 G_temp_element(element); 138 G_file_name (tmppath, element, "", mapset = G_mapset());138 G_file_name_tmp(tmppath, element, "", mapset = G_mapset()); 139 139 140 140 /* get user id and current time in seconds */ -
grass/trunk/lib/init/grass.py
r65347 r65348 986 986 continue 987 987 988 if k in ('TMPDIR', 'TEMP', 'TMP'):989 continue # don't overwrite TMPDIR set by create_tmp()990 991 988 debug("Environmental variable set {}={}".format(k, v)) 992 989 os.environ[k] = v … … 1701 1698 set_language(grass_config_dir) 1702 1699 1700 # Set shell (needs to be called before load_env()) 1701 sh, shellname = get_shell() 1702 grass_env_file = get_grass_env_file(sh, grass_config_dir) 1703 1704 # Load environmental variables from the file (needs to be called 1705 # before create_tmp()) 1706 load_env(grass_env_file) 1707 1703 1708 # Create the temporary directory and session grassrc file 1704 1709 tmpdir = create_tmp(user, gis_lock) … … 1711 1716 # Create the session grassrc file 1712 1717 gisrc = create_gisrc(tmpdir, gisrcrc) 1713 1714 # Set shell (needs to be called before load_env())1715 sh, shellname = get_shell()1716 grass_env_file = get_grass_env_file(sh, grass_config_dir)1717 1718 # Load environmental variables from the file1719 load_env(grass_env_file)1720 1718 1721 1719 ensure_home() -
grass/trunk/lib/init/variables.html
r64948 r65348 277 277 <dt>GRASS_VECTOR_TEMPORARY</dt> 278 278 <dd>[vectorlib]<br> If the environment variable 279 GRASS_VECTOR_TEMPORARY exists, vector library will operate with 280 temporary vector maps. New vector maps will be created 281 in <tt>$MAPSET/.tmp/$HOSTNAME/vector</tt>, existing vector maps 282 will be read also from this directory. Note that temporary vector 283 maps are not visible to the user 284 via <em><a href="g.list.html">g.list</a></em>. They are used 285 internally by the GRASS modules and deleted automatically when the 286 map is closed or GRASS session quited. Note that this variable is 287 dedicated for internal use only.</dd> 279 GRASS_VECTOR_TEMPORARY exists, GRASS vector library will operate 280 on temporary vector maps. New vector maps will be created in 281 temporary directory (see GRASS_TMPDIR_MAPSET variable), existing 282 vector maps will be read (if found) also from this directory. It 283 may be set to either: 284 <ul> 285 <li><tt>keep</tt> - the temporary vector map is not deleted when 286 closing the map. 287 <li><tt>move</tt> - the temporary vector map is moved to the 288 current mapset when closing the map.</li> 289 <li><tt>delete</tt> - the temporary vector map is deleted when 290 closing the map. 291 </li> 292 </ul> 293 Default value is <tt>keep</tt>. 294 295 Note that temporary vector maps are not visible to the user 296 via <em><a href="g.list.html">g.list</a></em> 297 or <em><a href="wxGUI.html">wxGUI</a></em>. They are used 298 internally by the GRASS modules and deleted automatically when 299 GRASS session is quited.</dd> 288 300 289 301 <dt>GRASS_VECTOR_TOPO_DEBUG</dt> … … 335 347 The default is set to the number of CPUs on the system. 336 348 Setting to '1' effectively disables parallel processing.</dd> 337 349 350 <dt>GRASS_TMPDIR_MAPSET</dt> 351 <dd> By default GRASS temporary directory is located in 352 <tt>$LOCATION/$MAPSET/.tmp/$HOSTNAME</tt>. If GRASS_TMPDIR_MAPSET is 353 set to '0', the temporary directory is located in TMPDIR 354 (environmental variable defined by the user or GRASS initialization 355 script if not given).</dd> 356 338 357 <dt>TMPDIR, TEMP, TMP</dt> 339 358 <dd>[Various GRASS GIS commands and wxGUI]<br> -
grass/trunk/lib/raster/close.c
r65323 r65348 376 376 if (fcb->null_cur_row > 0) { 377 377 /* if temporary NULL file exists, write it into cell_misc/name/null */ 378 if ( rename(fcb->null_temp_name, path)) {379 G_warning(_("Unable to rename null file '%s' to '%s': %s"),378 if (0 != G_rename_file(fcb->null_temp_name, path)) { 379 G_warning(_("Unable to rename file <%s> to <%s>: %s"), 380 380 fcb->null_temp_name, path, strerror(errno)); 381 381 stat = -1; 382 382 } 383 /* if rename() was successful what is left to remove() ? */383 /* if rename() was successful what is left to remove() ? */ 384 384 else { 385 385 remove(fcb->null_temp_name); 386 }386 } 387 387 } 388 388 else { … … 451 451 G_file_name(path, CELL_DIR, fcb->name, fcb->mapset); 452 452 remove(path); 453 if (rename(fcb->temp_name, path)) { 454 G_warning(_("Unable to rename cell file '%s' to '%s': %s"), 453 454 if (G_rename_file(fcb->temp_name, path)) { 455 G_warning(_("Unable to rename file <%s> to <%s>: %s"), 455 456 fcb->temp_name, path, strerror(errno)); 456 457 stat = -1; -
grass/trunk/lib/vector/Vlib/build.c
r64207 r65348 999 999 { 1000 1000 struct Plus_head *plus; 1001 char *path;1001 char path[GPATH_MAX]; 1002 1002 struct gvfile fp; 1003 1003 … … 1008 1008 dig_file_init(&fp); 1009 1009 1010 path = Vect__get_path(Map);1010 Vect__get_path(path, Map); 1011 1011 fp.file = G_fopen_new(path, GV_TOPO_ELEMENT); 1012 G_free(path);1013 1012 if (fp.file == NULL) { 1014 1013 G_warning(_("Unable to create topo file for vector map <%s>"), Map->name); … … 1248 1247 { 1249 1248 struct Plus_head *plus; 1250 char *file_path;1249 char file_path[GPATH_MAX]; 1251 1250 1252 1251 G_debug(1, "Vect_save_spatial_index()"); … … 1262 1261 if (plus->Spidx_new == TRUE) { 1263 1262 /* write out rtrees to sidx file */ 1264 file_path = Vect__get_element_path(Map, GV_SIDX_ELEMENT);1263 Vect__get_element_path(file_path, Map, GV_SIDX_ELEMENT); 1265 1264 G_debug(1, "Open sidx: %s", file_path); 1266 1265 dig_file_init(&(plus->spidx_fp)); 1267 1266 plus->spidx_fp.file = fopen(file_path, "w+"); 1268 G_free(file_path);1269 1267 if (plus->spidx_fp.file == NULL) { 1270 1268 G_warning(_("Unable to create spatial index file for vector map <%s>"), -
grass/trunk/lib/vector/Vlib/build_ogr.c
r51082 r65348 27 27 #include <ogr_api.h> 28 28 #endif 29 30 #include "local_proto.h" 29 31 30 32 /*! … … 125 127 126 128 sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name); 127 G_file_name(fname, elem, GV_FIDX_ELEMENT, Map->mapset);129 Vect__get_element_path(fname, Map, GV_FIDX_ELEMENT); 128 130 G_debug(4, "Open fidx: %s", fname); 129 131 dig_file_init(&fp); -
grass/trunk/lib/vector/Vlib/cindex.c
r58403 r65348 473 473 { 474 474 struct Plus_head *plus; 475 char *path;475 char path[GPATH_MAX]; 476 476 struct gvfile fp; 477 477 … … 483 483 dig_file_init(&fp); 484 484 485 path = Vect__get_path(Map);485 Vect__get_path(path, Map); 486 486 fp.file = G_fopen_new(path, GV_CIDX_ELEMENT); 487 G_free(path);488 487 if (fp.file == NULL) { 489 488 G_warning(_("Unable to create category index file for vector map <%s>"), … … 518 517 { 519 518 int ret; 520 char file_path[GPATH_MAX], *path;519 char file_path[GPATH_MAX], path[GPATH_MAX]; 521 520 struct gvfile fp; 522 521 struct Plus_head *Plus; … … 527 526 Plus = &(Map->plus); 528 527 529 path = Vect__get_path(Map);530 G_file_name(file_path, path, GV_CIDX_ELEMENT, Map->mapset);531 528 Vect__get_path(path, Map); 529 Vect__get_element_path(file_path, Map, GV_CIDX_ELEMENT); 530 532 531 if (access(file_path, F_OK) != 0) { /* does not exist */ 533 G_free(path);534 532 return 1; 535 533 } … … 537 535 dig_file_init(&fp); 538 536 fp.file = G_fopen_old(path, GV_CIDX_ELEMENT, Map->mapset); 539 G_free(path);540 537 541 538 if (fp.file == NULL) { /* category index file is not available */ -
grass/trunk/lib/vector/Vlib/close.c
r58354 r65348 324 324 void unlink_file(const struct Map_info *Map, const char *name) 325 325 { 326 char *path;326 char path[GPATH_MAX]; 327 327 328 328 /* delete old support files if available */ 329 path = Vect__get_element_path(Map, name);329 Vect__get_element_path(path, Map, name); 330 330 if (access(path, F_OK) == 0) { /* file exists? */ 331 331 G_debug(2, "\t%s: unlink", path); 332 332 unlink(path); 333 333 } 334 335 G_free(path); 336 } 334 } -
grass/trunk/lib/vector/Vlib/close_nat.c
r57444 r65348 6 6 Higher level functions for reading/writing/manipulating vectors. 7 7 8 (C) 2001-20 09, 2013by the GRASS Development Team8 (C) 2001-2015 by the GRASS Development Team 9 9 10 10 This program is free software under the GNU General Public License … … 16 16 17 17 #include <stdlib.h> 18 #include <unistd.h> 19 #include <errno.h> 18 20 19 21 #include <grass/vector.h> 22 #include <grass/glocale.h> 20 23 21 24 #include "local_proto.h" … … 53 56 /* delete temporary map ? */ 54 57 if (Map->temporary) { 55 if (getenv("GRASS_VECTOR_TEMPORARY") == NULL) { 56 G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED", Map->name); 57 Vect__delete(Map->name, TRUE); 58 int delete; 59 char *env = getenv("GRASS_VECTOR_TEMPORARY"); 60 61 delete = TRUE; 62 if (env) { 63 if (G_strcasecmp(env, "move") == 0) { 64 /* copy temporary vector map to the current mapset */ 65 char path_tmp[GPATH_MAX], path_map[GPATH_MAX]; 66 67 G_debug(1, "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"), 75 path_map, 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, strerror(errno)); 82 83 #ifdef TEMPORARY_MAP_DB 84 int i, ndblinks; 85 86 struct field_info *fi; 87 dbConnection connection; 88 struct dblinks *dblinks; 89 90 G_debug(1, "V1_close_nat(): copying attributes"); 91 /* copy also attributes */ 92 dblinks = Vect_new_dblinks_struct(); 93 db_get_connection(&connection); 94 ndblinks = Vect_get_num_dblinks(Map); 95 for (i = 0; i < ndblinks; i++) { 96 fi = Vect_get_dblink(Map, i); 97 if (DB_OK != db_copy_table(fi->driver, fi->database, fi->table, 98 connection.driverName, 99 connection.databaseName, 100 fi->table)) { 101 G_warning(_("Unable to copy table <%s>"), fi->table); 102 continue; 103 } 104 105 Vect_add_dblink(dblinks, fi->number, fi->name, 106 fi->table, fi->key, connection.databaseName, 107 connection.driverName); 108 G_free(fi); 109 } 110 G_free(Map->dblnk); 111 Map->dblnk = dblinks; 112 Map->temporary = FALSE; 113 Vect_write_dblinks(Map); 114 Map->temporary = TRUE; 115 #endif 116 } 117 else if (G_strcasecmp(env, "delete") == 0) { 118 /* delete temporary vector map */ 119 G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED", Map->name); 120 } 121 else { 122 /* do not delete temporary vector map */ 123 G_debug(1, "V1_close_nat(): temporary map <%s> IS NOT DELETED", 124 Map->name); 125 delete = FALSE; 126 } 58 127 } 59 else { 60 G_debug(1, "V1_close_nat(): temporary map <%s> IS NOT DELETED", Map->name); 128 129 if (delete) { 130 char path_tmp[GPATH_MAX]; 131 132 /* delete vector directory */ 133 Vect__get_element_path(path_tmp, Map, NULL); 134 G_recursive_remove(path_tmp); 135 136 #ifndef TEMPORARY_MAP_DB 137 if (G_strcasecmp(env, "move") != 0) { 138 int i, ndblinks; 139 140 dbDriver *driver; 141 dbString table_name; 142 143 struct field_info *fi; 144 145 db_init_string(&table_name); 146 147 /* drop also attribute table */ 148 ndblinks = Vect_get_num_dblinks(Map); 149 for (i = 0; i < ndblinks; i++) { 150 fi = Vect_get_dblink(Map, i); 151 152 driver = db_start_driver_open_database(fi->driver, fi->database); 153 if (driver == NULL) { 154 G_warning(_("Unable to open database <%s> by driver <%s>"), 155 fi->database, fi->driver); 156 continue; 157 } 158 159 db_set_string(&table_name, fi->table); 160 if (DB_OK != db_drop_table(driver, &table_name)) { 161 G_warning(_("Unable to drop table <%s>"), fi->table); 162 continue; 163 } 164 } 165 } 166 #endif 61 167 } 62 168 } -
grass/trunk/lib/vector/Vlib/close_pg.c
r60561 r65348 128 128 /* delete old support files if available */ 129 129 sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name); 130 131 G_file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset()); 130 Vect__get_element_path(file_path, Map, GV_TOPO_ELEMENT); 132 131 if (access(file_path, F_OK) == 0) /* file exists? */ 133 132 unlink(file_path); -
grass/trunk/lib/vector/Vlib/field.c
r55762 r65348 386 386 387 387 fi->key = G_store(GV_KEY_COLUMN); /* Should be: id/fid/gfid/... ? */ 388 #ifdef TEMPORARY_MAP_DB 389 if (Map->temporary) { 390 Vect__get_element_path(buf, Map, NULL); 391 if (strcmp(DB_DEFAULT_DRIVER, "sqlite") == 0) 392 strcat(buf, "/sqlite.db"); 393 else 394 strcat(buf, "/db.dbf"); 395 fi->database = G_store(buf); 396 fi->driver = DB_DEFAULT_DRIVER; 397 } 398 else { 399 fi->database = G_store(connection.databaseName); 400 fi->driver = G_store(connection.driverName); 401 } 402 #else 388 403 fi->database = G_store(connection.databaseName); 389 404 fi->driver = G_store(connection.driverName); 390 405 #endif 406 391 407 return fi; 392 408 } … … 561 577 char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname; 562 578 int fld; 563 char *c, *path;579 char *c, path[GPATH_MAX]; 564 580 int row, rule; 565 581 struct dblinks *dbl; … … 570 586 571 587 /* Read dblink for native format */ 572 path = Vect__get_path(Map);588 Vect__get_path(path, Map); 573 589 fd = G_fopen_old(path, GV_DBLN_ELEMENT, Map->mapset); 574 G_free(path);575 590 if (fd == NULL) { /* This may be correct, no tables defined */ 576 591 G_debug(1, "Cannot open vector database definition file"); … … 893 908 int i; 894 909 FILE *fd; 895 char *path, buf[1024];910 char path[GPATH_MAX], buf[1024]; 896 911 struct dblinks *dbl; 897 912 … … 905 920 dbl = Map->dblnk; 906 921 907 path = Vect__get_path(Map);922 Vect__get_path(path, Map); 908 923 fd = G_fopen_new(path, GV_DBLN_ELEMENT); 909 G_free(path);910 924 if (fd == NULL) { /* This may be correct, no tables defined */ 911 925 G_warning(_("Unable to create database definition file for vector map <%s>"), -
grass/trunk/lib/vector/Vlib/header.c
r63834 r65348 78 78 int Vect__write_head(const struct Map_info *Map) 79 79 { 80 char *path;80 char path[GPATH_MAX]; 81 81 FILE *head_fp; 82 82 83 path = Vect__get_path(Map);83 Vect__get_path(path, Map); 84 84 head_fp = G_fopen_new(path, GV_HEAD_ELEMENT); 85 G_free(path);86 85 if (head_fp == NULL) { 87 86 G_warning(_("Unable to create header file for vector map <%s>"), … … 119 118 FILE *head_fp; 120 119 char buff[2000]; 121 char *path, *ptr;120 char path[GPATH_MAX], *ptr; 122 121 123 122 /* Reset / init */ … … 125 124 126 125 G_debug(1, "Vect__read_head(): vector = %s@%s", Map->name, Map->mapset); 127 path = Vect__get_path(Map);126 Vect__get_path(path, Map); 128 127 head_fp = G_fopen_old(path, GV_HEAD_ELEMENT, Map->mapset); 129 G_free(path);130 128 if (head_fp == NULL) { 131 129 G_warning(_("Unable to open header file of vector <%s>"), -
grass/trunk/lib/vector/Vlib/local_proto.h
r58363 r65348 7 7 #define CACHE_FEATURE 0 8 8 #define CACHE_MAP 1 9 10 /*! Attributes of temporary maps */ 11 /* #define TEMPORARY_MAP_DB */ 9 12 10 13 /* Internal vector library subroutines which are not part of public … … 28 31 int Vect__open_old(struct Map_info *, const char *, const char *, 29 32 const char *, int, int, int); 30 char *Vect__get_path(c onst struct Map_info *);31 char *Vect__get_element_path(c onst struct Map_info *, const char *);33 char *Vect__get_path(char *, const struct Map_info *); 34 char *Vect__get_element_path(char *, const struct Map_info *, const char *); 32 35 33 36 /* write_nat.c */ -
grass/trunk/lib/vector/Vlib/map.c
r64278 r65348 23 23 #include <sys/stat.h> 24 24 #include <fcntl.h> 25 #include <errno.h> 25 26 26 27 #include <grass/glocale.h> … … 348 349 { 349 350 int ret; 350 char *path, path_buf[GPATH_MAX];351 char path[GPATH_MAX], path_buf[GPATH_MAX]; 351 352 char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; 352 const char *tmp, *mapset ;353 const char *tmp, *mapset, *env; 353 354 354 355 struct Map_info Map; … … 385 386 } 386 387 387 path = Vect__get_element_path(&Map, GV_DBLN_ELEMENT);388 Vect__get_element_path(path, &Map, GV_DBLN_ELEMENT); 388 389 G_debug(1, "dbln file: %s", path); 389 390 … … 440 441 } 441 442 } 442 G_free(path);443 443 444 444 /* Delete all files from vector/name directory */ 445 path = Vect__get_element_path(&Map, NULL);445 Vect__get_element_path(path, &Map, NULL); 446 446 Vect_close(&Map); 447 447 G_debug(3, "opendir '%s'", path); … … 469 469 closedir(dir); 470 470 471 /* NFS can create .nfsxxxxxxxx files for those deleted 472 * -> we have to move the directory to ./tmp before it is deleted */ 473 tmp = G_tempfile(); 474 475 G_debug(3, "rename '%s' to '%s'", path, tmp); 476 ret = rename(path, tmp); 477 if (ret == -1) { 478 G_warning(_("Unable to rename directory '%s' to '%s'"), path, tmp); 479 return -1; 480 } 481 G_free(path); 471 env = getenv("GRASS_TMPDIR_MAPSET"); 472 if (env && strcmp(env, "0") == 0) { 473 tmp = path; 474 } 475 else { 476 /* NFS can create .nfsxxxxxxxx files for those deleted 477 * -> we have to move the directory to ./tmp before it is deleted */ 478 tmp = G_tempfile(); 479 480 G_debug(3, "rename '%s' to '%s'", path, tmp); 481 482 ret = rename(path, tmp); 483 if (ret == -1) { 484 G_warning(_("Unable to rename directory '%s' to '%s'"), path, tmp); 485 return -1; 486 } 487 } 482 488 483 489 G_debug(3, "remove directory '%s'", tmp); … … 485 491 ret = rmdir(tmp); 486 492 if (ret == -1) { 487 G_warning(_("Unable to remove directory '%s'"), tmp); 493 G_warning(_("Unable to remove directory '%s': %s"), 494 tmp, strerror(errno)); 488 495 return -1; 489 496 } -
grass/trunk/lib/vector/Vlib/open.c
r64320 r65348 164 164 { 165 165 char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; 166 char *path;166 char path[GPATH_MAX]; 167 167 FILE *fp; 168 168 int level, level_request; … … 175 175 is_tmp); 176 176 177 if ( !is_tmp) {177 if (update && !is_tmp) { 178 178 is_tmp = getenv("GRASS_VECTOR_TEMPORARY") ? TRUE : FALSE; 179 179 G_debug(1, "Vect__open_old(): is_tmp = %d (check GRASS_VECTOR_TEMPORARY)", is_tmp); … … 222 222 } 223 223 224 path = Vect__get_path(Map);224 Vect__get_path(path, Map); 225 225 226 226 if (!ogr_mapset) { … … 254 254 } 255 255 } 256 G_file_name(file_path, path, GV_HEAD_ELEMENT, Map->mapset); 256 257 Vect__get_element_path(file_path, Map, GV_HEAD_ELEMENT); 257 258 if (access(file_path, F_OK) != 0) 258 259 return -1; … … 522 523 char file_path[GPATH_MAX]; 523 524 524 G_file_name(file_path, path, GV_TOPO_ELEMENT, G_mapset());525 Vect__get_element_path(file_path, Map, GV_TOPO_ELEMENT); 525 526 if (access(file_path, F_OK) == 0) /* topo file exists? */ 526 527 unlink(file_path); 527 528 528 G_file_name(file_path, path, GV_SIDX_ELEMENT, G_mapset());529 Vect__get_element_path(file_path, Map, GV_SIDX_ELEMENT); 529 530 if (access(file_path, F_OK) == 0) /* sidx file exists? */ 530 531 unlink(file_path); 531 532 532 G_file_name(file_path, path, GV_CIDX_ELEMENT, G_mapset());533 Vect__get_element_path(file_path, Map, GV_CIDX_ELEMENT); 533 534 if (access(file_path, F_OK) == 0) /* cidx file exists? */ 534 535 unlink(file_path); 535 536 536 537 if (format == GV_FORMAT_OGR || format == GV_FORMAT_POSTGIS) { 537 G_file_name(file_path, path, GV_FIDX_ELEMENT, G_mapset());538 Vect__get_element_path(file_path, Map, GV_FIDX_ELEMENT); 538 539 if (access(file_path, F_OK) == 0) /* fidx file exists? */ 539 540 unlink(file_path); 540 541 } 541 542 } 542 543 G_free(path);544 543 545 544 return level; … … 792 791 if (Map->format != GV_FORMAT_OGR_DIRECT && 793 792 getenv("GRASS_VECTOR_PGFILE") == NULL) { /* GRASS_VECTOR_PGFILE defined by v.out.postgis */ 794 char *path; 793 char *env; 794 char path[GPATH_MAX]; 795 795 796 796 G_debug(2, " using non-direct format"); … … 805 805 } 806 806 } 807 else { 807 808 env = getenv("GRASS_VECTOR_TEMPORARY"); 809 if (!Map->temporary || (env && strcmp(env, "move") == 0)) { 808 810 if (G_find_vector2(name, G_mapset()) != NULL) { 809 811 G_warning(_("Vector map <%s> already exists and will be overwritten"), … … 829 831 830 832 /* create history file */ 831 path = Vect__get_path(Map);833 Vect__get_path(path, Map); 832 834 Map->hist_fp = G_fopen_new(path, GV_HIST_ELEMENT); 833 G_free(path);834 835 if (Map->hist_fp == NULL) { 835 836 G_warning(_("Unable to open history file of vector map <%s>"), … … 964 965 int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info) 965 966 { 966 char *path,file_path[GPATH_MAX];967 char file_path[GPATH_MAX]; 967 968 struct stat stat_buf; 968 969 969 970 switch (Map->format) { 970 971 case GV_FORMAT_NATIVE: 971 path = Vect__get_path(Map); 972 G_file_name(file_path, path, GV_COOR_ELEMENT, Map->mapset); 973 G_free(path); 972 Vect__get_element_path(file_path, Map, GV_COOR_ELEMENT); 974 973 G_debug(1, "get coor info: %s", file_path); 975 974 if (0 != stat(file_path, &stat_buf)) { … … 1088 1087 { 1089 1088 int err, ret; 1090 char file_path[GPATH_MAX], *path;1089 char file_path[GPATH_MAX], path[GPATH_MAX]; 1091 1090 struct gvfile fp; 1092 1091 struct Coor_info CInfo; … … 1098 1097 Plus = &(Map->plus); 1099 1098 1100 path = Vect__get_path(Map); 1101 G_file_name(file_path, path, GV_TOPO_ELEMENT, Map->mapset); 1102 1099 Vect__get_path(path, Map); 1100 Vect__get_element_path(file_path, Map, GV_TOPO_ELEMENT); 1103 1101 if (access(file_path, F_OK) != 0) { /* does not exist */ 1104 G_free(path);1105 1102 return 1; 1106 1103 } … … 1108 1105 dig_file_init(&fp); 1109 1106 fp.file = G_fopen_old(path, GV_TOPO_ELEMENT, Map->mapset); 1110 G_free(path);1111 1107 1112 1108 if (fp.file == NULL) { /* topo file is not available */ … … 1187 1183 1188 1184 if (mode < 2) { 1189 char *path, file_path[GPATH_MAX];1185 char path[GPATH_MAX], file_path[GPATH_MAX]; 1190 1186 1191 path = Vect__get_path(Map); 1192 G_file_name(file_path, path, GV_SIDX_ELEMENT, Map->mapset); 1193 1187 Vect__get_path(path, Map); 1188 Vect__get_element_path(file_path, Map, GV_SIDX_ELEMENT); 1194 1189 if (access(file_path, F_OK) != 0) /* does not exist */ 1195 1190 return 1; 1196 1191 1197 1192 Plus->spidx_fp.file = G_fopen_old(path, GV_SIDX_ELEMENT, Map->mapset); 1198 G_free(path);1199 1193 1200 1194 if (Plus->spidx_fp.file == NULL) { /* sidx file is not available */ … … 1436 1430 \brief Get map directory name (internal use only) 1437 1431 1438 Allocate string should be freed by G_free(). 1439 1432 \param file_path path string buffer 1440 1433 \param Map pointer to Map_info struct 1441 1434 1442 \return allocated buffer containing path 1443 */ 1444 char *Vect__get_path(const struct Map_info *Map) 1445 { 1446 char path[GPATH_MAX]; 1447 1435 \return buffer containing path 1436 */ 1437 char *Vect__get_path(char *path, const struct Map_info *Map) 1438 { 1448 1439 if (Map->temporary) { 1449 1440 char path_tmp[GPATH_MAX]; … … 1455 1446 } 1456 1447 1457 return G_store(path);1448 return path; 1458 1449 } 1459 1450 … … 1468 1459 \return allocated buffer containing path 1469 1460 */ 1470 char *Vect__get_element_path(const struct Map_info *Map, const char *element) 1471 { 1472 char file_path[GPATH_MAX], *path; 1473 1474 path = Vect__get_path(Map); 1475 G_file_name(file_path, path, element, Map->mapset); 1476 G_free(path); 1477 1478 return G_store(file_path); 1479 } 1461 char *Vect__get_element_path(char *file_path, 1462 const struct Map_info *Map, const char *element) 1463 { 1464 char path[GPATH_MAX]; 1465 1466 Vect__get_path(path, Map); 1467 if (Map->temporary) 1468 G_file_name_tmp(file_path, path, element, Map->mapset); 1469 else 1470 G_file_name(file_path, path, element, Map->mapset); 1471 1472 return file_path; 1473 } 1474 -
grass/trunk/lib/vector/Vlib/open_nat.c
r57444 r65348 39 39 int V1_open_old_nat(struct Map_info *Map, int update) 40 40 { 41 char *path;41 char path[GPATH_MAX]; 42 42 struct Coor_info CInfo; 43 43 … … 45 45 Map->mapset); 46 46 47 path = Vect__get_path(Map);47 Vect__get_path(path, Map); 48 48 dig_file_init(&(Map->dig_fp)); 49 49 if (update) … … 52 52 Map->dig_fp.file = 53 53 G_fopen_old(path, GV_COOR_ELEMENT, Map->mapset); 54 G_free(path);55 54 56 55 if (Map->dig_fp.file == NULL) { … … 96 95 int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z) 97 96 { 98 char *path, file_path[GPATH_MAX];97 char path[GPATH_MAX]; 99 98 100 99 G_debug(1, "V1_open_new_nat(): name = %s with_z = %d is_tmp = %d", 101 100 name, with_z, Map->temporary); 102 103 path = Vect__get_path(Map);104 101 105 102 /* Set the 'coor' file version */ … … 108 105 Map->head.coor_version.back_major = GV_COOR_EARLIEST_MAJOR; 109 106 Map->head.coor_version.back_minor = GV_COOR_EARLIEST_MINOR; 110 107 108 Vect__get_path(path, Map); 109 111 110 /* TODO: open better */ 112 111 dig_file_init(&(Map->dig_fp)); … … 120 119 if (Map->dig_fp.file == NULL) 121 120 return -1; 122 121 123 122 /* if overwrite OK, any existing files have already been deleted by 124 123 * Vect_open_new(): remove this check ? */ 125 124 /* check to see if dig_plus file exists and if so, remove it */ 126 G_file_name(file_path, path, GV_TOPO_ELEMENT, G_mapset()); 127 G_free(path); 128 if (access(file_path, F_OK) == 0) 129 unlink(file_path); /* remove topo file if exists */ 125 Vect__get_element_path(path, Map, GV_TOPO_ELEMENT); 126 if (access(path, F_OK) == 0) 127 unlink(path); /* remove topo file if exists */ 130 128 131 129 /* set conversion matrices */
Note:
See TracChangeset
for help on using the changeset viewer.
