Skip to content

Commit

Permalink
Use macOS specific directory instead of /tmp
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Aug 31, 2023
1 parent 7c008d5 commit 05398d6
Showing 1 changed file with 61 additions and 63 deletions.
124 changes: 61 additions & 63 deletions src/utils2.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@
* This is important:
* (1) With the exception of splitPathAtDirectory(), splitPathAtExtension()
* and genPathname(), all input pathnames must have unix separators.
* (2) On Windows, when you specify a read or write to "/tmp/...",
* the filename is rewritten to use the Windows temp directory:
* /tmp ==> [Temp]... (Windows)
* (2) On macOS, iOS and Windows, for read or write to "/tmp/..."
* the filename is rewritten to use the OS specific temp directory:
* /tmp ==> [Temp]/...
* (3) This filename rewrite, along with the conversion from unix
* to Windows pathnames, happens in genPathname().
* to OS specific pathnames, happens in genPathname().
* (4) Use fopenReadStream() and fopenWriteStream() to open files,
* because these use genPathname() to find the platform-dependent
* filenames. Likewise for l_binaryRead() and l_binaryWrite().
Expand All @@ -154,13 +154,13 @@
* files to default places, both for generating debugging output
* and for supporting regression tests. Applications also need
* this ability for debugging.
* (8) Why do the pathname rewrite on Windows?
* (8) Why do the pathname rewrite on macOS, iOS and Windows?
* The goal is to have the library, and programs using the library,
* run on multiple platforms without changes. The location of
* temporary files depends on the platform as well as the user's
* configuration. Temp files on Windows are in some directory
* not known a priori. To make everything work seamlessly on
* Windows, every time you open a file for reading or writing,
* configuration. Temp files on some operating systems are in some
* directory not known a priori. To make everything work seamlessly on
* any OS, every time you open a file for reading or writing,
* use a special function such as fopenReadStream() or
* fopenWriteStream(); these call genPathname() to ensure that
* if it is a temp file, the correct path is used. To indicate
Expand Down Expand Up @@ -209,6 +209,10 @@
#include <stddef.h>
#include "allheaders.h"

#if defined(__APPLE__) || defined(_WIN32)
/* Rewrite paths starting with /tmp for macOS, iOS and Windows. */
#define REWRITE_TMP
#endif

