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