本站源代码
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.

140 lines
2.9KB

  1. // Package osfs provides a billy filesystem for the OS.
  2. package osfs // import "gopkg.in/src-d/go-billy.v4/osfs"
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "sync"
  8. "gopkg.in/src-d/go-billy.v4"
  9. "gopkg.in/src-d/go-billy.v4/helper/chroot"
  10. )
  11. const (
  12. defaultDirectoryMode = 0755
  13. defaultCreateMode = 0666
  14. )
  15. // OS is a filesystem based on the os filesystem.
  16. type OS struct{}
  17. // New returns a new OS filesystem.
  18. func New(baseDir string) billy.Filesystem {
  19. return chroot.New(&OS{}, baseDir)
  20. }
  21. func (fs *OS) Create(filename string) (billy.File, error) {
  22. return fs.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, defaultCreateMode)
  23. }
  24. func (fs *OS) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) {
  25. if flag&os.O_CREATE != 0 {
  26. if err := fs.createDir(filename); err != nil {
  27. return nil, err
  28. }
  29. }
  30. f, err := os.OpenFile(filename, flag, perm)
  31. if err != nil {
  32. return nil, err
  33. }
  34. return &file{File: f}, err
  35. }
  36. func (fs *OS) createDir(fullpath string) error {
  37. dir := filepath.Dir(fullpath)
  38. if dir != "." {
  39. if err := os.MkdirAll(dir, defaultDirectoryMode); err != nil {
  40. return err
  41. }
  42. }
  43. return nil
  44. }
  45. func (fs *OS) ReadDir(path string) ([]os.FileInfo, error) {
  46. l, err := ioutil.ReadDir(path)
  47. if err != nil {
  48. return nil, err
  49. }
  50. var s = make([]os.FileInfo, len(l))
  51. for i, f := range l {
  52. s[i] = f
  53. }
  54. return s, nil
  55. }
  56. func (fs *OS) Rename(from, to string) error {
  57. if err := fs.createDir(to); err != nil {
  58. return err
  59. }
  60. return os.Rename(from, to)
  61. }
  62. func (fs *OS) MkdirAll(path string, perm os.FileMode) error {
  63. return os.MkdirAll(path, defaultDirectoryMode)
  64. }
  65. func (fs *OS) Open(filename string) (billy.File, error) {
  66. return fs.OpenFile(filename, os.O_RDONLY, 0)
  67. }
  68. func (fs *OS) Stat(filename string) (os.FileInfo, error) {
  69. return os.Stat(filename)
  70. }
  71. func (fs *OS) Remove(filename string) error {
  72. return os.Remove(filename)
  73. }
  74. func (fs *OS) TempFile(dir, prefix string) (billy.File, error) {
  75. if err := fs.createDir(dir + string(os.PathSeparator)); err != nil {
  76. return nil, err
  77. }
  78. f, err := ioutil.TempFile(dir, prefix)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return &file{File: f}, nil
  83. }
  84. func (fs *OS) Join(elem ...string) string {
  85. return filepath.Join(elem...)
  86. }
  87. func (fs *OS) RemoveAll(path string) error {
  88. return os.RemoveAll(filepath.Clean(path))
  89. }
  90. func (fs *OS) Lstat(filename string) (os.FileInfo, error) {
  91. return os.Lstat(filepath.Clean(filename))
  92. }
  93. func (fs *OS) Symlink(target, link string) error {
  94. if err := fs.createDir(link); err != nil {
  95. return err
  96. }
  97. return os.Symlink(target, link)
  98. }
  99. func (fs *OS) Readlink(link string) (string, error) {
  100. return os.Readlink(link)
  101. }
  102. // Capabilities implements the Capable interface.
  103. func (fs *OS) Capabilities() billy.Capability {
  104. return billy.DefaultCapabilities
  105. }
  106. // file is a wrapper for an os.File which adds support for file locking.
  107. type file struct {
  108. *os.File
  109. m sync.Mutex
  110. }
上海开阖软件有限公司 沪ICP备12045867号-1