]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/tempfile.c
772ca4ef5a1c1078e2dd9ab8d08abbe531017fdf
4 * Temp file utilities for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
22 #include "cups-private.h"
26 #if defined(WIN32) || defined(__EMX__)
30 #endif /* WIN32 || __EMX__ */
34 * 'cupsTempFd()' - Creates a temporary file.
36 * The temporary filename is returned in the filename buffer.
37 * The temporary file is opened for reading and writing.
40 int /* O - New file descriptor or -1 on error */
41 cupsTempFd(char *filename
, /* I - Pointer to buffer */
42 int len
) /* I - Size of buffer */
44 int fd
; /* File descriptor for temp file */
45 int tries
; /* Number of tries */
46 const char *tmpdir
; /* TMPDIR environment var */
48 char tmppath
[1024]; /* Windows temporary directory */
49 DWORD curtime
; /* Current time */
51 struct timeval curtime
; /* Current time */
56 * See if TMPDIR is defined...
60 if ((tmpdir
= getenv("TEMP")) == NULL
)
62 GetTempPath(sizeof(tmppath
), tmppath
);
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.
73 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
74 # if defined(__APPLE__) && !TARGET_OS_IOS
75 tmpdir
= "/private/tmp"; /* /tmp is a symlink to /private/tmp */
78 # endif /* __APPLE__ && !TARGET_OS_IOS */
82 * Make the temporary name using the specified directory...
91 * Get the current time of day...
94 curtime
= GetTickCount() + tries
;
97 * Format a string using the hex time values...
100 snprintf(filename
, (size_t)len
- 1, "%s/%05lx%08lx", tmpdir
, GetCurrentProcessId(), curtime
);
103 * Get the current time of day...
106 gettimeofday(&curtime
, NULL
);
109 * Format a string using the hex time values...
112 snprintf(filename
, (size_t)len
- 1, "%s/%05x%08x", tmpdir
, (unsigned)getpid(), (unsigned)(curtime
.tv_sec
+ curtime
.tv_usec
+ tries
));
116 * Open the file in "exclusive" mode, making sure that we don't
117 * stomp on an existing file or someone's symlink crack...
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);
126 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
129 if (fd
< 0 && errno
!= EEXIST
)
134 while (fd
< 0 && tries
< 1000);
137 * Return the file descriptor...
145 * 'cupsTempFile()' - Generates a temporary filename.
147 * The temporary filename is returned in the filename buffer.
148 * This function is deprecated and will no longer generate a temporary
149 * filename - use @link cupsTempFd@ or @link cupsTempFile2@ instead.
154 char * /* O - Filename or @code NULL@ on error */
155 cupsTempFile(char *filename
, /* I - Pointer to buffer */
156 int len
) /* I - Size of buffer */
168 * 'cupsTempFile2()' - Creates a temporary CUPS file.
170 * The temporary filename is returned in the filename buffer.
171 * The temporary file is opened for writing.
173 * @since CUPS 1.2/OS X 10.5@
176 cups_file_t
* /* O - CUPS file or @code NULL@ on error */
177 cupsTempFile2(char *filename
, /* I - Pointer to buffer */
178 int len
) /* I - Size of buffer */
180 cups_file_t
*file
; /* CUPS file */
181 int fd
; /* File descriptor */
184 if ((fd
= cupsTempFd(filename
, len
)) < 0)
186 else if ((file
= cupsFileOpenFd(fd
, "w")) == NULL
)