-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from ekg/temp_dir
Temporary directory management
- Loading branch information
Showing
7 changed files
with
212 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#include <set> | ||
#include <iostream> | ||
#include <unistd.h> | ||
#include "tempfile.hpp" | ||
|
||
namespace temp_file { | ||
|
||
// We use this to make the API thread-safe | ||
std::recursive_mutex monitor; | ||
|
||
std::string temp_dir; | ||
|
||
bool keep_temp = false; | ||
|
||
/// Because the names are in a static object, we can delete them when | ||
/// std::exit() is called. | ||
struct Handler { | ||
std::set <std::string> filenames; | ||
std::string parent_directory; | ||
|
||
~Handler() { | ||
if (!keep_temp) { | ||
// No need to lock in static destructor | ||
for (auto &filename : filenames) { | ||
std::remove(filename.c_str()); | ||
} | ||
if (!parent_directory.empty()) { | ||
// There may be extraneous files in the directory still (like .fai files) | ||
auto directory = opendir(parent_directory.c_str()); | ||
|
||
dirent *dp; | ||
while ((dp = readdir(directory)) != nullptr) { | ||
// For every item still in it, delete it. | ||
// TODO: Maybe eventually recursively delete? | ||
std::remove((parent_directory + "/" + dp->d_name).c_str()); | ||
} | ||
closedir(directory); | ||
|
||
// Delete the directory itself | ||
std::remove(parent_directory.c_str()); | ||
} | ||
} | ||
|
||
} | ||
} handler; | ||
|
||
std::string create(const std::string &base, const std::string& suffix) { | ||
std::lock_guard <std::recursive_mutex> lock(monitor); | ||
|
||
if (handler.parent_directory.empty()) { | ||
// Make a parent directory for our temp files | ||
std::string tmpdirname_cpp = get_dir() + "/" + base + "XXXXXX"; | ||
char *tmpdirname = new char[tmpdirname_cpp.length() + 1]; | ||
strcpy(tmpdirname, tmpdirname_cpp.c_str()); | ||
auto got = mkdtemp(tmpdirname); | ||
if (got != nullptr) { | ||
// Save the directory we got | ||
handler.parent_directory = got; | ||
} else { | ||
std::cerr << "[seqwish::tempfile]: couldn't create temp directory: " << tmpdirname << std::endl; | ||
exit(1); | ||
} | ||
delete[] tmpdirname; | ||
} | ||
|
||
std::string tmpname = handler.parent_directory + "/XXXXXX" + suffix; | ||
// hack to use mkstemp to get us a safe temporary file name | ||
int fd = mkstemps(&tmpname[0], suffix.size()); | ||
if (fd != -1) { | ||
// we don't leave it open; we are assumed to open it again externally | ||
close(fd); | ||
} else { | ||
std::cerr << "[seqwish::tempfile]: couldn't create temp file on base " | ||
<< base << " : " << tmpname << std::endl; | ||
exit(1); | ||
} | ||
handler.filenames.insert(tmpname); | ||
return tmpname; | ||
} | ||
|
||
void remove(const std::string &filename) { | ||
std::lock_guard <std::recursive_mutex> lock(monitor); | ||
|
||
std::remove(filename.c_str()); | ||
handler.filenames.erase(filename); | ||
} | ||
|
||
void set_dir(const std::string &new_temp_dir) { | ||
std::lock_guard <std::recursive_mutex> lock(monitor); | ||
|
||
temp_dir = new_temp_dir; | ||
} | ||
|
||
std::string get_dir() { | ||
std::lock_guard <std::recursive_mutex> lock(monitor); | ||
|
||
// Get the default temp dir from environment variables. | ||
if (temp_dir.empty()) { | ||
char* cwd = get_current_dir_name(); | ||
temp_dir = std::string(cwd); | ||
free(cwd); | ||
/*const char *system_temp_dir = nullptr; | ||
for (const char *var_name : {"TMPDIR", "TMP", "TEMP", "TEMPDIR", "USERPROFILE"}) { | ||
if (system_temp_dir == nullptr) { | ||
system_temp_dir = getenv(var_name); | ||
} | ||
} | ||
temp_dir = (system_temp_dir == nullptr ? "/tmp" : system_temp_dir);*/ | ||
} | ||
|
||
return temp_dir; | ||
} | ||
|
||
void set_keep_temp(bool setting) { | ||
keep_temp = setting; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <mutex> | ||
#include <cstdio> | ||
#include <dirent.h> | ||
|
||
#include <cstring> | ||
|
||
/** | ||
* Temporary files. Create with create() and remove with remove(). All | ||
* temporary files will be deleted when the program exits normally or with | ||
* std::exit(). The files will be created in a directory determined from | ||
* environment variables, though this can be overridden with set_dir(). | ||
* The interface is thread-safe. | ||
*/ | ||
namespace temp_file { | ||
|
||
/// Create a temporary file starting with the given base name | ||
std::string create(const std::string& base, const std::string& suffix); | ||
|
||
/// Remove a temporary file | ||
void remove(const std::string& filename); | ||
|
||
/// Set a temp dir, overriding system defaults and environment variables. | ||
void set_dir(const std::string& new_temp_dir); | ||
|
||
/// Get the current temp dir | ||
std::string get_dir(); | ||
|
||
void set_keep_temp(bool setting); | ||
|
||
} // namespace temp_file |
Oops, something went wrong.