shm_fs.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. //go:build linux || netbsd || openbsd || dragonfly
  3. package shm
  4. import (
  5. "crypto/sha256"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "io/fs"
  10. "os"
  11. "path/filepath"
  12. "runtime"
  13. "kitty/tools/utils"
  14. "golang.org/x/sys/unix"
  15. )
  16. var _ = fmt.Print
  17. type file_based_mmap struct {
  18. f *os.File
  19. pos int64
  20. region []byte
  21. unlinked bool
  22. special_name string
  23. }
  24. func file_mmap(f *os.File, size uint64, access AccessFlags, truncate bool, special_name string) (MMap, error) {
  25. if truncate {
  26. err := truncate_or_unlink(f, size, os.Remove)
  27. if err != nil {
  28. return nil, err
  29. }
  30. }
  31. region, err := mmap(int(size), access, int(f.Fd()), 0)
  32. if err != nil {
  33. f.Close()
  34. os.Remove(f.Name())
  35. return nil, err
  36. }
  37. return &file_based_mmap{f: f, region: region, special_name: special_name}, nil
  38. }
  39. func (self *file_based_mmap) Seek(offset int64, whence int) (ret int64, err error) {
  40. switch whence {
  41. case io.SeekStart:
  42. self.pos = offset
  43. case io.SeekEnd:
  44. self.pos = int64(len(self.region)) + offset
  45. case io.SeekCurrent:
  46. self.pos += offset
  47. }
  48. return self.pos, nil
  49. }
  50. func (self *file_based_mmap) Read(b []byte) (n int, err error) {
  51. return Read(self, b)
  52. }
  53. func (self *file_based_mmap) Write(b []byte) (n int, err error) {
  54. return Write(self, b)
  55. }
  56. func (self *file_based_mmap) Stat() (fs.FileInfo, error) {
  57. return self.f.Stat()
  58. }
  59. func (self *file_based_mmap) Name() string {
  60. if self.special_name != "" {
  61. return self.special_name
  62. }
  63. return filepath.Base(self.f.Name())
  64. }
  65. func (self *file_based_mmap) Flush() error {
  66. return unix.Msync(self.region, unix.MS_SYNC)
  67. }
  68. func (self *file_based_mmap) FileSystemName() string {
  69. return self.f.Name()
  70. }
  71. func (self *file_based_mmap) Slice() []byte {
  72. return self.region
  73. }
  74. func (self *file_based_mmap) Close() (err error) {
  75. if self.region != nil {
  76. self.f.Close()
  77. err = munmap(self.region)
  78. self.region = nil
  79. }
  80. return err
  81. }
  82. func (self *file_based_mmap) Unlink() (err error) {
  83. if self.unlinked {
  84. return nil
  85. }
  86. self.unlinked = true
  87. return os.Remove(self.f.Name())
  88. }
  89. func (self *file_based_mmap) IsFileSystemBacked() bool { return true }
  90. func file_path_from_name(name string) string {
  91. // See https://github.com/openbsd/src/blob/master/lib/libc/gen/shm_open.c
  92. if runtime.GOOS == "openbsd" {
  93. hash := sha256.Sum256(utils.UnsafeStringToBytes(name))
  94. return filepath.Join(SHM_DIR, utils.UnsafeBytesToString(hash[:])+".shm")
  95. }
  96. return filepath.Join(SHM_DIR, name)
  97. }
  98. func create_temp(pattern string, size uint64) (ans MMap, err error) {
  99. special_name := ""
  100. var prefix, suffix string
  101. prefix, suffix, err = prefix_and_suffix(pattern)
  102. if err != nil {
  103. return
  104. }
  105. var f *os.File
  106. try := 0
  107. for {
  108. name := prefix + utils.RandomFilename() + suffix
  109. path := file_path_from_name(name)
  110. f, err = os.OpenFile(path, os.O_EXCL|os.O_CREATE|os.O_RDWR, 0600)
  111. if err != nil {
  112. if errors.Is(err, fs.ErrExist) {
  113. try += 1
  114. if try > 10000 {
  115. return nil, &os.PathError{Op: "createtemp", Path: prefix + "*" + suffix, Err: fs.ErrExist}
  116. }
  117. continue
  118. }
  119. if errors.Is(err, fs.ErrNotExist) {
  120. return nil, &ErrNotSupported{err: err}
  121. }
  122. return
  123. }
  124. break
  125. }
  126. return file_mmap(f, size, WRITE, true, special_name)
  127. }
  128. func open(name string) (*os.File, error) {
  129. ans, err := os.OpenFile(file_path_from_name(name), os.O_RDONLY, 0)
  130. if err != nil {
  131. if errors.Is(err, fs.ErrNotExist) {
  132. if _, serr := os.Stat(SHM_DIR); serr != nil && errors.Is(serr, fs.ErrNotExist) {
  133. return nil, &ErrNotSupported{err: serr}
  134. }
  135. }
  136. return nil, err
  137. }
  138. return ans, nil
  139. }
  140. func Open(name string, size uint64) (MMap, error) {
  141. ans, err := open(name)
  142. if err != nil {
  143. return nil, err
  144. }
  145. if size == 0 {
  146. s, err := ans.Stat()
  147. if err != nil {
  148. ans.Close()
  149. return nil, fmt.Errorf("Failed to stat SHM file with error: %w", err)
  150. }
  151. size = uint64(s.Size())
  152. }
  153. return file_mmap(ans, size, READ, false, name)
  154. }