Commit c178856c authored by Richard Curnow's avatar Richard Curnow

Remove the lockfile if the program terminates abnormally.

Original rev  : rc@rc0.org.uk--2004-mairix/mairix--dev--0.16--patch-29
parent a122673f
NEW IN VERSION 0.16.1
=====================
* Remove the lockfile if the program terminates for any reason.
NEW IN VERSION 0.16
===================
......
......@@ -2,7 +2,7 @@
mairix - message index builder and finder for maildir folders.
**********************************************************************
* Copyright (C) Richard P. Curnow 2002-2004
* Copyright (C) Richard P. Curnow 2002,2003,2004,2005
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
......@@ -311,7 +311,7 @@ static void import_toktable(char *data, unsigned int hash_key, int n_msgs, struc
/* No point going on - need to find out why the database got corrupted
* in the 1st place. Workaround for user - rebuild database from
* scratch by deleting it then rerunning. */
exit(1);
unlock_and_exit(1);
}
++index;
index &= out->mask;
......
......@@ -102,10 +102,15 @@ void lock_database(char *path, int forced_unlock)/*{{{*/
return;
}
/*}}}*/
void unlock_database(void)/*{{{*/
{
if (lock_file_name) unlink(lock_file_name);
return;
}
/*}}}*/
void unlock_and_exit(int code)/*{{{*/
{
unlock_database();
exit(code);
}
/*}}}*/
......@@ -572,7 +572,7 @@ int main (int argc, char **argv)/*{{{*/
if (!mfolder) {
if (output_folder_type != FT_RAW) {
fprintf(stderr, "No mfolder/MAIRIX_MFOLDER set\n");
exit(2);
unlock_and_exit(2);
}
mfolder = new_string("");
}
......@@ -599,7 +599,7 @@ int main (int argc, char **argv)/*{{{*/
"That folder appears to be one of the indexed mail folders!\n"
"For your own good, I refuse to output search results to an indexed mail folder.\n",
mfolder);
exit(3);
unlock_and_exit(3);
}
result = search_top(do_threads, do_augment, database_path, complete_mfolder, argv, output_folder_type, verbose);
......@@ -608,7 +608,7 @@ int main (int argc, char **argv)/*{{{*/
if (!maildir_folders && !mh_folders && !mboxen) {
fprintf(stderr, "No [mh_]folders/mboxen/MAIRIX_[MH_]FOLDERS set\n");
exit(2);
unlock_and_exit(2);
}
if (verbose) printf("Finding all currently existing messages...\n");
......@@ -623,7 +623,7 @@ int main (int argc, char **argv)/*{{{*/
/* The next call sorts the msgs array as part of looking for duplicates. */
if (check_message_list_for_duplicates(msgs)) {
fprintf(stderr, "Message list contains duplicates - check your 'folders' setting\n");
exit(2);
unlock_and_exit(2);
}
/* Try to open existing database */
......
......@@ -343,5 +343,6 @@ char *expand_string(const char *p);
/* In dotlock.c */
void lock_database(char *path, int forced_unlock);
void unlock_database(void);
void unlock_and_exit(int code);
#endif /* MAIRIX_H */
......@@ -228,7 +228,7 @@ static int looks_like_from_separator(off_t n, unsigned char *va, size_t len, int
/* If we hit the end of the file, it doesn't look like a real 'From' line. */
#ifdef DEBUG_DFA
exit(0);
unlock_and_exit(0);
#endif
return result;
}
......@@ -506,7 +506,7 @@ static void check_duplicates(struct extant_mbox *extant_mboxen, int n_extant)/*{
}
if (any_dupl) {
printf("Exiting, the mairixrc file needs fixing\n");
exit(1);
unlock_and_exit(1);
}
}
/*}}}*/
......@@ -874,7 +874,7 @@ int add_mbox_messages(struct database *db)/*{{{*/
}
if (!va) {
fprintf(stderr, "Couldn't create mapping of file %s\n", mb->path);
exit(1);
unlock_and_exit(1);
}
start = mb->start[j];
......
......@@ -2,7 +2,7 @@
mairix - message index builder and finder for maildir folders.
**********************************************************************
* Copyright (C) Richard P. Curnow 2002-2004
* Copyright (C) Richard P. Curnow 2002,2003,2004,2005
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
......@@ -92,12 +92,12 @@ struct read_db *open_db(char *filename)/*{{{*/
fd = open(filename, O_RDONLY);
if (fd < 0) {
perror("open");
exit (2);
unlock_and_exit (2);
}
if (fstat(fd, &sb) < 0) {
perror("stat");
exit(2);
unlock_and_exit(2);
}
len = sb.st_size;
......@@ -105,21 +105,21 @@ struct read_db *open_db(char *filename)/*{{{*/
data = (char *) mmap(0, len, PROT_READ, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
perror("reader:mmap");
exit(2);
unlock_and_exit(2);
}
if (!data) {
/* Empty file opened => database corrupt for sure */
if (close(fd) < 0) {
perror("close");
exit(2);
unlock_and_exit(2);
}
return NULL;
}
if (close(fd) < 0) {
perror("close");
exit(2);
unlock_and_exit(2);
}
result = new(struct read_db);
......@@ -134,20 +134,20 @@ struct read_db *open_db(char *filename)/*{{{*/
ucdata[2] == HEADER_MAGIC2) {
if (ucdata[3] != HEADER_MAGIC3) {
fprintf(stderr, "Another version of this program produced the existing database! Please rebuild.\n");
exit(2);
unlock_and_exit(2);
}
} else {
fprintf(stderr, "The existing database wasn't produced by this program! Please rebuild.\n");
exit(2);
unlock_and_exit(2);
}
/*}}}*/
/* {{{ Endianness check */
if (uidata[UI_ENDIAN] == 0x11223344) {
fprintf(stderr, "The endianness of the database is reversed for this machine\n");
exit(2);
unlock_and_exit(2);
} else if (uidata[UI_ENDIAN] != 0x44332211) {
fprintf(stderr, "The endianness of this machine is strange (or database is corrupt)\n");
exit(2);
unlock_and_exit(2);
}
/* }}} */
......@@ -200,7 +200,7 @@ void close_db(struct read_db *x)/*{{{*/
if (munmap(x->data, x->len) < 0) {
perror("munmap");
exit(2);
unlock_and_exit(2);
}
free(x);
return;
......
......@@ -1117,7 +1117,7 @@ int main (int argc, char **argv)/*{{{*/
if (argc < 2) {
fprintf(stderr, "Need a path\n");
exit(2);
unlock_and_exit(2);
}
msg = make_rfc822(argv[1]);
......
......@@ -89,7 +89,7 @@ static void build_match_vector(char *substring, unsigned long *a, unsigned long
len = strlen(substring);
if (len > 31 || len == 0) {
fprintf(stderr, "Can't match patterns longer than 31 characters or empty\n");
exit(2);
unlock_and_exit(2);
}
memset(a, 0xff, 256 * sizeof(unsigned long));
for (p=substring, i=0; *p; p++, i++) {
......@@ -520,7 +520,7 @@ static void find_date_matches_in_table(struct read_db *db, char *date_expr, char
status = scan_date_string(date_expr, &start, &has_start, &end, &has_end);
if (status) {
exit (2);
unlock_and_exit (2);
}
if (has_start && has_end) {
......@@ -973,7 +973,7 @@ static int do_search(struct read_db *db, char **args, char *output_path, int sho
out = fopen(output_path, "ab");
if (!out) {
fprintf(stderr, "Cannot open output folder %s\n", output_path);
exit(1);
unlock_and_exit(1);
}
for (i=0; i<db->n_msgs; i++) {
......@@ -1076,7 +1076,7 @@ static void create_dir(char *path)/*{{{*/
{
if (mkdir(path, 0700) < 0) {
fprintf(stderr, "Could not create directory %s\n", path);
exit(2);
unlock_and_exit(2);
}
fprintf(stderr, "Created directory %s\n", path);
return;
......
......@@ -2,7 +2,7 @@
mairix - message index builder and finder for maildir folders.
**********************************************************************
* Copyright (C) Richard P. Curnow 2002-2004
* Copyright (C) Richard P. Curnow 2002,2003,2004,2005
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
......@@ -94,29 +94,29 @@ static void create_rw_mapping(char *filename, size_t len, int *out_fd, char **ou
fd = open(filename, O_RDWR | O_CREAT, 0600);
if (fd < 0) {
perror("open");
exit(2);
unlock_and_exit(2);
}
if (fstat(fd, &sb) < 0) {
perror("stat");
exit(2);
unlock_and_exit(2);
}
if (sb.st_size < len) {
/* Extend */
if (lseek(fd, len - 1, SEEK_SET) < 0) {
perror("lseek");
exit(2);
unlock_and_exit(2);
}
if (write(fd, "\000", 1) < 0) {
perror("write");
exit(2);
unlock_and_exit(2);
}
} else if (sb.st_size > len) {
/* Truncate */
if (ftruncate(fd, len) < 0) {
perror("ftruncate");
exit(2);
unlock_and_exit(2);
}
} else {
/* Exactly the right length already - nothing to do! */
......@@ -125,7 +125,7 @@ static void create_rw_mapping(char *filename, size_t len, int *out_fd, char **ou
data = mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
perror("writer:mmap");
exit(2);
unlock_and_exit(2);
}
*out_data = data;
......@@ -557,7 +557,7 @@ void write_database(struct database *db, char *filename, int do_integrity_checks
}
if (!verify_mbox_size_constraints(db)) {
exit(1);
unlock_and_exit(1);
}
/* Work out mappings */
......@@ -585,15 +585,15 @@ void write_database(struct database *db, char *filename, int do_integrity_checks
/* Unmap / close file */
if (munmap(data, file_len) < 0) {
perror("munmap");
exit(2);
unlock_and_exit(2);
}
if (fsync(fd) < 0) {
perror("fsync");
exit(2);
unlock_and_exit(2);
}
if (close(fd) < 0) {
perror("close");
exit(2);
unlock_and_exit(2);
}
}
/*}}}*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment