filestorage.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package storage
  2. import (
  3. gofs "io/fs"
  4. "os"
  5. gopath "path"
  6. "path/filepath"
  7. "github.com/opencost/opencost/pkg/log"
  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. entries, err := os.ReadDir(p)
  45. if err != nil {
  46. return nil, err
  47. }
  48. files := make([]gofs.FileInfo, 0, len(entries))
  49. for _, entry := range entries {
  50. info, err := entry.Info()
  51. if err != nil {
  52. return nil, err
  53. }
  54. files = append(files, info)
  55. }
  56. return FilesToStorageInfo(files), nil
  57. }
  58. func (fs *FileStorage) ListDirectories(path string) ([]*StorageInfo, error) {
  59. p := gopath.Join(fs.baseDir, path)
  60. // Read files in the backup path
  61. entries, err := os.ReadDir(p)
  62. if err != nil {
  63. return nil, err
  64. }
  65. files := make([]gofs.FileInfo, 0, len(entries))
  66. for _, entry := range entries {
  67. info, err := entry.Info()
  68. if err != nil {
  69. return nil, err
  70. }
  71. files = append(files, info)
  72. }
  73. return DirFilesToStorageInfo(files, path, fs.baseDir), nil
  74. }
  75. // Read uses the relative path of the storage combined with the provided path to
  76. // read the contents.
  77. func (fs *FileStorage) Read(path string) ([]byte, error) {
  78. f := gopath.Join(fs.baseDir, path)
  79. b, err := os.ReadFile(f)
  80. if err != nil {
  81. if os.IsNotExist(err) {
  82. return nil, DoesNotExistError
  83. }
  84. return nil, errors.Wrap(err, "Failed to read file")
  85. }
  86. return b, nil
  87. }
  88. // Write uses the relative path of the storage combined with the provided path
  89. // to write a new file or overwrite an existing file.
  90. func (fs *FileStorage) Write(path string, data []byte) error {
  91. f, err := fs.prepare(path)
  92. if err != nil {
  93. return errors.Wrap(err, "Failed to prepare path")
  94. }
  95. err = os.WriteFile(f, data, os.ModePerm)
  96. if err != nil {
  97. return errors.Wrap(err, "Failed to write file")
  98. }
  99. return nil
  100. }
  101. // Remove uses the relative path of the storage combined with the provided path to
  102. // remove a file from storage permanently.
  103. func (fs *FileStorage) Remove(path string) error {
  104. f := gopath.Join(fs.baseDir, path)
  105. err := os.Remove(f)
  106. if err != nil {
  107. if os.IsNotExist(err) {
  108. return DoesNotExistError
  109. }
  110. return errors.Wrap(err, "Failed to remove file")
  111. }
  112. return nil
  113. }
  114. // Exists uses the relative path of the storage combined with the provided path to
  115. // determine if the file exists.
  116. func (fs *FileStorage) Exists(path string) (bool, error) {
  117. f := gopath.Join(fs.baseDir, path)
  118. return fileutil.FileExists(f)
  119. }
  120. // prepare checks to see if the directory being written to should be created before writing
  121. // the file, and then returns the correct full path.
  122. func (fs *FileStorage) prepare(path string) (string, error) {
  123. f := gopath.Join(fs.baseDir, path)
  124. dir := filepath.Dir(f)
  125. if _, e := os.Stat(dir); e != nil && os.IsNotExist(e) {
  126. err := os.MkdirAll(dir, os.ModePerm)
  127. if err != nil {
  128. return "", err
  129. }
  130. }
  131. return f, nil
  132. }
  133. // FilesToStorageInfo maps a []fs.FileInfo to []*storage.StorageInfo
  134. func FilesToStorageInfo(fileInfo []gofs.FileInfo) []*StorageInfo {
  135. var stats []*StorageInfo
  136. for _, info := range fileInfo {
  137. stats = append(stats, FileToStorageInfo(info))
  138. }
  139. return stats
  140. }
  141. // FileToStorageInfo maps a fs.FileInfo to *storage.StorageInfo
  142. func FileToStorageInfo(fileInfo gofs.FileInfo) *StorageInfo {
  143. return &StorageInfo{
  144. Name: fileInfo.Name(),
  145. Size: fileInfo.Size(),
  146. ModTime: fileInfo.ModTime(),
  147. }
  148. }
  149. // DirFilesToStorageInfo maps a []fs.FileInfo to []*storage.StorageInfo
  150. // but only returning StorageInfo for directories
  151. func DirFilesToStorageInfo(fileInfo []gofs.FileInfo, relativePath string, basePath string) []*StorageInfo {
  152. var stats []*StorageInfo
  153. for _, info := range fileInfo {
  154. if info.IsDir() {
  155. stats = append(stats, &StorageInfo{
  156. Name: filepath.Join(relativePath, info.Name()),
  157. Size: info.Size(),
  158. ModTime: info.ModTime(),
  159. })
  160. } else if info.Mode()&os.ModeSymlink == os.ModeSymlink {
  161. targetPath, err := os.Readlink(filepath.Join(basePath, relativePath, info.Name()))
  162. if err != nil {
  163. log.Warnf("Converting files to storage info failed on supposed symlink that failed to be Readlink-ed (%s): %s", filepath.Join(basePath, relativePath, info.Name()), err)
  164. continue
  165. }
  166. if targetInfo, err := os.Stat(targetPath); err == nil {
  167. if targetInfo.IsDir() {
  168. stats = append(stats, &StorageInfo{
  169. // The Name added here is the path not readlink-ed
  170. Name: filepath.Join(relativePath, info.Name()),
  171. Size: info.Size(),
  172. ModTime: info.ModTime(),
  173. })
  174. }
  175. } else if errors.Is(err, os.ErrNotExist) {
  176. continue
  177. } else {
  178. log.Warnf("Converting files to storage info failed to stat target (%s) of symlink (%s): %s", targetPath, info.Name(), err)
  179. continue
  180. }
  181. }
  182. }
  183. return stats
  184. }