filestorage.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package storage
  2. import (
  3. gofs "io/fs"
  4. "io/ioutil"
  5. "os"
  6. gopath "path"
  7. "path/filepath"
  8. "github.com/kubecost/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. // Read uses the relative path of the storage combined with the provided path to
  51. // read the contents.
  52. func (fs *FileStorage) Read(path string) ([]byte, error) {
  53. f := gopath.Join(fs.baseDir, path)
  54. b, err := ioutil.ReadFile(f)
  55. if err != nil {
  56. if os.IsNotExist(err) {
  57. return nil, DoesNotExistError
  58. }
  59. return nil, errors.Wrap(err, "Failed to read file")
  60. }
  61. return b, nil
  62. }
  63. // Write uses the relative path of the storage combined with the provided path
  64. // to write a new file or overwrite an existing file.
  65. func (fs *FileStorage) Write(path string, data []byte) error {
  66. f, err := fs.prepare(path)
  67. if err != nil {
  68. return errors.Wrap(err, "Failed to prepare path")
  69. }
  70. err = ioutil.WriteFile(f, data, os.ModePerm)
  71. if err != nil {
  72. return errors.Wrap(err, "Failed to write file")
  73. }
  74. return nil
  75. }
  76. // Remove uses the relative path of the storage combined with the provided path to
  77. // remove a file from storage permanently.
  78. func (fs *FileStorage) Remove(path string) error {
  79. f := gopath.Join(fs.baseDir, path)
  80. err := os.Remove(f)
  81. if err != nil {
  82. if os.IsNotExist(err) {
  83. return DoesNotExistError
  84. }
  85. return errors.Wrap(err, "Failed to remove file")
  86. }
  87. return nil
  88. }
  89. // Exists uses the relative path of the storage combined with the provided path to
  90. // determine if the file exists.
  91. func (fs *FileStorage) Exists(path string) (bool, error) {
  92. f := gopath.Join(fs.baseDir, path)
  93. return fileutil.FileExists(f)
  94. }
  95. // prepare checks to see if the directory being written to should be created before writing
  96. // the file, and then returns the correct full path.
  97. func (fs *FileStorage) prepare(path string) (string, error) {
  98. f := gopath.Join(fs.baseDir, path)
  99. dir := filepath.Dir(f)
  100. if _, e := os.Stat(dir); e != nil && os.IsNotExist(e) {
  101. err := os.MkdirAll(dir, os.ModePerm)
  102. if err != nil {
  103. return "", err
  104. }
  105. }
  106. return f, nil
  107. }
  108. // FilesToStorageInfo maps a []fs.FileInfo to []*storage.StorageInfo
  109. func FilesToStorageInfo(fileInfo []gofs.FileInfo) []*StorageInfo {
  110. var stats []*StorageInfo
  111. for _, info := range fileInfo {
  112. stats = append(stats, FileToStorageInfo(info))
  113. }
  114. return stats
  115. }
  116. // FileToStorageInfo maps a fs.FileInfo to *storage.StorageInfo
  117. func FileToStorageInfo(fileInfo gofs.FileInfo) *StorageInfo {
  118. return &StorageInfo{
  119. Name: fileInfo.Name(),
  120. Size: fileInfo.Size(),
  121. ModTime: fileInfo.ModTime(),
  122. }
  123. }