diff --git a/mz_os_posix.c b/mz_os_posix.c index 2f469612..326d0c55 100644 --- a/mz_os_posix.c +++ b/mz_os_posix.c @@ -326,7 +326,7 @@ int32_t mz_os_is_symlink(const char *path) { int32_t mz_os_make_symlink(const char *path, const char *target_path) { #if defined(NO_SYMLINK) return MZ_SUPPORT_ERROR; -#else +#else if (symlink(target_path, path) != 0) return MZ_INTERNAL_ERROR; return MZ_OK; @@ -352,6 +352,7 @@ int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_targ int32_t mz_os_get_temp_path(char *path, int32_t max_path, const char *prefix) { const char *tmp_dir = NULL; + char *temp_path = (char *)calloc(max_path, sizeof(char)); int32_t result = 0; if (!path || max_path <= 0) @@ -365,15 +366,30 @@ int32_t mz_os_get_temp_path(char *path, int32_t max_path, const char *prefix) { if (!tmp_dir) tmp_dir = "/tmp"; - /* Build template path for mktemp: /XXXXXX */ - result = snprintf(path, max_path, "%s/%sXXXXXX", tmp_dir, prefix ? prefix : ""); - if (result < 0 || result >= max_path) + /* Build template path for mkdtemp: /XXXXXX */ + result = snprintf(temp_path, max_path, "%s/%sXXXXXX", tmp_dir, prefix ? prefix : ""); + if (result < 0 || result >= max_path) { + free(temp_path); return MZ_BUF_ERROR; + } - /* mktemp replaces XXXXXX with unique characters */ - if (!mktemp(path)) + /* Create a temporary directory. + mkdtemp replaces XXXXXX with unique characters + */ + if (!mkdtemp(temp_path)) { + free(temp_path); return MZ_INTERNAL_ERROR; + } + + /* Create a filename inside the temporary directory */ + /* Use current time for the filename */ + result = snprintf(path, max_path, "%s/%lux", temp_path, time(NULL)); + if (result < 0 || result >= max_path) { + free(temp_path); + return MZ_BUF_ERROR; + } + free(temp_path); return MZ_OK; }