atomic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 https://mozilla.org/MPL/2.0/.
  6. package osutil
  7. import (
  8. "errors"
  9. "path/filepath"
  10. "github.com/syncthing/syncthing/lib/fs"
  11. )
  12. var (
  13. ErrClosed = errors.New("write to closed writer")
  14. TempPrefix = ".syncthing.tmp."
  15. )
  16. // An AtomicWriter is an *os.File that writes to a temporary file in the same
  17. // directory as the final path. On successful Close the file is renamed to
  18. // its final path. Any error on Write or during Close is accumulated and
  19. // returned on Close, so a lazy user can ignore errors until Close.
  20. type AtomicWriter struct {
  21. path string
  22. next fs.File
  23. fs fs.Filesystem
  24. err error
  25. }
  26. // CreateAtomic is like os.Create, except a temporary file name is used
  27. // instead of the given name. The file is created with secure (0600)
  28. // permissions.
  29. func CreateAtomic(path string) (*AtomicWriter, error) {
  30. fs := fs.NewFilesystem(fs.FilesystemTypeBasic, filepath.Dir(path))
  31. return CreateAtomicFilesystem(fs, filepath.Base(path))
  32. }
  33. // CreateAtomicFilesystem is like os.Create, except a temporary file name is used
  34. // instead of the given name. The file is created with secure (0600)
  35. // permissions.
  36. func CreateAtomicFilesystem(filesystem fs.Filesystem, path string) (*AtomicWriter, error) {
  37. // The security of this depends on the tempfile having secure
  38. // permissions, 0600, from the beginning. This is what ioutil.TempFile
  39. // does. We have a test that verifies that that is the case, should this
  40. // ever change in the standard library in the future.
  41. fd, err := TempFile(filesystem, filepath.Dir(path), TempPrefix)
  42. if err != nil {
  43. return nil, err
  44. }
  45. w := &AtomicWriter{
  46. path: path,
  47. next: fd,
  48. fs: filesystem,
  49. }
  50. return w, nil
  51. }
  52. // Write is like io.Writer, but is a no-op on an already failed AtomicWriter.
  53. func (w *AtomicWriter) Write(bs []byte) (int, error) {
  54. if w.err != nil {
  55. return 0, w.err
  56. }
  57. n, err := w.next.Write(bs)
  58. if err != nil {
  59. w.err = err
  60. w.next.Close()
  61. }
  62. return n, err
  63. }
  64. // Close closes the temporary file and renames it to the final path. It is
  65. // invalid to call Write() or Close() after Close().
  66. func (w *AtomicWriter) Close() error {
  67. if w.err != nil {
  68. return w.err
  69. }
  70. // Try to not leave temp file around, but ignore error.
  71. defer w.fs.Remove(w.next.Name())
  72. if err := w.next.Sync(); err != nil {
  73. w.err = err
  74. return err
  75. }
  76. if err := w.next.Close(); err != nil {
  77. w.err = err
  78. return err
  79. }
  80. if err := w.fs.Rename(w.next.Name(), w.path); err != nil {
  81. w.err = err
  82. return err
  83. }
  84. // fsync the directory too
  85. if fd, err := w.fs.Open(filepath.Dir(w.next.Name())); err == nil {
  86. fd.Sync()
  87. fd.Close()
  88. }
  89. // Set w.err to return appropriately for any future operations.
  90. w.err = ErrClosed
  91. return nil
  92. }