filestorage.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package storage
  2. import (
  3. gofs "io/fs"
  4. "io/ioutil"
  5. "os"
  6. gopath "path"
  7. "path/filepath"
  8. "github.com/opencost/opencost/pkg/util/fileutil"
  9. "github.com/pkg/errors"
  10. )
  11. // FileStorage leverages the file system to write data to disk.
  12. type FileStorage struct {
  13. baseDir string
  14. }
  15. // NewFileStorage returns a new storage API which leverages the file system.
  16. func NewFileStorage(baseDir string) Storage {
  17. return &FileStorage{baseDir}
  18. }
  19. // StorageType returns a string identifier for the type of storage used by the implementation.
  20. func (fs *FileStorage) StorageType() StorageType {
  21. return StorageTypeFile
  22. }
  23. // FullPath returns the storage working path combined with the path provided
  24. func (fs *FileStorage) FullPath(path string) string {
  25. return gopath.Join(fs.baseDir, path)
  26. }
  27. // Stat returns the StorageStats for the specific path.
  28. func (fs *FileStorage) Stat(path string) (*StorageInfo, error) {
  29. f := gopath.Join(fs.baseDir, path)
  30. st, err := os.Stat(f)
  31. if err != nil {
  32. if os.IsNotExist(err) {
  33. return nil, DoesNotExistError
  34. }
  35. return nil, errors.Wrap(err, "Failed to stat file")
  36. }
  37. return FileToStorageInfo(st), nil
  38. }
  39. // List uses the relative path of the storage combined with the provided path to return
  40. // storage information for the files.
  41. func (fs *FileStorage) List(path string) ([]*StorageInfo, error) {
  42. p := gopath.Join(fs.baseDir, path)
  43. // Read files in the backup path
  44. files, err := ioutil.ReadDir(p)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return FilesToStorageInfo(files), nil
  49. }
  50. func (fs *FileStorage) ListDirectories(path string) ([]*StorageInfo, error) {
  51. p := gopath.Join(fs.baseDir, path)
  52. // Read files in the backup path
  53. files, err := ioutil.ReadDir(p)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return DirFilesToStorageInfo(files, path), nil
  58. }
  59. // Read uses the relative path of the storage combined with the provided path to
  60. // read the contents.
  61. func (fs *FileStorage) Read(path string) ([]byte, error) {
  62. f := gopath.Join(fs.baseDir, path)
  63. b, err := ioutil.ReadFile(f)
  64. if err != nil {
  65. if os.IsNotExist(err) {
  66. return nil, DoesNotExistError
  67. }
  68. return nil, errors.Wrap(err, "Failed to read file")
  69. }
  70. return b, nil
  71. }
  72. // Write uses the relative path of the storage combined with the provided path
  73. // to write a new file or overwrite an existing file.
  74. func (fs *FileStorage) Write(path string, data []byte) error {
  75. f, err := fs.prepare(path)
  76. if err != nil {
  77. return errors.Wrap(err, "Failed to prepare path")
  78. }
  79. err = ioutil.WriteFile(f, data, os.ModePerm)
  80. if err != nil {
  81. return errors.Wrap(err, "Failed to write file")
  82. }
  83. return nil
  84. }
  85. // Remove uses the relative path of the storage combined with the provided path to
  86. // remove a file from storage permanently.
  87. func (fs *FileStorage) Remove(path string) error {
  88. f := gopath.Join(fs.baseDir, path)
  89. err := os.Remove(f)
  90. if err != nil {
  91. if os.IsNotExist(err) {
  92. return DoesNotExistError
  93. }
  94. return errors.Wrap(err, "Failed to remove file")
  95. }
  96. return nil
  97. }
  98. // Exists uses the relative path of the storage combined with the provided path to
  99. // determine if the file exists.
  100. func (fs *FileStorage) Exists(path string) (bool, error) {
  101. f := gopath.Join(fs.baseDir, path)
  102. return fileutil.FileExists(f)
  103. }
  104. // prepare checks to see if the directory being written to should be created before writing
  105. // the file, and then returns the correct full path.
  106. func (fs *FileStorage) prepare(path string) (string, error) {
  107. f := gopath.Join(fs.baseDir, path)
  108. dir := filepath.Dir(f)
  109. if _, e := os.Stat(dir); e != nil && os.IsNotExist(e) {
  110. err := os.MkdirAll(dir, os.ModePerm)
  111. if err != nil {
  112. return "", err
  113. }
  114. }
  115. return f, nil
  116. }
  117. // FilesToStorageInfo maps a []fs.FileInfo to []*storage.StorageInfo
  118. func FilesToStorageInfo(fileInfo []gofs.FileInfo) []*StorageInfo {
  119. var stats []*StorageInfo
  120. for _, info := range fileInfo {
  121. stats = append(stats, FileToStorageInfo(info))
  122. }
  123. return stats
  124. }
  125. // FileToStorageInfo maps a fs.FileInfo to *storage.StorageInfo
  126. func FileToStorageInfo(fileInfo gofs.FileInfo) *StorageInfo {
  127. return &StorageInfo{
  128. Name: fileInfo.Name(),
  129. Size: fileInfo.Size(),
  130. ModTime: fileInfo.ModTime(),
  131. }
  132. }
  133. // DirFilesToStorageInfo maps a []fs.FileInfo to []*storage.StorageInfo
  134. // but only returning StorageInfo for directories
  135. func DirFilesToStorageInfo(fileInfo []gofs.FileInfo, path string) []*StorageInfo {
  136. var stats []*StorageInfo
  137. for _, info := range fileInfo {
  138. if info.IsDir() {
  139. stats = append(stats, &StorageInfo{
  140. Name: filepath.Join(path, info.Name()),
  141. Size: info.Size(),
  142. ModTime: info.ModTime(),
  143. })
  144. }
  145. }
  146. return stats
  147. }