/*--------------------------------------------------------------------*
* Safe string operations *
Expand Down Expand Up @@ -1781,8 +1785,8 @@ FILE *fp;
* of the text in the split files will be identical to the original.
* (3) The output filenames are in the form:
* <rootpath>_N.<ext>, N = 1, ... n
* (4) This handles the temp directory pathname conversion on Windows:
* /tmp ==> [Windows Temp directory]
* (4) This handles the temp directory pathname conversion where needed:
* /tmp ==> [OS specific temp directory]
* (5) Files can also be sharded into sets of lines by the program 'split':
* split -n l/<n> <filename>
* Using 'split', the resulting files have approximately equal
Expand Down Expand Up @@ -1860,8 +1864,8 @@ SARRAY *sa;
* Notes:
* (1) This should be used whenever you want to run fopen() to
* read from a stream. Never call fopen() directory.
* (2) This handles the temp directory pathname conversion on Windows:
* /tmp ==> [Windows Temp directory]
* (2) This handles the temp directory pathname conversion where needed:
* /tmp ==> [OS specific temp directory]
* </pre>
*/
FILE *
Expand Down Expand Up @@ -1902,8 +1906,8 @@ FILE *fp;
* Notes:
* (1) This should be used whenever you want to run fopen() to
* write or append to a stream. Never call fopen() directory.
* (2) This handles the temp directory pathname conversion on Windows:
* /tmp ==> [Windows Temp directory]
* (2) This handles the temp directory pathname conversion where needed:
* /tmp ==> [OS specific temp directory]
* </pre>
*/
FILE *
Expand Down Expand Up @@ -2126,7 +2130,7 @@ lept_free(void *ptr)
/*!
* \brief lept_mkdir()
*
* \param[in] subdir of /tmp or its equivalent on Windows
* \param[in] subdir of /tmp or its OS specific equivalent
* \return 0 on success, non-zero on failure
*
* <pre>
Expand Down Expand Up @@ -2196,7 +2200,7 @@ l_uint32 attributes;
/*!
* \brief lept_rmdir()
*
* \param[in] subdir of /tmp or its equivalent on Windows
* \param[in] subdir of /tmp or its OS specific equivalent
* \return 0 on success, non-zero on failure
*
* <pre>
Expand Down Expand Up @@ -2283,9 +2287,8 @@ char *realdir;
* Notes:
* (1) Always use unix pathname separators.
* (2) By calling genPathname(), if the pathname begins with "/tmp"
* this does an automatic directory translation on Windows
* to a path in the Windows [Temp] directory:
* "/tmp" ==> [Temp] (Windows)
* this does an automatic directory translation on
* operating systems which use a different path.
* </pre>
*/
void
Expand Down Expand Up @@ -2338,9 +2341,8 @@ char *realdir;
* all files in /tmp.
* (3) Use unix pathname separators.
* (4) By calling genPathname(), if the pathname begins with "/tmp"
* this does an automatic directory translation on Windows
* to a path in the Windows [Temp] directory:
* "/tmp" ==> [Temp] (Windows)
* this does an automatic directory translation on
* operating systems which use a different path.
* (5) Error conditions:
* * returns -1 if the directory is not found
* * returns the number of files (> 0) that it was unable to remove.
Expand Down Expand Up @@ -2390,8 +2392,8 @@ SARRAY *sa;
* <pre>
* Notes:
* (1) By calling genPathname(), this does an automatic directory
* translation on Windows to a path in the Windows [Temp] directory:
* "/tmp/..." ==> [Temp]/... (Windows)
* this does an automatic directory translation on
* operating systems which use a different path.
* </pre>
*/
l_int32
Expand Down Expand Up @@ -2430,6 +2432,8 @@ l_int32 ret;
* (4) Unlike the other lept_* functions in this section, this can remove
* any file -- it is not restricted to files that are in /tmp or a
* subdirectory of it.
* this does an automatic directory translation on
* operating systems which use a different path.
* </pre>
*/
l_int32
Expand Down Expand Up @@ -2475,9 +2479,8 @@ l_int32 ret;
* be freed by the caller.
* (6) Reminders:
* (a) specify files using unix pathnames
* (b) for Windows, translates
* /tmp ==> [Temp]
* where [Temp] is the Windows temp directory
* (b) this does an automatic directory translation on
* operating systems which use a different path for /tmp.
* (7) Examples:
* * newdir = NULL, newtail = NULL ==> /tmp/src-tail
* * newdir = NULL, newtail = abc ==> /tmp/abc
Expand Down Expand Up @@ -2571,9 +2574,8 @@ l_int32 ret;
* be freed by the caller.
* (6) Reminders:
* (a) specify files using unix pathnames
* (b) for Windows, translates
* /tmp ==> [Temp]
* where [Temp] is the Windows temp directory
* (b) this does an automatic directory translation on
* operating systems which use a different path for /tmp
* (7) Examples:
* * newdir = NULL, newtail = NULL ==> /tmp/src-tail
* * newdir = NULL, newtail = abc ==> /tmp/abc
Expand Down Expand Up @@ -3053,12 +3055,13 @@ size_t len;
* %fname == NULL.
* * from the name of a file in the local directory placed in
* %fname, with %dir == NULL.
* * if in a "/tmp" directory and on Windows, the Windows
* temp directory is used.
* (2) On Windows, if the root of %dir is '/tmp', this does a name
* translation:
* "/tmp" ==> [Temp] (Windows)
* where [Temp] is the Windows temp directory.
* * if in a "/tmp" directory and on iOS, macOS or Windows,
* the OS specific temp directory is used.
* (2) This does an automatic directory translation on
* operating systems which use a different path.
* That path is determined
* * on Windows: by GetTempPath()
* * on macOS, iOS: by confstr() (see man page)
* (3) On unix, the TMPDIR variable is ignored. No rewriting
* of temp directories is permitted.
* (4) There are four cases for the input:
Expand All @@ -3075,7 +3078,11 @@ char *
genPathname(const char *dir,
const char *fname)
{
l_int32 is_win32 = FALSE;
#if defined(REWRITE_TMP)
l_int32 rewrite_tmp = TRUE;
#else
l_int32 rewrite_tmp = FALSE;
#endif /* _WIN32 */
char *cdir, *pathout;
l_int32 dirlen, namelen;
size_t size;
Expand Down Expand Up @@ -3108,20 +3115,26 @@ size_t size;
return (char *)ERROR_PTR("pathout not made", __func__, NULL);
}

