Loading...
Searching...
No Matches
tempfile.c
Go to the documentation of this file.
/*!
* \file lib/gis/tempfile.c
*
* \brief GIS Library - Temporary file functions.
*
* (C) 2001-2015 by the GRASS Development Team
*
* This program is free software under the GNU General Public License
* (>=v2). Read the file COPYING that comes with GRASS for details.
*
* \author Original author CERL
*/
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <grass/gis.h>
#include "gis_local_proto.h"
static struct Counter unique;
static int initialized;
/*!
\brief Initialize environment for creating tempfiles.
*/
void G_init_tempfile(void)
{
if (G_is_initialized(&initialized))
return;
G_init_counter(&unique, 0);
G_initialize_done(&initialized);
}
/*!
* \brief Returns a temporary file name.
*
* This routine returns a pointer to a string containing a unique
* temporary file name that can be used as a temporary file within the
* module. Successive calls to G_tempfile() will generate new
* names. Only the file name is generated. The file itself is not
* created. To create the file, the module must use standard UNIX
* functions which create and open files, e.g., <i>creat()</i> or
* <i>fopen()</i>.
*
* Successive calls will generate different names the names are of the
* form pid.n where pid is the programs process id number and n is a
* unique identifier.
*
* <b>Note:</b> It is recommended to <i>unlink()</i> (remove) the
* temp file on exit/error. Only if GRASS is left with 'exit', the GIS
* mapset management will clean up the temp directory (ETC/clean_temp).
*
* \return pointer to a character string containing the name. The name
* is copied to allocated memory and may be released by the unix free()
* routine.
*/
char *G_tempfile(void)
{
return G_tempfile_pid(getpid());
}
/*!
* \brief Returns a temporary file name.
*
* Similar to G_tempfile(), but the temporary file name will include
* a provided base directory instead of the path to the current mapset.
*
* \return pointer to a character string containing the name. The name
* is copied to allocated memory and may be released by the unix free()
* routine.
*/
char *G_tempfile_basedir(const char *basedir)
{
return G_tempfile_pid_basedir(getpid(), basedir);
}
/*!
* \brief Create tempfile from process id.
*
* See G_tempfile().
*
* \param pid
* \return pointer to string path
*/
char *G_tempfile_pid(int pid)
{
char path[GPATH_MAX];
char name[GNAME_MAX];
char element[100];
if (pid <= 0)
pid = getpid();
G_temp_element(element);
G_init_tempfile();
do {
int uniq = G_counter_next(&unique);
snprintf(name, sizeof(name), "%d.%d", pid, uniq);
G_file_name(path, element, name, G_mapset());
} while (access(path, F_OK) == 0);
G_debug(2, "G_tempfile_pid(): %s", path);
return G_store(path);
}
/*!
* \brief Create tempfile from process id in given base directory.
*
* See G_tempfile_basedir().
*
* \param pid
* \param basedir
* \return pointer to string path
*/
char *G_tempfile_pid_basedir(int pid, const char *basedir)
{
char path[GPATH_MAX];
char name[GNAME_MAX];
char element[100];
if (pid <= 0)
pid = getpid();
G__temp_element_basedir(element, basedir);
G_init_tempfile();
do {
int uniq = G_counter_next(&unique);
snprintf(name, sizeof(name), "%d.%d", pid, uniq);
G_file_name_basedir(path, element, name, G_mapset(), basedir);
} while (access(path, F_OK) == 0);
G_debug(2, "G_tempfile_pid(): %s", path);
return G_store(path);
}
/*!
* \brief Populates element with a path string.
*
* \param[out] element element name
*/
void G_temp_element(char *element)
{
G__temp_element(element, FALSE);
}
/*!
* \brief Populates element with a path string (internal use only!)
*
* \param[out] element element name
* \param tmp TRUE to use G_make_mapset_element_tmp() instead of
* G_make_mapset_element()
*/
void G__temp_element(char *element, int tmp)
{
const char *machine;
strcpy(element, ".tmp");
machine = G__machine_name();
if (machine != NULL && *machine != 0) {
strcat(element, "/");
strcat(element, machine);
}
if (!tmp)
G_make_mapset_object_group(element);
else
G_make_mapset_object_group_tmp(element);
G_debug(2, "G__temp_element(): %s (tmp=%d)", element, tmp);
}
/*!
* \brief Populates element with a path string (internal use only!)
*
* \param[out] element element name
* \param basedir
*/
void G__temp_element_basedir(char *element, const char *basedir)
{
const char *machine;
strcpy(element, ".tmp");
machine = G__machine_name();
if (machine != NULL && *machine != 0) {
strcat(element, "/");
strcat(element, machine);
}
if (basedir && *basedir)
G_make_mapset_object_group_basedir(element, basedir);
else
G_make_mapset_object_group(element);
G_debug(2, "G__temp_element_basedir(): %s", element);
}
NULL
#define NULL
Definition ccmath.h:32
G_initialize_done
void G_initialize_done(int *p)
Definition counter.c:77
G_init_counter
void G_init_counter(struct Counter *c, int v)
Definition counter.c:38
G_is_initialized
int G_is_initialized(int *p)
Definition counter.c:60
G_counter_next
int G_counter_next(struct Counter *c)
Definition counter.c:46
FALSE
#define FALSE
Definition dbfopen.c:55
G_debug
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition debug.c:66
G_file_name
char * G_file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition file_name.c:61
G_file_name_basedir
char * G_file_name_basedir(char *path, const char *element, const char *name, const char *mapset, const char *basedir)
Builds full path names to GIS data files in temporary directory (for internal use only).
Definition file_name.c:155
G__machine_name
const char * G__machine_name(void)
Definition mach_name.c:16
G_mapset
const char * G_mapset(void)
Get current mapset name.
Definition mapset.c:33
G_make_mapset_object_group_tmp
int G_make_mapset_object_group_tmp(const char *type)
Create directory for type of objects in the temporary directory.
Definition mapset_msc.c:154
G_make_mapset_object_group
int G_make_mapset_object_group(const char *type)
Create directory for group of elements of a given type.
Definition mapset_msc.c:75
G_make_mapset_object_group_basedir
int G_make_mapset_object_group_basedir(const char *type, const char *basedir)
Create directory for type of objects in the temporary directory.
Definition mapset_msc.c:176
name
const char * name
Definition named_colr.c:6
strcpy
#define strcpy
Definition parson.c:66
G_store
char * G_store(const char *s)
Copy string to allocated memory.
Definition strings.c:87
element
Definition lidar.h:85
path
Definition path.h:15
G_temp_element
void G_temp_element(char *element)
Populates element with a path string.
Definition tempfile.c:148
G_tempfile_pid
char * G_tempfile_pid(int pid)
Create tempfile from process id.
Definition tempfile.c:90
G__temp_element
void G__temp_element(char *element, int tmp)
Populates element with a path string (internal use only!).
Definition tempfile.c:160
G_tempfile
char * G_tempfile(void)
Returns a temporary file name.
Definition tempfile.c:62
G__temp_element_basedir
void G__temp_element_basedir(char *element, const char *basedir)
Populates element with a path string (internal use only!).
Definition tempfile.c:185
G_tempfile_pid_basedir
char * G_tempfile_pid_basedir(int pid, const char *basedir)
Create tempfile from process id in given base directory.
Definition tempfile.c:121
G_init_tempfile
void G_init_tempfile(void)
Initialize environment for creating tempfiles.
Definition tempfile.c:29
G_tempfile_basedir
char * G_tempfile_basedir(const char *basedir)
Returns a temporary file name.
Definition tempfile.c:77