From c507373f4c9cf4d6d68cf8b43d8b89d8f3628f5c Mon Sep 17 00:00:00 2001 From: Thomas White Date: Tue, 10 Jul 2018 11:29:38 +0200 Subject: indexamajig: Move into temporary folder when probing and preparing indexing methods --- src/im-sandbox.c | 86 ++++++++++++++++++++++++++++++++----------------------- src/im-sandbox.h | 2 ++ src/indexamajig.c | 42 +++++++++++++++++++++++---- 3 files changed, 88 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/im-sandbox.c b/src/im-sandbox.c index 0794750f..ad27641b 100644 --- a/src/im-sandbox.c +++ b/src/im-sandbox.c @@ -90,7 +90,7 @@ struct sandbox struct sb_shm *shared; sem_t *queue_sem; - char *tmpdir; + const char *tmpdir; /* Final output */ Stream *stream; @@ -638,7 +638,6 @@ static void start_worker_process(struct sandbox *sb, int slot) } free(sb->fhs); free(sb->fds); - free(sb->tmpdir); free(sb->running); /* Not freed because it's not worth passing them down just for * this purpose: event list file handle, @@ -883,17 +882,62 @@ static void delete_temporary_folder(const char *tmpdir, int n_proc) unlink(path); } - rmdir(workerdir); + if ( rmdir(workerdir) ) { + ERROR("Failed to delete worker temporary folder: %s\n", + strerror(errno)); + } } - rmdir(tmpdir); + if ( rmdir(tmpdir) ) { + ERROR("Failed to delete temporary folder: %s\n", strerror(errno)); + } +} + + +char *create_tempdir(const char *temp_location) +{ + char *tmpdir; + size_t ll; + struct stat s; + + if ( temp_location == NULL ) { + temp_location = ""; + } + + ll = 64+strlen(temp_location); + tmpdir = malloc(ll); + if ( tmpdir == NULL ) { + ERROR("Failed to allocate temporary directory name\n"); + return NULL; + } + snprintf(tmpdir, ll, "%s/indexamajig.%i", temp_location, getpid()); + + if ( stat(tmpdir, &s) == -1 ) { + + int r; + + if ( errno != ENOENT ) { + ERROR("Failed to stat temporary folder.\n"); + return NULL; + } + + r = mkdir(tmpdir, S_IRWXU); + if ( r ) { + ERROR("Failed to create temporary folder: %s\n", + strerror(errno)); + return NULL; + } + + } + + return tmpdir; } void create_sandbox(struct index_args *iargs, int n_proc, char *prefix, int config_basename, FILE *fh, - Stream *stream, const char *tempdir, int serial_start) + Stream *stream, const char *tmpdir, int serial_start) { int i; struct sandbox *sb; @@ -923,6 +967,7 @@ void create_sandbox(struct index_args *iargs, int n_proc, char *prefix, sb->n_proc = n_proc; sb->iargs = iargs; sb->serial = serial_start; + sb->tmpdir = tmpdir; sb->fds = NULL; sb->fhs = NULL; @@ -957,36 +1002,6 @@ void create_sandbox(struct index_args *iargs, int n_proc, char *prefix, return; } - if ( tempdir == NULL ) { - tempdir = ""; - } - - 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); - if ( r ) { - ERROR("Failed to create temporary folder: %s\n", - strerror(errno)); - return; - } - - } - /* Fill the queue */ pthread_mutex_lock(&sb->shared->queue_lock); sb->shared->n_events = 0; @@ -1116,7 +1131,6 @@ void create_sandbox(struct index_args *iargs, int n_proc, char *prefix, sb->shared->n_crystals); delete_temporary_folder(sb->tmpdir, n_proc); - free(sb->tmpdir); munmap(sb->shared, sizeof(struct sb_shm)); free(sb); diff --git a/src/im-sandbox.h b/src/im-sandbox.h index 06c9a33c..1412d199 100644 --- a/src/im-sandbox.h +++ b/src/im-sandbox.h @@ -71,6 +71,8 @@ struct sb_shm int n_crystals; }; +extern char *create_tempdir(const char *temp_location); + extern void create_sandbox(struct index_args *iargs, int n_proc, char *prefix, int config_basename, FILE *fh, Stream *stream, const char *tempdir, int serial_start); diff --git a/src/indexamajig.c b/src/indexamajig.c index 3ad2695f..d4d04986 100644 --- a/src/indexamajig.c +++ b/src/indexamajig.c @@ -260,7 +260,10 @@ int main(int argc, char *argv[]) char *intrad = NULL; char *pkrad = NULL; char *int_str = NULL; - char *tempdir = NULL; + char *temp_location = NULL; /* e.g. /tmp */ + char *tmpdir; /* e.g. /tmp/indexamajig.12345 */ + char *rn; /* e.g. /home/taw/indexing */ + int r; char *int_diag = NULL; char *geom_filename = NULL; struct beam_params beam; @@ -580,7 +583,7 @@ int main(int argc, char *argv[]) break; case 317 : - tempdir = strdup(optarg); + temp_location = strdup(optarg); break; case 318 : @@ -881,8 +884,8 @@ int main(int argc, char *argv[]) return 1; } - if ( tempdir == NULL ) { - tempdir = strdup("."); + if ( temp_location == NULL ) { + temp_location = strdup("."); } /* Open input */ @@ -1084,13 +1087,29 @@ int main(int argc, char *argv[]) } + tmpdir = create_tempdir(temp_location); + if ( tmpdir == NULL ) return 1; + + /* Change into temporary folder, temporarily, to control the crap + * dropped by indexing programs during setup */ + rn = getcwd(NULL, 0); + r = chdir(tmpdir); + if ( r ) { + ERROR("Failed to chdir to temporary folder: %s\n", + strerror(errno)); + return 1; + } + if ( indm_str == NULL ) { + char *rn; + STATUS("No indexing methods specified. I will try to "); STATUS("automatically detect the available methods.\n"); STATUS("To disable auto-detection of indexing methods, specify "); STATUS("which methods to use with --indexing=.\n"); STATUS("Use --indexing=none to disable indexing and integration.\n"); + indm_str = detect_indexing_methods(iargs.cell); } @@ -1159,6 +1178,16 @@ int main(int argc, char *argv[]) } + /* Change back to where we were before. Sandbox code will create + * worker subdirs inside the temporary folder, and process_image will + * change into them. */ + r = chdir(rn); + if ( r ) { + ERROR("Failed to chdir: %s\n", strerror(errno)); + return 1; + } + free(rn); + /* Open output stream */ st = open_stream_for_write_4(outfile, geom_filename, iargs.cell, argc, argv, indm_str); @@ -1172,13 +1201,14 @@ int main(int argc, char *argv[]) gsl_set_error_handler_off(); create_sandbox(&iargs, n_proc, prefix, config_basename, fh, - st, tempdir, serial_start); + st, tmpdir, serial_start); free_imagefile_field_list(iargs.copyme); cell_free(iargs.cell); free(iargs.beam->photon_energy_from); free(prefix); - free(tempdir); + free(temp_location); + free(tmpdir); free_detector_geometry(iargs.det); free(iargs.hdf5_peak_path); close_stream(st); -- cgit v1.2.3