Skip to content

Commit

Permalink
we always append to the file
Browse files Browse the repository at this point in the history
  • Loading branch information
alandekok committed Jul 25, 2017
1 parent 6cfa48d commit 4d4b6ca
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/include/exfile.h
Expand Up @@ -36,7 +36,7 @@ extern "C" {
typedef struct exfile_t exfile_t;

exfile_t *exfile_init(TALLOC_CTX *ctx, uint32_t entries, uint32_t idle, bool locking);
int exfile_open(exfile_t *lf, char const *filename, mode_t permissions, bool append);
int exfile_open(exfile_t *lf, char const *filename, mode_t permissions);
int exfile_close(exfile_t *lf, int fd);

#ifdef __cplusplus
Expand Down
25 changes: 12 additions & 13 deletions src/main/exfile.c
Expand Up @@ -205,10 +205,9 @@ 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 append If true seek to the end of the file.
* @return an FD used to write to the file, or -1 on error.
*/
int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool append)
int exfile_open(exfile_t *ef, char const *filename, mode_t permissions)
{
int i, found, tries, unused, oldest;
uint32_t hash;
Expand All @@ -224,8 +223,7 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool app
found = exfile_open_mkdir(ef, filename, permissions);
if (found < 0) return -1;

if (append) (void) lseek(found, 0, SEEK_END);

(void) lseek(found, 0, SEEK_END);
return found;
}

Expand Down Expand Up @@ -316,10 +314,15 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool app

reopen:
/*
* Open the file and try to lock it.
* Open the file.
*/
ef->entries[i].fd = exfile_open_mkdir(ef, filename, permissions);
if (ef->entries[i].fd < 0) goto error;
if (ef->entries[i].fd < 0) {
error:
exfile_cleanup_entry(&ef->entries[i]);
PTHREAD_MUTEX_UNLOCK(&(ef->mutex));
return -1;
}

/*
* Try to lock it. If we can't lock it, it's because
Expand All @@ -336,11 +339,7 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool app
*/
if (lseek(ef->entries[i].fd, 0, SEEK_SET) < 0) {
fr_strerror_printf("Failed to seek in file %s: %s", filename, strerror(errno));

error:
exfile_cleanup_entry(&ef->entries[i]);
PTHREAD_MUTEX_UNLOCK(&(ef->mutex));
return -1;
goto error;
}

/*
Expand All @@ -357,7 +356,7 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool app
/*
* Close the file and re-open it. It may
* have been deleted. If it was deleted,
* then it should now be unlocked.
* then the new file should now be unlocked.
*/
close(ef->entries[i].fd);
ef->entries[i].fd = open(filename, O_WRONLY | O_CREAT, permissions);
Expand Down Expand Up @@ -391,7 +390,7 @@ int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, bool app
* If we're appending, eek to the end of the file before
* returning the FD to the caller.
*/
if (append) lseek(ef->entries[i].fd, 0, SEEK_END);
(void) lseek(ef->entries[i].fd, 0, SEEK_END);

/*
* Return holding the mutex for the entry.
Expand Down
2 changes: 1 addition & 1 deletion src/modules/rlm_detail/rlm_detail.c
Expand Up @@ -388,7 +388,7 @@ static rlm_rcode_t CC_HINT(nonnull) detail_do(void *instance, REQUEST *request,
#endif
#endif

outfd = exfile_open(inst->ef, buffer, inst->perm, true);
outfd = exfile_open(inst->ef, buffer, inst->perm);
if (outfd < 0) {
RERROR("Couldn't open file %s: %s", buffer, fr_strerror());
return RLM_MODULE_FAIL;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/rlm_linelog/rlm_linelog.c
Expand Up @@ -248,7 +248,7 @@ static rlm_rcode_t CC_HINT(nonnull) mod_do_linelog(void *instance, REQUEST *requ
return RLM_MODULE_FAIL;
}

fd = exfile_open(inst->ef, path, inst->permissions, true);
fd = exfile_open(inst->ef, path, inst->permissions);
if (fd < 0) {
ERROR("rlm_linelog: Failed to open %s: %s", path, fr_syserror(errno));
return RLM_MODULE_FAIL;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/rlm_sql/sql.c
Expand Up @@ -487,7 +487,7 @@ void rlm_sql_query_log(rlm_sql_t *inst, REQUEST *request,
return;
}

fd = exfile_open(inst->ef, filename, 0640, true);
fd = exfile_open(inst->ef, filename, 0640);
if (fd < 0) {
ERROR("rlm_sql (%s): Couldn't open logfile '%s': %s", inst->name,
expanded, fr_syserror(errno));
Expand Down

0 comments on commit 4d4b6ca

Please sign in to comment.