util.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "fmt"
  9. "path/filepath"
  10. "sync"
  11. "time"
  12. "github.com/pkg/errors"
  13. "github.com/syncthing/syncthing/lib/fs"
  14. )
  15. type Holdable interface {
  16. Holders() string
  17. }
  18. func newDeadlockDetector(timeout time.Duration) *deadlockDetector {
  19. return &deadlockDetector{
  20. timeout: timeout,
  21. lockers: make(map[string]sync.Locker),
  22. }
  23. }
  24. type deadlockDetector struct {
  25. timeout time.Duration
  26. lockers map[string]sync.Locker
  27. }
  28. func (d *deadlockDetector) Watch(name string, mut sync.Locker) {
  29. d.lockers[name] = mut
  30. go func() {
  31. for {
  32. time.Sleep(d.timeout / 4)
  33. ok := make(chan bool, 2)
  34. go func() {
  35. mut.Lock()
  36. _ = 1 // empty critical section
  37. mut.Unlock()
  38. ok <- true
  39. }()
  40. go func() {
  41. time.Sleep(d.timeout)
  42. ok <- false
  43. }()
  44. if r := <-ok; !r {
  45. msg := fmt.Sprintf("deadlock detected at %s", name)
  46. for otherName, otherMut := range d.lockers {
  47. if otherHolder, ok := otherMut.(Holdable); ok {
  48. msg += "\n===" + otherName + "===\n" + otherHolder.Holders()
  49. }
  50. }
  51. panic(msg)
  52. }
  53. }
  54. }()
  55. }
  56. // inWritableDir calls fn(path), while making sure that the directory
  57. // containing `path` is writable for the duration of the call.
  58. func inWritableDir(fn func(string) error, targetFs fs.Filesystem, path string, ignorePerms bool) error {
  59. dir := filepath.Dir(path)
  60. info, err := targetFs.Stat(dir)
  61. if err != nil {
  62. return err
  63. }
  64. if !info.IsDir() {
  65. return errors.New("Not a directory: " + path)
  66. }
  67. if info.Mode()&0200 == 0 {
  68. // A non-writeable directory (for this user; we assume that's the
  69. // relevant part). Temporarily change the mode so we can delete the
  70. // file or directory inside it.
  71. if err := targetFs.Chmod(dir, 0755); err == nil {
  72. // Chmod succeeded, we should change the permissions back on the way
  73. // out. If we fail we log the error as we have irrevocably messed up
  74. // at this point. :( (The operation we were called to wrap has
  75. // succeeded or failed on its own so returning an error to the
  76. // caller is inappropriate.)
  77. defer func() {
  78. if err := targetFs.Chmod(dir, info.Mode()&fs.ModePerm); err != nil && !fs.IsNotExist(err) {
  79. logFn := l.Warnln
  80. if ignorePerms {
  81. logFn = l.Debugln
  82. }
  83. logFn("Failed to restore directory permissions after gaining write access:", err)
  84. }
  85. }()
  86. }
  87. }
  88. return fn(path)
  89. }