Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 44 additions & 26 deletions plugins/in_tail/tail_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,44 @@ int flb_tail_file_offset_marker_matches(struct flb_tail_file *file)
return FLB_TRUE;
}

static void update_resumable_offset_state(struct flb_tail_file *file)
{
#ifdef FLB_HAVE_SQLDB
if (file->config->db) {
flb_tail_db_file_offset(file, file->config);
return;
}
#endif

flb_tail_file_update_offset_marker(file);
}
Comment thread
cosmo0920 marked this conversation as resolved.

int flb_tail_file_reset_on_truncate(struct flb_tail_file *file,
int64_t size_delta,
const char *caller)
{
int64_t offset;
struct flb_tail_config *ctx = file->config;

offset = lseek(file->fd, 0, SEEK_SET);
if (offset == -1) {
flb_errno();
return -1;
}

flb_plg_debug(ctx->ins,
"%s: inode=%"PRIu64" file truncated %s (diff: %"PRId64" bytes)",
caller, file->inode, file->name, size_delta);

file->offset = offset;
file->stream_offset = offset;
file->last_processed_bytes = 0;
file->buf_len = 0;

update_resumable_offset_state(file);
return 0;
}

static uint64_t stat_get_st_dev(struct stat *st)
{
#ifdef FLB_SYSTEM_WINDOWS
Expand Down Expand Up @@ -1678,9 +1716,10 @@ int flb_tail_file_remove_all(struct flb_tail_config *ctx)
static int adjust_counters(struct flb_tail_config *ctx, struct flb_tail_file *file)
{
int ret;
int64_t offset;
struct stat st;

(void) ctx;

ret = fstat(file->fd, &st);
if (ret == -1) {
flb_errno();
Expand All @@ -1694,23 +1733,10 @@ static int adjust_counters(struct flb_tail_config *ctx, struct flb_tail_file *fi

/* Check if the file was truncated by comparing current size with previous size */
if (size_delta < 0) {
offset = lseek(file->fd, 0, SEEK_SET);
if (offset == -1) {
flb_errno();
if (flb_tail_file_reset_on_truncate(file, size_delta,
"adjust_counters") == -1) {
return FLB_TAIL_ERROR;
}

flb_plg_debug(ctx->ins, "adjust_counters: inode=%"PRIu64" file truncated %s (diff: %"PRId64" bytes)",
file->inode, file->name, size_delta);
file->offset = offset;
file->buf_len = 0;

/* Update offset in the database file */
#ifdef FLB_HAVE_SQLDB
if (ctx->db) {
flb_tail_db_file_offset(file, ctx);
}
#endif
}
else {
// Avoid negative pending_bytes when fstat() has stale data and size < offset
Expand Down Expand Up @@ -1766,11 +1792,7 @@ int flb_tail_file_chunk(struct flb_tail_file *file)
file->buf_len -= processed_bytes;
file->buf_data[file->buf_len] = '\0';

#ifdef FLB_HAVE_SQLDB
if (file->config->db) {
flb_tail_db_file_offset(file, file->config);
}
#endif
update_resumable_offset_state(file);
return adjust_counters(ctx, file);
}
}
Expand Down Expand Up @@ -1954,11 +1976,7 @@ int flb_tail_file_chunk(struct flb_tail_file *file)
file->buf_len -= processed_bytes;
file->buf_data[file->buf_len] = '\0';

#ifdef FLB_HAVE_SQLDB
if (file->config->db) {
flb_tail_db_file_offset(file, file->config);
}
#endif
update_resumable_offset_state(file);

/* adjust file counters, returns FLB_TAIL_OK or FLB_TAIL_ERROR */
ret = adjust_counters(ctx, file);
Expand Down
3 changes: 3 additions & 0 deletions plugins/in_tail/tail_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,8 @@ static inline off_t flb_tail_file_db_offset(struct flb_tail_file *file)

int flb_tail_file_update_offset_marker(struct flb_tail_file *file);
int flb_tail_file_offset_marker_matches(struct flb_tail_file *file);
int flb_tail_file_reset_on_truncate(struct flb_tail_file *file,
int64_t size_delta,
const char *caller);

#endif
Loading
Loading