From: Nick Porter Date: Wed, 9 Nov 2022 17:52:22 +0000 (+0000) Subject: Amend exfile_open() to optionally return the offset in the opened file X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7689de55196f4e8336a563120dd5715b0d1b3876;p=thirdparty%2Ffreeradius-server.git Amend exfile_open() to optionally return the offset in the opened file Allow for detecting a new / blank file so headers can be added. --- diff --git a/src/lib/server/exfile.c b/src/lib/server/exfile.c index d3944f4c272..7bedaa13d82 100644 --- a/src/lib/server/exfile.c +++ b/src/lib/server/exfile.c @@ -273,17 +273,19 @@ static int exfile_open_mkdir(exfile_t *ef, char const *filename, mode_t permissi * @param ef The logfile context returned from exfile_init(). * @param filename the file to open. * @param permissions to use. + * @param offset Optional pointer to store offset in when seeking the end of file. * @return * - FD used to write to the file. * - -1 on failure. */ -int exfile_open(exfile_t *ef, char const *filename, mode_t permissions) +int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, off_t *offset) { int i, tries, unused = -1, found = -1, oldest = -1; bool do_cleanup = false; uint32_t hash; fr_time_t now; struct stat st; + off_t real_offset; if (!ef || !filename) return -1; @@ -294,7 +296,8 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions) found = exfile_open_mkdir(ef, filename, permissions); if (found < 0) return -1; - (void) lseek(found, 0, SEEK_END); + real_offset = lseek(found, 0, SEEK_END); + if (offset) *offset = real_offset; return found; } @@ -499,7 +502,8 @@ try_lock: * Seek to the end of the file before returning the FD to * the caller. */ - (void) lseek(ef->entries[i].fd, 0, SEEK_END); + real_offset = lseek(ef->entries[i].fd, 0, SEEK_END); + if (offset) *offset = real_offset; /* * Return holding the mutex for the entry. diff --git a/src/lib/server/exfile.h b/src/lib/server/exfile.h index 662904abff0..1fe9b15623b 100644 --- a/src/lib/server/exfile.h +++ b/src/lib/server/exfile.h @@ -41,7 +41,7 @@ void exfile_enable_triggers(exfile_t *ef, CONF_SECTION *cs, char const *trigger fr_pair_list_t *trigger_args); CC_ACQUIRE_HANDLE("exfile_fd") -int exfile_open(exfile_t *lf, char const *filename, mode_t permissions); +int exfile_open(exfile_t *lf, char const *filename, mode_t permissions, off_t *offset); int exfile_close(exfile_t *lf, CC_RELEASE_HANDLE("exfile_fd") int fd); diff --git a/src/modules/rlm_detail/rlm_detail.c b/src/modules/rlm_detail/rlm_detail.c index 6ada03e519e..10c4f24828a 100644 --- a/src/modules/rlm_detail/rlm_detail.c +++ b/src/modules/rlm_detail/rlm_detail.c @@ -372,7 +372,7 @@ static unlang_action_t CC_HINT(nonnull) detail_do(rlm_rcode_t *p_result, module_ RDEBUG2("%s expands to %s", inst->filename, buffer); - outfd = exfile_open(inst->ef, buffer, inst->perm); + outfd = exfile_open(inst->ef, buffer, inst->perm, NULL); if (outfd < 0) { RPERROR("Couldn't open file %s", buffer); RETURN_MODULE_FAIL; diff --git a/src/modules/rlm_linelog/rlm_linelog.c b/src/modules/rlm_linelog/rlm_linelog.c index 6512e10cbc9..fa480bd2d51 100644 --- a/src/modules/rlm_linelog/rlm_linelog.c +++ b/src/modules/rlm_linelog/rlm_linelog.c @@ -663,7 +663,7 @@ build_vector: *p = '/'; } - fd = exfile_open(inst->file.ef, path, inst->file.permissions); + fd = exfile_open(inst->file.ef, path, inst->file.permissions, NULL); if (fd < 0) { RERROR("Failed to open %s: %s", path, fr_syserror(errno)); rcode = RLM_MODULE_FAIL; diff --git a/src/modules/rlm_sql/sql.c b/src/modules/rlm_sql/sql.c index db6777db8c2..70529110a93 100644 --- a/src/modules/rlm_sql/sql.c +++ b/src/modules/rlm_sql/sql.c @@ -607,7 +607,7 @@ void rlm_sql_query_log(rlm_sql_t const *inst, request_t *request, sql_acct_secti return; } - fd = exfile_open(inst->ef, filename, 0640); + fd = exfile_open(inst->ef, filename, 0640, NULL); if (fd < 0) { ERROR("Couldn't open logfile '%s': %s", expanded, fr_syserror(errno));