GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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 
23 static struct Counter unique;
24 static int initialized;
25 
26 /*!
27  \brief Initialize environment for creating tempfiles.
28  */
29 void G_init_tempfile(void)
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  */
62 char *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  */
77 char *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  */
90 char *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  */
120 char *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();
129  G_init_tempfile();
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  */
148 {
150 }
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  */
159 void 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  */
185 void 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
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition: file_name.c:61
int G_make_mapset_object_group(const char *)
Create directory for group of elements of a given type.
Definition: mapset_msc.c:74
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
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:154
int G_make_mapset_object_group_tmp(const char *)
Create directory for type of objects in the temporary directory.
Definition: mapset_msc.c:153
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:175
int G_is_initialized(int *)
Definition: counter.c:60
void G_initialize_done(int *)
Definition: counter.c:77
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
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
#define GPATH_MAX
Definition: gis.h:194
#define FALSE
Definition: gis.h:83
#define GNAME_MAX
Definition: gis.h:191
const char * G__machine_name(void)
Definition: mach_name.c:17
const char * name
Definition: named_colr.c:6
#define strcpy
Definition: parson.c:62
Definition: gis.h:619
Definition: lidar.h:85
Definition: path.h:15
void G_temp_element(char *element)
Populates element with a path string.
Definition: tempfile.c:147
char * G_tempfile_pid_basedir(int pid, const char *basedir)
Create tempfile from process id in given base directory.
Definition: tempfile.c:120
char * G_tempfile_basedir(const char *basedir)
Returns a temporary file name.
Definition: tempfile.c:77
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
void G_init_tempfile(void)
Initialize environment for creating tempfiles.
Definition: tempfile.c:29
char * G_tempfile_pid(int pid)
Create tempfile from process id.
Definition: tempfile.c:90