/*
- * "$Id: tempfile.c 4918 2006-01-12 05:14:40Z mike $"
+ * Temp file utilities for CUPS.
*
- * Temp file utilities for the Common UNIX Printing System (CUPS).
+ * Copyright © 2007-2018 by Apple Inc.
+ * Copyright © 1997-2006 by Easy Software Products.
*
- * Copyright 1997-2005 by Easy Software Products.
- *
- * These coded instructions, statements, and computer programs are the
- * property of Easy Software Products and are protected by Federal
- * copyright law. Distribution and use rights are outlined in the file
- * "LICENSE.txt" which should have been included with this file. If this
- * file is missing or damaged please contact Easy Software Products
- * at:
- *
- * Attn: CUPS Licensing Information
- * Easy Software Products
- * 44141 Airport View Drive, Suite 204
- * Hollywood, Maryland 20636 USA
- *
- * Voice: (301) 373-9600
- * EMail: cups-info@cups.org
- * WWW: http://www.cups.org
- *
- * This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- * cupsTempFd() - Create a temporary file.
- * cupsTempFile() - Generate a temporary filename.
- * cupsTempFile2() - Create a temporary CUPS file.
+ * Licensed under Apache License v2.0. See the file "LICENSE" for more
+ * information.
*/
/*
* Include necessary headers...
*/
-#include "globals.h"
-#include "debug.h"
+#include "cups-private.h"
+#include "debug-internal.h"
#include <stdlib.h>
-#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
-#if defined(WIN32) || defined(__EMX__)
+#if defined(_WIN32) || defined(__EMX__)
# include <io.h>
#else
# include <unistd.h>
-#endif /* WIN32 || __EMX__ */
+#endif /* _WIN32 || __EMX__ */
/*
- * 'cupsTempFd()' - Create a temporary file.
+ * 'cupsTempFd()' - Creates a temporary file.
*
- * The temporary filename is stored in the filename buffer.
+ * The temporary filename is returned in the filename buffer.
+ * The temporary file is opened for reading and writing.
*/
-int /* O - New file descriptor */
+int /* O - New file descriptor or -1 on error */
cupsTempFd(char *filename, /* I - Pointer to buffer */
int len) /* I - Size of buffer */
{
int fd; /* File descriptor for temp file */
int tries; /* Number of tries */
const char *tmpdir; /* TMPDIR environment var */
-#ifdef WIN32
- char tmppath[1024]; /* Windows temporary directory */
+#if defined(__APPLE__) || defined(_WIN32)
+ char tmppath[1024]; /* Temporary directory */
+#endif /* __APPLE__ || _WIN32 */
+#ifdef _WIN32
DWORD curtime; /* Current time */
#else
struct timeval curtime; /* Current time */
-#endif /* WIN32 */
+#endif /* _WIN32 */
/*
* See if TMPDIR is defined...
*/
-#ifdef WIN32
+#ifdef _WIN32
if ((tmpdir = getenv("TEMP")) == NULL)
{
- GetTempPath(sizeof(tmppath), tmppath);
+ GetTempPathA(sizeof(tmppath), tmppath);
tmpdir = tmppath;
}
-#else
- if ((tmpdir = getenv("TMPDIR")) == NULL)
- {
- /*
- * Put root temp files in restricted temp directory...
- */
- if (getuid() == 0)
- tmpdir = CUPS_REQUESTS "/tmp";
+#elif defined(__APPLE__)
+ /*
+ * On macOS and iOS, the TMPDIR environment variable is not always the best
+ * location to place temporary files due to sandboxing. Instead, the confstr
+ * function should be called to get the proper per-user, per-process TMPDIR
+ * value.
+ */
+
+ if ((tmpdir = getenv("TMPDIR")) != NULL && access(tmpdir, W_OK))
+ tmpdir = NULL;
+
+ if (!tmpdir)
+ {
+ if (confstr(_CS_DARWIN_USER_TEMP_DIR, tmppath, sizeof(tmppath)))
+ tmpdir = tmppath;
else
- tmpdir = "/tmp";
+ tmpdir = "/private/tmp"; /* This should never happen */
}
-#endif /* WIN32 */
+
+#else
+ /*
+ * Previously we put root temporary files in the default CUPS temporary
+ * directory under /var/spool/cups. However, since the scheduler cleans
+ * out temporary files there and runs independently of the user apps, we
+ * don't want to use it unless specifically told to by cupsd.
+ */
+
+ if ((tmpdir = getenv("TMPDIR")) == NULL)
+ tmpdir = "/tmp";
+#endif /* _WIN32 */
/*
* Make the temporary name using the specified directory...
do
{
-#ifdef WIN32
+#ifdef _WIN32
/*
* Get the current time of day...
*/
* Format a string using the hex time values...
*/
- snprintf(filename, len - 1, "%s/%05lx%08lx", tmpdir,
- GetCurrentProcessId(), curtime);
+ snprintf(filename, (size_t)len - 1, "%s/%05lx%08lx", tmpdir, GetCurrentProcessId(), curtime);
#else
/*
* Get the current time of day...
* Format a string using the hex time values...
*/
- snprintf(filename, len - 1, "%s/%08lx%05lx", tmpdir,
- (unsigned long)curtime.tv_sec, (unsigned long)curtime.tv_usec);
-#endif /* WIN32 */
+ snprintf(filename, (size_t)len - 1, "%s/%05x%08x", tmpdir, (unsigned)getpid(), (unsigned)(curtime.tv_sec + curtime.tv_usec + tries));
+#endif /* _WIN32 */
/*
* Open the file in "exclusive" mode, making sure that we don't
* stomp on an existing file or someone's symlink crack...
*/
-#ifdef WIN32
+#ifdef _WIN32
fd = open(filename, _O_CREAT | _O_RDWR | _O_TRUNC | _O_BINARY,
_S_IREAD | _S_IWRITE);
#elif defined(O_NOFOLLOW)
fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
#else
fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
-#endif /* WIN32 */
+#endif /* _WIN32 */
if (fd < 0 && errno != EEXIST)
break;
/*
- * 'cupsTempFile()' - Generate a temporary filename.
+ * 'cupsTempFile()' - Generates a temporary filename.
*
- * The temporary filename is stored in the filename buffer.
- * This function is deprecated - use cupsTempFd() or cupsTempFile2()
- * instead.
+ * The temporary filename is returned in the filename buffer.
+ * This function is deprecated and will no longer generate a temporary
+ * filename - use @link cupsTempFd@ or @link cupsTempFile2@ instead.
*
* @deprecated@
*/
-char * /* O - Filename */
+char * /* O - Filename or @code NULL@ on error */
cupsTempFile(char *filename, /* I - Pointer to buffer */
int len) /* I - Size of buffer */
{
- int fd; /* File descriptor for temp file */
- _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
+ (void)len;
+ if (filename)
+ *filename = '\0';
- /*
- * See if a filename was specified...
- */
-
- if (filename == NULL)
- {
- filename = cg->tempfile;
- len = sizeof(cg->tempfile);
- }
-
- /*
- * Create the temporary file...
- */
-
- if ((fd = cupsTempFd(filename, len)) < 0)
- return (NULL);
-
- /*
- * Close the temp file - it'll be reopened later as needed...
- */
-
- close(fd);
-
- /*
- * Return the temp filename...
- */
-
- return (filename);
+ return (NULL);
}
/*
- * 'cupsTempFile2()' - Create a temporary CUPS file.
+ * 'cupsTempFile2()' - Creates a temporary CUPS file.
*
- * The temporary filename is stored in the filename buffer.
+ * The temporary filename is returned in the filename buffer.
+ * The temporary file is opened for writing.
*
- * @since CUPS 1.2@
+ * @since CUPS 1.2/macOS 10.5@
*/
-cups_file_t * /* O - CUPS file or NULL on error */
+cups_file_t * /* O - CUPS file or @code NULL@ on error */
cupsTempFile2(char *filename, /* I - Pointer to buffer */
int len) /* I - Size of buffer */
{
else
return (file);
}
-
-
-/*
- * End of "$Id: tempfile.c 4918 2006-01-12 05:14:40Z mike $".
- */