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