2014-10-21 22:02:20 +00:00
|
|
|
package filesystem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/docker/docker-registry/storagedriver"
|
2014-10-29 01:15:40 +00:00
|
|
|
"github.com/docker/docker-registry/storagedriver/factory"
|
2014-10-21 22:02:20 +00:00
|
|
|
)
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
const driverName = "filesystem"
|
|
|
|
const defaultRootDirectory = "/tmp/registry/storage"
|
2014-10-29 01:15:40 +00:00
|
|
|
|
|
|
|
func init() {
|
2014-11-17 23:44:07 +00:00
|
|
|
factory.Register(driverName, &filesystemDriverFactory{})
|
2014-10-29 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// filesystemDriverFactory implements the factory.StorageDriverFactory interface
|
2014-10-29 01:15:40 +00:00
|
|
|
type filesystemDriverFactory struct{}
|
|
|
|
|
|
|
|
func (factory *filesystemDriverFactory) Create(parameters map[string]string) (storagedriver.StorageDriver, error) {
|
|
|
|
return FromParameters(parameters), nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Driver is a storagedriver.StorageDriver implementation backed by a local
|
|
|
|
// filesystem. All provided paths will be subpaths of the RootDirectory
|
|
|
|
type Driver struct {
|
2014-10-21 22:02:20 +00:00
|
|
|
rootDirectory string
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// FromParameters constructs a new Driver with a given parameters map
|
2014-10-29 01:15:40 +00:00
|
|
|
// Optional Parameters:
|
|
|
|
// - rootdirectory
|
2014-11-17 23:44:07 +00:00
|
|
|
func FromParameters(parameters map[string]string) *Driver {
|
|
|
|
var rootDirectory = defaultRootDirectory
|
2014-10-29 01:15:40 +00:00
|
|
|
if parameters != nil {
|
|
|
|
rootDir, ok := parameters["rootdirectory"]
|
|
|
|
if ok {
|
|
|
|
rootDirectory = rootDir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return New(rootDirectory)
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// New constructs a new Driver with a given rootDirectory
|
|
|
|
func New(rootDirectory string) *Driver {
|
|
|
|
return &Driver{rootDirectory}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// subPath returns the absolute path of a key within the Driver's storage
|
|
|
|
func (d *Driver) subPath(subPath string) string {
|
2014-10-21 22:02:20 +00:00
|
|
|
return path.Join(d.rootDirectory, subPath)
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Implement the storagedriver.StorageDriver interface
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// GetContent retrieves the content stored at "path" as a []byte.
|
|
|
|
func (d *Driver) GetContent(path string) ([]byte, error) {
|
2014-10-21 22:02:20 +00:00
|
|
|
contents, err := ioutil.ReadFile(d.subPath(path))
|
|
|
|
if err != nil {
|
2014-11-13 01:19:19 +00:00
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
return contents, nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// PutContent stores the []byte content at a location designated by "path".
|
|
|
|
func (d *Driver) PutContent(subPath string, contents []byte) error {
|
2014-10-21 22:02:20 +00:00
|
|
|
fullPath := d.subPath(subPath)
|
|
|
|
parentDir := path.Dir(fullPath)
|
|
|
|
err := os.MkdirAll(parentDir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(fullPath, contents, 0644)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// ReadStream retrieves an io.ReadCloser for the content stored at "path" with a
|
|
|
|
// given byte offset.
|
|
|
|
func (d *Driver) ReadStream(path string, offset uint64) (io.ReadCloser, error) {
|
2014-10-21 22:02:20 +00:00
|
|
|
file, err := os.OpenFile(d.subPath(path), os.O_RDONLY, 0644)
|
|
|
|
if err != nil {
|
2014-11-19 01:41:48 +00:00
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
seekPos, err := file.Seek(int64(offset), os.SEEK_SET)
|
|
|
|
if err != nil {
|
|
|
|
file.Close()
|
|
|
|
return nil, err
|
|
|
|
} else if seekPos < int64(offset) {
|
|
|
|
file.Close()
|
2014-11-13 01:19:19 +00:00
|
|
|
return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// WriteStream stores the contents of the provided io.ReadCloser at a location
|
|
|
|
// designated by the given path.
|
|
|
|
func (d *Driver) WriteStream(subPath string, offset, size uint64, reader io.ReadCloser) error {
|
2014-10-21 22:02:20 +00:00
|
|
|
defer reader.Close()
|
|
|
|
|
2014-11-07 20:58:48 +00:00
|
|
|
resumableOffset, err := d.CurrentSize(subPath)
|
2014-10-21 22:02:20 +00:00
|
|
|
if _, pathNotFound := err.(storagedriver.PathNotFoundError); err != nil && !pathNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset > resumableOffset {
|
2014-11-13 01:19:19 +00:00
|
|
|
return storagedriver.InvalidOffsetError{Path: subPath, Offset: offset}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fullPath := d.subPath(subPath)
|
|
|
|
parentDir := path.Dir(fullPath)
|
|
|
|
err = os.MkdirAll(parentDir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var file *os.File
|
|
|
|
if offset == 0 {
|
|
|
|
file, err = os.Create(fullPath)
|
|
|
|
} else {
|
|
|
|
file, err = os.OpenFile(fullPath, os.O_WRONLY|os.O_APPEND, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
buf := make([]byte, 32*1024)
|
|
|
|
for {
|
|
|
|
bytesRead, er := reader.Read(buf)
|
|
|
|
if bytesRead > 0 {
|
|
|
|
bytesWritten, ew := file.WriteAt(buf[0:bytesRead], int64(offset))
|
|
|
|
if bytesWritten > 0 {
|
|
|
|
offset += uint64(bytesWritten)
|
|
|
|
}
|
|
|
|
if ew != nil {
|
|
|
|
err = ew
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if bytesRead != bytesWritten {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if er == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
err = er
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// CurrentSize retrieves the curernt size in bytes of the object at the given
|
|
|
|
// path.
|
|
|
|
func (d *Driver) CurrentSize(subPath string) (uint64, error) {
|
2014-10-21 22:02:20 +00:00
|
|
|
fullPath := d.subPath(subPath)
|
|
|
|
|
|
|
|
fileInfo, err := os.Stat(fullPath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return 0, err
|
|
|
|
} else if err != nil {
|
2014-11-13 01:19:19 +00:00
|
|
|
return 0, storagedriver.PathNotFoundError{Path: subPath}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
return uint64(fileInfo.Size()), nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// List returns a list of the objects that are direct descendants of the given
|
|
|
|
// path.
|
|
|
|
func (d *Driver) List(subPath string) ([]string, error) {
|
2014-11-20 22:11:49 +00:00
|
|
|
if subPath[len(subPath)-1] != '/' {
|
|
|
|
subPath += "/"
|
|
|
|
}
|
2014-11-04 17:52:24 +00:00
|
|
|
fullPath := d.subPath(subPath)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
dir, err := os.Open(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileNames, err := dir.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make([]string, 0, len(fileNames))
|
|
|
|
for _, fileName := range fileNames {
|
2014-11-04 17:52:24 +00:00
|
|
|
keys = append(keys, path.Join(subPath, fileName))
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Move moves an object stored at sourcePath to destPath, removing the original
|
|
|
|
// object.
|
|
|
|
func (d *Driver) Move(sourcePath string, destPath string) error {
|
2014-11-19 01:41:48 +00:00
|
|
|
source := d.subPath(sourcePath)
|
|
|
|
dest := d.subPath(destPath)
|
|
|
|
|
|
|
|
if _, err := os.Stat(source); os.IsNotExist(err) {
|
|
|
|
return storagedriver.PathNotFoundError{Path: sourcePath}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.Rename(source, dest)
|
2014-10-21 22:02:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
|
|
|
func (d *Driver) Delete(subPath string) error {
|
2014-10-21 22:02:20 +00:00
|
|
|
fullPath := d.subPath(subPath)
|
|
|
|
|
|
|
|
_, err := os.Stat(fullPath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
} else if err != nil {
|
2014-11-13 01:19:19 +00:00
|
|
|
return storagedriver.PathNotFoundError{Path: subPath}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(fullPath)
|
|
|
|
return err
|
|
|
|
}
|