]>
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"
16 #include "debug-internal.h"
20 #if defined(_WIN32) || defined(__EMX__)
24 #endif /* _WIN32 || __EMX__ */
28 * 'cupsTempFd()' - Creates a temporary file.
30 * The temporary filename is returned in the filename buffer.
31 * The temporary file is opened for reading and writing.
34 int /* O - New file descriptor or -1 on error */
35 cupsTempFd(char *filename
, /* I - Pointer to buffer */
36 int len
) /* I - Size of buffer */
38 int fd
; /* File descriptor for temp file */
39 int tries
; /* Number of tries */
40 const char *tmpdir
; /* TMPDIR environment var */
41 #if defined(__APPLE__) || defined(_WIN32)
42 char tmppath
[1024]; /* Temporary directory */
43 #endif /* __APPLE__ || _WIN32 */
45 DWORD curtime
; /* Current time */
47 struct timeval curtime
; /* Current time */
52 * See if TMPDIR is defined...
56 if ((tmpdir
= getenv("TEMP")) == NULL
)
58 GetTempPathA(sizeof(tmppath
), tmppath
);
62 #elif defined(__APPLE__)
64 * On macOS and iOS, the TMPDIR environment variable is not always the best
65 * location to place temporary files due to sandboxing. Instead, the confstr
66 * function should be called to get the proper per-user, per-process TMPDIR
70 if ((tmpdir
= getenv("TMPDIR")) != NULL
&& access(tmpdir
, W_OK
))
75 if (confstr(_CS_DARWIN_USER_TEMP_DIR
, tmppath
, sizeof(tmppath
)))
78 tmpdir
= "/private/tmp"; /* This should never happen */
83 * Previously we put root temporary files in the default CUPS temporary
84 * directory under /var/spool/cups. However, since the scheduler cleans
85 * out temporary files there and runs independently of the user apps, we
86 * don't want to use it unless specifically told to by cupsd.
89 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
94 * Make the temporary name using the specified directory...
103 * Get the current time of day...
106 curtime
= GetTickCount() + tries
;
109 * Format a string using the hex time values...
112 snprintf(filename
, (size_t)len
- 1, "%s/%05lx%08lx", tmpdir
, GetCurrentProcessId(), curtime
);
115 * Get the current time of day...
118 gettimeofday(&curtime
, NULL
);
121 * Format a string using the hex time values...
124 snprintf(filename
, (size_t)len
- 1, "%s/%05x%08x", tmpdir
, (unsigned)getpid(), (unsigned)(curtime
.tv_sec
+ curtime
.tv_usec
+ tries
));
128 * Open the file in "exclusive" mode, making sure that we don't
129 * stomp on an existing file or someone's symlink crack...
133 fd
= open(filename
, _O_CREAT
| _O_RDWR
| _O_TRUNC
| _O_BINARY
,
134 _S_IREAD
| _S_IWRITE
);
135 #elif defined(O_NOFOLLOW)
136 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
| O_NOFOLLOW
, 0600);
138 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
141 if (fd
< 0 && errno
!= EEXIST
)
146 while (fd
< 0 && tries
< 1000);
149 * Return the file descriptor...
157 * 'cupsTempFile()' - Generates a temporary filename.
159 * The temporary filename is returned in the filename buffer.
160 * This function is deprecated and will no longer generate a temporary
161 * filename - use @link cupsTempFd@ or @link cupsTempFile2@ instead.
166 char * /* O - Filename or @code NULL@ on error */
167 cupsTempFile(char *filename
, /* I - Pointer to buffer */
168 int len
) /* I - Size of buffer */
180 * 'cupsTempFile2()' - Creates a temporary CUPS file.
182 * The temporary filename is returned in the filename buffer.
183 * The temporary file is opened for writing.
185 * @since CUPS 1.2/macOS 10.5@
188 cups_file_t
* /* O - CUPS file or @code NULL@ on error */
189 cupsTempFile2(char *filename
, /* I - Pointer to buffer */
190 int len
) /* I - Size of buffer */
192 cups_file_t
*file
; /* CUPS file */
193 int fd
; /* File descriptor */
196 if ((fd
= cupsTempFd(filename
, len
)) < 0)
198 else if ((file
= cupsFileOpenFd(fd
, "w")) == NULL
)