From 192035927ee47b6f92ccb528da40e3c437c607c8 Mon Sep 17 00:00:00 2001 From: LTLA Date: Mon, 19 Feb 2024 16:34:54 -0800 Subject: [PATCH] Use the write-and-rename paradigm for all JSON files. This ensures that clients don't pick up partial writes for any JSON files, including logs, manifests, summaries and so on. --- utils.go | 82 ++++++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 44 deletions(-) diff --git a/utils.go b/utils.go index 7a74d51..fb18ea1 100644 --- a/utils.go +++ b/utils.go @@ -26,15 +26,45 @@ func newGlobalConfiguration(registry string) globalConfiguration { } } -func dumpJson(path string, output interface{}) error { - str, err := json.MarshalIndent(output, "", " ") +func dumpJson(path string, content interface{}) error { + // Using the save-and-rename paradigm to avoid clients picking up partial writes. + temp, err := os.CreateTemp(filepath.Dir(path), ".temp*.json") + if err != nil { + return fmt.Errorf("failed to create temporary file when saving %q; %w", path, err) + } + + is_closed := false + defer func() { + if !is_closed { + temp.Close() + } + }() + + err = os.Chmod(temp.Name(), 0644) if err != nil { - return fmt.Errorf("failed to marshal JSON; %w", err) + return fmt.Errorf("failed to set temporary file permissions when saving %q; %w", path, err); } - err = os.WriteFile(path, str, 0644) + as_str, err := json.MarshalIndent(content, "", " ") if err != nil { - return fmt.Errorf("failed to write to %q; %w", path, err) + return fmt.Errorf("failed to marshal JSON to save to %q; %w", path, err) + } + + _, err = temp.Write(as_str) + if err != nil { + return fmt.Errorf("failed to write JSON to temporary file for %q; %w", path, err) + } + + temp_name := temp.Name() + is_closed = true + err = temp.Close() + if err != nil { + return fmt.Errorf("failed to close temporary file when saving to %q; %w", path, err) + } + + err = os.Rename(temp_name, path) + if err != nil { + return fmt.Errorf("failed to rename temporary file to %q; %w", path, err) } return nil @@ -63,47 +93,11 @@ func isMissingOrBadName(name *string) error { const logDirName = "..logs" -func dumpLog(registry string, output interface{}) error { +func dumpLog(registry string, content interface{}) error { path := time.Now().Format(time.RFC3339) + "_" + strconv.Itoa(100000 + rand.Intn(900000)) - return dumpJson(filepath.Join(registry, logDirName, path), output) + return dumpJson(filepath.Join(registry, logDirName, path), content) } func dumpResponse(response_dir, reqname string, content interface{}) error { - // Using the save-and-rename paradigm to avoid clients picking up partial writes. - temp, err := os.CreateTemp(response_dir, "TEMP") - if err != nil { - return fmt.Errorf("failed to create temporary file for response to %q; %w", reqname, err) - } - - is_closed := false - defer func() { - if !is_closed { - temp.Close() - } - }() - - as_str, err := json.MarshalIndent(content, "", " ") - if err != nil { - return fmt.Errorf("failed to marshal JSON for response to %q; %w", reqname, err) - } - - _, err = temp.Write(as_str) - if err != nil { - return fmt.Errorf("failed to write JSON for response to %q; %w", reqname, err) - } - - temp_name := temp.Name() - is_closed = true - err = temp.Close() - if err != nil { - return fmt.Errorf("failed to close file for response to %q; %w", reqname, err) - } - - logpath := filepath.Join(response_dir, reqname) - err = os.Rename(temp_name, logpath) - if err != nil { - return fmt.Errorf("failed to rename response to %q; %w", reqname, err) - } - - return nil + return dumpJson(filepath.Join(response_dir, reqname), content) }