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

fix: configurable temp file directory #638

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions internal/testutils/simulator/repository_simulator_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func InitLocalEnv() error {
os.Exit(1)
}

if err = os.Mkdir(filepath.Join(tmpDir,metadataPath), 0750); err != nil {
if err = os.Mkdir(filepath.Join(tmpDir, metadataPath), 0750); err != nil {
slog.Error("Repository simulator: failed to create dir", "err", err)
}

if err = os.Mkdir(filepath.Join(tmpDir,targetsPath), 0750); err != nil {
if err = os.Mkdir(filepath.Join(tmpDir, targetsPath), 0750); err != nil {
slog.Error("Repository simulator: failed to create dir", "err", err)
}

Expand All @@ -76,6 +76,7 @@ func InitMetadataDir() (*RepositorySimulator, string, string, error) {
slog.Error("Failed to create root", "err", err)
os.Exit(1)
}
defer f.Close()

if _, err = f.Write(sim.SignedRoots[0]); err != nil {
slog.Error("Repository simulator setup: failed to write signed roots", "err", err)
Expand Down
45 changes: 6 additions & 39 deletions metadata/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,38 +579,6 @@ func (update *Updater) preOrderDepthFirstWalk(targetFilePath string) (*metadata.
return nil, fmt.Errorf("target %s not found", targetFilePath)
}

func moveFile(source, destination string) (err error) {
// can only safely rename on any OS if source and destination are in the same directory
if filepath.Dir(source) == filepath.Dir(destination) {
return os.Rename(source, destination)
}

inputFile, err := os.Open(source)
if err != nil {
return fmt.Errorf("couldn't open source file: %s", err)
}
defer inputFile.Close()
outputFile, err := os.Create(destination)
if err != nil {
return fmt.Errorf("couldn't open dest file: %s", err)
}
defer outputFile.Close()
c, err := io.Copy(outputFile, inputFile)
if err != nil {
return fmt.Errorf("writing to output file failed: %s", err)
}
if c <= 0 {
return fmt.Errorf("nothing copied to output file")
}
inputFile.Close()
// The copy was successful, so now delete the original file
err = os.Remove(source)
if err != nil {
return fmt.Errorf("failed removing original file: %s", err)
}
return nil
}

// persistMetadata writes metadata to disk atomically to avoid data loss
func (update *Updater) persistMetadata(roleName string, data []byte) error {
log := metadata.GetLogger()
Expand All @@ -620,12 +588,8 @@ func (update *Updater) persistMetadata(roleName string, data []byte) error {
}
// caching enabled, proceed with persisting the metadata locally
fileName := filepath.Join(update.cfg.LocalMetadataDir, fmt.Sprintf("%s.json", url.QueryEscape(roleName)))
cwd, err := os.Getwd()
if err != nil {
return err
}
// create a temporary file
file, err := os.CreateTemp(cwd, "tuf_tmp")
file, err := os.CreateTemp(update.cfg.LocalMetadataDir, "tuf_tmp")
trishankatdatadog marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand All @@ -642,9 +606,12 @@ func (update *Updater) persistMetadata(roleName string, data []byte) error {
}

// can't move/rename an open file on windows, so close it first
file.Close()
err = file.Close()
if err != nil {
return err
}
// if all okay, rename the temporary file to the desired one
err = moveFile(file.Name(), fileName)
err = os.Rename(file.Name(), fileName)
if err != nil {
return err
}
Expand Down
Loading