github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/storage/local_directory/storage.go (about) 1 package localdirectory 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "os" 8 "path/filepath" 9 10 "github.com/filecoin-project/bacalhau/pkg/model" 11 "github.com/filecoin-project/bacalhau/pkg/storage" 12 "github.com/filecoin-project/bacalhau/pkg/storage/util" 13 "github.com/filecoin-project/bacalhau/pkg/system" 14 "github.com/rs/zerolog/log" 15 ) 16 17 type StorageProvider struct { 18 LocalDirectoryPath string 19 } 20 21 func NewStorage(_ *system.CleanupManager, localDirectoryPath string) (*StorageProvider, error) { 22 storageHandler := &StorageProvider{ 23 LocalDirectoryPath: localDirectoryPath, 24 } 25 log.Debug().Msgf("Local directory driver createde: %s", localDirectoryPath) 26 27 // check if the localDirectoryPath exists and error if it doesn't 28 if _, err := os.Stat(localDirectoryPath); errors.Is(err, os.ErrNotExist) { 29 return nil, fmt.Errorf("local directory path %s does not exist", localDirectoryPath) 30 } 31 32 return storageHandler, nil 33 } 34 35 func (driver *StorageProvider) IsInstalled(context.Context) (bool, error) { 36 return true, nil 37 } 38 39 func (driver *StorageProvider) HasStorageLocally(_ context.Context, volume model.StorageSpec) (bool, error) { 40 localPath, err := driver.getPathToVolume(volume) 41 if err != nil { 42 return false, err 43 } 44 if _, err := os.Stat(localPath); errors.Is(err, os.ErrNotExist) { 45 return false, nil 46 } 47 return true, nil 48 } 49 50 func (driver *StorageProvider) GetVolumeSize(_ context.Context, volume model.StorageSpec) (uint64, error) { 51 localPath, err := driver.getPathToVolume(volume) 52 if err != nil { 53 return 0, err 54 } 55 return util.DirSize(localPath) 56 } 57 58 func (driver *StorageProvider) PrepareStorage( 59 _ context.Context, 60 storageSpec model.StorageSpec, 61 ) (storage.StorageVolume, error) { 62 localPath, err := driver.getPathToVolume(storageSpec) 63 if err != nil { 64 return storage.StorageVolume{}, err 65 } 66 return storage.StorageVolume{ 67 Type: storage.StorageVolumeConnectorBind, 68 Source: localPath, 69 Target: storageSpec.Path, 70 }, nil 71 } 72 73 func (driver *StorageProvider) CleanupStorage(context.Context, model.StorageSpec, storage.StorageVolume) error { 74 return nil 75 } 76 77 func (driver *StorageProvider) Upload(context.Context, string) (model.StorageSpec, error) { 78 return model.StorageSpec{}, fmt.Errorf("not implemented") 79 } 80 81 func (driver *StorageProvider) Explode(_ context.Context, spec model.StorageSpec) ([]model.StorageSpec, error) { 82 return []model.StorageSpec{ 83 spec, 84 }, nil 85 } 86 87 func (driver *StorageProvider) getPathToVolume(volume model.StorageSpec) (string, error) { 88 // join the driver.LocalDirectoryPath with the volume.SourcePath 89 // use the os.PathSeparator to make sure we are using the correct separator for the OS 90 localPath := filepath.Join(driver.LocalDirectoryPath, volume.SourcePath) 91 return localPath, nil 92 } 93 94 // Compile time interface check: 95 var _ storage.Storage = (*StorageProvider)(nil)