GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 Create tempfile from process id.
69  *
70  * See G_tempfile().
71  *
72  * \param pid
73  * \return pointer to string path
74  */
75 char *G_tempfile_pid(int pid)
76 {
77  char path[GPATH_MAX];
78  char name[GNAME_MAX];
79  char element[100];
80 
81  if (pid <= 0)
82  pid = getpid();
83  G_temp_element(element);
85  do {
86  int uniq = G_counter_next(&unique);
87  sprintf(name, "%d.%d", pid, uniq);
88  G_file_name(path, element, name, G_mapset());
89  }
90  while (access(path, F_OK) == 0);
91 
92  G_debug(2, "G_tempfile_pid(): %s", path);
93 
94  return G_store(path);
95 }
96 
97 /*!
98  * \brief Populates element with a path string.
99  *
100  * \param[out] element element name
101  */
103 {
104  G__temp_element(element, FALSE);
105 }
106 
107 /*!
108  * \brief Populates element with a path string (internal use only!)
109  *
110  * \param[out] element element name
111  * \param tmp TRUE to use G_make_mapset_element_tmp() instead of G_make_mapset_element()
112  */
113 void G__temp_element(char *element, int tmp)
114 {
115  const char *machine;
116 
117  strcpy(element, ".tmp");
118  machine = G__machine_name();
119  if (machine != NULL && *machine != 0) {
120  strcat(element, "/");
121  strcat(element, machine);
122  }
123 
124  if (!tmp)
126  else
128 
129  G_debug(2, "G__temp_element(): %s (tmp=%d)", element, tmp);
130 }
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_tempfile_pid(int pid)
Create tempfile from process id.
Definition: tempfile.c:75
void G_temp_element(char *element)
Populates element with a path string.
Definition: tempfile.c:102
Definition: gis.h:604
int G_make_mapset_object_group_tmp(const char *)
Create directory for type of objects in the temporary directory.
Definition: mapset_msc.c:153
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:62
int G_counter_next(struct Counter *)
Definition: counter.c:46
#define NULL
Definition: ccmath.h:32
void G_initialize_done(int *)
Definition: counter.c:76
Definition: lidar.h:86
void G__temp_element(char *element, int tmp)
Populates element with a path string (internal use only!)
Definition: tempfile.c:113
const char * G__machine_name(void)
Definition: mach_name.c:17
#define FALSE
Definition: gis.h:63
void G_init_counter(struct Counter *, int)
Definition: counter.c:38
#define GPATH_MAX
Definition: gis.h:180
void G_init_tempfile(void)
Initialize environment for creating tempfiles.
Definition: tempfile.c:29
int G_is_initialized(int *)
Definition: counter.c:59
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
#define GNAME_MAX
Definition: gis.h:177
Definition: path.h:16
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
const char * name
Definition: named_colr.c:7
int G_debug(int, const char *,...) __attribute__((format(printf
int G_make_mapset_object_group(const char *)
Create directory for group of elements of a given type.
Definition: mapset_msc.c:74