util.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (C) 2014 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 model
  7. import (
  8. "context"
  9. "errors"
  10. "fmt"
  11. "path/filepath"
  12. "time"
  13. "github.com/prometheus/client_golang/prometheus"
  14. "github.com/syncthing/syncthing/lib/fs"
  15. )
  16. // inWritableDir calls fn(path), while making sure that the directory
  17. // containing `path` is writable for the duration of the call.
  18. func inWritableDir(fn func(string) error, targetFs fs.Filesystem, path string, ignorePerms bool) error {
  19. dir := filepath.Dir(path)
  20. info, err := targetFs.Stat(dir)
  21. if err != nil {
  22. return err
  23. }
  24. if !info.IsDir() {
  25. return errors.New("Not a directory: " + path)
  26. }
  27. const permBits = fs.ModePerm | fs.ModeSetuid | fs.ModeSetgid | fs.ModeSticky
  28. var parentErr error
  29. if mode := info.Mode() & permBits; mode&0o200 == 0 {
  30. // A non-writeable directory (for this user; we assume that's the
  31. // relevant part). Temporarily change the mode so we can delete the
  32. // file or directory inside it.
  33. parentErr = targetFs.Chmod(dir, mode|0o700)
  34. if parentErr != nil {
  35. l.Debugf("Failed to make parent directory writable: %v", parentErr)
  36. } else {
  37. // Chmod succeeded, we should change the permissions back on the way
  38. // out. If we fail we log the error as we have irrevocably messed up
  39. // at this point. :( (The operation we were called to wrap has
  40. // succeeded or failed on its own so returning an error to the
  41. // caller is inappropriate.)
  42. defer func() {
  43. if err := targetFs.Chmod(dir, mode); err != nil && !fs.IsNotExist(err) {
  44. logFn := l.Warnln
  45. if ignorePerms {
  46. logFn = l.Debugln
  47. }
  48. logFn("Failed to restore directory permissions after gaining write access:", err)
  49. }
  50. }()
  51. }
  52. }
  53. err = fn(path)
  54. if fs.IsPermission(err) && parentErr != nil {
  55. err = fmt.Errorf("error after failing to make parent directory writable: %w", err)
  56. }
  57. return err
  58. }
  59. // addTimeUntilCancelled adds time to the counter for the duration of the
  60. // Context. We do this piecemeal so that polling the counter during a long
  61. // operation shows a relevant value, instead of the counter just increasing
  62. // by a large amount at the end of the operation.
  63. func addTimeUntilCancelled(ctx context.Context, counter prometheus.Counter) {
  64. t0 := time.Now()
  65. defer func() {
  66. if dur := time.Since(t0).Seconds(); dur > 0 {
  67. counter.Add(dur)
  68. }
  69. }()
  70. ticker := time.NewTicker(time.Second)
  71. defer ticker.Stop()
  72. for {
  73. select {
  74. case t := <-ticker.C:
  75. if dur := t.Sub(t0).Seconds(); dur > 0 {
  76. counter.Add(dur)
  77. }
  78. t0 = t
  79. case <-ctx.Done():
  80. return
  81. }
  82. }
  83. }