35 lines
632 B
Go
35 lines
632 B
Go
//go:build darwin || linux
|
|
|
|
package attachment
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"golang.org/x/sys/unix"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
func lock(ctx context.Context, f *os.File) error {
|
|
for {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
err := unix.Flock(int(f.Fd()), unix.LOCK_EX|unix.LOCK_NB)
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if !errors.Is(err, unix.EWOULDBLOCK) && !errors.Is(err, unix.EAGAIN) {
|
|
return err
|
|
}
|
|
timer := time.NewTimer(10 * time.Millisecond)
|
|
select {
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
return ctx.Err()
|
|
case <-timer.C:
|
|
}
|
|
}
|
|
}
|
|
func unlock(f *os.File) { _ = unix.Flock(int(f.Fd()), unix.LOCK_UN) }
|