]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/tempfile.c
Merge changes from CUPS 1.4svn-r7282.
[thirdparty/cups.git] / cups / tempfile.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: tempfile.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
4 * Temp file utilities for the Common UNIX Printing System (CUPS).
5 *
5a738aea 6 * Copyright 2007-2008 by Apple Inc.
2abf387c 7 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
2abf387c 19 * cupsTempFd() - Creates a temporary file.
20 * cupsTempFile() - Generates a temporary filename.
21 * cupsTempFile2() - Creates a temporary CUPS file.
ef416fc2 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/*
2abf387c 42 * 'cupsTempFd()' - Creates a temporary file.
ef416fc2 43 *
2abf387c 44 * The temporary filename is returned in the filename buffer.
45 * The temporary file is opened for reading and writing.
ef416fc2 46 */
47
2abf387c 48int /* O - New file descriptor or -1 on error */
ef416fc2 49cupsTempFd(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
09a101d6 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 */
ef416fc2 80
09a101d6 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__ */
ef416fc2 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/*
2abf387c 155 * 'cupsTempFile()' - Generates a temporary filename.
ef416fc2 156 *
2abf387c 157 * The temporary filename is returned in the filename buffer.
5a738aea
MS
158 * This function is deprecated - use @link cupsTempFd@ or
159 * @link cupsTempFile2@ instead.
ef416fc2 160 *
161 * @deprecated@
162 */
163
2abf387c 164char * /* O - Filename or NULL on error */
ef416fc2 165cupsTempFile(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/*
2abf387c 204 * 'cupsTempFile2()' - Creates a temporary CUPS file.
ef416fc2 205 *
2abf387c 206 * The temporary filename is returned in the filename buffer.
207 * The temporary file is opened for writing.
ef416fc2 208 *
209 * @since CUPS 1.2@
210 */
211
212cups_file_t * /* O - CUPS file or NULL on error */
213cupsTempFile2(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/*
bc44d920 234 * End of "$Id: tempfile.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 235 */