900f67e3d0
* Implement psclient interface * Add string method to pieceID type * try to fix linter errors * Whoops missed an error * More linter errors * Typo * Lol double typo * Get everything working, begin adding tests for psclient rpc * goimports * Forgot to change the piecestore cli when changed the piecestore code * Fix CLI * remove ID length, added validator to pieceID * Move grpc ranger to client Change client PUT api to take a reader rather than return a writer * GRPCRanger -> PieceRanger; Make PieceRanger a RangeCloser * Forgot to remove offset * Added message upon successful store * Do that thing dennis and kaloyan wanted * goimports * Make closeConn a part of the interface for psclient * Use interface * Removed uneccessary new lines * goimport * Whoops * Actually we don't want to use the interface in Piece Ranger * Renamed piecestore in examples to piecestore-client; moved piecestore-cli to examples * Make comments look nicer
128 lines
3.0 KiB
Go
128 lines
3.0 KiB
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package pstore
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"storj.io/storj/pkg/ranger"
|
|
)
|
|
|
|
// IDLength -- Minimum ID length
|
|
const IDLength = 20
|
|
|
|
// Errors
|
|
var (
|
|
ArgError = errs.Class("argError")
|
|
FSError = errs.Class("fsError")
|
|
)
|
|
|
|
// PathByID creates datapath from id and dir
|
|
func PathByID(id, dir string) (string, error) {
|
|
if len(id) < IDLength {
|
|
return "", ArgError.New("Invalid id length")
|
|
}
|
|
if dir == "" {
|
|
return "", ArgError.New("No path provided")
|
|
}
|
|
|
|
folder1 := string(id[0:2])
|
|
folder2 := string(id[2:4])
|
|
fileName := string(id[4:])
|
|
|
|
return path.Join(dir, folder1, folder2, fileName), nil
|
|
}
|
|
|
|
// StoreWriter stores data into piece store in multiple writes
|
|
// id is the id of the data to be stored
|
|
// dir is the pstore directory containing all other data stored
|
|
// returns error if failed and nil if successful
|
|
func StoreWriter(id string, dir string) (io.WriteCloser, error) {
|
|
dataPath, err := PathByID(id, dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Create directory path on file system
|
|
if err = os.MkdirAll(filepath.Dir(dataPath), 0700); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Create File on file system
|
|
return os.OpenFile(dataPath, os.O_RDWR|os.O_CREATE, 0755)
|
|
}
|
|
|
|
// RetrieveReader retrieves data from pstore directory
|
|
// id is the id of the stored data
|
|
// offset is the offset of the data that you are reading. Useful for multiple connections to split the data transfer
|
|
// length is the amount of data to read. Read all data if -1
|
|
// dir is the pstore directory containing all other data stored
|
|
// returns error if failed and nil if successful
|
|
func RetrieveReader(ctx context.Context, id string, offset int64, length int64, dir string) (io.ReadCloser, error) {
|
|
dataPath, err := PathByID(id, dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fileInfo, err := os.Stat(dataPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// If offset is greater than file size return
|
|
if offset >= fileInfo.Size() || offset < 0 {
|
|
return nil, ArgError.New("Invalid offset: %v", offset)
|
|
}
|
|
|
|
// If length less than 0 read the entire file
|
|
if length <= -1 {
|
|
length = fileInfo.Size()
|
|
}
|
|
|
|
// If trying to read past the end of the file, just read to the end
|
|
if fileInfo.Size() < offset+length {
|
|
length = fileInfo.Size() - offset
|
|
}
|
|
|
|
dataFile, err := os.OpenFile(dataPath, os.O_RDONLY, 0755)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Created a section reader so that we can concurrently retrieve the same file.
|
|
rr, err := ranger.FileHandleRanger(dataFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return rr.Range(ctx, offset, length)
|
|
}
|
|
|
|
// Delete deletes data from farmer
|
|
// id is the id of the data to be stored
|
|
// dir is the pstore directory containing all other data stored
|
|
// returns error if failed and nil if successful
|
|
func Delete(id string, dir string) error {
|
|
dataPath, err := PathByID(id, dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err = os.Stat(dataPath); os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
|
|
if err = os.Remove(dataPath); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|