]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/tempfile.c
Fix build issues, add missing files.
[thirdparty/cups.git] / cups / tempfile.c
CommitLineData
ef416fc2 1/*
07623986 2 * Temp file utilities for CUPS.
ef416fc2 3 *
0bb02bfa
MS
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
ef416fc2 6 *
0bb02bfa
MS
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
ef416fc2 9 */
10
11/*
12 * Include necessary headers...
13 */
14
71e16022 15#include "cups-private.h"
ef416fc2 16#include <stdlib.h>
ef416fc2 17#include <fcntl.h>
18#include <sys/stat.h>
19dc16f7 19#if defined(_WIN32) || defined(__EMX__)
ef416fc2 20# include <io.h>
21#else
22# include <unistd.h>
19dc16f7 23#endif /* _WIN32 || __EMX__ */
ef416fc2 24
25
26/*
2abf387c 27 * 'cupsTempFd()' - Creates a temporary file.
ef416fc2 28 *
2abf387c 29 * The temporary filename is returned in the filename buffer.
30 * The temporary file is opened for reading and writing.
ef416fc2 31 */
32
2abf387c 33int /* O - New file descriptor or -1 on error */
ef416fc2 34cupsTempFd(char *filename, /* I - Pointer to buffer */
35 int len) /* I - Size of buffer */
36{
37 int fd; /* File descriptor for temp file */
38 int tries; /* Number of tries */
39 const char *tmpdir; /* TMPDIR environment var */
19dc16f7 40#if defined(__APPLE__) || defined(_WIN32)
0bb02bfa 41 char tmppath[1024]; /* Temporary directory */
19dc16f7
MS
42#endif /* __APPLE__ || _WIN32 */
43#ifdef _WIN32
ef416fc2 44 DWORD curtime; /* Current time */
45#else
46 struct timeval curtime; /* Current time */
19dc16f7 47#endif /* _WIN32 */
ef416fc2 48
49
50 /*
51 * See if TMPDIR is defined...
52 */
53
19dc16f7 54#ifdef _WIN32
ef416fc2 55 if ((tmpdir = getenv("TEMP")) == NULL)
56 {
57 GetTempPath(sizeof(tmppath), tmppath);
58 tmpdir = tmppath;
59 }
0bb02bfa
MS
60
61#elif defined(__APPLE__)
62 /*
63 * On macOS and iOS, the TMPDIR environment variable is not always the best
64 * location to place temporary files due to sandboxing. Instead, the confstr
9b440f8a 65 * function should be called to get the proper per-user, per-process TMPDIR
18dfe3cb 66 * value.
0bb02bfa
MS
67 */
68
18dfe3cb 69 if ((tmpdir = getenv("TMPDIR")) != NULL && access(tmpdir, W_OK))
9b440f8a 70 tmpdir = NULL;
0bb02bfa 71
9b440f8a 72 if (!tmpdir)
0bb02bfa
MS
73 {
74 if (confstr(_CS_DARWIN_USER_TEMP_DIR, tmppath, sizeof(tmppath)))
75 tmpdir = tmppath;
76 else
77 tmpdir = "/private/tmp"; /* This should never happen */
78 }
79
ef416fc2 80#else
09a101d6 81 /*
82 * Previously we put root temporary files in the default CUPS temporary
83 * directory under /var/spool/cups. However, since the scheduler cleans
84 * out temporary files there and runs independently of the user apps, we
85 * don't want to use it unless specifically told to by cupsd.
86 */
ef416fc2 87
09a101d6 88 if ((tmpdir = getenv("TMPDIR")) == NULL)
09a101d6 89 tmpdir = "/tmp";
19dc16f7 90#endif /* _WIN32 */
ef416fc2 91
92 /*
93 * Make the temporary name using the specified directory...
94 */
95
96 tries = 0;
97
98 do
99 {
19dc16f7 100#ifdef _WIN32
ef416fc2 101 /*
102 * Get the current time of day...
103 */
104
105 curtime = GetTickCount() + tries;
106
107 /*
108 * Format a string using the hex time values...
109 */
110
07623986 111 snprintf(filename, (size_t)len - 1, "%s/%05lx%08lx", tmpdir, GetCurrentProcessId(), curtime);
ef416fc2 112#else
113 /*
114 * Get the current time of day...
115 */
116
117 gettimeofday(&curtime, NULL);
118
119 /*
120 * Format a string using the hex time values...
121 */
122
07623986 123 snprintf(filename, (size_t)len - 1, "%s/%05x%08x", tmpdir, (unsigned)getpid(), (unsigned)(curtime.tv_sec + curtime.tv_usec + tries));
19dc16f7 124#endif /* _WIN32 */
ef416fc2 125
126 /*
127 * Open the file in "exclusive" mode, making sure that we don't
128 * stomp on an existing file or someone's symlink crack...
129 */
130
19dc16f7 131#ifdef _WIN32
ef416fc2 132 fd = open(filename, _O_CREAT | _O_RDWR | _O_TRUNC | _O_BINARY,
133 _S_IREAD | _S_IWRITE);
134#elif defined(O_NOFOLLOW)
135 fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
136#else
137 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
19dc16f7 138#endif /* _WIN32 */
ef416fc2 139
140 if (fd < 0 && errno != EEXIST)
141 break;
142
143 tries ++;
144 }
145 while (fd < 0 && tries < 1000);
146
147 /*
148 * Return the file descriptor...
149 */
150
151 return (fd);
152}
153
154
155/*
2abf387c 156 * 'cupsTempFile()' - Generates a temporary filename.
ef416fc2 157 *
2abf387c 158 * The temporary filename is returned in the filename buffer.
240214ef
MS
159 * This function is deprecated and will no longer generate a temporary
160 * filename - use @link cupsTempFd@ or @link cupsTempFile2@ instead.
ef416fc2 161 *
162 * @deprecated@
163 */
164
568fa3fa 165char * /* O - Filename or @code NULL@ on error */
ef416fc2 166cupsTempFile(char *filename, /* I - Pointer to buffer */
167 int len) /* I - Size of buffer */
168{
240214ef 169 (void)len;
ef416fc2 170
240214ef
MS
171 if (filename)
172 *filename = '\0';
ef416fc2 173
240214ef 174 return (NULL);
ef416fc2 175}
176
177
178/*
2abf387c 179 * 'cupsTempFile2()' - Creates a temporary CUPS file.
ef416fc2 180 *
2abf387c 181 * The temporary filename is returned in the filename buffer.
182 * The temporary file is opened for writing.
ef416fc2 183 *
8072030b 184 * @since CUPS 1.2/macOS 10.5@
ef416fc2 185 */
186
568fa3fa 187cups_file_t * /* O - CUPS file or @code NULL@ on error */
ef416fc2 188cupsTempFile2(char *filename, /* I - Pointer to buffer */
189 int len) /* I - Size of buffer */
190{
191 cups_file_t *file; /* CUPS file */
192 int fd; /* File descriptor */
193
194
195 if ((fd = cupsTempFd(filename, len)) < 0)
196 return (NULL);
197 else if ((file = cupsFileOpenFd(fd, "w")) == NULL)
198 {
199 close(fd);
200 unlink(filename);
201 return (NULL);
202 }
203 else
204 return (file);
205}