lotus/ffiwrapper/files.go

54 lines
927 B
Go
Raw Normal View History

2020-03-26 02:50:56 +00:00
package ffiwrapper
import (
"io"
"os"
"sync"
"golang.org/x/xerrors"
)
2020-05-28 17:15:15 +00:00
func ToReadableFile(r io.Reader, n int64) (*os.File, func() error, error) {
2020-03-26 02:50:56 +00:00
f, ok := r.(*os.File)
if ok {
return f, func() error { return nil }, nil
}
var w *os.File
f, w, err := os.Pipe()
if err != nil {
return nil, nil, err
}
var wait sync.Mutex
var werr error
wait.Lock()
go func() {
defer wait.Unlock()
var copied int64
copied, werr = io.CopyN(w, r, n)
if werr != nil {
log.Warnf("toReadableFile: copy error: %+v", werr)
}
err := w.Close()
if werr == nil && err != nil {
werr = err
log.Warnf("toReadableFile: close error: %+v", err)
return
}
if copied != n {
log.Warnf("copied different amount than expected: %d != %d", copied, n)
werr = xerrors.Errorf("copied different amount than expected: %d != %d", copied, n)
}
}()
return f, func() error {
wait.Lock()
return werr
}, nil
}