1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
package fileutil
import (
"os"
"github.com/pkg/errors"
)
// File represents a wrapper on os.File that supports read, write, seek and
// close methods, but they won't be called if an error occurred before.
type File struct {
File *os.File
err error
}
// OpenFile calls os.OpenFile method and returns the os.File wrapped.
func OpenFile(name string, flag int, perm os.FileMode) (*File, error) {
f, err := os.OpenFile(name, flag, perm)
if err != nil {
return nil, FileError(err, name)
}
return &File{
File: f,
}, nil
}
// error writes f.err if it's not set and returns f.err.
func (f *File) error(err error) error {
if f.err == nil && err != nil {
f.err = FileError(err, f.File.Name())
}
return f.err
}
// Close wraps `func (*os.File) Close` it will always call Close but the error
// return will be the first error thrown if any.
func (f *File) Close() error {
return f.error(f.File.Close())
}
// Read wraps `func (*os.File) Read` but doesn't perform the operation if a
// previous error was thrown.
func (f *File) Read(b []byte) (n int, err error) {
if f.err != nil {
return 0, f.err
}
n, err = f.File.Read(b)
return n, f.error(err)
}
// ReadAt wraps `func (*os.File) ReadAt` but doesn't perform the operation if a
// previous error was thrown.
func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
if f.err != nil {
return 0, f.err
}
n, err = f.File.ReadAt(b, off)
return n, f.error(err)
}
// Seek wraps `func (*os.File) Seek` but doesn't perform the operation if a
// previous error was thrown.
func (f *File) Seek(offset int64, whence int) (ret int64, err error) {
if f.err != nil {
return 0, f.err
}
ret, err = f.File.Seek(offset, whence)
return ret, f.error(err)
}
// Write wraps `func (*os.File) Write` but doesn't perform the operation if a
// previous error was thrown.
func (f *File) Write(b []byte) (n int, err error) {
if f.err != nil {
return 0, f.err
}
n, err = f.File.Write(b)
return n, f.error(err)
}
// WriteAt wraps `func (*os.File) WriteAt` but doesn't perform the operation if
// a previous error was thrown.
func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
if f.err != nil {
return 0, f.err
}
n, err = f.File.WriteAt(b, off)
return n, f.error(err)
}
// WriteString wraps `func (*os.File) WriteString` but doesn't perform the
// operation if a previous error was thrown.
func (f *File) WriteString(s string) (n int, err error) {
if f.err != nil {
return 0, f.err
}
n, err = f.File.WriteString(s)
return n, f.error(err)
}
// FileError is a wrapper for errors of the os package.
func FileError(err error, filename string) error {
if err == nil {
return nil
}
switch e := err.(type) {
case *os.PathError:
return errors.Errorf("%s %s failed: %v", e.Op, e.Path, e.Err)
case *os.LinkError:
return errors.Errorf("%s %s %s failed: %v", e.Op, e.Old, e.New, e.Err)
case *os.SyscallError:
return errors.Errorf("%s failed: %v", e.Syscall, e.Err)
default:
return errors.Wrapf(err, "unexpected error on %s", filename)
}
}
|