aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2010-09-07 11:31:19 +0200
committerThomas White <taw@physics.org>2012-02-22 15:26:56 +0100
commitac01ef9764709b3b20c87bb6278822f4c5a3fdd3 (patch)
tree6c8b549fbb6e43ed52c2f3948bd170d235c48183
parent6e463f8b649e20853c87167e23321fa07534dd1b (diff)
indexamajig: Check --prefix and second-guess the user if it seems appropriate
I know this is kind of naughty, but this is one of the #1 failure modes right now.
-rw-r--r--src/indexamajig.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/indexamajig.c b/src/indexamajig.c
index 89b297c0..466369d0 100644
--- a/src/indexamajig.c
+++ b/src/indexamajig.c
@@ -24,6 +24,8 @@
#include <gsl/gsl_errno.h>
#include <pthread.h>
#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include "utils.h"
#include "hdf5-file.h"
@@ -158,10 +160,51 @@ static void show_help(const char *s)
" --verbose Be verbose about indexing.\n"
" --gpu Use the GPU to speed up the simulation.\n"
" -j <n> Run <n> analyses in parallel. Default 1.\n"
+"\n"
+"\nOptions you probably won't need:\n\n"
+" --no-check-prefix Don't attempt to correct the --prefix.\n"
);
}
+static char *check_prefix(char *prefix)
+{
+ int r;
+ struct stat statbuf;
+ char *new;
+ size_t len;
+
+ /* Is "prefix" a directory? */
+ r = stat(prefix, &statbuf);
+ if ( r != 0 ) {
+ /* "prefix" probably doesn't exist. This is fine - assume
+ * the user knows what they're doing, and that "prefix"
+ * suffixed with the actual filename will produce something
+ * sensible. */
+ return prefix;
+ }
+
+ if ( !S_ISDIR(statbuf.st_mode) ) {
+ /* Also fine, as above. */
+ return prefix;
+ }
+
+ /* Does the prefix end in a slash? */
+ if ( prefix[strlen(prefix)-1] == '/' ) {
+ /* This looks sensible. */
+ return prefix;
+ }
+
+ STATUS("Your prefix ('%s') is a directory, but doesn't end"
+ " with a slash. I'm going to add it for you.\n", prefix);
+ len = strlen(prefix)+2;
+ new = malloc(len);
+ snprintf(new, len, "%s/", prefix);
+ free(prefix);
+ return new;
+}
+
+
static struct image *get_simage(struct image *template, int alternate)
{
struct image *image;
@@ -597,6 +640,8 @@ int main(int argc, char *argv[])
if ( prefix == NULL ) {
prefix = strdup("");
+ } else {
+ prefix = check_prefix(prefix);
}
if ( (nthreads == 0) || (nthreads > MAX_THREADS) ) {