Skip to content

feat(inputs.directory_monitor): Allow to preserve timestamps when moving file#18921

Merged
skartikey merged 3 commits into
influxdata:masterfrom
highlyavailable:directory-monitor-preserve-timestamps
May 28, 2026
Merged

feat(inputs.directory_monitor): Allow to preserve timestamps when moving file#18921
skartikey merged 3 commits into
influxdata:masterfrom
highlyavailable:directory-monitor-preserve-timestamps

Conversation

@highlyavailable

@highlyavailable highlyavailable commented May 17, 2026

Copy link
Copy Markdown
Contributor

Summary

When a processed file is moved to the finished or error directory, the plugin copies it to a new file, so the moved file gets the current time as its modification time and the original file history is lost. This is a problem for the auditing and tracking use cases described in #16885.

This adds an optional preserve_timestamps setting. When enabled, the source file's access and modification times are captured before the original is removed, then reapplied to the moved file with os.Chtimes. It defaults to false, so existing behavior is unchanged. Added a test covering both the enabled and disabled paths.

Checklist

Related issues

partially resolves #16885

When a processed file is moved to the finished or error directory, the
plugin copies it to a new file, so the moved file gets the current time
as its modification time and the original file history is lost.

This adds an optional preserve_timestamps setting. When enabled, the
source file's access and modification times are captured before the
original is removed and reapplied to the moved file with os.Chtimes.
The option defaults to false, so existing behavior is unchanged.
@telegraf-tiger

Copy link
Copy Markdown
Contributor

Thanks so much for the pull request!
🤝 ✒️ Just a reminder that the CLA has not yet been signed, and we'll need it before merging. Please sign the CLA when you get a chance, then post a comment here saying !signed-cla

@telegraf-tiger telegraf-tiger Bot added feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins labels May 17, 2026
Comment thread plugins/inputs/directory_monitor/directory_monitor.go
@highlyavailable

Copy link
Copy Markdown
Contributor Author

!signed-cla

@srebhan srebhan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Thanks @highlyavailable for your contribution! Just one comment from my side...

Comment thread plugins/inputs/directory_monitor/directory_monitor.go Outdated
@srebhan srebhan changed the title feat(inputs.directory_monitor): add preserve_timestamps option feat(inputs.directory_monitor): Allow to preserve file timestamps when moving file May 19, 2026
@srebhan srebhan changed the title feat(inputs.directory_monitor): Allow to preserve file timestamps when moving file feat(inputs.directory_monitor): Allow to preserve timestamps when moving file May 19, 2026
@srebhan srebhan self-assigned this May 19, 2026
Co-authored-by: Sven Rebhan <36194019+srebhan@users.noreply.github.com>
@Hipska

Hipska commented May 19, 2026

Copy link
Copy Markdown
Contributor

Does the current state of the code actually sets the created date to the original file create date? According to the documentation, it would only set the access and modification time.

@srebhan

srebhan commented May 20, 2026

Copy link
Copy Markdown
Member

@highlyavailable can you please run make fmt and make docs on the code and push the changes to make the CI pipelines happy?!

@srebhan

srebhan commented May 20, 2026

Copy link
Copy Markdown
Member

AND please restore the PR description template, especially the AI checkboxes. Those items are there for a reason. :-)

@highlyavailable

highlyavailable commented May 20, 2026

Copy link
Copy Markdown
Contributor Author

no, it only sets the access and modification time. The call is here:

if err := os.Chtimes(dstPath, srcTimes.AccessTime(), srcTimes.ModTime()); err != nil {

the sample.conf comment says "access and modification" so the docs match the behavior. do you thhink this should be changed?

@highlyavailable

Copy link
Copy Markdown
Contributor Author

@srebhan I addressed both of your comments!

@telegraf-tiger

Copy link
Copy Markdown
Contributor

@Hipska

Hipska commented May 20, 2026

Copy link
Copy Markdown
Contributor

So this PR does not solve the mentioned Feature Request, were it states:

the system should preserve the file's original created and updated timestamps.

@highlyavailable

highlyavailable commented May 20, 2026

Copy link
Copy Markdown
Contributor Author

So this PR does not solve the mentioned Feature Request, were it states:

the system should preserve the file's original created and updated timestamps.

to go back on my last response, the "updated" part of the issue's wording is modification time, which this PR actually does preserve. so the only remaining issue is not perserving creation time.

Go's stlib doesn't have a setter (that I know of) to set that creation/birth time. os.Chtimes covers access and mod times, but I believe birth time would need per-platform syscalls (macOS setattrlist, Windows SetFileTime, not sure about Linux but I could look into it)

I feel this is a reasonable (partial) fix given the intent of the issue, but I should have called out that it doesn't satisfy the full reqs in the feture request.

I could either update the docs to make clear only access+mod times are covered, or update the PR attempting to include per-platform creation-time support as a follow-up. any thoughts either way?

also, just wanted to say thank you @Hipska for the thorough review, its my first time contributing to this project and I appreciate it.

@Hipska

Hipska commented May 20, 2026

Copy link
Copy Markdown
Contributor

Would the creation time be preserved when using os.Rename? If so, @srebhan would you agree to first using this and fallback to copy+chtimes as earlier suggested in #18921 (comment)

@highlyavailable

Copy link
Copy Markdown
Contributor Author

Would the creation time be preserved when using os.Rename? If so, @srebhan would you agree to first using this and fallback to copy+chtimes as earlier suggested in #18921 (comment)

yes, on the same FS os.Rename (which wraps rename(2), source) just relinks the directory entry, so access+modification+ creation times would all stay intact.

And as we discussed, cross-FS it fails with EXDEV and we would need to fall back to copy + Chtimes, which only restores access and modification time

@srebhan

srebhan commented May 20, 2026

Copy link
Copy Markdown
Member

@Hipska we cannot use os.Rename as there is no reliable way to know when it succeeds or fails. The behavior is highly OS-dependent and (on some OS) also depend on if you are using mount boundaries. We had this and moved away from it to the current code. So a clear "no" for going back.

@highlyavailable let us get this in as-is first. Please adapt your PR description and change "resolves" to "partially resolves"... In a second PR, implement platform dependent ways of restoring the creation/birth time. On Windows that's pretty easy (use SetFileTime) but I'm not sure that's even possible on e.g. Linux...

@highlyavailable

Copy link
Copy Markdown
Contributor Author

@srebhan I just updated the description, thank you both for working on this with me

@srebhan srebhan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @highlyavailable and sorry for the late response!

@srebhan srebhan added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label May 27, 2026
@srebhan srebhan removed their assignment May 27, 2026

@skartikey skartikey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@highlyavailable Thanks for the contribution!

@skartikey skartikey merged commit 2739c01 into influxdata:master May 28, 2026
31 checks passed
@github-actions github-actions Bot added this to the v1.39.0 milestone May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[inputs.directory_monitor] Preserve original file timestamps (created/updated time) when moving files to "finished_directory"

4 participants