]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/tempfile.c
2 * Temp file utilities for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
18 #if defined(WIN32) || defined(__EMX__)
22 #endif /* WIN32 || __EMX__ */
26 * 'cupsTempFd()' - Creates a temporary file.
28 * The temporary filename is returned in the filename buffer.
29 * The temporary file is opened for reading and writing.
32 int /* O - New file descriptor or -1 on error */
33 cupsTempFd(char *filename
, /* I - Pointer to buffer */
34 int len
) /* I - Size of buffer */
36 int fd
; /* File descriptor for temp file */
37 int tries
; /* Number of tries */
38 const char *tmpdir
; /* TMPDIR environment var */
40 char tmppath
[1024]; /* Windows temporary directory */
41 DWORD curtime
; /* Current time */
43 struct timeval curtime
; /* Current time */
48 * See if TMPDIR is defined...
52 if ((tmpdir
= getenv("TEMP")) == NULL
)
54 GetTempPath(sizeof(tmppath
), tmppath
);
59 * Previously we put root temporary files in the default CUPS temporary
60 * directory under /var/spool/cups. However, since the scheduler cleans
61 * out temporary files there and runs independently of the user apps, we
62 * don't want to use it unless specifically told to by cupsd.
65 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
66 # if defined(__APPLE__) && !TARGET_OS_IOS
67 tmpdir
= "/private/tmp"; /* /tmp is a symlink to /private/tmp */
70 # endif /* __APPLE__ && !TARGET_OS_IOS */
74 * Make the temporary name using the specified directory...
83 * Get the current time of day...
86 curtime
= GetTickCount() + tries
;
89 * Format a string using the hex time values...
92 snprintf(filename
, (size_t)len
- 1, "%s/%05lx%08lx", tmpdir
, GetCurrentProcessId(), curtime
);
95 * Get the current time of day...
98 gettimeofday(&curtime
, NULL
);
101 * Format a string using the hex time values...
104 snprintf(filename
, (size_t)len
- 1, "%s/%05x%08x", tmpdir
, (unsigned)getpid(), (unsigned)(curtime
.tv_sec
+ curtime
.tv_usec
+ tries
));
108 * Open the file in "exclusive" mode, making sure that we don't
109 * stomp on an existing file or someone's symlink crack...
113 fd
= open(filename
, _O_CREAT
| _O_RDWR
| _O_TRUNC
| _O_BINARY
,
114 _S_IREAD
| _S_IWRITE
);
115 #elif defined(O_NOFOLLOW)
116 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
| O_NOFOLLOW
, 0600);
118 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
121 if (fd
< 0 && errno
!= EEXIST
)
126 while (fd
< 0 && tries
< 1000);
129 * Return the file descriptor...
137 * 'cupsTempFile()' - Generates a temporary filename.
139 * The temporary filename is returned in the filename buffer.
140 * This function is deprecated and will no longer generate a temporary
141 * filename - use @link cupsTempFd@ or @link cupsTempFile2@ instead.
146 char * /* O - Filename or @code NULL@ on error */
147 cupsTempFile(char *filename
, /* I - Pointer to buffer */
148 int len
) /* I - Size of buffer */
160 * 'cupsTempFile2()' - Creates a temporary CUPS file.
162 * The temporary filename is returned in the filename buffer.
163 * The temporary file is opened for writing.
165 * @since CUPS 1.2/macOS 10.5@
168 cups_file_t
* /* O - CUPS file or @code NULL@ on error */
169 cupsTempFile2(char *filename
, /* I - Pointer to buffer */
170 int len
) /* I - Size of buffer */
172 cups_file_t
*file
; /* CUPS file */
173 int fd
; /* File descriptor */
176 if ((fd
= cupsTempFd(filename
, len
)) < 0)
178 else if ((file
= cupsFileOpenFd(fd
, "w")) == NULL
)