google / renameio

Package renameio provides a way to atomically create or replace a file or symbolic link.
Apache License 2.0
609 stars 27 forks source link

Add support for 'WithModificationTIme()' option #41

Open anatol opened 1 year ago

anatol commented 1 year ago

In some situations I need to set modification time for the new file.

Having an Option WithPermissions

func WithModificationTime(time time.Time) Option {
    return optionFunc(func(cfg *config) {
        cfg.modTime = &time
    })
}

that takes provided time into account and updates the resulting file would make it possible.

stapelberg commented 1 year ago

I was about to say “can’t you os.Chtimes() before calling CloseAtomicallyReplace()?”, but I think the problem might be that the t.Sync() and/or t.Close() that CloseAtomicallyReplace calls change the modification time themselves, so the Chtimes needs to happen just above https://github.com/google/renameio/blob/0450d4d46d68d9e2184fe3a7cea9ec500b37a68a/tempfile.go#L165

Sounds reasonable, pull requests welcome. We probably need to make both atime and mtime configurable in a single option as that’s what os.Chtimes() wants.