-
Notifications
You must be signed in to change notification settings - Fork 4.4k
fix: make core self-updates atomic on macOS #3124
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
Open
LovePeachBlossom
wants to merge
2
commits into
MetaCubeX:Meta
Choose a base branch
from
LovePeachBlossom:codex/fix-macos-core-update
base: Meta
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| package updater | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| type fileMetadata struct { | ||
| mode os.FileMode | ||
| ownership fileOwnership | ||
| } | ||
|
|
||
| type fileOperations struct { | ||
| copy func(io.Writer, io.Reader) (int64, error) | ||
| rename func(string, string) error | ||
| sign func(string) error | ||
| verify func(string) error | ||
| syncDir func(string) error | ||
| } | ||
|
|
||
| func defaultFileOperations() fileOperations { | ||
| return fileOperations{ | ||
| copy: io.Copy, | ||
| rename: os.Rename, | ||
| sign: signCoreFile, | ||
| verify: verifyCoreFile, | ||
| syncDir: syncDirectory, | ||
| } | ||
| } | ||
|
|
||
| func metadataFromFileInfo(info os.FileInfo) fileMetadata { | ||
| return fileMetadata{ | ||
| mode: info.Mode(), | ||
| ownership: ownershipFromFileInfo(info), | ||
| } | ||
| } | ||
|
|
||
| // stageFile copies src into a unique file beside dst. The destination is not | ||
| // touched until commitStagedFile atomically renames the fully prepared file. | ||
| func stageFile(src, dst string, metadata fileMetadata, sign bool, fileOps fileOperations) (tempPath string, err error) { | ||
| srcFile, err := os.Open(src) | ||
| if err != nil { | ||
| return "", fmt.Errorf("opening source %s: %w", src, err) | ||
| } | ||
| srcOpen := true | ||
| defer func() { | ||
| if srcOpen { | ||
| _ = srcFile.Close() | ||
| } | ||
| }() | ||
|
|
||
| tempFile, err := os.CreateTemp(filepath.Dir(dst), stagedFilePrefix(dst)) | ||
| if err != nil { | ||
| return "", fmt.Errorf("creating replacement beside %s: %w", dst, err) | ||
| } | ||
| createdPath := tempFile.Name() | ||
| tempPath = createdPath | ||
| tempOpen := true | ||
| keepTemp := false | ||
| defer func() { | ||
| if tempOpen { | ||
| _ = tempFile.Close() | ||
| } | ||
| if !keepTemp { | ||
| _ = os.Remove(createdPath) | ||
| } | ||
| }() | ||
|
|
||
| if _, err = fileOps.copy(tempFile, srcFile); err != nil { | ||
| return "", fmt.Errorf("copying %s: %w", src, err) | ||
| } | ||
| if err = tempFile.Sync(); err != nil { | ||
| return "", fmt.Errorf("syncing replacement %s: %w", tempPath, err) | ||
| } | ||
| if err = tempFile.Close(); err != nil { | ||
| tempOpen = false | ||
| return "", fmt.Errorf("closing replacement %s: %w", tempPath, err) | ||
| } | ||
| tempOpen = false | ||
| if err = srcFile.Close(); err != nil { | ||
| srcOpen = false | ||
| return "", fmt.Errorf("closing source %s: %w", src, err) | ||
| } | ||
| srcOpen = false | ||
|
|
||
| if err = applyFileMetadata(tempPath, metadata); err != nil { | ||
| return "", err | ||
| } | ||
| if sign { | ||
| if err = fileOps.sign(tempPath); err != nil { | ||
| return "", fmt.Errorf("signing replacement: %w", err) | ||
| } | ||
| // codesign may rewrite the file. Reapply trusted metadata before the | ||
| // final sync and verification. | ||
| if err = applyFileMetadata(tempPath, metadata); err != nil { | ||
| return "", err | ||
| } | ||
| } | ||
| if err = syncFile(tempPath); err != nil { | ||
| return "", err | ||
| } | ||
| if sign { | ||
| if err = fileOps.verify(tempPath); err != nil { | ||
| return "", fmt.Errorf("verifying replacement signature: %w", err) | ||
| } | ||
| } | ||
|
|
||
| keepTemp = true | ||
| return tempPath, nil | ||
| } | ||
|
|
||
| func applyFileMetadata(path string, metadata fileMetadata) error { | ||
| if err := applyFileOwnership(path, metadata.ownership); err != nil { | ||
| return fmt.Errorf("preserving ownership on %s: %w", path, err) | ||
| } | ||
|
|
||
| mode := metadata.mode.Perm() | metadata.mode&(os.ModeSetuid|os.ModeSetgid|os.ModeSticky) | ||
| if err := os.Chmod(path, mode); err != nil { | ||
| return fmt.Errorf("preserving mode on %s: %w", path, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func commitStagedFile(tempPath, dst string, fileOps fileOperations) (replaced bool, err error) { | ||
| if err = fileOps.rename(tempPath, dst); err != nil { | ||
| return false, fmt.Errorf("renaming %s to %s: %w", tempPath, dst, err) | ||
| } | ||
| if err = fileOps.syncDir(filepath.Dir(dst)); err != nil { | ||
| return true, fmt.Errorf("syncing destination directory: %w", err) | ||
| } | ||
| return true, nil | ||
| } | ||
|
|
||
| func atomicReplaceFile(src, dst string, metadata fileMetadata, sign bool, fileOps fileOperations) (replaced bool, err error) { | ||
| tempPath, err := stageFile(src, dst, metadata, sign, fileOps) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| defer func() { | ||
| _ = os.Remove(tempPath) | ||
| }() | ||
| return commitStagedFile(tempPath, dst, fileOps) | ||
| } | ||
|
|
||
| func atomicCopyFile(src, dst string, fileOps fileOperations) (replaced bool, err error) { | ||
| info, err := os.Stat(src) | ||
| if err != nil { | ||
| return false, fmt.Errorf("stating source %s: %w", src, err) | ||
| } | ||
| return atomicReplaceFile(src, dst, metadataFromFileInfo(info), false, fileOps) | ||
| } | ||
|
|
||
| func stagedFilePrefix(dst string) string { | ||
| return "." + filepath.Base(dst) + ".update-" | ||
| } | ||
|
|
||
| // cleanStagedFiles removes orphaned files left if a previous updater process | ||
| // was killed before its deferred cleanup could run. | ||
| func cleanStagedFiles(dst string) error { | ||
| dir := filepath.Dir(dst) | ||
| entries, err := os.ReadDir(dir) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| prefix := stagedFilePrefix(dst) | ||
| var cleanupErr error | ||
| for _, entry := range entries { | ||
| if entry.IsDir() || !strings.HasPrefix(entry.Name(), prefix) { | ||
| continue | ||
| } | ||
| if err = os.Remove(filepath.Join(dir, entry.Name())); err != nil && cleanupErr == nil { | ||
| cleanupErr = err | ||
| } | ||
| } | ||
| return cleanupErr | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| //go:build !aix && !android && !darwin && !dragonfly && !freebsd && !illumos && !linux && !netbsd && !openbsd && !solaris | ||
|
|
||
| package updater | ||
|
|
||
| import "os" | ||
|
|
||
| type fileOwnership struct { | ||
| valid bool | ||
| } | ||
|
|
||
| func ownershipFromFileInfo(_ os.FileInfo) fileOwnership { | ||
| return fileOwnership{} | ||
| } | ||
|
|
||
| func applyFileOwnership(_ string, _ fileOwnership) error { | ||
| return nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.