2019-03-11 08:06:56 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package filestore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// blobReader implements reading blobs
|
|
|
|
type blobReader struct {
|
|
|
|
*os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBlobReader(file *os.File) *blobReader {
|
|
|
|
return &blobReader{file}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns how large is the blob.
|
|
|
|
func (blob *blobReader) Size() (int64, error) {
|
|
|
|
stat, err := blob.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return stat.Size(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// blobWriter implements writing blobs
|
|
|
|
type blobWriter struct {
|
2019-03-18 14:29:54 +00:00
|
|
|
ref storage.BlobRef
|
|
|
|
store *Store
|
|
|
|
closed bool
|
2019-03-11 08:06:56 +00:00
|
|
|
|
|
|
|
*os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBlobWriter(ref storage.BlobRef, store *Store, file *os.File) *blobWriter {
|
2019-03-18 14:29:54 +00:00
|
|
|
return &blobWriter{ref, store, false, file}
|
2019-03-11 08:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel discards the blob.
|
|
|
|
func (blob *blobWriter) Cancel() error {
|
2019-03-18 14:29:54 +00:00
|
|
|
if blob.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
blob.closed = true
|
2019-03-11 08:06:56 +00:00
|
|
|
err := blob.File.Close()
|
|
|
|
removeErr := os.Remove(blob.File.Name())
|
|
|
|
return Error.Wrap(errs.Combine(err, removeErr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit moves the file to the target location.
|
|
|
|
func (blob *blobWriter) Commit() error {
|
2019-03-18 14:29:54 +00:00
|
|
|
if blob.closed {
|
|
|
|
return Error.New("already closed")
|
|
|
|
}
|
|
|
|
blob.closed = true
|
2019-03-11 08:06:56 +00:00
|
|
|
err := blob.store.dir.Commit(blob.File, blob.ref)
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns how much has been written so far.
|
|
|
|
func (blob *blobWriter) Size() (int64, error) {
|
|
|
|
pos, err := blob.Seek(0, io.SeekCurrent)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return pos, err
|
|
|
|
}
|