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