Changeset 65348


Ignore:
Timestamp:
Jun 1, 2015, 7:57:03 AM (9 years ago)
Author:
martinl
Message:

make GRASS data TMPDIR configurable (GRASS_TMPDIR_MAPSET && TMPDIR)
changes in API (new fns): G_file_name_tmp(), G_make_mapset_element_tmp(), G_recursive_remove()
change GRASS_VECTOR_TEMPORARY to support various modes (delete, keep, and move)
update GIS/Raster/Vector API to support this feature
update initialization script to allow defining TMPDIR

Location:
grass/trunk
Files:
23 edited

Legend:

Unmodified
Added
Removed
  • grass/trunk/include/defs/gis.h

    r63960 r65348  
    248248char *G_file_name_misc(char *, const char *, const char *, const char *,
    249249                       const char *);
     250char *G_file_name_tmp(char *, const char *, const char *, const char *);
    250251
    251252/* find_file.c */
     
    438439/* mapset_msc.c */
    439440int G_make_mapset_element(const char *);
     441int G_make_mapset_element_tmp(const char *);
    440442int G__make_mapset_element_misc(const char *, const char *);
    441443int G_mapset_permissions(const char *);
     
    597599int G_remove(const char *, const char *);
    598600int G_remove_misc(const char *, const char *, const char *);
     601int G_recursive_remove(const char *);
    599602
    600603/* rename.c */
  • grass/trunk/lib/gis/file_name.c

    r55607 r65348  
    44   \brief GIS library - Determine GRASS data base file name
    55
    6    (C) 2001-2008, 2013 by the GRASS Development Team
     6   (C) 2001-2015 by the GRASS Development Team
    77
    88   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.
    1010
    1111   \author Original author CERL
     
    1313
    1414#include <string.h>
     15#include <stdlib.h>
    1516#include <grass/gis.h>
    1617
    1718#include "gis_local_proto.h"
    1819
     20char *file_name(char *, const char *, const char *,
     21                const char *, const char *, const char *);
     22
    1923/*!
    2024  \brief Builds full path names to GIS data files
    2125
    22   If name is of the form "nnn@ppp" then path is set as if name had
    23   been nnn and mapset had been ppp (mapset parameter itself is ignored
    24   in 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).
    2529 
    2630  \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)
    2832  \param name name of file to build path to (fully qualified names allowed)
    2933  \param mapset mapset name
     
    3438                   const char *element, const char *name, const char *mapset)
    3539{
    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*/
     54char *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*/
     78char *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
     93char *file_name(char *path,
     94                const char *dir, const char *element, const char *name,
     95                const char *mapset, const char *base)
     96{
    3897    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);
    49101    }
    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);
    63121    }
    64122
    65     if (pname && *pname) {
     123    if (dir && *dir) { /* misc element */
    66124        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        }
    68147    }
    69148
     
    72151    return path;
    73152}
    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 name
    87      * must split the name into name, mapset if it is
    88      * in the name@mapset format
    89      */
    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     else
    97         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  
    2020#include <grass/glocale.h>
    2121
     22static int make_mapset_element(const char *, const char *);
     23
    2224/*!
    2325   \brief Create element in the current mapset.
    2426
    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
    2934
    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
    3437 */
    3538int G_make_mapset_element(const char *p_element)
    3639{
    3740    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 */
     56int 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
     64int make_mapset_element(const char *p_path, const char *p_element)
     65{
     66    char path[GPATH_MAX], *p;
    3967    const char *element;
    4068
     
    4371        return 0;
    4472
    45     G_file_name(p = path, NULL, NULL, G_mapset());
     73    strncpy(path, p_path, GPATH_MAX);
     74    p = path;
    4675    while (*p)
    4776        p++;
     
    76105
    77106   \param dir directory path
    78    \param name element name
     107   \param name element to be created in mapset
    79108
    80    \return 0 ?
    81    \return ?
     109   \return 0 no element defined
     110   \return 1 on success
    82111 */
    83112int G__make_mapset_element_misc(const char *dir, const char *name)
  • grass/trunk/lib/gis/open.c

    r65195 r65348  
    44 * \brief GIS Library - Open file functions
    55 *
    6  * (C) 1999-2009 by the GRASS Development Team
     6 * (C) 1999-2015 by the GRASS Development Team
    77 *
    88 * This program is free software under the GNU General Public
     
    5454{
    5555    int fd;
     56    int is_tmp;
    5657    char path[GPATH_MAX];
    5758    char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
    58 
    59 
     59   
    6060    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);
    6168
    6269    /* READ */
     
    7279        }
    7380
    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       
    8188        if ((fd = open(path, 0)) < 0)
    8289            G_warning(_("G__open(read): Unable to open '%s': %s"),
     
    99106            return -1;
    100107
    101         G_file_name(path, element, name, mapset);
    102 
    103108        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);
    105113            close(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666));
    106114        }
  • grass/trunk/lib/gis/remove.c

    r63640 r65348  
    2222#include <grass/gis.h>
    2323
    24 static int recursive_remove(const char *path);
    2524static int G__remove(int misc, const char *dir, const char *element,
    2625                     const char *name);
     
    9695        return 0;
    9796
    98     if (recursive_remove(path) == 0)
     97    if (G_recursive_remove(path) == 0)
    9998        return 1;
    10099
     
    102101}
    103102
    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*/
     113int G_recursive_remove(const char *path)
    106114{
    107115    DIR *dirp;
     
    111119
    112120    if (G_lstat(path, &sb))
    113         return 1;
     121        return -1;
    114122    if (!S_ISDIR(sb.st_mode))
    115         return remove(path) == 0 ? 0 : 1;
     123        return remove(path) == 0 ? 0 : -1;
    116124
    117125    if ((dirp = opendir(path)) == NULL)
    118         return 1;
     126        return -1;
    119127    while ((dp = readdir(dirp)) != NULL) {
    120128        if (dp->d_name[0] == '.')
     
    123131            continue;
    124132        sprintf(path2, "%s/%s", path, dp->d_name);
    125         recursive_remove(path2);
     133        G_recursive_remove(path2);
    126134    }
    127135    closedir(dirp);
    128136
    129     return rmdir(path) == 0 ? 0 : 1;
     137    return rmdir(path) == 0 ? 0 : -1;
    130138}
  • grass/trunk/lib/gis/rename.c

    r63640 r65348  
    44 * \brief GIS Library - Rename file functions.
    55 *
    6  * (C) 2001-2009 by the GRASS Development Team
     6 * (C) 2001-2015 by the GRASS Development Team
    77 *
    88 * This program is free software under the GNU General Public License
     
    2020
    2121/*!
    22   \brief Rename a file in the filesystem.
     22  \brief Rename a file or a directory in the filesystem.
    2323 
    2424  The file or directory <i>oldname</i> is renamed to <i>newname</i>.
     
    3232int G_rename_file(const char *oldname, const char *newname)
    3333{
     34    int ret;
    3435
    3536#ifdef __MINGW32__
    3637    remove(newname);
    3738#endif
     39   
     40    ret = rename(oldname, newname);
    3841
    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;
    4051}
    4152
  • grass/trunk/lib/gis/tempfile.c

    r65195 r65348  
    44 * \brief GIS Library - Temporary file functions.
    55 *
    6  * (C) 2001-2009 by the GRASS Development Team
     6 * (C) 2001-2015 by the GRASS Development Team
    77 *
    88 * This program is free software under the GNU General Public License
     
    1515#include <unistd.h>
    1616#include <sys/stat.h>
     17#include <stdlib.h>
     18
    1719#include <grass/gis.h>
    1820
     
    8486        int uniq = G_counter_next(&unique);
    8587        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());
    8789    }
    8890    while (access(path, F_OK) == 0);
    8991
     92    G_debug(2, "G_tempfile_pid(): %s", path);
     93   
    9094    return G_store(path);
    9195}
     
    98102void G_temp_element(char *element)
    99103{
    100     const char *machine;
     104    const char *machine, *env;
    101105
    102106    strcpy(element, ".tmp");
     
    106110        strcat(element, machine);
    107111    }
    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 : "");
    109121}
  • grass/trunk/lib/init/clean_temp.c

    r63843 r65348  
    136136    /* Get the mapset temp directory */
    137137    G_temp_element(element);
    138     G_file_name(tmppath, element, "", mapset = G_mapset());
     138    G_file_name_tmp(tmppath, element, "", mapset = G_mapset());
    139139
    140140    /* get user id and current time in seconds */
  • grass/trunk/lib/init/grass.py

    r65347 r65348  
    986986            continue
    987987       
    988         if k in ('TMPDIR', 'TEMP', 'TMP'):
    989             continue # don't overwrite TMPDIR set by create_tmp()
    990        
    991988        debug("Environmental variable set {}={}".format(k, v))
    992989        os.environ[k] = v
     
    17011698    set_language(grass_config_dir)
    17021699
     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
    17031708    # Create the temporary directory and session grassrc file
    17041709    tmpdir = create_tmp(user, gis_lock)
     
    17111716    # Create the session grassrc file
    17121717    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 file
    1719     load_env(grass_env_file)
    17201718
    17211719    ensure_home()
  • grass/trunk/lib/init/variables.html

    r64948 r65348  
    277277  <dt>GRASS_VECTOR_TEMPORARY</dt>
    278278  <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>
    288300 
    289301  <dt>GRASS_VECTOR_TOPO_DEBUG</dt>
     
    335347    The default is set to the number of CPUs on the system.
    336348    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 
    338357  <dt>TMPDIR, TEMP, TMP</dt>
    339358  <dd>[Various GRASS GIS commands and wxGUI]<br>
  • grass/trunk/lib/raster/close.c

    r65323 r65348  
    376376        if (fcb->null_cur_row > 0) {
    377377            /* 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"),
    380380                          fcb->null_temp_name, path, strerror(errno));
    381381                stat = -1;
    382382            }
    383             /* if rename() was successful what is left to remove() ? */
     383            /* if rename() was successful what is left to remove() ? */
    384384            else {
    385385                remove(fcb->null_temp_name);
    386             }
     386            }
    387387        }
    388388        else {
     
    451451        G_file_name(path, CELL_DIR, fcb->name, fcb->mapset);
    452452        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"),
    455456                      fcb->temp_name, path, strerror(errno));
    456457            stat = -1;
  • grass/trunk/lib/vector/Vlib/build.c

    r64207 r65348  
    999999{
    10001000    struct Plus_head *plus;
    1001     char *path;
     1001    char path[GPATH_MAX];
    10021002    struct gvfile fp;
    10031003
     
    10081008    dig_file_init(&fp);
    10091009
    1010     path = Vect__get_path(Map);
     1010    Vect__get_path(path, Map);
    10111011    fp.file = G_fopen_new(path, GV_TOPO_ELEMENT);
    1012     G_free(path);
    10131012    if (fp.file == NULL) {
    10141013        G_warning(_("Unable to create topo file for vector map <%s>"), Map->name);
     
    12481247{
    12491248    struct Plus_head *plus;
    1250     char *file_path;
     1249    char file_path[GPATH_MAX];
    12511250
    12521251    G_debug(1, "Vect_save_spatial_index()");
     
    12621261    if (plus->Spidx_new == TRUE) {
    12631262        /*  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);
    12651264        G_debug(1, "Open sidx: %s", file_path);
    12661265        dig_file_init(&(plus->spidx_fp));
    12671266        plus->spidx_fp.file = fopen(file_path, "w+");
    1268         G_free(file_path);
    12691267        if (plus->spidx_fp.file == NULL) {
    12701268            G_warning(_("Unable to create spatial index file for vector map <%s>"),
  • grass/trunk/lib/vector/Vlib/build_ogr.c

    r51082 r65348  
    2727#include <ogr_api.h>
    2828#endif
     29
     30#include "local_proto.h"
    2931
    3032/*!
     
    125127
    126128    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);
    128130    G_debug(4, "Open fidx: %s", fname);
    129131    dig_file_init(&fp);
  • grass/trunk/lib/vector/Vlib/cindex.c

    r58403 r65348  
    473473{
    474474    struct Plus_head *plus;
    475     char *path;
     475    char path[GPATH_MAX];
    476476    struct gvfile fp;
    477477
     
    483483    dig_file_init(&fp);
    484484   
    485     path = Vect__get_path(Map);
     485    Vect__get_path(path, Map);
    486486    fp.file = G_fopen_new(path, GV_CIDX_ELEMENT);
    487     G_free(path);
    488487    if (fp.file == NULL) {
    489488        G_warning(_("Unable to create category index file for vector map <%s>"),
     
    518517{
    519518    int ret;
    520     char file_path[GPATH_MAX], *path;
     519    char file_path[GPATH_MAX], path[GPATH_MAX];
    521520    struct gvfile fp;
    522521    struct Plus_head *Plus;
     
    527526    Plus = &(Map->plus);
    528527
    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   
    532531    if (access(file_path, F_OK) != 0) { /* does not exist */
    533         G_free(path);
    534532        return 1;
    535533    }
     
    537535    dig_file_init(&fp);
    538536    fp.file = G_fopen_old(path, GV_CIDX_ELEMENT, Map->mapset);
    539     G_free(path);
    540537   
    541538    if (fp.file == NULL) {      /* category index file is not available */
  • grass/trunk/lib/vector/Vlib/close.c

    r58354 r65348  
    324324void unlink_file(const struct Map_info *Map, const char *name)
    325325{
    326     char *path;
     326    char path[GPATH_MAX];
    327327
    328328    /* delete old support files if available */
    329     path = Vect__get_element_path(Map, name);
     329    Vect__get_element_path(path, Map, name);
    330330    if (access(path, F_OK) == 0) { /* file exists? */
    331331        G_debug(2, "\t%s: unlink", path);
    332332        unlink(path);
    333333    }
    334 
    335     G_free(path);
    336 }
     334}
  • grass/trunk/lib/vector/Vlib/close_nat.c

    r57444 r65348  
    66   Higher level functions for reading/writing/manipulating vectors.
    77
    8    (C) 2001-2009, 2013 by the GRASS Development Team
     8   (C) 2001-2015 by the GRASS Development Team
    99
    1010   This program is free software under the GNU General Public License
     
    1616
    1717#include <stdlib.h>
     18#include <unistd.h>
     19#include <errno.h>
    1820
    1921#include <grass/vector.h>
     22#include <grass/glocale.h>
    2023
    2124#include "local_proto.h"
     
    5356    /* delete temporary map ? */
    5457    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            }
    58127        }
    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
    61167        }
    62168    }
  • grass/trunk/lib/vector/Vlib/close_pg.c

    r60561 r65348  
    128128        /* delete old support files if available */
    129129        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);
    132131        if (access(file_path, F_OK) == 0) /* file exists? */
    133132            unlink(file_path);
  • grass/trunk/lib/vector/Vlib/field.c

    r55762 r65348  
    386386
    387387    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
    388403    fi->database = G_store(connection.databaseName);
    389404    fi->driver = G_store(connection.driverName);
    390 
     405#endif
     406   
    391407    return fi;
    392408}
     
    561577    char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
    562578    int fld;
    563     char *c, *path;
     579    char *c, path[GPATH_MAX];
    564580    int row, rule;
    565581    struct dblinks *dbl;
     
    570586
    571587    /* Read dblink for native format */
    572     path = Vect__get_path(Map);
     588    Vect__get_path(path, Map);
    573589    fd = G_fopen_old(path, GV_DBLN_ELEMENT, Map->mapset);
    574     G_free(path);
    575590    if (fd == NULL) {           /* This may be correct, no tables defined */
    576591        G_debug(1, "Cannot open vector database definition file");
     
    893908    int i;
    894909    FILE *fd;
    895     char *path, buf[1024];
     910    char path[GPATH_MAX], buf[1024];
    896911    struct dblinks *dbl;
    897912
     
    905920    dbl = Map->dblnk;
    906921
    907     path = Vect__get_path(Map);
     922    Vect__get_path(path, Map);
    908923    fd = G_fopen_new(path, GV_DBLN_ELEMENT);
    909     G_free(path);
    910924    if (fd == NULL) {           /* This may be correct, no tables defined */
    911925        G_warning(_("Unable to create database definition file for vector map <%s>"),
  • grass/trunk/lib/vector/Vlib/header.c

    r63834 r65348  
    7878int Vect__write_head(const struct Map_info *Map)
    7979{
    80     char *path;
     80    char path[GPATH_MAX];
    8181    FILE *head_fp;
    8282
    83     path = Vect__get_path(Map);
     83    Vect__get_path(path, Map);
    8484    head_fp = G_fopen_new(path, GV_HEAD_ELEMENT);
    85     G_free(path);
    8685    if (head_fp == NULL) {
    8786        G_warning(_("Unable to create header file for vector map <%s>"),
     
    119118    FILE *head_fp;
    120119    char buff[2000];
    121     char *path, *ptr;
     120    char path[GPATH_MAX], *ptr;
    122121
    123122    /* Reset / init */
     
    125124   
    126125    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);
    128127    head_fp = G_fopen_old(path, GV_HEAD_ELEMENT, Map->mapset);
    129     G_free(path);
    130128    if (head_fp == NULL) {
    131129        G_warning(_("Unable to open header file of vector <%s>"),
  • grass/trunk/lib/vector/Vlib/local_proto.h

    r58363 r65348  
    77#define CACHE_FEATURE 0
    88#define CACHE_MAP     1
     9
     10/*! Attributes of temporary maps */
     11/* #define TEMPORARY_MAP_DB */
    912
    1013/* Internal vector library subroutines which are not part of public
     
    2831int Vect__open_old(struct Map_info *, const char *, const char *,
    2932                   const char *, int, int, int);
    30 char *Vect__get_path(const struct Map_info *);
    31 char *Vect__get_element_path(const struct Map_info *, const char *);
     33char *Vect__get_path(char *, const struct Map_info *);
     34char *Vect__get_element_path(char *, const struct Map_info *, const char *);
    3235
    3336/* write_nat.c */
  • grass/trunk/lib/vector/Vlib/map.c

    r64278 r65348  
    2323#include <sys/stat.h>
    2424#include <fcntl.h>
     25#include <errno.h>
    2526
    2627#include <grass/glocale.h>
     
    348349{
    349350    int ret;
    350     char *path, path_buf[GPATH_MAX];
     351    char path[GPATH_MAX], path_buf[GPATH_MAX];
    351352    char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
    352     const char *tmp, *mapset;
     353    const char *tmp, *mapset, *env;
    353354   
    354355    struct Map_info Map;
     
    385386    }
    386387       
    387     path = Vect__get_element_path(&Map, GV_DBLN_ELEMENT);
     388    Vect__get_element_path(path, &Map, GV_DBLN_ELEMENT);
    388389    G_debug(1, "dbln file: %s", path);
    389390
     
    440441        }
    441442    }
    442     G_free(path);
    443443   
    444444    /* Delete all files from vector/name directory */
    445     path = Vect__get_element_path(&Map, NULL);
     445    Vect__get_element_path(path, &Map, NULL);
    446446    Vect_close(&Map);
    447447    G_debug(3, "opendir '%s'", path);
     
    469469    closedir(dir);
    470470   
    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    }
    482488
    483489    G_debug(3, "remove directory '%s'", tmp);
     
    485491    ret = rmdir(tmp);
    486492    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));
    488495        return -1;
    489496    }
  • grass/trunk/lib/vector/Vlib/open.c

    r64320 r65348  
    164164{
    165165    char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
    166     char *path;
     166    char path[GPATH_MAX];
    167167    FILE *fp;
    168168    int level, level_request;
     
    175175            is_tmp);
    176176   
    177     if (!is_tmp) {
     177    if (update && !is_tmp) {
    178178        is_tmp = getenv("GRASS_VECTOR_TEMPORARY") ? TRUE : FALSE;
    179179        G_debug(1, "Vect__open_old(): is_tmp = %d (check GRASS_VECTOR_TEMPORARY)", is_tmp);
     
    222222    }
    223223
    224     path = Vect__get_path(Map);
     224    Vect__get_path(path, Map);
    225225
    226226    if (!ogr_mapset) {
     
    254254                }
    255255            }
    256             G_file_name(file_path, path, GV_HEAD_ELEMENT, Map->mapset);
     256
     257            Vect__get_element_path(file_path, Map, GV_HEAD_ELEMENT);
    257258            if (access(file_path, F_OK) != 0)
    258259                return -1;
     
    522523        char file_path[GPATH_MAX];
    523524
    524         G_file_name(file_path, path, GV_TOPO_ELEMENT, G_mapset());
     525        Vect__get_element_path(file_path, Map, GV_TOPO_ELEMENT);
    525526        if (access(file_path, F_OK) == 0)       /* topo file exists? */
    526527            unlink(file_path);
    527528
    528         G_file_name(file_path, path, GV_SIDX_ELEMENT, G_mapset());
     529        Vect__get_element_path(file_path, Map, GV_SIDX_ELEMENT);
    529530        if (access(file_path, F_OK) == 0)       /* sidx file exists? */
    530531            unlink(file_path);
    531532
    532         G_file_name(file_path, path, GV_CIDX_ELEMENT, G_mapset());
     533        Vect__get_element_path(file_path, Map, GV_CIDX_ELEMENT);
    533534        if (access(file_path, F_OK) == 0)       /* cidx file exists? */
    534535            unlink(file_path);
    535536
    536537        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);
    538539            if (access(file_path, F_OK) == 0)   /* fidx file exists? */
    539540                unlink(file_path);
    540541        }
    541542    }
    542 
    543     G_free(path);
    544543   
    545544    return level;
     
    792791    if (Map->format != GV_FORMAT_OGR_DIRECT &&
    793792        getenv("GRASS_VECTOR_PGFILE") == NULL) { /* GRASS_VECTOR_PGFILE defined by v.out.postgis */
    794         char *path;
     793        char *env;
     794        char path[GPATH_MAX];
    795795       
    796796        G_debug(2, " using non-direct format");
     
    805805            }
    806806        }
    807         else {
     807
     808        env = getenv("GRASS_VECTOR_TEMPORARY");
     809        if (!Map->temporary || (env && strcmp(env, "move") == 0)) {
    808810            if (G_find_vector2(name, G_mapset()) != NULL) {
    809811                G_warning(_("Vector map <%s> already exists and will be overwritten"),
     
    829831
    830832        /* create history file */
    831         path = Vect__get_path(Map);
     833        Vect__get_path(path, Map);
    832834        Map->hist_fp = G_fopen_new(path, GV_HIST_ELEMENT);
    833         G_free(path);
    834835        if (Map->hist_fp == NULL) {
    835836            G_warning(_("Unable to open history file of vector map <%s>"),
     
    964965int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
    965966{
    966     char *path, file_path[GPATH_MAX];
     967    char file_path[GPATH_MAX];
    967968    struct stat stat_buf;
    968969   
    969970    switch (Map->format) {
    970971    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);
    974973        G_debug(1, "get coor info: %s", file_path);
    975974        if (0 != stat(file_path, &stat_buf)) {
     
    10881087{
    10891088    int err, ret;
    1090     char file_path[GPATH_MAX], *path;
     1089    char file_path[GPATH_MAX], path[GPATH_MAX];
    10911090    struct gvfile fp;
    10921091    struct Coor_info CInfo;
     
    10981097    Plus = &(Map->plus);
    10991098
    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);
    11031101    if (access(file_path, F_OK) != 0) {  /* does not exist */
    1104         G_free(path);
    11051102        return 1;
    11061103    }
     
    11081105    dig_file_init(&fp);
    11091106    fp.file = G_fopen_old(path, GV_TOPO_ELEMENT, Map->mapset);
    1110     G_free(path);
    11111107
    11121108    if (fp.file == NULL) {      /* topo file is not available */
     
    11871183
    11881184    if (mode < 2) {
    1189         char *path, file_path[GPATH_MAX];
     1185        char path[GPATH_MAX], file_path[GPATH_MAX];
    11901186       
    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);
    11941189        if (access(file_path, F_OK) != 0)       /* does not exist */
    11951190            return 1;
    11961191
    11971192        Plus->spidx_fp.file = G_fopen_old(path, GV_SIDX_ELEMENT, Map->mapset);
    1198         G_free(path);
    11991193       
    12001194        if (Plus->spidx_fp.file == NULL) {  /* sidx file is not available */
     
    14361430  \brief Get map directory name (internal use only)
    14371431
    1438   Allocate string should be freed by G_free().
    1439 
     1432  \param file_path path string buffer
    14401433  \param Map pointer to Map_info struct
    14411434
    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*/
     1437char *Vect__get_path(char *path, const struct Map_info *Map)
     1438{
    14481439    if (Map->temporary) {
    14491440        char path_tmp[GPATH_MAX];
     
    14551446    }
    14561447   
    1457     return G_store(path);
     1448    return path;
    14581449}
    14591450
     
    14681459  \return allocated buffer containing path
    14691460*/
    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 }
     1461char *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  
    3939int V1_open_old_nat(struct Map_info *Map, int update)
    4040{
    41     char *path;
     41    char path[GPATH_MAX];
    4242    struct Coor_info CInfo;
    4343
     
    4545            Map->mapset);
    4646
    47     path = Vect__get_path(Map);
     47    Vect__get_path(path, Map);
    4848    dig_file_init(&(Map->dig_fp));
    4949    if (update)
     
    5252        Map->dig_fp.file =
    5353            G_fopen_old(path, GV_COOR_ELEMENT, Map->mapset);
    54     G_free(path);
    5554   
    5655    if (Map->dig_fp.file == NULL) {
     
    9695int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
    9796{
    98     char *path, file_path[GPATH_MAX];
     97    char path[GPATH_MAX];
    9998
    10099    G_debug(1, "V1_open_new_nat(): name = %s with_z = %d is_tmp = %d",
    101100            name, with_z, Map->temporary);
    102 
    103     path = Vect__get_path(Map);
    104101
    105102    /* Set the 'coor' file version */
     
    108105    Map->head.coor_version.back_major = GV_COOR_EARLIEST_MAJOR;
    109106    Map->head.coor_version.back_minor = GV_COOR_EARLIEST_MINOR;
    110 
     107   
     108    Vect__get_path(path, Map);
     109   
    111110    /* TODO: open better */
    112111    dig_file_init(&(Map->dig_fp));
     
    120119    if (Map->dig_fp.file == NULL)
    121120        return -1;
    122 
     121   
    123122    /* if overwrite OK, any existing files have already been deleted by
    124123     * Vect_open_new(): remove this check ? */
    125124    /* 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 */
    130128   
    131129    /* set conversion matrices */
Note: See TracChangeset for help on using the changeset viewer.