diff options
Diffstat (limited to 'cups/tempfile.c')
-rw-r--r-- | cups/tempfile.c | 52 |
1 files changed, 35 insertions, 17 deletions
diff --git a/cups/tempfile.c b/cups/tempfile.c index da705a94..7b341e13 100644 --- a/cups/tempfile.c +++ b/cups/tempfile.c @@ -1,8 +1,8 @@ /* * Temp file utilities for CUPS. * - * Copyright 2007-2014 by Apple Inc. - * Copyright 1997-2006 by Easy Software Products. + * Copyright © 2007-2018 by Apple Inc. + * Copyright © 1997-2006 by Easy Software Products. * * These coded instructions, statements, and computer programs are the * property of Apple Inc. and are protected by Federal copyright @@ -21,11 +21,11 @@ #include <stdlib.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__ */ /* @@ -42,24 +42,46 @@ cupsTempFd(char *filename, /* I - Pointer to 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); tmpdir = tmppath; } + +#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 = "/private/tmp"; /* This should never happen */ + } + #else /* * Previously we put root temporary files in the default CUPS temporary @@ -69,12 +91,8 @@ cupsTempFd(char *filename, /* I - Pointer to buffer */ */ if ((tmpdir = getenv("TMPDIR")) == NULL) -# if defined(__APPLE__) && !TARGET_OS_IOS - tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */ -# else tmpdir = "/tmp"; -# endif /* __APPLE__ && !TARGET_OS_IOS */ -#endif /* WIN32 */ +#endif /* _WIN32 */ /* * Make the temporary name using the specified directory... @@ -84,7 +102,7 @@ cupsTempFd(char *filename, /* I - Pointer to buffer */ do { -#ifdef WIN32 +#ifdef _WIN32 /* * Get the current time of day... */ @@ -108,21 +126,21 @@ cupsTempFd(char *filename, /* I - Pointer to buffer */ */ snprintf(filename, (size_t)len - 1, "%s/%05x%08x", tmpdir, (unsigned)getpid(), (unsigned)(curtime.tv_sec + curtime.tv_usec + tries)); -#endif /* WIN32 */ +#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; |