Skip to content

Commit

Permalink
Merge pull request #3 from alapidas/develop
Browse files Browse the repository at this point in the history
Improve FSFiler implementation
  • Loading branch information
alapidas committed Oct 7, 2015
2 parents c73f403 + e9f6a3f commit 77f14ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ A simple filesystem interface in Go

The filesystem package provides a unified interface to a real backed filesystem. An example use case for this might be a server that wants to provide CRUD access to a filesystem.

Be careful - The current implementation of this buffers entire files into memory.

Here's an example of how you might use this package.

The first step is to create a Filesystemer object based on the absolute filesystem path you want to represent (using a provided constructor):
Expand Down
29 changes: 19 additions & 10 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,31 @@ var (
// has access to the underlying file.
type FSFiler interface {
os.FileInfo
File() []byte
File() ([]byte, error)
AbsPath() string
}

// A FSFile object contains all the information that you would normally find
// in a os.FileInfo object, plus the file itself in a byte slice.
type FSFile struct {
os.FileInfo
file []byte
absPath string
}

var _ FSFiler = (*FSFile)(nil)

// File returns the file stored in a FSFile
func (fsfile *FSFile) File() []byte {
return fsfile.file
func (fsfile *FSFile) File() ([]byte, error) {
bytes, err := ioutil.ReadFile(fsfile.AbsPath())
if err != nil {
return nil, err
}
return bytes, err
}

// AbsPath returns the absolute path of the file on the filesystem
func (fsfile *FSFile) AbsPath() string {
return fsfile.absPath
}

// Filesystemer is the base interface that any filesystem should implement.
Expand Down Expand Up @@ -194,7 +204,7 @@ func (fs *TransientFilesystem) MkPath(path string) error {
if fs.trie.Get(trie.Prefix(absPath)).(os.FileInfo).IsDir() {
return true, nil
} else {
return false, fmt.Errorf("Specified path %s exists in memory as a file, but does not exist on disk", path)
return false, fmt.Errorf("specified path %s exists in memory as a file, but does not exist on disk", path)
}
}
return false, nil
Expand Down Expand Up @@ -337,9 +347,8 @@ func (fs *TransientFilesystem) GetFile(path string) (FSFiler, error) {
if item == nil {
return nil, ErrPathNoExist
}
fileBytes, _ := ioutil.ReadFile(absPath)
fi, _ := os.Stat(realPath(fs, path))
return &FSFile{FileInfo: fi, file: fileBytes}, nil
return &FSFile{FileInfo: fi, absPath: absPath}, nil
}

// A passthrough file system doesn't have an internal representation of the
Expand Down Expand Up @@ -467,7 +476,7 @@ func (fs *PassThroughFilesystem) GetFile(path string) (FSFiler, error) {
}
return nil, fmt.Errorf("Specified path %s is a directory", realPath(fs, path))
}
fileBytes, _ := ioutil.ReadFile(realPath(fs, path))
fi, _ := os.Stat(realPath(fs, path))
return &FSFile{FileInfo: fi, file: fileBytes}, nil
absPath := realPath(fs, path)
fi, _ := os.Stat(absPath)
return &FSFile{FileInfo: fi, absPath: absPath}, nil
}
6 changes: 4 additions & 2 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ func (suite *TheSuite) TestPassThroughFilesystemer_GetFile(c *C) {

newFile, err := fs.GetFile("myDir/theFile")
c.Assert(err, IsNil)
c.Assert(fileBytes, DeepEquals, newFile.File())
f, _ := newFile.File()
c.Assert(fileBytes, DeepEquals, f)

}

Expand Down Expand Up @@ -282,6 +283,7 @@ func (suite *TheSuite) TestTransientFilesystemer_GetFile(c *C) {

newFile, err := fs.GetFile("myDir/theFile")
c.Assert(err, IsNil)
c.Assert(fileBytes, DeepEquals, newFile.File())
f, _ := newFile.File()
c.Assert(fileBytes, DeepEquals, f)

}

0 comments on commit 77f14ac

Please sign in to comment.