GRASS GIS 8 Programmer's Manual 8.4.1(2025)-45ca3179ab
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{
79 return G_tempfile_pid_basedir(getpid(), basedir);
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 sprintf(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 * \return pointer to string path
119 */
120char *G_tempfile_pid_basedir(int pid, const char *basedir)
121{
122 char path[GPATH_MAX];
123 char name[GNAME_MAX];
124 char element[100];
125
126 if (pid <= 0)
127 pid = getpid();
130 do {
131 int uniq = G_counter_next(&unique);
132
133 sprintf(name, "%d.%d", pid, uniq);
135 } while (access(path, F_OK) == 0);
136
137 G_debug(2, "G_tempfile_pid(): %s", path);
138
139 return G_store(path);
140}
141
142/*!
143 * \brief Populates element with a path string.
144 *
145 * \param[out] element element name
146 */
151
152/*!
153 * \brief Populates element with a path string (internal use only!)
154 *
155 * \param[out] element element name
156 * \param tmp TRUE to use G_make_mapset_element_tmp() instead of
157 * G_make_mapset_element()
158 */
159void G__temp_element(char *element, int tmp)
160{
161 const char *machine;
162
163 strcpy(element, ".tmp");
164 machine = G__machine_name();
165 if (machine != NULL && *machine != 0) {
166 strcat(element, "/");
167 strcat(element, machine);
168 }
169
170 if (!tmp)
172 else
174
175 G_debug(2, "G__temp_element(): %s (tmp=%d)", element, tmp);
176}
177
178/*!
179 * \brief Populates element with a path string (internal use only!)
180 *
181 * \param[out] element element name
182 * \param tmp TRUE to use G_make_mapset_element_tmp() instead of
183 * G_make_mapset_element()
184 */
185void G__temp_element_basedir(char *element, const char *basedir)
186{
187 const char *machine;
188
189 strcpy(element, ".tmp");
190 machine = G__machine_name();
191 if (machine != NULL && *machine != 0) {
192 strcat(element, "/");
193 strcat(element, machine);
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
void G_initialize_done(int *p)
Definition counter.c:77
void G_init_counter(struct Counter *c, int v)
Definition counter.c:38
int G_is_initialized(int *p)
Definition counter.c:60
int G_counter_next(struct Counter *c)
Definition counter.c:46
#define FALSE
Definition dbfopen.c:74
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition debug.c:66
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
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:154
const char * G__machine_name(void)
Definition mach_name.c:17
const char * G_mapset(void)
Get current mapset name.
Definition mapset.c:33
int G_make_mapset_object_group_tmp(const char *type)
Create directory for type of objects in the temporary directory.
Definition mapset_msc.c:153
int G_make_mapset_object_group(const char *type)
Create directory for group of elements of a given type.
Definition mapset_msc.c:74
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:175
const char * name
Definition named_colr.c:6
#define strcpy
Definition parson.c:62
char * G_store(const char *s)
Copy string to allocated memory.
Definition strings.c:87
Definition path.h:15
void G_temp_element(char *element)
Populates element with a path string.
Definition tempfile.c:147
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:159
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:120
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