Skip to content

Commit

Permalink
More helpful error messages. Closes #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
drougge committed Jun 30, 2011
1 parent 60dc693 commit b242f10
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
5 changes: 4 additions & 1 deletion db.c
Expand Up @@ -970,7 +970,10 @@ void db_read_cfg(const char *filename)

MD5_Init(&ctx);
fh = fopen(filename, "r");
assert(fh);
if (!fh) {
fprintf(stderr, "Failed to open config (%s)\n", filename);
exit(1);
}
while (fgets(buf, sizeof(buf), fh)) {
int len = strlen(buf);
assert(len && buf[len - 1] == '\n');
Expand Down
23 changes: 22 additions & 1 deletion mm.c
Expand Up @@ -293,6 +293,19 @@ static int mm_init_old(void)
return 0;
}

static void assertdir(const char *name)
{
char fn[1024];
struct stat sb;
int len = snprintf(fn, sizeof(fn), "%s/%s", basedir, name);
assert(len < (int)sizeof(fn));
int r = stat(fn, &sb);
if (r || !S_ISDIR(sb.st_mode)) {
fprintf(stderr, "Directory %s doesn't exist\n", fn);
exit(1);
}
}

/* Note that this returns 1 for a new cache, not failure as such. */
int mm_init(void)
{
Expand Down Expand Up @@ -321,7 +334,15 @@ int mm_init(void)
len = snprintf(fn, sizeof(fn), "%s/LOCK", basedir);
assert(len < (int)sizeof(fn));
mm_lock_fd = open(fn, O_RDWR | O_CREAT | O_EXLOCK, 0600);
assert(mm_lock_fd != -1);
if (mm_lock_fd == -1) {
char buf[len + 28];
snprintf(buf, sizeof(buf), "Failed to open lockfile (%s)", fn);
perror(buf);
exit(1);
}
assertdir("dump");
assertdir("log");
assertdir("mm_cache");
len = read(mm_lock_fd, &clean, 1);
if (len != 1) clean = 'U';
pos = lseek(mm_lock_fd, 0, SEEK_SET);
Expand Down
7 changes: 5 additions & 2 deletions server.c
Expand Up @@ -104,12 +104,15 @@ int main(int argc, char **argv)
logconn->flags = CONNFLAG_LOG;
logconn->trans.conn = logconn;

assert(argc == 2);
if (argc != 2) {
fprintf(stderr, "Usage: %s configfile\n", argv[0]);
return 1;
}
db_read_cfg(argv[1]);
printf("initing mm..\n");
if (mm_init()) populate_from_dump();
if (!*logdumpindex && blacklisted_guid()) {
printf("Don't use the example GUID\n");
fprintf(stderr, "Don't use the example GUID\n");
return 1;
}
mm_print();
Expand Down

0 comments on commit b242f10

Please sign in to comment.