zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/pkg/storage/s3/driver.go (about)

     1  package s3
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  
     7  	// Add s3 support.
     8  	"github.com/docker/distribution/registry/storage/driver"
     9  	_ "github.com/docker/distribution/registry/storage/driver/s3-aws"
    10  
    11  	storageConstants "zotregistry.dev/zot/pkg/storage/constants"
    12  )
    13  
    14  type Driver struct {
    15  	store driver.StorageDriver
    16  }
    17  
    18  func New(storeDriver driver.StorageDriver) *Driver {
    19  	return &Driver{store: storeDriver}
    20  }
    21  
    22  func (driver *Driver) Name() string {
    23  	return storageConstants.S3StorageDriverName
    24  }
    25  
    26  func (driver *Driver) EnsureDir(path string) error {
    27  	return nil
    28  }
    29  
    30  func (driver *Driver) DirExists(path string) bool {
    31  	if fi, err := driver.store.Stat(context.Background(), path); err == nil && fi.IsDir() {
    32  		return true
    33  	}
    34  
    35  	return false
    36  }
    37  
    38  func (driver *Driver) Reader(path string, offset int64) (io.ReadCloser, error) {
    39  	return driver.store.Reader(context.Background(), path, offset)
    40  }
    41  
    42  func (driver *Driver) ReadFile(path string) ([]byte, error) {
    43  	return driver.store.GetContent(context.Background(), path)
    44  }
    45  
    46  func (driver *Driver) Delete(path string) error {
    47  	return driver.store.Delete(context.Background(), path)
    48  }
    49  
    50  func (driver *Driver) Stat(path string) (driver.FileInfo, error) {
    51  	return driver.store.Stat(context.Background(), path)
    52  }
    53  
    54  func (driver *Driver) Writer(filepath string, append bool) (driver.FileWriter, error) { //nolint:predeclared
    55  	return driver.store.Writer(context.Background(), filepath, append)
    56  }
    57  
    58  func (driver *Driver) WriteFile(filepath string, content []byte) (int, error) {
    59  	var n int
    60  
    61  	if stwr, err := driver.store.Writer(context.Background(), filepath, false); err == nil {
    62  		defer stwr.Close()
    63  
    64  		if n, err = stwr.Write(content); err != nil {
    65  			return -1, err
    66  		}
    67  
    68  		if err := stwr.Commit(); err != nil {
    69  			return -1, err
    70  		}
    71  	} else {
    72  		return -1, err
    73  	}
    74  
    75  	return n, nil
    76  }
    77  
    78  func (driver *Driver) Walk(path string, f driver.WalkFn) error {
    79  	return driver.store.Walk(context.Background(), path, f)
    80  }
    81  
    82  func (driver *Driver) List(fullpath string) ([]string, error) {
    83  	return driver.store.List(context.Background(), fullpath)
    84  }
    85  
    86  func (driver *Driver) Move(sourcePath string, destPath string) error {
    87  	return driver.store.Move(context.Background(), sourcePath, destPath)
    88  }
    89  
    90  func (driver *Driver) SameFile(path1, path2 string) bool {
    91  	fi1, _ := driver.store.Stat(context.Background(), path1)
    92  
    93  	fi2, _ := driver.store.Stat(context.Background(), path2)
    94  
    95  	if fi1 != nil && fi2 != nil {
    96  		if fi1.IsDir() == fi2.IsDir() &&
    97  			fi1.ModTime() == fi2.ModTime() &&
    98  			fi1.Path() == fi2.Path() &&
    99  			fi1.Size() == fi2.Size() {
   100  			return true
   101  		}
   102  	}
   103  
   104  	return false
   105  }
   106  
   107  /*
   108  	Link put an empty file that will act like a link between the original file and deduped one
   109  
   110  because s3 doesn't support symlinks, wherever the storage will encounter an empty file, it will get the original one
   111  from cache.
   112  */
   113  func (driver *Driver) Link(src, dest string) error {
   114  	return driver.store.PutContent(context.Background(), dest, []byte{})
   115  }