]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/tempfile.c
Import CUPS 1.4svn-r7356.
[thirdparty/cups.git] / cups / tempfile.c
1 /*
2 * "$Id: tempfile.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * Temp file utilities for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 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 * Contents:
18 *
19 * cupsTempFd() - Creates a temporary file.
20 * cupsTempFile() - Generates a temporary filename.
21 * cupsTempFile2() - Creates a temporary CUPS file.
22 */
23
24 /*
25 * Include necessary headers...
26 */
27
28 #include "globals.h"
29 #include "debug.h"
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
34 #if defined(WIN32) || defined(__EMX__)
35 # include <io.h>
36 #else
37 # include <unistd.h>
38 #endif /* WIN32 || __EMX__ */
39
40
41 /*
42 * 'cupsTempFd()' - Creates a temporary file.
43 *
44 * The temporary filename is returned in the filename buffer.
45 * The temporary file is opened for reading and writing.
46 */
47
48 int /* O - New file descriptor or -1 on error */
49 cupsTempFd(char *filename, /* I - Pointer to buffer */
50 int len) /* I - Size of buffer */
51 {
52 int fd; /* File descriptor for temp file */
53 int tries; /* Number of tries */
54 const char *tmpdir; /* TMPDIR environment var */
55 #ifdef WIN32
56 char tmppath[1024]; /* Windows temporary directory */
57 DWORD curtime; /* Current time */
58 #else
59 struct timeval curtime; /* Current time */
60 #endif /* WIN32 */
61
62
63 /*
64 * See if TMPDIR is defined...
65 */
66
67 #ifdef WIN32
68 if ((tmpdir = getenv("TEMP")) == NULL)
69 {
70 GetTempPath(sizeof(tmppath), tmppath);
71 tmpdir = tmppath;
72 }
73 #else
74 /*
75 * Previously we put root temporary files in the default CUPS temporary
76 * directory under /var/spool/cups. However, since the scheduler cleans
77 * out temporary files there and runs independently of the user apps, we
78 * don't want to use it unless specifically told to by cupsd.
79 */
80
81 if ((tmpdir = getenv("TMPDIR")) == NULL)
82 # ifdef __APPLE__
83 tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */
84 # else
85 tmpdir = "/tmp";
86 # endif /* __APPLE__ */
87 #endif /* WIN32 */
88
89 /*
90 * Make the temporary name using the specified directory...
91 */
92
93 tries = 0;
94
95 do
96 {
97 #ifdef WIN32
98 /*
99 * Get the current time of day...
100 */
101
102 curtime = GetTickCount() + tries;
103
104 /*
105 * Format a string using the hex time values...
106 */
107
108 snprintf(filename, len - 1, "%s/%05lx%08lx", tmpdir,
109 GetCurrentProcessId(), curtime);
110 #else
111 /*
112 * Get the current time of day...
113 */
114
115 gettimeofday(&curtime, NULL);
116
117 /*
118 * Format a string using the hex time values...
119 */
120
121 snprintf(filename, len - 1, "%s/%08lx%05lx", tmpdir,
122 (unsigned long)curtime.tv_sec, (unsigned long)curtime.tv_usec);
123 #endif /* WIN32 */
124
125 /*
126 * Open the file in "exclusive" mode, making sure that we don't
127 * stomp on an existing file or someone's symlink crack...
128 */
129
130 #ifdef WIN32
131 fd = open(filename, _O_CREAT | _O_RDWR | _O_TRUNC | _O_BINARY,
132 _S_IREAD | _S_IWRITE);
133 #elif defined(O_NOFOLLOW)
134 fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
135 #else
136 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
137 #endif /* WIN32 */
138
139 if (fd < 0 && errno != EEXIST)
140 break;
141
142 tries ++;
143 }
144 while (fd < 0 && tries < 1000);
145
146 /*
147 * Return the file descriptor...
148 */
149
150 return (fd);
151 }
152
153
154 /*
155 * 'cupsTempFile()' - Generates a temporary filename.
156 *
157 * The temporary filename is returned in the filename buffer.
158 * This function is deprecated - use @link cupsTempFd@ or
159 * @link cupsTempFile2@ instead.
160 *
161 * @deprecated@
162 */
163
164 char * /* O - Filename or @code NULL@ on error */
165 cupsTempFile(char *filename, /* I - Pointer to buffer */
166 int len) /* I - Size of buffer */
167 {
168 int fd; /* File descriptor for temp file */
169 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
170
171
172 /*
173 * See if a filename was specified...
174 */
175
176 if (filename == NULL)
177 {
178 filename = cg->tempfile;
179 len = sizeof(cg->tempfile);
180 }
181
182 /*
183 * Create the temporary file...
184 */
185
186 if ((fd = cupsTempFd(filename, len)) < 0)
187 return (NULL);
188
189 /*
190 * Close the temp file - it'll be reopened later as needed...
191 */
192
193 close(fd);
194
195 /*
196 * Return the temp filename...
197 */
198
199 return (filename);
200 }
201
202
203 /*
204 * 'cupsTempFile2()' - Creates a temporary CUPS file.
205 *
206 * The temporary filename is returned in the filename buffer.
207 * The temporary file is opened for writing.
208 *
209 * @since CUPS 1.2@
210 */
211
212 cups_file_t * /* O - CUPS file or @code NULL@ on error */
213 cupsTempFile2(char *filename, /* I - Pointer to buffer */
214 int len) /* I - Size of buffer */
215 {
216 cups_file_t *file; /* CUPS file */
217 int fd; /* File descriptor */
218
219
220 if ((fd = cupsTempFd(filename, len)) < 0)
221 return (NULL);
222 else if ((file = cupsFileOpenFd(fd, "w")) == NULL)
223 {
224 close(fd);
225 unlink(filename);
226 return (NULL);
227 }
228 else
229 return (file);
230 }
231
232
233 /*
234 * End of "$Id: tempfile.c 6649 2007-07-11 21:46:42Z mike $".
235 */