lib/fs: Unwrap mtimeFile, get fd the "correct" way (ref #6875) (#6877)

This commit is contained in:
Audrius Butkevicius
2020-08-07 07:47:48 +02:00
committed by GitHub
parent ff84f075d5
commit e9bb17307d
8 changed files with 337 additions and 157 deletions
+38
View File
@@ -14,6 +14,9 @@ type copyRangeImplementationBasicFile func(src, dst basicFile, srcOffset, dstOff
func copyRangeImplementationForBasicFile(impl copyRangeImplementationBasicFile) copyRangeImplementation {
return func(src, dst File, srcOffset, dstOffset, size int64) error {
src = unwrap(src)
dst = unwrap(dst)
// Then see if it's basic files
srcFile, srcOk := src.(basicFile)
dstFile, dstOk := dst.(basicFile)
if !srcOk || !dstOk {
@@ -22,3 +25,38 @@ func copyRangeImplementationForBasicFile(impl copyRangeImplementationBasicFile)
return impl(srcFile, dstFile, srcOffset, dstOffset, size)
}
}
func withFileDescriptors(first, second basicFile, fn func(first, second uintptr) (int, error)) (int, error) {
fc, err := first.SyscallConn()
if err != nil {
return 0, err
}
sc, err := second.SyscallConn()
if err != nil {
return 0, err
}
var n int
var ferr, serr, fnerr error
ferr = fc.Control(func(first uintptr) {
serr = sc.Control(func(second uintptr) {
n, fnerr = fn(first, second)
})
})
if ferr != nil {
return n, ferr
}
if serr != nil {
return n, serr
}
return n, fnerr
}
func unwrap(f File) File {
for {
if wrapped, ok := f.(interface{ unwrap() File }); ok {
f = wrapped.unwrap()
} else {
return f
}
}
}