#ifdef _WIN32
is_win32 = TRUE;
#endif /* _WIN32 */

/* First handle %dir (which may be a full pathname).
* There is no path rewriting on unix, and on win32, we do not
* rewrite unless the specified directory is /tmp or
* a subdirectory of /tmp */
if (!is_win32 || dirlen < 4 ||
if (!rewrite_tmp || dirlen < 4 ||
(dirlen == 4 && strncmp(cdir, "/tmp", 4) != 0) || /* not in "/tmp" */
(dirlen > 4 && strncmp(cdir, "/tmp/", 5) != 0)) { /* not in "/tmp/" */
stringCopy(pathout, cdir, dirlen);
} else { /* Rewrite for win32 with "/tmp" specified for the directory. */
#ifdef _WIN32
} else { /* Rewrite with "/tmp" specified for the directory. */
#if defined(__APPLE__)
size_t n = confstr(_CS_DARWIN_USER_TEMP_DIR, pathout, size);
if (n == 0 || n > size) {
/* Fall back to using /tmp */
stringCopy(pathout, cdir, dirlen);
} else {
/* Add the rest of cdir */
if (dirlen > 4)
stringCat(pathout, size, cdir + 4);
}
#elif defined(_WIN32)
l_int32 tmpdirlen;
char tmpdir[MAX_PATH];
GetTempPathA(sizeof(tmpdir), tmpdir); /* get the Windows temp dir */
Expand Down Expand Up @@ -3166,9 +3179,7 @@ size_t size;
* which is:
* /tmp/%subdir (unix)
* [Temp]/%subdir (Windows, macOS, iOS)
* where [Temp] is a path determined
* - on Windows: by GetTempPath()
* - on macOS, iOS: by confstr() (see man page)
* where [Temp] is the OS path
* and %subdir is in general a set of nested subdirectories:
* dir1/dir2/.../dirN
* which in use would not typically exceed 2 levels.
Expand All @@ -3195,25 +3206,12 @@ size_t pathlen;

memset(result, 0, nbytes);

#ifdef __APPLE__
{
size_t n = confstr(_CS_DARWIN_USER_TEMP_DIR, result, nbytes);
if (n == 0) {
L_ERROR("failed to find tmp dir, %s\n", __func__, strerror(errno));
return 1;
} else if (n > nbytes) {
return ERROR_INT("result array too small for path\n", __func__, 1);
}
dir = pathJoin(result, subdir);
}
#else
dir = pathJoin("/tmp", subdir);
#endif /* ~ __APPLE__ */

#ifndef _WIN32
path = stringNew(dir);
#else
#if defined(REWRITE_TMP)
path = genPathname(dir, NULL);
#else
path = stringNew(dir);
#endif /* ~ _WIN32 */
pathlen = strlen(path);
if (pathlen < nbytes - 1) {
Expand Down

0 comments on commit 05398d6

Please sign in to comment.