]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Amend exfile_open() to optionally return the offset in the opened file
authorNick Porter <nick@portercomputing.co.uk>
Wed, 9 Nov 2022 17:52:22 +0000 (17:52 +0000)
committerNick Porter <nick@portercomputing.co.uk>
Tue, 22 Nov 2022 11:15:37 +0000 (11:15 +0000)
Allow for detecting a new / blank file so headers can be added.

src/lib/server/exfile.c
src/lib/server/exfile.h
src/modules/rlm_detail/rlm_detail.c
src/modules/rlm_linelog/rlm_linelog.c
src/modules/rlm_sql/sql.c

index d3944f4c2720cd631c70f11d9b3abfc52dc43a32..7bedaa13d825b00bb28a23e596abcca4430fa8d3 100644 (file)
@@ -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.
index 662904abff08bdfb03ac421e20610587192b2d75..1fe9b15623b3a66ac71fd6a70c66337a4ee1b2de 100644 (file)
@@ -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);
 
index 6ada03e519e8f312e44495737adb50dea3bfa028..10c4f24828aa2e2cb79a3c545ac547cb5da05857 100644 (file)
@@ -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;
index 6512e10cbc9bdfc4dc0ec0d42537a99ba67b262f..fa480bd2d512dcf0ed63e8f15a0e31d5dc5e0cef 100644 (file)
@@ -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;
index db6777db8c24f0ae35b9c513dda6c5b288212f23..70529110a93a7c780064169e928254dbecd9642e 100644 (file)
@@ -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));