mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-06 10:12:40 +00:00
https://github.com/XTLS/Xray-core/pull/7316#issuecomment-6575472342 --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
31 lines
754 B
Go
31 lines
754 B
Go
package xdrive
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"time"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// readFile retries os.ReadFile for up to 2 seconds on sharing violations:
|
|
// os.Rename (MoveFileEx) in Put holds the file with DELETE access, which
|
|
// os.ReadFile does not share, until a moment after its new name appears in List
|
|
func readFile(ctx context.Context, name string) ([]byte, error) {
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
delay := time.Millisecond
|
|
for {
|
|
data, err := os.ReadFile(name)
|
|
if !errors.Is(err, windows.ERROR_SHARING_VIOLATION) || time.Now().Add(delay).After(deadline) {
|
|
return data, err
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, err
|
|
case <-time.After(delay):
|
|
}
|
|
delay = min(2*delay, 100*time.Millisecond)
|
|
}
|
|
}
|