summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2015-07-20 11:02:41 +0100
committerRobin Watts <robin.watts@artifex.com>2015-07-20 17:18:59 +0100
commit08cf4d8a9353b6bb938492cb9e7c3d1ba4aabf08 (patch)
tree705a39c9f3d5853e93384d7217ef2cdb9e08f4b1
parent6d006a43d50bcbfc20cbb574c6953cfcfe906208 (diff)
downloadmupdf-08cf4d8a9353b6bb938492cb9e7c3d1ba4aabf08.tar.xz
Tweak fz_tempfile to include directory hint.
In android, we can't write to '.', and we don't have TMP defined. Therefore use the path of the supplied file as a hint.
-rw-r--r--include/mupdf/fitz/output.h9
-rw-r--r--source/fitz/tempfile.c36
2 files changed, 40 insertions, 5 deletions
diff --git a/include/mupdf/fitz/output.h b/include/mupdf/fitz/output.h
index 36d194f8..94a3e45f 100644
--- a/include/mupdf/fitz/output.h
+++ b/include/mupdf/fitz/output.h
@@ -103,8 +103,13 @@ int fz_snprintf(char *buffer, int space, const char *fmt, ...);
/*
fz_tempfilename: Get a temporary filename based upon 'base'.
- This must be freed.
+
+ 'hint' is the path of a file (normally the existing document file)
+ supplied to give the function an idea of what directory to use. This
+ may or may not be used depending on the implementations whim.
+
+ The returned path must be freed.
*/
-char *fz_tempfilename(fz_context *ctx, const char *base);
+char *fz_tempfilename(fz_context *ctx, const char *base, const char *hint);
#endif
diff --git a/source/fitz/tempfile.c b/source/fitz/tempfile.c
index 2115923e..77c7c04a 100644
--- a/source/fitz/tempfile.c
+++ b/source/fitz/tempfile.c
@@ -1,17 +1,47 @@
#include "mupdf/fitz.h"
/* Produce a (hopefully) unique temporary filename based upon a given
- * 'base' name.
+ * 'base' name, with a 'hint' for where to create it.
*
* Isolated into a file that can be modified on a per-platform basis
* if required.
*/
-char *fz_tempfilename(fz_context *ctx, const char *base)
+/* For now, put temporary files with the hint. */
+#define USE_HINT_FOR_DIR
+
+#if defined(_WIN32) || defined(_WIN64)
+#define DIRSEP '\\'
+#else
+#define DIRSEP '/'
+#endif
+
+char *fz_tempfilename(fz_context *ctx, const char *base, const char *hint)
{
- char *tmp = tempnam(".", base);
+ char *tmp;
char *ret;
+#ifdef USE_HINT_FOR_DIR
+ char *hintpath;
+ int hintlen;
+
+ hintlen = strlen(hint);
+ hintpath = fz_malloc(ctx, 1 + hintlen);
+ if (hint == NULL)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "Failed to construct temporary file name");
+
+ while (hintlen > 0 && hint[hintlen-1] != DIRSEP)
+ hintlen--;
+
+ if (hintlen > 0)
+ memcpy(hintpath, hint, hintlen);
+ hintpath[hintlen] = 0;
+ tmp = tempnam(hintlen > 0 ? hintpath : ".", base);
+ fz_free(ctx, hintpath);
+#else
+ tmp = tempnam(".", base);
+#endif
+
if (tmp == NULL)
fz_throw(ctx, FZ_ERROR_GENERIC, "Failed to construct temporary file name");
ret = fz_strdup(ctx, tmp);