internal/helper: relocate from helper
All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m26s
Test / Hakurei (push) Successful in 3m15s
Test / Hpkg (push) Successful in 4m8s
Test / Sandbox (race detector) (push) Successful in 4m16s
Test / Hakurei (race detector) (push) Successful in 5m5s
Test / Flake checks (push) Successful in 1m23s
All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m26s
Test / Hakurei (push) Successful in 3m15s
Test / Hpkg (push) Successful in 4m8s
Test / Sandbox (race detector) (push) Successful in 4m16s
Test / Hakurei (race detector) (push) Successful in 5m5s
Test / Flake checks (push) Successful in 1m23s
This package is ugly and is pending removal only kept alive by xdg-dbus-proxy. Its exported symbols are made available until v0.4.0 where it will be removed for #24. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
110
internal/helper/proc/pipe.go
Normal file
110
internal/helper/proc/pipe.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package proc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// NewWriterTo returns a [File] that receives content from wt on fulfillment.
|
||||
func NewWriterTo(wt io.WriterTo) File { return &writeToFile{wt: wt} }
|
||||
|
||||
// writeToFile exports the read end of a pipe with data written by an [io.WriterTo].
|
||||
type writeToFile struct {
|
||||
wt io.WriterTo
|
||||
BaseFile
|
||||
}
|
||||
|
||||
func (f *writeToFile) ErrCount() int { return 3 }
|
||||
func (f *writeToFile) Fulfill(ctx context.Context, dispatchErr func(error)) error {
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.Set(r)
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
_, err = f.wt.WriteTo(w)
|
||||
dispatchErr(err)
|
||||
dispatchErr(w.Close())
|
||||
close(done)
|
||||
runtime.KeepAlive(r)
|
||||
}()
|
||||
go func() {
|
||||
select {
|
||||
case <-done:
|
||||
dispatchErr(nil)
|
||||
case <-ctx.Done():
|
||||
dispatchErr(w.Close()) // this aborts WriteTo with file already closed
|
||||
runtime.KeepAlive(r)
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewStat returns a [File] implementing the behaviour
|
||||
// of the receiving end of xdg-dbus-proxy stat fd.
|
||||
func NewStat(s *io.Closer) File { return &statFile{s: s} }
|
||||
|
||||
var (
|
||||
ErrStatFault = errors.New("generic stat fd fault")
|
||||
ErrStatRead = errors.New("unexpected stat behaviour")
|
||||
)
|
||||
|
||||
// statFile implements xdg-dbus-proxy stat fd behaviour.
|
||||
type statFile struct {
|
||||
s *io.Closer
|
||||
BaseFile
|
||||
}
|
||||
|
||||
func (f *statFile) ErrCount() int { return 2 }
|
||||
func (f *statFile) Fulfill(ctx context.Context, dispatchErr func(error)) error {
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.Set(w)
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
var n int
|
||||
|
||||
n, err = r.Read(make([]byte, 1))
|
||||
switch n {
|
||||
case -1:
|
||||
if err == nil {
|
||||
err = ErrStatFault
|
||||
}
|
||||
dispatchErr(err)
|
||||
case 0:
|
||||
if err == nil {
|
||||
err = ErrStatRead
|
||||
}
|
||||
dispatchErr(err)
|
||||
case 1:
|
||||
dispatchErr(err)
|
||||
default:
|
||||
panic("unreachable")
|
||||
}
|
||||
runtime.KeepAlive(w)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
select {
|
||||
case <-done:
|
||||
dispatchErr(nil)
|
||||
case <-ctx.Done():
|
||||
dispatchErr(r.Close()) // this aborts Read with file already closed
|
||||
runtime.KeepAlive(w)
|
||||
}
|
||||
}()
|
||||
|
||||
// this gets closed by the caller
|
||||
*f.s = r
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user