From 46dcc94ec094d061724930c85944af7ea6dfb0f8 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Thu, 10 Oct 2013 17:56:06 +0200 Subject: indexamajig: Add --temp-dir --- src/im-sandbox.c | 107 +++++++++++++++++++++++++++++++--------------------- src/im-sandbox.h | 2 +- src/indexamajig.c | 14 ++++++- src/process_image.c | 32 ++++++++++------ src/process_image.h | 2 +- 5 files changed, 99 insertions(+), 58 deletions(-) diff --git a/src/im-sandbox.c b/src/im-sandbox.c index a748b8e0..2c6e44ee 100644 --- a/src/im-sandbox.c +++ b/src/im-sandbox.c @@ -102,6 +102,8 @@ struct sandbox int *stream_pipe_write; char **last_filename; + char *tmpdir; + struct sb_reader *reader; }; @@ -179,7 +181,7 @@ static char *get_pattern(FILE *fh, char **use_this_one_instead, static void run_work(const struct index_args *iargs, int filename_pipe, int results_pipe, Stream *st, - int cookie) + int cookie, const char *tmpdir) { int allDone = 0; FILE *fh; @@ -226,7 +228,7 @@ static void run_work(const struct index_args *iargs, pargs.filename = line; pargs.n_crystals = 0; - process_image(iargs, &pargs, st, cookie); + process_image(iargs, &pargs, st, cookie, tmpdir); /* Request another image */ c = sprintf(buf, "%i\n", pargs.n_crystals); @@ -448,44 +450,6 @@ static void *run_reader(void *rdv) } -static int create_temporary_folder(signed int n) -{ - int r; - char tmp[64]; - struct stat s; - - if ( n < 0 ) { - snprintf(tmp, 63, "indexamajig.%i", getpid()); - } else { - snprintf(tmp, 63, "worker.%i", n); - } - - if ( stat(tmp, &s) == -1 ) { - if ( errno != ENOENT ) { - ERROR("Failed to stat temporary folder.\n"); - return 1; - } - - r = mkdir(tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - if ( r ) { - ERROR("Failed to create temporary folder: %s\n", - strerror(errno)); - return 1; - } - - } - - r = chdir(tmp); - if ( r ) { - ERROR("Failed to chdir to temporary folder: %s\n", - strerror(errno)); - return 1; - } - - return 0; -} - - static void start_worker_process(struct sandbox *sb, int slot, int argc, char *argv[]) { @@ -521,6 +485,9 @@ static void start_worker_process(struct sandbox *sb, int slot, int j; struct sigaction sa; int r; + char *tmp; + struct stat s; + size_t ll; /* First, disconnect the signal handler */ sa.sa_flags = 0; @@ -532,7 +499,28 @@ static void start_worker_process(struct sandbox *sb, int slot, return; } - create_temporary_folder(slot); + ll = 64 + strlen(sb->tmpdir); + tmp = malloc(ll); + if ( tmp == NULL ) { + ERROR("Failed to allocate temporary dir\n"); + return; + } + + snprintf(tmp, 63, "%s/worker.%i", sb->tmpdir, slot); + + if ( stat(tmp, &s) == -1 ) { + if ( errno != ENOENT ) { + ERROR("Failed to stat temporary folder.\n"); + return; + } + + r = mkdir(tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if ( r ) { + ERROR("Failed to create temporary folder: %s\n", + strerror(errno)); + return; + } + } /* Free resources which will not be needed by worker */ for ( j=0; jn_proc; j++ ) { @@ -562,7 +550,7 @@ static void start_worker_process(struct sandbox *sb, int slot, write_command(st, argc, argv); write_line(st, "FLUSH"); run_work(sb->iargs, filename_pipe[0], result_pipe[1], - st, slot); + st, slot, tmp); close_stream(st); //close(filename_pipe[0]); @@ -640,7 +628,7 @@ static void handle_zombie(struct sandbox *sb) void create_sandbox(struct index_args *iargs, int n_proc, char *prefix, int config_basename, FILE *fh, char *use_this_one_instead, - int ofd, int argc, char *argv[]) + int ofd, int argc, char *argv[], const char *tempdir) { int i; int allDone; @@ -648,6 +636,8 @@ void create_sandbox(struct index_args *iargs, int n_proc, char *prefix, int r; pthread_t reader_thread; struct sandbox *sb; + size_t ll; + struct stat s; sb = calloc(1, sizeof(struct sandbox)); if ( sb == NULL ) { @@ -729,7 +719,35 @@ void create_sandbox(struct index_args *iargs, int n_proc, char *prefix, return; } - if ( create_temporary_folder(-1) ) return; + if ( tempdir == NULL ) { + tempdir = strdup(""); + } + + ll = 64+strlen(tempdir); + sb->tmpdir = malloc(ll); + if ( sb->tmpdir == NULL ) { + ERROR("Failed to allocate temporary directory name\n"); + return; + } + snprintf(sb->tmpdir, ll, "%s/indexamajig.%i", tempdir, getpid()); + + if ( stat(sb->tmpdir, &s) == -1 ) { + + int r; + + if ( errno != ENOENT ) { + ERROR("Failed to stat temporary folder.\n"); + return; + } + + r = mkdir(sb->tmpdir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if ( r ) { + ERROR("Failed to create temporary folder: %s\n", + strerror(errno)); + return; + } + + } /* Fork the right number of times */ lock_sandbox(sb); @@ -912,6 +930,7 @@ void create_sandbox(struct index_args *iargs, int n_proc, char *prefix, free(sb->filename_pipes); free(sb->result_fhs); free(sb->pids); + free(sb->tmpdir); pthread_mutex_destroy(&sb->lock); diff --git a/src/im-sandbox.h b/src/im-sandbox.h index 9248b226..c0c8eba4 100644 --- a/src/im-sandbox.h +++ b/src/im-sandbox.h @@ -39,4 +39,4 @@ extern void create_sandbox(struct index_args *iargs, int n_proc, char *prefix, int config_basename, FILE *fh, char *use_this_one_instead, int streamfd, - int argc, char *argv[]); + int argc, char *argv[], const char *tempdir); diff --git a/src/indexamajig.c b/src/indexamajig.c index b36bbbc2..7d3747ba 100644 --- a/src/indexamajig.c +++ b/src/indexamajig.c @@ -132,6 +132,7 @@ static void show_help(const char *s) "\n" "\nOptions for greater performance:\n\n" " -j Run analyses in parallel. Default 1.\n" +" --temp-dir= Put the temporary folder under .\n" "\n" "\nOptions you probably won't need:\n\n" " --no-check-prefix Don't attempt to correct the --prefix.\n" @@ -171,6 +172,7 @@ int main(int argc, char *argv[]) struct index_args iargs; char *intrad = NULL; char *int_str = NULL; + char *tempdir = NULL; /* Defaults */ iargs.cell = NULL; @@ -253,6 +255,7 @@ int main(int argc, char *argv[]) {"int-radius", 1, NULL, 14}, {"median-filter", 1, NULL, 15}, {"integration", 1, NULL, 16}, + {"temp-dir", 1, NULL, 17}, {0, 0, NULL, 0} }; @@ -377,6 +380,10 @@ int main(int argc, char *argv[]) int_str = strdup(optarg); break; + case 17 : + tempdir = strdup(optarg); + break; + case 0 : break; @@ -391,6 +398,10 @@ int main(int argc, char *argv[]) } + if ( tempdir == NULL ) { + tempdir = strdup("."); + } + if ( filename == NULL ) { filename = strdup("-"); } @@ -559,9 +570,10 @@ int main(int argc, char *argv[]) iargs.ipriv = ipriv; create_sandbox(&iargs, n_proc, prefix, config_basename, fh, - use_this_one_instead, ofd, argc, argv); + use_this_one_instead, ofd, argc, argv, tempdir); free(prefix); + free(tempdir); free_detector_geometry(iargs.det); return 0; diff --git a/src/process_image.c b/src/process_image.c index 6d214c33..0e8eedbc 100644 --- a/src/process_image.c +++ b/src/process_image.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "utils.h" #include "hdf5-file.h" @@ -50,7 +51,7 @@ void process_image(const struct index_args *iargs, struct pattern_args *pargs, - Stream *st, int cookie) + Stream *st, int cookie, const char *tmpdir) { float *data_for_measurement; size_t data_size; @@ -58,27 +59,21 @@ void process_image(const struct index_args *iargs, struct pattern_args *pargs, struct hdfile *hdfile; struct image image; int i; - char filename[1024]; - - /* Prefix to jump out of temporary folder */ - if ( pargs->filename[0] != '/' ) { - snprintf(filename, 1023, "../../%s", pargs->filename); - } else { - snprintf(filename, 1023, "%s", pargs->filename); - } + int r; + char *rn; image.features = NULL; image.data = NULL; image.flags = NULL; image.copyme = iargs->copyme; image.id = cookie; - image.filename = pargs->filename; /* Relative to top level */ + image.filename = pargs->filename; image.beam = iargs->beam; image.det = iargs->det; image.crystals = NULL; image.n_crystals = 0; - hdfile = hdfile_open(filename); /* Relative to temporary folder */ + hdfile = hdfile_open(image.filename); if ( hdfile == NULL ) return; if ( iargs->element != NULL ) { @@ -151,9 +146,24 @@ void process_image(const struct index_args *iargs, struct pattern_args *pargs, free(image.data); image.data = data_for_measurement; + rn = get_current_dir_name(); + + r = chdir(tmpdir); + if ( r ) { + ERROR("Failed to chdir to temporary folder: %s\n", + strerror(errno)); + return; + } + /* Index the pattern */ index_pattern(&image, iargs->indm, iargs->ipriv); + r = chdir(rn); + if ( r ) { + ERROR("Failed to chdir: %s\n", strerror(errno)); + return; + } + pargs->n_crystals = image.n_crystals; /* Default beam parameters */ diff --git a/src/process_image.h b/src/process_image.h index ac197927..b9339cd1 100644 --- a/src/process_image.h +++ b/src/process_image.h @@ -89,7 +89,7 @@ struct pattern_args extern void process_image(const struct index_args *iargs, struct pattern_args *pargs, Stream *st, - int cookie); + int cookie, const char *tmpdir); #endif /* PROCESS_IMAGEs_H */ -- cgit v1.2.3