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