filestorage.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package storage
  2. import (
  3. gofs "io/fs"
  4. "io/ioutil"
  5. "os"
  6. gopath "path"
  7. "path/filepath"
  8. "github.com/kubecost/cost-model/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. // FullPath returns the storage working path combined with the path provided
  20. func (fs *FileStorage) FullPath(path string) string {
  21. return gopath.Join(fs.baseDir, path)
  22. }
  23. // Stat returns the StorageStats for the specific path.
  24. func (fs *FileStorage) Stat(path string) (*StorageInfo, error) {
  25. f := gopath.Join(fs.baseDir, path)
  26. st, err := os.Stat(f)
  27. if err != nil {
  28. if os.IsNotExist(err) {
  29. return nil, DoesNotExistError
  30. }
  31. return nil, errors.Wrap(err, "Failed to stat file")
  32. }
  33. return FileToStorageInfo(st), nil
  34. }
  35. // List uses the relative path of the storage combined with the provided path to return
  36. // storage information for the files.
  37. func (fs *FileStorage) List(path string) ([]*StorageInfo, error) {
  38. p := gopath.Join(fs.baseDir, path)
  39. // Read files in the backup path
  40. files, err := ioutil.ReadDir(p)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return FilesToStorageInfo(files), nil
  45. }
  46. // Read uses the relative path of the storage combined with the provided path to
  47. // read the contents.
  48. func (fs *FileStorage) Read(path string) ([]byte, error) {
  49. f := gopath.Join(fs.baseDir, path)
  50. b, err := ioutil.ReadFile(f)
  51. if err != nil {
  52. if os.IsNotExist(err) {
  53. return nil, DoesNotExistError
  54. }
  55. return nil, errors.Wrap(err, "Failed to read file")
  56. }
  57. return b, nil
  58. }
  59. // Write uses the relative path of the storage combined with the provided path
  60. // to write a new file or overwrite an existing file.
  61. func (fs *FileStorage) Write(path string, data []byte) error {
  62. f := fs.prepare(path)
  63. err := ioutil.WriteFile(f, data, os.ModePerm)
  64. if err != nil {
  65. return errors.Wrap(err, "Failed to write file")
  66. }
  67. return nil
  68. }
  69. // Remove uses the relative path of the storage combined with the provided path to
  70. // remove a file from storage permanently.
  71. func (fs *FileStorage) Remove(path string) error {
  72. f := gopath.Join(fs.baseDir, path)
  73. err := os.Remove(f)
  74. if err != nil {
  75. if os.IsNotExist(err) {
  76. return DoesNotExistError
  77. }
  78. return errors.Wrap(err, "Failed to remove file")
  79. }
  80. return nil
  81. }
  82. // Exists uses the relative path of the storage combined with the provided path to
  83. // determine if the file exists.
  84. func (fs *FileStorage) Exists(path string) (bool, error) {
  85. f := gopath.Join(fs.baseDir, path)
  86. return fileutil.FileExists(f)
  87. }
  88. // prepare checks to see if the directory being written to should be created before writing
  89. // the file, and then returns the correct full path.
  90. func (fs *FileStorage) prepare(path string) string {
  91. f := gopath.Join(fs.baseDir, path)
  92. dir := filepath.Dir(f)
  93. if _, e := os.Stat(dir); e != nil && os.IsNotExist(e) {
  94. os.MkdirAll(dir, os.ModePerm)
  95. }
  96. return f
  97. }
  98. // FilesToStorageInfo maps a []fs.FileInfo to []*storage.StorageInfo
  99. func FilesToStorageInfo(fileInfo []gofs.FileInfo) []*StorageInfo {
  100. var stats []*StorageInfo
  101. for _, info := range fileInfo {
  102. stats = append(stats, FileToStorageInfo(info))
  103. }
  104. return stats
  105. }
  106. // FileToStorageInfo maps a fs.FileInfo to *storage.StorageInfo
  107. func FileToStorageInfo(fileInfo gofs.FileInfo) *StorageInfo {
  108. return &StorageInfo{
  109. Name: fileInfo.Name(),
  110. Size: fileInfo.Size(),
  111. ModTime: fileInfo.ModTime(),
  112. }
  113. }