Files
stalwart-migrator/internal/backup/binary.go
T
jcoffey-dev 4b0bec8956 Add SPDX headers to every Go file
GPLv3's "How to Apply These Terms" asks for a notice in each source file;
this is the modern two-line SPDX form of it rather than the full paragraph.
82 files, including tests.

The blank line after the header is load-bearing. In Go a comment block
immediately preceding `package X` becomes the package doc comment, so
without the separator the SPDX lines would be absorbed into the doc for the
eleven packages whose doc.go (or main.go) opens with one, and `go doc` would
print them. Verified it doesn't.
2026-08-23 18:03:15 -07:00

36 lines
1.3 KiB
Go

// SPDX-FileCopyrightText: 2026 LINUXexpert-org
// SPDX-License-Identifier: GPL-3.0-or-later
package backup
import (
"fmt"
"os"
)
// PreserveBinary moves the currently-installed binary aside to
// "<binaryPath>.v<sourceVersion>" so cutover can install the new one at the
// original path while the exact old binary stays on disk - an operator
// putting the machine back by hand needs it, and re-downloading a specific
// old release is not something to be doing under pressure. It never deletes
// the old binary, and it's idempotent: if a prior attempt at this run
// already preserved it, calling this again just returns the existing
// preserved path rather than erroring on a missing source file.
func PreserveBinary(binaryPath, sourceVersion string) (preservedPath string, err error) {
if sourceVersion == "" {
return "", fmt.Errorf("backup: cannot preserve %s without a source version to suffix it with", binaryPath)
}
preservedPath = binaryPath + ".v" + sourceVersion
if _, statErr := os.Stat(preservedPath); statErr == nil {
return preservedPath, nil
} else if !os.IsNotExist(statErr) {
return "", fmt.Errorf("backup: stat %s: %w", preservedPath, statErr)
}
if err := os.Rename(binaryPath, preservedPath); err != nil {
return "", fmt.Errorf("backup: preserve %s as %s: %w", binaryPath, preservedPath, err)
}
return preservedPath, nil
}