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