]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/tempfile.c
License change: Apache License, Version 2.0.
[thirdparty/cups.git] / cups / tempfile.c
CommitLineData
ef416fc2 1/*
07623986 2 * Temp file utilities for CUPS.
ef416fc2 3 *
07623986
MS
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 6 *
e3101897 7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
ef416fc2 8 */
9
10/*
11 * Include necessary headers...
12 */
13
71e16022 14#include "cups-private.h"
ef416fc2 15#include <stdlib.h>
ef416fc2 16#include <fcntl.h>
17#include <sys/stat.h>
18#if defined(WIN32) || defined(__EMX__)
19# include <io.h>
20#else
21# include <unistd.h>
22#endif /* WIN32 || __EMX__ */
23
24
25/*
2abf387c 26 * 'cupsTempFd()' - Creates a temporary file.
ef416fc2 27 *
2abf387c 28 * The temporary filename is returned in the filename buffer.
29 * The temporary file is opened for reading and writing.
ef416fc2 30 */
31
2abf387c 32int /* O - New file descriptor or -1 on error */
ef416fc2 33cupsTempFd(char *filename, /* I - Pointer to buffer */
34 int len) /* I - Size of buffer */
35{
36 int fd; /* File descriptor for temp file */
37 int tries; /* Number of tries */
38 const char *tmpdir; /* TMPDIR environment var */
39#ifdef WIN32
40 char tmppath[1024]; /* Windows temporary directory */
41 DWORD curtime; /* Current time */
42#else
43 struct timeval curtime; /* Current time */
44#endif /* WIN32 */
45
46
47 /*
48 * See if TMPDIR is defined...
49 */
50
51#ifdef WIN32
52 if ((tmpdir = getenv("TEMP")) == NULL)
53 {
54 GetTempPath(sizeof(tmppath), tmppath);
55 tmpdir = tmppath;
56 }
57#else
09a101d6 58 /*
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.
63 */
ef416fc2 64
09a101d6 65 if ((tmpdir = getenv("TMPDIR")) == NULL)
879ee463 66# if defined(__APPLE__) && !TARGET_OS_IOS
09a101d6 67 tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */
68# else
69 tmpdir = "/tmp";
879ee463 70# endif /* __APPLE__ && !TARGET_OS_IOS */
ef416fc2 71#endif /* WIN32 */
72
73 /*
74 * Make the temporary name using the specified directory...
75 */
76
77 tries = 0;
78
79 do
80 {
81#ifdef WIN32
82 /*
83 * Get the current time of day...
84 */
85
86 curtime = GetTickCount() + tries;
87
88 /*
89 * Format a string using the hex time values...
90 */
91
07623986 92 snprintf(filename, (size_t)len - 1, "%s/%05lx%08lx", tmpdir, GetCurrentProcessId(), curtime);
ef416fc2 93#else
94 /*
95 * Get the current time of day...
96 */
97
98 gettimeofday(&curtime, NULL);
99
100 /*
101 * Format a string using the hex time values...
102 */
103
07623986 104 snprintf(filename, (size_t)len - 1, "%s/%05x%08x", tmpdir, (unsigned)getpid(), (unsigned)(curtime.tv_sec + curtime.tv_usec + tries));
ef416fc2 105#endif /* WIN32 */
106
107 /*
108 * Open the file in "exclusive" mode, making sure that we don't
109 * stomp on an existing file or someone's symlink crack...
110 */
111
112#ifdef WIN32
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);
117#else
118 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
119#endif /* WIN32 */
120
121 if (fd < 0 && errno != EEXIST)
122 break;
123
124 tries ++;
125 }
126 while (fd < 0 && tries < 1000);
127
128 /*
129 * Return the file descriptor...
130 */
131
132 return (fd);
133}
134
135
136/*
2abf387c 137 * 'cupsTempFile()' - Generates a temporary filename.
ef416fc2 138 *
2abf387c 139 * The temporary filename is returned in the filename buffer.
240214ef
MS
140 * This function is deprecated and will no longer generate a temporary
141 * filename - use @link cupsTempFd@ or @link cupsTempFile2@ instead.
ef416fc2 142 *
143 * @deprecated@
144 */
145
568fa3fa 146char * /* O - Filename or @code NULL@ on error */
ef416fc2 147cupsTempFile(char *filename, /* I - Pointer to buffer */
148 int len) /* I - Size of buffer */
149{
240214ef 150 (void)len;
ef416fc2 151
240214ef
MS
152 if (filename)
153 *filename = '\0';
ef416fc2 154
240214ef 155 return (NULL);
ef416fc2 156}
157
158
159/*
2abf387c 160 * 'cupsTempFile2()' - Creates a temporary CUPS file.
ef416fc2 161 *
2abf387c 162 * The temporary filename is returned in the filename buffer.
163 * The temporary file is opened for writing.
ef416fc2 164 *
8072030b 165 * @since CUPS 1.2/macOS 10.5@
ef416fc2 166 */
167
568fa3fa 168cups_file_t * /* O - CUPS file or @code NULL@ on error */
ef416fc2 169cupsTempFile2(char *filename, /* I - Pointer to buffer */
170 int len) /* I - Size of buffer */
171{
172 cups_file_t *file; /* CUPS file */
173 int fd; /* File descriptor */
174
175
176 if ((fd = cupsTempFd(filename, len)) < 0)
177 return (NULL);
178 else if ((file = cupsFileOpenFd(fd, "w")) == NULL)
179 {
180 close(fd);
181 unlink(filename);
182 return (NULL);
183 }
184 else
185 return (file);
186}