]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/tempfile.c
2 * Temp file utilities for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include "cups-private.h"
19 #if defined(WIN32) || defined(__EMX__)
23 #endif /* WIN32 || __EMX__ */
27 * 'cupsTempFd()' - Creates a temporary file.
29 * The temporary filename is returned in the filename buffer.
30 * The temporary file is opened for reading and writing.
33 int /* O - New file descriptor or -1 on error */
34 cupsTempFd(char *filename
, /* I - Pointer to buffer */
35 int len
) /* I - Size of buffer */
37 int fd
; /* File descriptor for temp file */
38 int tries
; /* Number of tries */
39 const char *tmpdir
; /* TMPDIR environment var */
40 #if defined(__APPLE__) || defined(WIN32)
41 char tmppath
[1024]; /* Temporary directory */
42 #endif /* __APPLE__ || WIN32 */
44 DWORD curtime
; /* Current time */
46 struct timeval curtime
; /* Current time */
51 * See if TMPDIR is defined...
55 if ((tmpdir
= getenv("TEMP")) == NULL
)
57 GetTempPath(sizeof(tmppath
), tmppath
);
61 #elif defined(__APPLE__)
63 * On macOS and iOS, the TMPDIR environment variable is not always the best
64 * location to place temporary files due to sandboxing. Instead, the confstr
65 * function should be called to get the proper per-user, per-process TMPDIR
69 if ((tmpdir
= getenv("TMPDIR")) != NULL
&& access(tmpdir
, W_OK
))
74 if (confstr(_CS_DARWIN_USER_TEMP_DIR
, tmppath
, sizeof(tmppath
)))
77 tmpdir
= "/private/tmp"; /* This should never happen */
82 * Previously we put root temporary files in the default CUPS temporary
83 * directory under /var/spool/cups. However, since the scheduler cleans
84 * out temporary files there and runs independently of the user apps, we
85 * don't want to use it unless specifically told to by cupsd.
88 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
93 * Make the temporary name using the specified directory...
102 * Get the current time of day...
105 curtime
= GetTickCount() + tries
;
108 * Format a string using the hex time values...
111 snprintf(filename
, (size_t)len
- 1, "%s/%05lx%08lx", tmpdir
, GetCurrentProcessId(), curtime
);
114 * Get the current time of day...
117 gettimeofday(&curtime
, NULL
);
120 * Format a string using the hex time values...
123 snprintf(filename
, (size_t)len
- 1, "%s/%05x%08x", tmpdir
, (unsigned)getpid(), (unsigned)(curtime
.tv_sec
+ curtime
.tv_usec
+ tries
));
127 * Open the file in "exclusive" mode, making sure that we don't
128 * stomp on an existing file or someone's symlink crack...
132 fd
= open(filename
, _O_CREAT
| _O_RDWR
| _O_TRUNC
| _O_BINARY
,
133 _S_IREAD
| _S_IWRITE
);
134 #elif defined(O_NOFOLLOW)
135 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
| O_NOFOLLOW
, 0600);
137 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
140 if (fd
< 0 && errno
!= EEXIST
)
145 while (fd
< 0 && tries
< 1000);
148 * Return the file descriptor...
156 * 'cupsTempFile()' - Generates a temporary filename.
158 * The temporary filename is returned in the filename buffer.
159 * This function is deprecated and will no longer generate a temporary
160 * filename - use @link cupsTempFd@ or @link cupsTempFile2@ instead.
165 char * /* O - Filename or @code NULL@ on error */
166 cupsTempFile(char *filename
, /* I - Pointer to buffer */
167 int len
) /* I - Size of buffer */
179 * 'cupsTempFile2()' - Creates a temporary CUPS file.
181 * The temporary filename is returned in the filename buffer.
182 * The temporary file is opened for writing.
184 * @since CUPS 1.2/macOS 10.5@
187 cups_file_t
* /* O - CUPS file or @code NULL@ on error */
188 cupsTempFile2(char *filename
, /* I - Pointer to buffer */
189 int len
) /* I - Size of buffer */
191 cups_file_t
*file
; /* CUPS file */
192 int fd
; /* File descriptor */
195 if ((fd
= cupsTempFd(filename
, len
)) < 0)
197 else if ((file
= cupsFileOpenFd(fd
, "w")) == NULL
)