Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to flush renamed files in cache #1387

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions pkg/apk/apk/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,17 +965,17 @@ func (a *APK) cachePackage(ctx context.Context, pkg InstallablePackage, exp *exp
ctlHex := hex.EncodeToString(exp.ControlHash)
ctlDst := filepath.Join(cacheDir, ctlHex+".ctl.tar.gz")

if err := os.Rename(exp.ControlFile, ctlDst); err != nil {
return nil, fmt.Errorf("renaming control file: %w", err)
if err := rename(exp.ControlFile, ctlDst); err != nil {
return nil, err
}

exp.ControlFile = ctlDst

if exp.SignatureFile != "" {
sigDst := filepath.Join(cacheDir, ctlHex+".sig.tar.gz")

if err := os.Rename(exp.SignatureFile, sigDst); err != nil {
return nil, fmt.Errorf("renaming control file: %w", err)
if err := rename(exp.SignatureFile, sigDst); err != nil {
return nil, err
}

exp.SignatureFile = sigDst
Expand All @@ -984,22 +984,24 @@ func (a *APK) cachePackage(ctx context.Context, pkg InstallablePackage, exp *exp
datHex := hex.EncodeToString(exp.PackageHash)
datDst := filepath.Join(cacheDir, datHex+".dat.tar.gz")

if err := os.Rename(exp.PackageFile, datDst); err != nil {
return nil, fmt.Errorf("renaming control file: %w", err)
if err := rename(exp.PackageFile, datDst); err != nil {
return nil, err
}

exp.PackageFile = datDst

tarDst := strings.TrimSuffix(exp.PackageFile, ".gz")
if err := os.Rename(exp.TarFile, tarDst); err != nil {
return nil, fmt.Errorf("renaming control file: %w", err)
}
exp.TarFile = tarDst

if err := exp.TarFS.Close(); err != nil {
return nil, fmt.Errorf("closing tarfs: %w", err)
}

tarDst := strings.TrimSuffix(exp.PackageFile, ".gz")

if err := rename(exp.TarFile, tarDst); err != nil {
return nil, err
}

exp.TarFile = tarDst

// Re-initialize the tarfs with the renamed file.
// TODO: Split out the tarfs Index creation from the FS.
// TODO: Consolidate ExpandAPK(), cachedPackage(), and cachePackage().
Expand Down Expand Up @@ -1372,3 +1374,31 @@ func packageRefs(pkgs []*RepositoryPackage) []string {
}
return names
}

func rename(src, dst string) error {
if err := os.Rename(src, dst); err != nil {
return fmt.Errorf("renaming %s: %w", src, err)
}

// This feels dumb but I have a hunch that it's necessary when renaming
// a file on gcsfuse. We seem to get a "file not found" error when reading
// something immediately after renaming it.
if err := flush(dst); err != nil {
return fmt.Errorf("flushing %s: %w", dst, err)
}

return nil
}

func flush(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}

if err := f.Sync(); err != nil {
return err
}

return f.Close()
}
Loading