本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.6KB

  1. // Copyright ©2015 The Go Authors
  2. // Copyright ©2015 Steve Francia <spf@spf13.com>
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package afero
  16. import (
  17. "bytes"
  18. "io"
  19. "os"
  20. "path/filepath"
  21. "sort"
  22. "strconv"
  23. "sync"
  24. "time"
  25. )
  26. // byName implements sort.Interface.
  27. type byName []os.FileInfo
  28. func (f byName) Len() int { return len(f) }
  29. func (f byName) Less(i, j int) bool { return f[i].Name() < f[j].Name() }
  30. func (f byName) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
  31. // ReadDir reads the directory named by dirname and returns
  32. // a list of sorted directory entries.
  33. func (a Afero) ReadDir(dirname string) ([]os.FileInfo, error) {
  34. return ReadDir(a.Fs, dirname)
  35. }
  36. func ReadDir(fs Fs, dirname string) ([]os.FileInfo, error) {
  37. f, err := fs.Open(dirname)
  38. if err != nil {
  39. return nil, err
  40. }
  41. list, err := f.Readdir(-1)
  42. f.Close()
  43. if err != nil {
  44. return nil, err
  45. }
  46. sort.Sort(byName(list))
  47. return list, nil
  48. }
  49. // ReadFile reads the file named by filename and returns the contents.
  50. // A successful call returns err == nil, not err == EOF. Because ReadFile
  51. // reads the whole file, it does not treat an EOF from Read as an error
  52. // to be reported.
  53. func (a Afero) ReadFile(filename string) ([]byte, error) {
  54. return ReadFile(a.Fs, filename)
  55. }
  56. func ReadFile(fs Fs, filename string) ([]byte, error) {
  57. f, err := fs.Open(filename)
  58. if err != nil {
  59. return nil, err
  60. }
  61. defer f.Close()
  62. // It's a good but not certain bet that FileInfo will tell us exactly how much to
  63. // read, so let's try it but be prepared for the answer to be wrong.
  64. var n int64
  65. if fi, err := f.Stat(); err == nil {
  66. // Don't preallocate a huge buffer, just in case.
  67. if size := fi.Size(); size < 1e9 {
  68. n = size
  69. }
  70. }
  71. // As initial capacity for readAll, use n + a little extra in case Size is zero,
  72. // and to avoid another allocation after Read has filled the buffer. The readAll
  73. // call will read into its allocated internal buffer cheaply. If the size was
  74. // wrong, we'll either waste some space off the end or reallocate as needed, but
  75. // in the overwhelmingly common case we'll get it just right.
  76. return readAll(f, n+bytes.MinRead)
  77. }
  78. // readAll reads from r until an error or EOF and returns the data it read
  79. // from the internal buffer allocated with a specified capacity.
  80. func readAll(r io.Reader, capacity int64) (b []byte, err error) {
  81. buf := bytes.NewBuffer(make([]byte, 0, capacity))
  82. // If the buffer overflows, we will get bytes.ErrTooLarge.
  83. // Return that as an error. Any other panic remains.
  84. defer func() {
  85. e := recover()
  86. if e == nil {
  87. return
  88. }
  89. if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge {
  90. err = panicErr
  91. } else {
  92. panic(e)
  93. }
  94. }()
  95. _, err = buf.ReadFrom(r)
  96. return buf.Bytes(), err
  97. }
  98. // ReadAll reads from r until an error or EOF and returns the data it read.
  99. // A successful call returns err == nil, not err == EOF. Because ReadAll is
  100. // defined to read from src until EOF, it does not treat an EOF from Read
  101. // as an error to be reported.
  102. func ReadAll(r io.Reader) ([]byte, error) {
  103. return readAll(r, bytes.MinRead)
  104. }
  105. // WriteFile writes data to a file named by filename.
  106. // If the file does not exist, WriteFile creates it with permissions perm;
  107. // otherwise WriteFile truncates it before writing.
  108. func (a Afero) WriteFile(filename string, data []byte, perm os.FileMode) error {
  109. return WriteFile(a.Fs, filename, data, perm)
  110. }
  111. func WriteFile(fs Fs, filename string, data []byte, perm os.FileMode) error {
  112. f, err := fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
  113. if err != nil {
  114. return err
  115. }
  116. n, err := f.Write(data)
  117. if err == nil && n < len(data) {
  118. err = io.ErrShortWrite
  119. }
  120. if err1 := f.Close(); err == nil {
  121. err = err1
  122. }
  123. return err
  124. }
  125. // Random number state.
  126. // We generate random temporary file names so that there's a good
  127. // chance the file doesn't exist yet - keeps the number of tries in
  128. // TempFile to a minimum.
  129. var rand uint32
  130. var randmu sync.Mutex
  131. func reseed() uint32 {
  132. return uint32(time.Now().UnixNano() + int64(os.Getpid()))
  133. }
  134. func nextSuffix() string {
  135. randmu.Lock()
  136. r := rand
  137. if r == 0 {
  138. r = reseed()
  139. }
  140. r = r*1664525 + 1013904223 // constants from Numerical Recipes
  141. rand = r
  142. randmu.Unlock()
  143. return strconv.Itoa(int(1e9 + r%1e9))[1:]
  144. }
  145. // TempFile creates a new temporary file in the directory dir
  146. // with a name beginning with prefix, opens the file for reading
  147. // and writing, and returns the resulting *File.
  148. // If dir is the empty string, TempFile uses the default directory
  149. // for temporary files (see os.TempDir).
  150. // Multiple programs calling TempFile simultaneously
  151. // will not choose the same file. The caller can use f.Name()
  152. // to find the pathname of the file. It is the caller's responsibility
  153. // to remove the file when no longer needed.
  154. func (a Afero) TempFile(dir, prefix string) (f File, err error) {
  155. return TempFile(a.Fs, dir, prefix)
  156. }
  157. func TempFile(fs Fs, dir, prefix string) (f File, err error) {
  158. if dir == "" {
  159. dir = os.TempDir()
  160. }
  161. nconflict := 0
  162. for i := 0; i < 10000; i++ {
  163. name := filepath.Join(dir, prefix+nextSuffix())
  164. f, err = fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
  165. if os.IsExist(err) {
  166. if nconflict++; nconflict > 10 {
  167. randmu.Lock()
  168. rand = reseed()
  169. randmu.Unlock()
  170. }
  171. continue
  172. }
  173. break
  174. }
  175. return
  176. }
  177. // TempDir creates a new temporary directory in the directory dir
  178. // with a name beginning with prefix and returns the path of the
  179. // new directory. If dir is the empty string, TempDir uses the
  180. // default directory for temporary files (see os.TempDir).
  181. // Multiple programs calling TempDir simultaneously
  182. // will not choose the same directory. It is the caller's responsibility
  183. // to remove the directory when no longer needed.
  184. func (a Afero) TempDir(dir, prefix string) (name string, err error) {
  185. return TempDir(a.Fs, dir, prefix)
  186. }
  187. func TempDir(fs Fs, dir, prefix string) (name string, err error) {
  188. if dir == "" {
  189. dir = os.TempDir()
  190. }
  191. nconflict := 0
  192. for i := 0; i < 10000; i++ {
  193. try := filepath.Join(dir, prefix+nextSuffix())
  194. err = fs.Mkdir(try, 0700)
  195. if os.IsExist(err) {
  196. if nconflict++; nconflict > 10 {
  197. randmu.Lock()
  198. rand = reseed()
  199. randmu.Unlock()
  200. }
  201. continue
  202. }
  203. if err == nil {
  204. name = try
  205. }
  206. break
  207. }
  208. return
  209. }
上海开阖软件有限公司 沪ICP备12045867号-1