From c8f9670d702c918b831c189b5da0b5910eccff5e Mon Sep 17 00:00:00 2001 From: alexhudson Date: Thu, 26 Jul 2007 10:38:10 +0000 Subject: [PATCH] Fixed a bug in a temporary file handle, and made the file open/close checks more correct --- src/agents/queue/queue.c | 25 +++++++++++++++---------- src/agents/queue/queue.h | 4 ++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/agents/queue/queue.c b/src/agents/queue/queue.c index 6912bbd..39b537d 100644 --- a/src/agents/queue/queue.c +++ b/src/agents/queue/queue.c @@ -65,21 +65,26 @@ static int HandleDSN(FILE *data, FILE *control); #define MAX_CHARS_IN_PDBSEARCH 512 -#define FOPEN_CHECK(handle, path, mode) fopen_check((handle), (path), (mode), __LINE__) -void -fopen_check(FILE *handle, char *path, char *mode, int line) +#define FOPEN_CHECK(handle, path, mode) fopen_check(&(handle), (path), (mode), __LINE__) +FILE * +fopen_check(FILE **handle, char *path, char *mode, int line) { - LogAssertF(handle == NULL, "File handle already open on line %d", line); - handle = fopen(path, mode); + LogAssertF(*handle == NULL, "File handle already open on line %d", line); + *handle = fopen(path, mode); + return *handle; } -#define FCLOSE_CHECK(f) fclose_check((f), __LINE__) +#define FCLOSE_CHECK(f) fclose_check(&(f), __LINE__) int -fclose_check(FILE *fh, int line) +fclose_check(FILE **fh, int line) { int ret; - ret = fclose(fh); - LogAssertF(ret == 0, "File close failed on line %d: %d", line, errno); + ret = fclose(*fh); + if (ret == 0) { + *fh = NULL; + } else { + LogFailureF("File close failed on line %d: %d", line, errno); + } return ret; } @@ -720,7 +725,7 @@ StartOver: && (sb.st_size > 8) && ((qEnvelope = (unsigned char *)MemMalloc(sb.st_size + 1)) != NULL) && ((temp = fopen(path, "rb")) != NULL) - && (fread(qEnvelope, sizeof(unsigned char), sb.st_size, fh) == (size_t)sb.st_size)) { + && (fread(qEnvelope, sizeof(unsigned char), sb.st_size, temp) == (size_t)sb.st_size)) { /* Sort the control file as follows: QUEUE_DATE QUEUE_FLAGS diff --git a/src/agents/queue/queue.h b/src/agents/queue/queue.h index 1146c74..e1cf148 100644 --- a/src/agents/queue/queue.h +++ b/src/agents/queue/queue.h @@ -92,8 +92,8 @@ typedef struct _Queue { extern MessageQueue Queue; -void fopen_check(FILE *handle, char *path, char *mode, int line); -int fclose_check(FILE *fh, int line); +FILE * fopen_check(FILE **handle, char *path, char *mode, int line); +int fclose_check(FILE **handle, int line); int unlink_check(char *path, int line); int rename_check(const char *oldpath, const char *newpath, int line);