GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
tempfile.c
Go to the documentation of this file.
1/*!
2 * \file lib/gis/tempfile.c
3 *
4 * \brief GIS Library - Temporary file functions.
5 *
6 * (C) 2001-2015 by the GRASS Development Team
7 *
8 * This program is free software under the GNU General Public License
9 * (>=v2). Read the file COPYING that comes with GRASS for details.
10 *
11 * \author Original author CERL
12 */
13
14#include <string.h>
15#include <unistd.h>
16#include <sys/stat.h>
17#include <stdlib.h>
18
19#include <grass/gis.h>
20
21#include "gis_local_proto.h"
22
23static struct Counter unique;
24static int initialized;
25
26/*!
27 \brief Initialize environment for creating tempfiles.
28 */
30{
31 if (G_is_initialized(&initialized))
32 return;
33
34 G_init_counter(&unique, 0);
35
36 G_initialize_done(&initialized);
37}
38
39/*!
40 * \brief Returns a temporary file name.
41 *
42 * This routine returns a pointer to a string containing a unique
43 * temporary file name that can be used as a temporary file within the
44 * module. Successive calls to G_tempfile() will generate new
45 * names. Only the file name is generated. The file itself is not
46 * created. To create the file, the module must use standard UNIX
47 * functions which create and open files, e.g., <i>creat()</i> or
48 * <i>fopen()</i>.
49 *
50 * Successive calls will generate different names the names are of the
51 * form pid.n where pid is the programs process id number and n is a
52 * unique identifier.
53 *
54 * <b>Note:</b> It is recommended to <i>unlink()</i> (remove) the
55 * temp file on exit/error. Only if GRASS is left with 'exit', the GIS
56 * mapset management will clean up the temp directory (ETC/clean_temp).
57 *
58 * \return pointer to a character string containing the name. The name
59 * is copied to allocated memory and may be released by the unix free()
60 * routine.
61 */
62char *G_tempfile(void)
63{
64 return G_tempfile_pid(getpid());
65}
66
67/*!
68 * \brief Returns a temporary file name.
69 *
70 * Similar to G_tempfile(), but the temporary file name will include
71 * a provided base directory instead of the path to the current mapset.
72 *
73 * \return pointer to a character string containing the name. The name
74 * is copied to allocated memory and may be released by the unix free()
75 * routine.
76 */
77char *G_tempfile_basedir(const char *basedir)
78{
80}
81
82/*!
83 * \brief Create tempfile from process id.
84 *
85 * See G_tempfile().
86 *
87 * \param pid
88 * \return pointer to string path
89 */
90char *G_tempfile_pid(int pid)
91{
92 char path[GPATH_MAX];
93 char name[GNAME_MAX];
94 char element[100];
95
96 if (pid <= 0)
97 pid = getpid();
100 do {
101 int uniq = G_counter_next(&unique);
102
103 snprintf(name, sizeof(name), "%d.%d", pid, uniq);
105 } while (access(path, F_OK) == 0);
106
107 G_debug(2, "G_tempfile_pid(): %s", path);
108
109 return G_store(path);
110}
111
112/*!
113 * \brief Create tempfile from process id in given base directory.
114 *
115 * See G_tempfile_basedir().
116 *
117 * \param pid
118 * \param basedir
119 * \return pointer to string path
120 */
121char *G_tempfile_pid_basedir(int pid, const char *basedir)
122{
123 char path[GPATH_MAX];
124 char name[GNAME_MAX];
125 char element[100];
126
127 if (pid <= 0)
128 pid = getpid();
131 do {
132 int uniq = G_counter_next(&unique);
133
134 snprintf(name, sizeof(name), "%d.%d", pid, uniq);
136 } while (access(path, F_OK) == 0);
137
138 G_debug(2, "G_tempfile_pid(): %s", path);
139
140 return G_store(path);
141}
142
143/*!
144 * \brief Populates element with a path string.
145 *
146 * \param[out] element element name
147 */
152
153/*!
154 * \brief Populates element with a path string (internal use only!)
155 *
156 * \param[out] element element name
157 * \param tmp TRUE to use G_make_mapset_element_tmp() instead of
158 * G_make_mapset_element()
159 */
160void G__temp_element(char *element, int tmp)
161{
162 const char *machine;
163
164 strcpy(element, ".tmp");
166 if (machine != NULL && *machine != 0) {
167 strcat(element, "/");
169 }
170
171 if (!tmp)
173 else
175
176 G_debug(2, "G__temp_element(): %s (tmp=%d)", element, tmp);
177}
178
179/*!
180 * \brief Populates element with a path string (internal use only!)
181 *
182 * \param[out] element element name
183 * \param basedir
184 */
186{
187 const char *machine;
188
189 strcpy(element, ".tmp");
191 if (machine != NULL && *machine != 0) {
192 strcat(element, "/");
194 }
195
196 if (basedir && *basedir)
198 else
200
201 G_debug(2, "G__temp_element_basedir(): %s", element);
202}
#define NULL
Definition ccmath.h:32
int G_make_mapset_object_group(const char *)
Create directory for group of elements of a given type.
Definition mapset_msc.c:75
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition file_name.c:61
char * G_file_name_basedir(char *, const char *, const char *, const char *, const char *)
Builds full path names to GIS data files in temporary directory (for internal use only)
Definition file_name.c:155
int G_make_mapset_object_group_tmp(const char *)
Create directory for type of objects in the temporary directory.
Definition mapset_msc.c:154
int G_make_mapset_object_group_basedir(const char *, const char *)
Create directory for type of objects in the temporary directory.
Definition mapset_msc.c:176
int G_is_initialized(int *)
Definition counter.c:60
void G_initialize_done(int *)
Definition counter.c:77
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:87
int G_counter_next(struct Counter *)
Definition counter.c:46
void G_init_counter(struct Counter *, int)
Definition counter.c:38
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:33
#define GPATH_MAX
Definition gis.h:199
#define FALSE
Definition gis.h:82
#define GNAME_MAX
Definition gis.h:196
const char * G__machine_name(void)
Definition mach_name.c:16
const char * name
Definition named_colr.c:6
#define strcpy
Definition parson.c:66
Definition gis.h:625
Definition path.h:15
void G_temp_element(char *element)
Populates element with a path string.
Definition tempfile.c:148
char * G_tempfile_pid(int pid)
Create tempfile from process id.
Definition tempfile.c:90
void G__temp_element(char *element, int tmp)
Populates element with a path string (internal use only!)
Definition tempfile.c:160
char * G_tempfile(void)
Returns a temporary file name.
Definition tempfile.c:62
void G__temp_element_basedir(char *element, const char *basedir)
Populates element with a path string (internal use only!)
Definition tempfile.c:185
char * G_tempfile_pid_basedir(int pid, const char *basedir)
Create tempfile from process id in given base directory.
Definition tempfile.c:121
void G_init_tempfile(void)
Initialize environment for creating tempfiles.
Definition tempfile.c:29
char * G_tempfile_basedir(const char *basedir)
Returns a temporary file name.
Definition tempfile.c:77
#define access
Definition unistd.h:7
#define getpid
Definition unistd.h:20
#define F_OK
Definition unistd.h:22