]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7deaa1a | 2 | * "$Id: tempfile.c 6039 2006-10-17 02:24:34Z mike $" |
ef416fc2 | 3 | * |
4 | * Temp file utilities for the Common UNIX Printing System (CUPS). | |
5 | * | |
2abf387c | 6 | * Copyright 1997-2006 by Easy Software Products. |
ef416fc2 | 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 | * | |
2abf387c | 28 | * cupsTempFd() - Creates a temporary file. |
29 | * cupsTempFile() - Generates a temporary filename. | |
30 | * cupsTempFile2() - Creates a temporary CUPS file. | |
ef416fc2 | 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 | /* | |
2abf387c | 51 | * 'cupsTempFd()' - Creates a temporary file. |
ef416fc2 | 52 | * |
2abf387c | 53 | * The temporary filename is returned in the filename buffer. |
54 | * The temporary file is opened for reading and writing. | |
ef416fc2 | 55 | */ |
56 | ||
2abf387c | 57 | int /* O - New file descriptor or -1 on error */ |
ef416fc2 | 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 | /* | |
2abf387c | 162 | * 'cupsTempFile()' - Generates a temporary filename. |
ef416fc2 | 163 | * |
2abf387c | 164 | * The temporary filename is returned in the filename buffer. |
ef416fc2 | 165 | * This function is deprecated - use cupsTempFd() or cupsTempFile2() |
166 | * instead. | |
167 | * | |
168 | * @deprecated@ | |
169 | */ | |
170 | ||
2abf387c | 171 | char * /* O - Filename or NULL on error */ |
ef416fc2 | 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 | /* | |
2abf387c | 211 | * 'cupsTempFile2()' - Creates a temporary CUPS file. |
ef416fc2 | 212 | * |
2abf387c | 213 | * The temporary filename is returned in the filename buffer. |
214 | * The temporary file is opened for writing. | |
ef416fc2 | 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 | /* | |
f7deaa1a | 241 | * End of "$Id: tempfile.c 6039 2006-10-17 02:24:34Z mike $". |
ef416fc2 | 242 | */ |