filestorage.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package storage
  2. import (
  3. "fmt"
  4. gofs "io/fs"
  5. "os"
  6. gopath "path"
  7. "path/filepath"
  8. "github.com/opencost/opencost/core/pkg/log"
  9. "github.com/opencost/opencost/core/pkg/util/fileutil"
  10. "github.com/pkg/errors"
  11. )
  12. // FileStorage leverages the file system to write data to disk.
  13. type FileStorage struct {
  14. baseDir string
  15. }
  16. // NewFileStorage returns a new storage API which leverages the file system.
  17. func NewFileStorage(baseDir string) Storage {
  18. return &FileStorage{baseDir}
  19. }
  20. // String returns the base directory for the file storage.
  21. func (fs *FileStorage) String() string {
  22. return fs.baseDir
  23. }
  24. // StorageType returns a string identifier for the type of storage used by the implementation.
  25. func (fs *FileStorage) StorageType() StorageType {
  26. return StorageTypeFile
  27. }
  28. // FullPath returns the storage working path combined with the path provided
  29. func (fs *FileStorage) FullPath(path string) string {
  30. return gopath.Join(fs.baseDir, path)
  31. }
  32. // Stat returns the StorageStats for the specific path.
  33. func (fs *FileStorage) Stat(path string) (*StorageInfo, error) {
  34. f := gopath.Join(fs.baseDir, path)
  35. st, err := os.Stat(f)
  36. if err != nil {
  37. if os.IsNotExist(err) {
  38. return nil, DoesNotExistError
  39. }
  40. return nil, errors.Wrap(err, "Failed to stat file")
  41. }
  42. return FileToStorageInfo(st), nil
  43. }
  44. // List uses the relative path of the storage combined with the provided path to return
  45. // storage information for the files.
  46. func (fs *FileStorage) List(path string) ([]*StorageInfo, error) {
  47. p := gopath.Join(fs.baseDir, path)
  48. // Read files in the backup path
  49. entries, err := os.ReadDir(p)
  50. if err != nil {
  51. if os.IsNotExist(err) {
  52. return []*StorageInfo{}, nil
  53. }
  54. return nil, err
  55. }
  56. files := make([]gofs.FileInfo, 0, len(entries))
  57. for _, entry := range entries {
  58. info, err := entry.Info()
  59. if err != nil {
  60. return nil, err
  61. }
  62. files = append(files, info)
  63. }
  64. return FilesToStorageInfo(files), nil
  65. }
  66. func (fs *FileStorage) ListDirectories(path string) ([]*StorageInfo, error) {
  67. p := gopath.Join(fs.baseDir, path)
  68. // Read files in the backup path
  69. entries, err := os.ReadDir(p)
  70. if err != nil {
  71. if os.IsNotExist(err) {
  72. return []*StorageInfo{}, nil
  73. }
  74. return nil, err
  75. }
  76. files := make([]gofs.FileInfo, 0, len(entries))
  77. for _, entry := range entries {
  78. info, err := entry.Info()
  79. if err != nil {
  80. return nil, err
  81. }
  82. files = append(files, info)
  83. }
  84. return DirFilesToStorageInfo(files, path, fs.baseDir), nil
  85. }
  86. // Read uses the relative path of the storage combined with the provided path to
  87. // read the contents.
  88. //
  89. // It takes advantage of flock() based locking to improve safety.
  90. func (fs *FileStorage) Read(path string) ([]byte, error) {
  91. f := gopath.Join(fs.baseDir, path)
  92. b, err := fileutil.ReadLocked(f)
  93. if err != nil {
  94. if errors.Is(err, os.ErrNotExist) {
  95. return nil, DoesNotExistError
  96. }
  97. return nil, fmt.Errorf("reading %s: %w", f, err)
  98. }
  99. return b, nil
  100. }
  101. // Write uses the relative path of the storage combined with the provided path
  102. // to write a new file or overwrite an existing file.
  103. //
  104. // It takes advantage of flock() based locking to improve safety.
  105. func (fs *FileStorage) Write(path string, data []byte) error {
  106. f, err := fs.prepare(path)
  107. if err != nil {
  108. return errors.Wrap(err, "Failed to prepare path")
  109. }
  110. if _, err := fileutil.WriteLocked(f, data); err != nil {
  111. return fmt.Errorf("writing %s: %w", f, err)
  112. }
  113. return nil
  114. }
  115. // Remove uses the relative path of the storage combined with the provided path to
  116. // remove a file from storage permanently.
  117. func (fs *FileStorage) Remove(path string) error {
  118. f := gopath.Join(fs.baseDir, path)
  119. err := os.Remove(f)
  120. if err != nil {
  121. if os.IsNotExist(err) {
  122. return DoesNotExistError
  123. }
  124. return errors.Wrap(err, "Failed to remove file")
  125. }
  126. return nil
  127. }
  128. // Exists uses the relative path of the storage combined with the provided path to
  129. // determine if the file exists.
  130. func (fs *FileStorage) Exists(path string) (bool, error) {
  131. f := gopath.Join(fs.baseDir, path)
  132. return fileutil.FileExists(f)
  133. }
  134. // prepare checks to see if the directory being written to should be created before writing
  135. // the file, and then returns the correct full path.
  136. func (fs *FileStorage) prepare(path string) (string, error) {
  137. f := gopath.Join(fs.baseDir, path)
  138. dir := filepath.Dir(f)
  139. if _, e := os.Stat(dir); e != nil && os.IsNotExist(e) {
  140. err := os.MkdirAll(dir, os.ModePerm)
  141. if err != nil {
  142. return "", err
  143. }
  144. }
  145. return f, nil
  146. }
  147. // FilesToStorageInfo maps a []fs.FileInfo to []*storage.StorageInfo
  148. func FilesToStorageInfo(fileInfo []gofs.FileInfo) []*StorageInfo {
  149. var stats []*StorageInfo
  150. for _, info := range fileInfo {
  151. if info.IsDir() {
  152. continue
  153. }
  154. stats = append(stats, FileToStorageInfo(info))
  155. }
  156. return stats
  157. }
  158. // FileToStorageInfo maps a fs.FileInfo to *storage.StorageInfo
  159. func FileToStorageInfo(fileInfo gofs.FileInfo) *StorageInfo {
  160. return &StorageInfo{
  161. Name: fileInfo.Name(),
  162. Size: fileInfo.Size(),
  163. ModTime: fileInfo.ModTime(),
  164. }
  165. }
  166. // DirFilesToStorageInfo maps a []fs.FileInfo to []*storage.StorageInfo
  167. // but only returning StorageInfo for directories
  168. func DirFilesToStorageInfo(fileInfo []gofs.FileInfo, relativePath string, basePath string) []*StorageInfo {
  169. var stats []*StorageInfo
  170. for _, info := range fileInfo {
  171. if info.IsDir() {
  172. stats = append(stats, &StorageInfo{
  173. Name: filepath.Join(relativePath, info.Name()),
  174. Size: info.Size(),
  175. ModTime: info.ModTime(),
  176. })
  177. } else if info.Mode()&os.ModeSymlink == os.ModeSymlink {
  178. targetPath, err := os.Readlink(filepath.Join(basePath, relativePath, info.Name()))
  179. if err != nil {
  180. 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)
  181. continue
  182. }
  183. if targetInfo, err := os.Stat(targetPath); err == nil {
  184. if targetInfo.IsDir() {
  185. stats = append(stats, &StorageInfo{
  186. // The Name added here is the path not readlink-ed
  187. Name: filepath.Join(relativePath, info.Name()),
  188. Size: info.Size(),
  189. ModTime: info.ModTime(),
  190. })
  191. }
  192. } else if errors.Is(err, os.ErrNotExist) {
  193. continue
  194. } else {
  195. log.Warnf("Converting files to storage info failed to stat target (%s) of symlink (%s): %s", targetPath, info.Name(), err)
  196. continue
  197. }
  198. }
  199. }
  200. return stats
  201. }