Skip to content

Commit

Permalink
use tmp file so the cached file always correct
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Dec 13, 2023
1 parent 0ce5b72 commit 44a50ab
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type Cluster struct {
prefix string
byoc bool

cachedir string
cacheDir string
tmpDir string
maxConn int
hits atomic.Int32
hbytes atomic.Int64
Expand Down Expand Up @@ -73,7 +74,8 @@ func NewCluster(
prefix: "https://openbmclapi.bangbang93.com",
byoc: byoc,

cachedir: cacheDir,
cacheDir: cacheDir,
tmpDir: filepath.Join(cacheDir, ".tmp"),
maxConn: 128,

client: &http.Client{
Expand All @@ -85,6 +87,9 @@ func NewCluster(
},
}
cr.Server.Handler = cr
os.MkdirAll(cr.cacheDir, 0755)
os.RemoveAll(cr.tmpDir)
os.Mkdir(cr.tmpDir, 0700)
return
}

Expand Down Expand Up @@ -308,7 +313,7 @@ func (cr *Cluster) queryURLHeader(method string, url string, header map[string]s
}

func (cr *Cluster) getHashPath(hash string) string {
return ufile.JoinPath(cr.cachedir, hash[:2], hash)
return ufile.JoinPath(cr.cacheDir, hash[:2], hash)
}

type FileInfo struct {
Expand Down Expand Up @@ -560,7 +565,7 @@ func (cr *Cluster) gc(files []FileInfo) {
fileset[cr.getHashPath(files[i].Hash)] = struct{}{}
}
stack := make([]string, 0, 10)
stack = append(stack, cr.cachedir)
stack = append(stack, cr.cacheDir)
for len(stack) > 0 {
p := stack[len(stack)-1]
stack = stack[:len(stack)-1]
Expand Down Expand Up @@ -603,20 +608,20 @@ func (cr *Cluster) downloadFileBuf(ctx context.Context, f *FileInfo, hashMethod

hw := hashMethod.New()

hspt := cr.getHashPath(f.Hash)
if fd, err = os.Create(hspt); err != nil {
if fd, err = os.CreateTemp(cr.tmpDir, "*.downloading"); err != nil {
return
}
tfile := fd.Name()

_, err = io.CopyBuffer(io.MultiWriter(hw, fd), res.Body, buf)
stat, err2 := fd.Stat()
if err2 != nil {
return err2
}
fd.Close()
if err != nil {
return
}
var stat os.FileInfo
if stat, err = os.Stat(hspt); err != nil {
return
}
if t := stat.Size(); f.Size >= 0 && t != f.Size {
err = fmt.Errorf("File size wrong, got %s, expect %s", bytesToUnit((float64)(t)), bytesToUnit((float64)(f.Size)))
} else if hs := hex.EncodeToString(hw.Sum(buf[:0])); hs != f.Hash {
Expand All @@ -627,12 +632,17 @@ func (cr *Cluster) downloadFileBuf(ctx context.Context, f *FileInfo, hashMethod
f0, _ := os.Open(cr.getHashPath(f.Hash))
b0, _ := io.ReadAll(f0)
if len(b0) < 16*1024 {
logDebug("File content:", (string)(b0), "//for", f.Path)
logDebug("File path:", tfile, "; for", f.Path)
}
}
return
}

hspt := cr.getHashPath(f.Hash)
if err = os.Rename(tfile, hspt); err != nil {
return
}

if config.Hijack {
if !strings.HasPrefix(f.Path, "/openbmclapi/download/") {
target := filepath.Join(hijackPath, filepath.FromSlash(f.Path))
Expand Down

0 comments on commit 44a50ab

Please sign in to comment.