atomic.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package osutil
  7. import (
  8. "errors"
  9. "io/ioutil"
  10. "os"
  11. "path/filepath"
  12. "runtime"
  13. )
  14. var (
  15. ErrClosed = errors.New("write to closed writer")
  16. TempPrefix = ".syncthing.tmp."
  17. )
  18. // An AtomicWriter is an *os.File that writes to a temporary file in the same
  19. // directory as the final path. On successfull Close the file is renamed to
  20. // it's final path. Any error on Write or during Close is accumulated and
  21. // returned on Close, so a lazy user can ignore errors until Close.
  22. type AtomicWriter struct {
  23. path string
  24. next *os.File
  25. err error
  26. }
  27. // CreateAtomic is like os.Create with a FileMode, except a temporary file
  28. // name is used instead of the given name.
  29. func CreateAtomic(path string, mode os.FileMode) (*AtomicWriter, error) {
  30. fd, err := ioutil.TempFile(filepath.Dir(path), TempPrefix)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if err := os.Chmod(fd.Name(), mode); err != nil {
  35. fd.Close()
  36. os.Remove(fd.Name())
  37. return nil, err
  38. }
  39. w := &AtomicWriter{
  40. path: path,
  41. next: fd,
  42. }
  43. return w, nil
  44. }
  45. // Write is like io.Writer, but is a no-op on an already failed AtomicWriter.
  46. func (w *AtomicWriter) Write(bs []byte) (int, error) {
  47. if w.err != nil {
  48. return 0, w.err
  49. }
  50. n, err := w.next.Write(bs)
  51. if err != nil {
  52. w.err = err
  53. w.next.Close()
  54. }
  55. return n, err
  56. }
  57. // Close closes the temporary file and renames it to the final path. It is
  58. // invalid to call Write() or Close() after Close().
  59. func (w *AtomicWriter) Close() error {
  60. if w.err != nil {
  61. return w.err
  62. }
  63. // Try to not leave temp file around, but ignore error.
  64. defer os.Remove(w.next.Name())
  65. if err := w.next.Close(); err != nil {
  66. w.err = err
  67. return err
  68. }
  69. // Remove the destination file, on Windows only. If it fails, and not due
  70. // to the file not existing, we won't be able to complete the rename
  71. // either. Return this error because it may be more informative. On non-
  72. // Windows we want the atomic rename behavior so we don't attempt remove.
  73. if runtime.GOOS == "windows" {
  74. if err := os.Remove(w.path); err != nil && !os.IsNotExist(err) {
  75. return err
  76. }
  77. }
  78. if err := os.Rename(w.next.Name(), w.path); err != nil {
  79. w.err = err
  80. return err
  81. }
  82. // Set w.err to return appropriately for any future operations.
  83. w.err = ErrClosed
  84. return nil
  85. }