gitea

Development moved to Codeberg

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
// +build plan9

package osfs

import (
	"io"
	"os"
	"path/filepath"
	"syscall"
)

func (f *file) Lock() error {
	// Plan 9 uses a mode bit instead of explicit lock/unlock syscalls.
	//
	// Per http://man.cat-v.org/plan_9/5/stat: “Exclusive use files may be open
	// for I/O by only one fid at a time across all clients of the server. If a
	// second open is attempted, it draws an error.”
	//
	// There is no obvious way to implement this function using the exclusive use bit.
	// See https://golang.org/src/cmd/go/internal/lockedfile/lockedfile_plan9.go
	// for how file locking is done by the go tool on Plan 9.
	return nil
}

func (f *file) Unlock() error {
	return nil
}

func rename(from, to string) error {
	// If from and to are in different directories, copy the file
	// since Plan 9 does not support cross-directory rename.
	if filepath.Dir(from) != filepath.Dir(to) {
		fi, err := os.Stat(from)
		if err != nil {
			return &os.LinkError{"rename", from, to, err}
		}
		if fi.Mode().IsDir() {
			return &os.LinkError{"rename", from, to, syscall.EISDIR}
		}
		fromFile, err := os.Open(from)
		if err != nil {
			return &os.LinkError{"rename", from, to, err}
		}
		toFile, err := os.OpenFile(to, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fi.Mode())
		if err != nil {
			return &os.LinkError{"rename", from, to, err}
		}
		_, err = io.Copy(toFile, fromFile)
		if err != nil {
			return &os.LinkError{"rename", from, to, err}
		}

		// Copy mtime and mode from original file.
		// We need only one syscall if we avoid os.Chmod and os.Chtimes.
		dir := fi.Sys().(*syscall.Dir)
		var d syscall.Dir
		d.Null()
		d.Mtime = dir.Mtime
		d.Mode = dir.Mode
		if err = dirwstat(to, &d); err != nil {
			return &os.LinkError{"rename", from, to, err}
		}

		// Remove original file.
		err = os.Remove(from)
		if err != nil {
			return &os.LinkError{"rename", from, to, err}
		}
		return nil
	}
	return os.Rename(from, to)
}

func dirwstat(name string, d *syscall.Dir) error {
	var buf [syscall.STATFIXLEN]byte

	n, err := d.Marshal(buf[:])
	if err != nil {
		return &os.PathError{"dirwstat", name, err}
	}
	if err = syscall.Wstat(name, buf[:n]); err != nil {
		return &os.PathError{"dirwstat", name, err}
	}
	return nil
}