2020-05-11 20:57:46 +02:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package sync2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// ReadAtWriteAtCloser implements all io.ReaderAt, io.WriterAt and io.Closer.
|
2020-05-11 20:57:46 +02:00
|
|
|
type ReadAtWriteAtCloser interface {
|
|
|
|
io.ReaderAt
|
|
|
|
io.WriterAt
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// PipeWriter allows closing the writer with an error.
|
2020-05-11 20:57:46 +02:00
|
|
|
type PipeWriter interface {
|
|
|
|
io.WriteCloser
|
|
|
|
CloseWithError(reason error) error
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// PipeReader allows closing the reader with an error.
|
2020-05-11 20:57:46 +02:00
|
|
|
type PipeReader interface {
|
|
|
|
io.ReadCloser
|
|
|
|
CloseWithError(reason error) error
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// memory implements ReadAtWriteAtCloser on a memory buffer.
|
2020-05-11 20:57:46 +02:00
|
|
|
type memory []byte
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// Size returns size of memory buffer.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (memory memory) Size() int { return len(memory) }
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// ReadAt implements io.ReaderAt methods.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (memory memory) ReadAt(data []byte, at int64) (amount int, err error) {
|
|
|
|
if at > int64(len(memory)) {
|
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
amount = copy(data, memory[at:])
|
|
|
|
return amount, nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// WriteAt implements io.WriterAt methods.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (memory memory) WriteAt(data []byte, at int64) (amount int, err error) {
|
|
|
|
if at > int64(len(memory)) {
|
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
amount = copy(memory[at:], data)
|
|
|
|
return amount, nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// Close implements io.Closer implementation.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (memory memory) Close() error { return nil }
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// offsetFile implements ReadAt, WriteAt offset to the file with reference counting.
|
2020-05-11 20:57:46 +02:00
|
|
|
type offsetFile struct {
|
|
|
|
file *os.File
|
|
|
|
offset int64
|
|
|
|
open *int64 // number of handles open
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// ReadAt implements io.ReaderAt methods.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (file offsetFile) ReadAt(data []byte, at int64) (amount int, err error) {
|
|
|
|
return file.file.ReadAt(data, file.offset+at)
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// WriteAt implements io.WriterAt methods.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (file offsetFile) WriteAt(data []byte, at int64) (amount int, err error) {
|
|
|
|
return file.file.WriteAt(data, file.offset+at)
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// Close implements io.Closer methods.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (file offsetFile) Close() error {
|
|
|
|
if atomic.AddInt64(file.open, -1) == 0 {
|
|
|
|
return file.file.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|