storj/pkg/ranger/file.go
Dennis Coyle bb8d4f8d22 cleanup
2018-04-25 10:55:26 -04:00

41 lines
795 B
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package ranger
import (
"io"
"os"
)
// FileHandleRanger returns a Closer from a file handle. The
// Closer's Close method will call fh.Close().
// Footgun: If FileHandleRanger fails, fh.Close will not have been called.
func FileHandleRanger(fh *os.File) (Closer, error) {
stat, err := fh.Stat()
if err != nil {
return nil, Error.Wrap(err)
}
return struct {
Ranger
io.Closer
}{
Ranger: ReaderAtRanger(fh, stat.Size()),
Closer: fh,
}, nil
}
// FileRanger returns a Closer from a path.
func FileRanger(path string) (Closer, error) {
fh, err := os.Open(path)
if err != nil {
return nil, err
}
r, err := FileHandleRanger(fh)
if err != nil {
fh.Close()
return nil, err
}
return r, nil
}