Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tempdir support #1122

Merged
merged 4 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/meep.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,7 @@ class flux_vol {
// directory, unless the source file hasn't changed.

const char *make_output_directory(const char *exename, const char *jobname = NULL);
char *make_output_directory(); // make temporary directory
void trash_output_directory(const char *dirname);
FILE *create_output_file(const char *dirname, const char *fname);

Expand Down
39 changes: 29 additions & 10 deletions src/output_directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,9 @@ using namespace std;

namespace meep {

const char symlink_name[] = "latest_output";

void structure::set_output_directory(const char *name) {
char buf[300];
outdir = name;
outdir = name; /* fixme: make a copy */
if (verbosity > 0) master_printf("Using output directory %s/\n", name);
if (readlink(symlink_name, buf, 300) > 0) {
// Link already exists.
unlink(symlink_name);
}
symlink(name, symlink_name);
outdir = name;
}

void fields::set_output_directory(const char *name) {
Expand Down Expand Up @@ -137,6 +128,34 @@ const char *make_output_directory(const char *exename, const char *jobname) {
return outdirname;
}

/* similar to above, but creates a temporary directory in /tmp
(note that the caller should delete[] the return value, but
it's not a big deal if they forget and leak memory since
this function is not called many times in a typical run) */
char *make_output_directory() {
char *outdirname = NULL; // set to tmpdir/meepXXXXXX
static const char meeptemplate[] = "/meepXXXXXX";
const char *tmpdir;

// standard name of Unix temporary directory, cribbed from libuv
if (NULL != (tmpdir = getenv("TMPDIR"))) goto got_tmpdir;
if (NULL != (tmpdir = getenv("TMP"))) goto got_tmpdir;
if (NULL != (tmpdir = getenv("TEMP"))) goto got_tmpdir;
if (NULL != (tmpdir = getenv("TEMPDIR"))) goto got_tmpdir;
tmpdir = "/tmp";
got_tmpdir:

size_t len = strlen(tmpdir) + strlen(meeptemplate) + 1;
outdirname = new char[len];
strcat(strcpy(outdirname, tmpdir), meeptemplate);

if (am_master() && !mkdtemp(outdirname)) {
abort("failed to create temporary output directory \"%s\"", outdirname);
}
broadcast(0, outdirname, len);
return outdirname;
}

void trash_output_directory(const char *dirname) {
if (am_master()) mkdir(dirname, 00777);
}
Expand Down