storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/storage-interface.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2016 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  )
    23  
    24  // StorageAPI interface.
    25  type StorageAPI interface {
    26  	// Stringified version of disk.
    27  	String() string
    28  
    29  	// Storage operations.
    30  	IsOnline() bool // Returns true if disk is online.
    31  	IsLocal() bool
    32  
    33  	Hostname() string   // Returns host name if remote host.
    34  	Endpoint() Endpoint // Returns endpoint.
    35  
    36  	Close() error
    37  	GetDiskID() (string, error)
    38  	SetDiskID(id string)
    39  	Healing() *healingTracker // Returns nil if disk is not healing.
    40  
    41  	DiskInfo(ctx context.Context) (info DiskInfo, err error)
    42  	NSScanner(ctx context.Context, cache dataUsageCache) (dataUsageCache, error)
    43  
    44  	// Volume operations.
    45  	MakeVol(ctx context.Context, volume string) (err error)
    46  	MakeVolBulk(ctx context.Context, volumes ...string) (err error)
    47  	ListVols(ctx context.Context) (vols []VolInfo, err error)
    48  	StatVol(ctx context.Context, volume string) (vol VolInfo, err error)
    49  	DeleteVol(ctx context.Context, volume string, forceDelete bool) (err error)
    50  
    51  	// WalkDir will walk a directory on disk and return a metacache stream on wr.
    52  	WalkDir(ctx context.Context, opts WalkDirOptions, wr io.Writer) error
    53  
    54  	// Metadata operations
    55  	DeleteVersion(ctx context.Context, volume, path string, fi FileInfo, forceDelMarker bool) error
    56  	DeleteVersions(ctx context.Context, volume string, versions []FileInfo) []error
    57  	WriteMetadata(ctx context.Context, volume, path string, fi FileInfo) error
    58  	UpdateMetadata(ctx context.Context, volume, path string, fi FileInfo) error
    59  	ReadVersion(ctx context.Context, volume, path, versionID string, readData bool) (FileInfo, error)
    60  	RenameData(ctx context.Context, srcVolume, srcPath string, fi FileInfo, dstVolume, dstPath string) error
    61  
    62  	// File operations.
    63  	ListDir(ctx context.Context, volume, dirPath string, count int) ([]string, error)
    64  	ReadFile(ctx context.Context, volume string, path string, offset int64, buf []byte, verifier *BitrotVerifier) (n int64, err error)
    65  	AppendFile(ctx context.Context, volume string, path string, buf []byte) (err error)
    66  	CreateFile(ctx context.Context, volume, path string, size int64, reader io.Reader) error
    67  	ReadFileStream(ctx context.Context, volume, path string, offset, length int64) (io.ReadCloser, error)
    68  	RenameFile(ctx context.Context, srcVolume, srcPath, dstVolume, dstPath string) error
    69  	CheckParts(ctx context.Context, volume string, path string, fi FileInfo) error
    70  	CheckFile(ctx context.Context, volume string, path string) (err error)
    71  	Delete(ctx context.Context, volume string, path string, recursive bool) (err error)
    72  	VerifyFile(ctx context.Context, volume, path string, fi FileInfo) error
    73  
    74  	// Write all data, syncs the data to disk.
    75  	// Should be used for smaller payloads.
    76  	WriteAll(ctx context.Context, volume string, path string, b []byte) (err error)
    77  
    78  	// Read all.
    79  	ReadAll(ctx context.Context, volume string, path string) (buf []byte, err error)
    80  
    81  	GetDiskLoc() (poolIdx, setIdx, diskIdx int) // Retrieve location indexes.
    82  	SetDiskLoc(poolIdx, setIdx, diskIdx int)    // Set location indexes.
    83  }
    84  
    85  // storageReader is an io.Reader view of a disk
    86  type storageReader struct {
    87  	storage      StorageAPI
    88  	volume, path string
    89  	offset       int64
    90  }
    91  
    92  func (r *storageReader) Read(p []byte) (n int, err error) {
    93  	nn, err := r.storage.ReadFile(context.TODO(), r.volume, r.path, r.offset, p, nil)
    94  	r.offset += nn
    95  	n = int(nn)
    96  
    97  	if err == io.ErrUnexpectedEOF && nn > 0 {
    98  		err = io.EOF
    99  	}
   100  	return
   101  }
   102  
   103  // storageWriter is a io.Writer view of a disk.
   104  type storageWriter struct {
   105  	storage      StorageAPI
   106  	volume, path string
   107  }
   108  
   109  func (w *storageWriter) Write(p []byte) (n int, err error) {
   110  	err = w.storage.AppendFile(context.TODO(), w.volume, w.path, p)
   111  	if err == nil {
   112  		n = len(p)
   113  	}
   114  	return
   115  }
   116  
   117  // StorageWriter returns a new io.Writer which appends data to the file
   118  // at the given disk, volume and path.
   119  func StorageWriter(storage StorageAPI, volume, path string) io.Writer {
   120  	return &storageWriter{storage, volume, path}
   121  }
   122  
   123  // StorageReader returns a new io.Reader which reads data to the file
   124  // at the given disk, volume, path and offset.
   125  func StorageReader(storage StorageAPI, volume, path string, offset int64) io.Reader {
   126  	return &storageReader{storage, volume, path, offset}
   127  }