summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/filesystem.c22
-rw-r--r--src/filesystem.h15
2 files changed, 26 insertions, 11 deletions
diff --git a/src/filesystem.c b/src/filesystem.c
index 38af498..03614ce 100644
--- a/src/filesystem.c
+++ b/src/filesystem.c
@@ -426,7 +426,7 @@ lilv_dir_for_each(const char* path,
}
char*
-lilv_create_temporary_directory(const char* pattern)
+lilv_create_temporary_directory_in(const char* pattern, const char* parent)
{
#ifdef _WIN32
static const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
@@ -438,13 +438,10 @@ lilv_create_temporary_directory(const char* pattern)
return NULL;
}
- char* const tmpdir = lilv_temp_directory_path();
- char* const path_pattern = lilv_path_join(tmpdir, pattern);
+ char* const path_pattern = lilv_path_join(parent, pattern);
const size_t path_pattern_len = strlen(path_pattern);
char* const suffix = path_pattern + path_pattern_len - 6;
- free(tmpdir);
-
for (unsigned attempt = 0; attempt < 128; ++attempt) {
for (unsigned i = 0; i < 6; ++i) {
suffix[i] = chars[rand() % n_chars];
@@ -457,14 +454,23 @@ lilv_create_temporary_directory(const char* pattern)
return NULL;
#else
- char* const tmpdir = lilv_temp_directory_path();
- char* const path_pattern = lilv_path_join(tmpdir, pattern);
+ char* const path_pattern = lilv_path_join(parent, pattern);
- free(tmpdir);
return mkdtemp(path_pattern); // NOLINT (not a leak)
#endif
}
+char*
+lilv_create_temporary_directory(const char* pattern)
+{
+ char* const tmpdir = lilv_temp_directory_path();
+ char* const result = lilv_create_temporary_directory_in(pattern, tmpdir);
+
+ free(tmpdir);
+
+ return result;
+}
+
int
lilv_create_directories(const char* dir_path)
{
diff --git a/src/filesystem.h b/src/filesystem.h
index a82ec6c..1a8dc68 100644
--- a/src/filesystem.h
+++ b/src/filesystem.h
@@ -146,12 +146,21 @@ lilv_dir_for_each(const char* path,
void (*f)(const char* path, const char* name, void* data));
/**
- Create a unique temporary directory.
+ Create a unique temporary directory in a specific directory.
The last six characters of `pattern` must be `XXXXXX` and will be replaced
with random characters. This works roughly like mkdtemp, except the pattern
- should only be a directory name, not a full path. The returned path will be
- in a suitable system temporary directory.
+ should only be a directory name, not a full path. The created path will be
+ a child of the given parent directory.
+*/
+char*
+lilv_create_temporary_directory_in(const char* pattern, const char* parent);
+
+/**
+ Create a unique temporary directory.
+
+ This is like lilv_create_temporary_directory_in(), except it creates the
+ directory in the system temporary directory.
*/
char*
lilv_create_temporary_directory(const char* pattern);