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