// Copyright (c) 2026 Probo Inc . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. package update import ( "fmt" "io" "os" "path/filepath" ) func copyFile(src, dst string) error { in, err := os.Open(src) if err != nil { return fmt.Errorf("cannot open %s: %w", src, err) } defer func() { _ = in.Close() }() if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { return fmt.Errorf("cannot ensure %s: %w", filepath.Dir(dst), err) } out, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o755) if err != nil { return fmt.Errorf("cannot create %s: %w", dst, err) } if _, err := io.Copy(out, in); err != nil { _ = out.Close() _ = os.Remove(dst) return fmt.Errorf("cannot copy to %s: %w", dst, err) } if err := out.Sync(); err != nil { _ = out.Close() _ = os.Remove(dst) return fmt.Errorf("cannot fsync %s: %w", dst, err) } if err := out.Close(); err != nil { _ = os.Remove(dst) return fmt.Errorf("cannot close %s: %w", dst, err) } return nil }