natefinch / atomic

atomic is a go package for atomic file writing
MIT License
198 stars 14 forks source link

Allow user to wrap an existing write function #21

Open adowair opened 2 years ago

adowair commented 2 years ago

First off, thank you @natefinch and all for putting this tool together!

I have the following usecase and wonder if this project can be extended to support it: today, atomic.WriteFile() uses io.Copy() to do the actual I/O to the temporary file. io.Copy is called with os.File and io.Reader, which makes a lot of sense for most cases. I have a particular case where I would like to override the I/O behavior, but still have the file write be atomic.

Can we consider introducing a new function NewWriter() (or WrapWriter()), like so:

func NewWriter(w func(filename string, r io.Reader) error) func(string, io.Reader) error {
  return func(...) error {
    // Create temp file
    // Call w
    // Sync and rename resultant file
}

Is this within the purview of the project? If so and if people are happy with this proposal, I am happy to submit a PR for this. Thanks!

adowair commented 10 months ago

Hello @natefinch, checking in on this again. The downstream issue https://github.com/kopia/kopia/issues/1887 remains open. Do you think we can merge the PR #22 to address this? Thank you!