From ac660e72bfbc13cdf12b46252f161b5c6c3caac0 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Tue, 2 Dec 2014 21:00:42 -0800 Subject: [PATCH] Replace StorageLayer.CurrentSize interface call with Stat To support single-flight Size and ModTime queries against backend storage file, we are replacing the CurrentSize call with a Stat call. A FileInfo interface is provided for backends to provide a type, with a default implementation called FileInfoInternal, for use by driver implementations. More work needs to follow this change to update all the driver implementations. --- storagedriver/fileinfo.go | 79 ++++++++++++++++++++++++++++++++++ storagedriver/storagedriver.go | 7 ++- 2 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 storagedriver/fileinfo.go diff --git a/storagedriver/fileinfo.go b/storagedriver/fileinfo.go new file mode 100644 index 00000000..82e3d546 --- /dev/null +++ b/storagedriver/fileinfo.go @@ -0,0 +1,79 @@ +package storagedriver + +import "time" + +// FileInfo returns information about a given path. Inspired by os.FileInfo, +// it elides the base name method for a full path instead. +type FileInfo interface { + // Path provides the full path of the target of this file info. + Path() string + + // Size returns current length in bytes of the file. The return value can + // be used to write to the end of the file at path. The value is + // meaningless if IsDir returns true. + Size() int64 + + // ModTime returns the modification time for the file. For backends that + // don't have a modification time, the creation time should be returned. + ModTime() time.Time + + // IsDir returns true if the path is a directory. + IsDir() bool +} + +// NOTE(stevvooe): The next two types, FileInfoFields and FileInfoInternal +// should only be used by storagedriver implementations. They should moved to +// a "driver" package, similar to database/sql. + +// FileInfoFields provides the exported fields for implementing FileInfo +// interface in storagedriver implementations. It should be used with +// InternalFileInfo. +type FileInfoFields struct { + // Path provides the full path of the target of this file info. + Path string + + // Size is current length in bytes of the file. The value of this field + // can be used to write to the end of the file at path. The value is + // meaningless if IsDir is set to true. + Size int64 + + // ModTime returns the modification time for the file. For backends that + // don't have a modification time, the creation time should be returned. + ModTime time.Time + + // IsDir returns true if the path is a directory. + IsDir bool +} + +// FileInfoInternal implements the FileInfo interface. This should only be +// used by storagedriver implementations that don't have a specialized +// FileInfo type. +type FileInfoInternal struct { + FileInfoFields +} + +var _ FileInfo = FileInfoInternal{} +var _ FileInfo = &FileInfoInternal{} + +// Path provides the full path of the target of this file info. +func (fi FileInfoInternal) Path() string { + return fi.FileInfoFields.Path +} + +// Size returns current length in bytes of the file. The return value can +// be used to write to the end of the file at path. The value is +// meaningless if IsDir returns true. +func (fi FileInfoInternal) Size() int64 { + return fi.FileInfoFields.Size +} + +// ModTime returns the modification time for the file. For backends that +// don't have a modification time, the creation time should be returned. +func (fi FileInfoInternal) ModTime() time.Time { + return fi.FileInfoFields.ModTime +} + +// IsDir returns true if the path is a directory. +func (fi FileInfoInternal) IsDir() bool { + return fi.FileInfoFields.IsDir +} diff --git a/storagedriver/storagedriver.go b/storagedriver/storagedriver.go index d257e4b2..754c8bb6 100644 --- a/storagedriver/storagedriver.go +++ b/storagedriver/storagedriver.go @@ -54,10 +54,9 @@ type StorageDriver interface { // The offset must be no larger than the CurrentSize for this path. WriteStream(path string, offset, size int64, readCloser io.ReadCloser) error - // CurrentSize retrieves the curernt size in bytes of the object at the - // given path. - // It should be safe to read or write anywhere up to this point. - CurrentSize(path string) (uint64, error) + // Stat retrieves the FileInfo for the given path, including the current + // size in bytes and the creation time. + Stat(path string) (FileInfo, error) // List returns a list of the objects that are direct descendants of the //given path.