package storage import ( "errors" "fmt" "io" "os" "path/filepath" ) // Open validates the digest and returns a ReadCloser for the blob. // Returns ErrBlobNotFound if the blob does not exist on disk. func (s *Store) Open(digest string) (io.ReadCloser, error) { if err := validateDigest(digest); err != nil { return nil, err } f, err := os.Open(s.blobPath(digest)) if err != nil { if errors.Is(err, os.ErrNotExist) { return nil, ErrBlobNotFound } return nil, fmt.Errorf("storage: open blob: %w", err) } return f, nil } // Stat returns the size of the blob in bytes. // Returns ErrBlobNotFound if the blob does not exist on disk. func (s *Store) Stat(digest string) (int64, error) { if err := validateDigest(digest); err != nil { return 0, err } info, err := os.Stat(s.blobPath(digest)) if err != nil { if errors.Is(err, os.ErrNotExist) { return 0, ErrBlobNotFound } return 0, fmt.Errorf("storage: stat blob: %w", err) } return info.Size(), nil } // Delete removes the blob file and attempts to clean up its prefix // directory. Non-empty or already-removed prefix directories are // silently ignored. func (s *Store) Delete(digest string) error { if err := validateDigest(digest); err != nil { return err } path := s.blobPath(digest) if err := os.Remove(path); err != nil { if errors.Is(err, os.ErrNotExist) { return ErrBlobNotFound } return fmt.Errorf("storage: delete blob: %w", err) } // Best-effort cleanup of the prefix directory. _ = os.Remove(filepath.Dir(path)) return nil } // Exists reports whether the blob exists on disk. func (s *Store) Exists(digest string) bool { if err := validateDigest(digest); err != nil { return false } _, err := os.Stat(s.blobPath(digest)) return err == nil }