diff options
Diffstat (limited to 'miscutils/ooqd.c')
-rw-r--r-- | miscutils/ooqd.c | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/miscutils/ooqd.c b/miscutils/ooqd.c index 4317e05..85114c9 100644 --- a/miscutils/ooqd.c +++ b/miscutils/ooqd.c @@ -29,6 +29,7 @@ int ooqd_main(int argc, char *argv[]) { char *queue_file; FILE *file; + struct stat sb; char curr_qfile[256]; if (argc != 2) @@ -50,17 +51,37 @@ int ooqd_main(int argc, char *argv[]) for(;;) { - /* Try to move queue_file to curr_qfile. This provide at most - * once behavior and allows producers to create a new - * queue_file while we process the old one. - */ - if (rename(queue_file, curr_qfile) == -1) + if (stat(queue_file, &sb) == -1) { if (errno == ENOENT) { sleep(WAIT_TIME); continue; } + report_err("stat failed"); + return 1; + } + + /* Remove curr_qfile. Renaming queue_file to curr_qfile + * will silently fail to delete queue_file if queue_file and + * curr_qfile are hard links. + */ + if (unlink(curr_qfile) == -1) + { + /* Expect ENOENT */ + if (errno != ENOENT) + { + report_err("unlink failed"); + return 1; + } + } + + /* Try to move queue_file to curr_qfile. This provide at most + * once behavior and allows producers to create a new + * queue_file while we process the old one. + */ + if (rename(queue_file, curr_qfile) == -1) + { report_err("rename failed"); return 1; } |