chore: optimizing copyFile in updater
Some checks are pending
Test / test (1.20, macos-13) (push) Waiting to run
Test / test (1.20, macos-latest) (push) Waiting to run
Test / test (1.20, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.20, ubuntu-latest) (push) Waiting to run
Test / test (1.20, windows-latest) (push) Waiting to run
Test / test (1.21, macos-13) (push) Waiting to run
Test / test (1.21, macos-latest) (push) Waiting to run
Test / test (1.21, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.21, ubuntu-latest) (push) Waiting to run
Test / test (1.21, windows-latest) (push) Waiting to run
Test / test (1.22, macos-13) (push) Waiting to run
Test / test (1.22, macos-latest) (push) Waiting to run
Test / test (1.22, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.22, ubuntu-latest) (push) Waiting to run
Test / test (1.22, windows-latest) (push) Waiting to run
Test / test (1.23, macos-13) (push) Waiting to run
Test / test (1.23, macos-latest) (push) Waiting to run
Test / test (1.23, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.23, ubuntu-latest) (push) Waiting to run
Test / test (1.23, windows-latest) (push) Waiting to run
Test / test (1.24, macos-13) (push) Waiting to run
Test / test (1.24, macos-latest) (push) Waiting to run
Test / test (1.24, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.24, ubuntu-latest) (push) Waiting to run
Test / test (1.24, windows-latest) (push) Waiting to run
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run

This commit is contained in:
wwqgtxx 2025-07-26 01:32:49 +08:00
parent fb043df1b6
commit a9b7e705f0

View File

@ -279,7 +279,7 @@ func (u *CoreUpdater) clean(updateDir string) {
// Existing files are overwritten // Existing files are overwritten
// All files are created inside outDir, subdirectories are not created // All files are created inside outDir, subdirectories are not created
// Return the output file name // Return the output file name
func (u *CoreUpdater) gzFileUnpack(gzfile, outDir string) (string, error) { func (u *CoreUpdater) gzFileUnpack(gzfile, outDir string) (outputName string, err error) {
f, err := os.Open(gzfile) f, err := os.Open(gzfile)
if err != nil { if err != nil {
return "", fmt.Errorf("os.Open(): %w", err) return "", fmt.Errorf("os.Open(): %w", err)
@ -311,14 +311,10 @@ func (u *CoreUpdater) gzFileUnpack(gzfile, outDir string) (string, error) {
originalName = strings.TrimSuffix(originalName, ".gz") originalName = strings.TrimSuffix(originalName, ".gz")
} }
outputName := filepath.Join(outDir, originalName) outputName = filepath.Join(outDir, originalName)
// Create the output file // Create the output file
wc, err := os.OpenFile( wc, err := os.OpenFile(outputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)
outputName,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
0o755,
)
if err != nil { if err != nil {
return "", fmt.Errorf("os.OpenFile(%s): %w", outputName, err) return "", fmt.Errorf("os.OpenFile(%s): %w", outputName, err)
} }
@ -343,7 +339,7 @@ func (u *CoreUpdater) gzFileUnpack(gzfile, outDir string) (string, error) {
// Existing files are overwritten // Existing files are overwritten
// All files are created inside 'outDir', subdirectories are not created // All files are created inside 'outDir', subdirectories are not created
// Return the output file name // Return the output file name
func (u *CoreUpdater) zipFileUnpack(zipfile, outDir string) (string, error) { func (u *CoreUpdater) zipFileUnpack(zipfile, outDir string) (outputName string, err error) {
zrc, err := zip.OpenReader(zipfile) zrc, err := zip.OpenReader(zipfile)
if err != nil { if err != nil {
return "", fmt.Errorf("zip.OpenReader(): %w", err) return "", fmt.Errorf("zip.OpenReader(): %w", err)
@ -375,7 +371,7 @@ func (u *CoreUpdater) zipFileUnpack(zipfile, outDir string) (string, error) {
}() }()
fi := zf.FileInfo() fi := zf.FileInfo()
name := fi.Name() name := fi.Name()
outputName := filepath.Join(outDir, name) outputName = filepath.Join(outDir, name)
if fi.IsDir() { if fi.IsDir() {
return "", fmt.Errorf("the target file is a directory") return "", fmt.Errorf("the target file is a directory")
@ -402,14 +398,38 @@ func (u *CoreUpdater) zipFileUnpack(zipfile, outDir string) (string, error) {
} }
// Copy file on disk // Copy file on disk
func (u *CoreUpdater) copyFile(src, dst string) error { func (u *CoreUpdater) copyFile(src, dst string) (err error) {
d, e := os.ReadFile(src) rc, err := os.Open(src)
if e != nil { if err != nil {
return e return fmt.Errorf("os.Open(%s): %w", src, err)
} }
e = os.WriteFile(dst, d, 0o644)
if e != nil { defer func() {
return e closeErr := rc.Close()
if closeErr != nil && err == nil {
err = closeErr
} }
}()
// Create the output file
// If the file does not exist, creates it with permissions perm (before umask);
// otherwise truncates it before writing, without changing permissions.
wc, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return fmt.Errorf("os.OpenFile(%s): %w", dst, err)
}
defer func() {
closeErr := wc.Close()
if closeErr != nil && err == nil {
err = closeErr
}
}()
_, err = io.Copy(wc, rc)
if err != nil {
return fmt.Errorf("io.Copy(): %w", err)
}
return nil return nil
} }