Skip to content

Commit

Permalink
tempdir support (NanoComp#1122)
Browse files Browse the repository at this point in the history
* make_output_directory() makes tempdir

* rm old undocumented feature of symlinking latest_output in structure::set_output_directory

* make temporary directory in tmpdir

* more informative abort message
  • Loading branch information
stevengj authored Feb 14, 2020
1 parent 0e70a14 commit fd99ae3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
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

0 comments on commit fd99ae3

Please sign in to comment.