filestorage.go 4.8 KB

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