]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/tempfile.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / tempfile.c
1 /*
2 * "$Id: tempfile.c 6039 2006-10-17 02:24:34Z 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 if ((tmpdir = getenv("TMPDIR")) == NULL)
84 {
85 /*
86 * Put root temp files in restricted temp directory...
87 */
88
89 if (getuid() == 0)
90 tmpdir = CUPS_REQUESTS "/tmp";
91 else
92 tmpdir = "/tmp";
93 }
94 #endif /* WIN32 */
95
96 /*
97 * Make the temporary name using the specified directory...
98 */
99
100 tries = 0;
101
102 do
103 {
104 #ifdef WIN32
105 /*
106 * Get the current time of day...
107 */
108
109 curtime = GetTickCount() + tries;
110
111 /*
112 * Format a string using the hex time values...
113 */
114
115 snprintf(filename, len - 1, "%s/%05lx%08lx", tmpdir,
116 GetCurrentProcessId(), curtime);
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
128 snprintf(filename, len - 1, "%s/%08lx%05lx", tmpdir,
129 (unsigned long)curtime.tv_sec, (unsigned long)curtime.tv_usec);
130 #endif /* WIN32 */
131
132 /*
133 * Open the file in "exclusive" mode, making sure that we don't
134 * stomp on an existing file or someone's symlink crack...
135 */
136
137 #ifdef WIN32
138 fd = open(filename, _O_CREAT | _O_RDWR | _O_TRUNC | _O_BINARY,
139 _S_IREAD | _S_IWRITE);
140 #elif defined(O_NOFOLLOW)
141 fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
142 #else
143 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
144 #endif /* WIN32 */
145
146 if (fd < 0 && errno != EEXIST)
147 break;
148
149 tries ++;
150 }
151 while (fd < 0 && tries < 1000);
152
153 /*
154 * Return the file descriptor...
155 */
156
157 return (fd);
158 }
159
160
161 /*
162 * 'cupsTempFile()' - Generates a temporary filename.
163 *
164 * The temporary filename is returned in the filename buffer.
165 * This function is deprecated - use cupsTempFd() or cupsTempFile2()
166 * instead.
167 *
168 * @deprecated@
169 */
170
171 char * /* O - Filename or NULL on error */
172 cupsTempFile(char *filename, /* I - Pointer to buffer */
173 int len) /* I - Size of buffer */
174 {
175 int fd; /* File descriptor for temp file */
176 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
177
178
179 /*
180 * See if a filename was specified...
181 */
182
183 if (filename == NULL)
184 {
185 filename = cg->tempfile;
186 len = sizeof(cg->tempfile);
187 }
188
189 /*
190 * Create the temporary file...
191 */
192
193 if ((fd = cupsTempFd(filename, len)) < 0)
194 return (NULL);
195
196 /*
197 * Close the temp file - it'll be reopened later as needed...
198 */
199
200 close(fd);
201
202 /*
203 * Return the temp filename...
204 */
205
206 return (filename);
207 }
208
209
210 /*
211 * 'cupsTempFile2()' - Creates a temporary CUPS file.
212 *
213 * The temporary filename is returned in the filename buffer.
214 * The temporary file is opened for writing.
215 *
216 * @since CUPS 1.2@
217 */
218
219 cups_file_t * /* O - CUPS file or NULL on error */
220 cupsTempFile2(char *filename, /* I - Pointer to buffer */
221 int len) /* I - Size of buffer */
222 {
223 cups_file_t *file; /* CUPS file */
224 int fd; /* File descriptor */
225
226
227 if ((fd = cupsTempFd(filename, len)) < 0)
228 return (NULL);
229 else if ((file = cupsFileOpenFd(fd, "w")) == NULL)
230 {
231 close(fd);
232 unlink(filename);
233 return (NULL);
234 }
235 else
236 return (file);
237 }
238
239
240 /*
241 * End of "$Id: tempfile.c 6039 2006-10-17 02:24:34Z mike $".
242 */