stasatdaglabs 8a4ece1101
[NOD-1223] Reorganize project (#868)
* [NOD-1223] Move all network stuff into a new network package.

* [NOD-1223] Delete the unused package testutil.

* [NOD-1223] Move infrastructure stuff into a new instrastructure package.

* [NOD-1223] Move domain stuff into a new domain package.
2020-08-13 17:27:25 +03:00

45 lines
888 B
Go

package ff
import (
"github.com/pkg/errors"
"io"
"sync"
)
// lockableFile represents a flat file on disk that has been opened for either
// read or read/write access. It also contains a read-write mutex to support
// multiple concurrent readers.
type lockableFile struct {
sync.RWMutex
file
isClosed bool
}
// file is an interface which acts very similar to a *os.File and is typically
// implemented by it. It exists so the test code can provide mock files for
// properly testing corruption and file system issues.
type file interface {
io.Closer
io.WriterAt
io.ReaderAt
Truncate(size int64) error
Sync() error
}
func (lf *lockableFile) Close() error {
if lf.isClosed {
return errors.Errorf("cannot close an already closed file")
}
lf.isClosed = true
lf.Lock()
defer lf.Unlock()
if lf.file == nil {
return nil
}
return errors.WithStack(lf.file.Close())
}