util.go 2.8 KB

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