-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a temporary-write-and-rename approach to write manifests to disk, so that the appliers never see partially written files. Google's renameio library does this by creating temorary files whose names start with a dot. So let the appliers ignore anything that starts with a dot. That way, there's also no more need to ignore chmod events. They actually *can* matter, when files become readable or unreadable for the current process. Signed-off-by: Tom Wieczorek <[email protected]>
- Loading branch information
Showing
10 changed files
with
268 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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 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,55 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
/* | ||
Copyright 2022 k0s authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package file | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/google/renameio/v2" | ||
"go.uber.org/multierr" | ||
) | ||
|
||
// WriteAtomically will atomically create or replace a file. The contents of the | ||
// file will be those that the write callback writes to the Writer that gets | ||
// passed in. WriteAtomically will buffer the contents in a hidden (i.e. its | ||
// name will start with a dot), temporary file. When write returns without an | ||
// error, the temporary file will be renamed to fileName, otherwise it will be | ||
// deleted without touching the target file. | ||
func WriteAtomically(fileName string, perm os.FileMode, write func(file io.Writer) error) (err error) { | ||
file, err := renameio.NewPendingFile( | ||
fileName, | ||
renameio.WithTempDir(filepath.Dir(fileName)), | ||
renameio.WithPermissions(perm), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
err = multierr.Append(err, file.Cleanup()) | ||
}() | ||
|
||
if err := write(file); err != nil { | ||
return err | ||
} | ||
|
||
return file.CloseAtomicallyReplace() | ||
} |
This file contains 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,40 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
/* | ||
Copyright 2022 k0s authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package file | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"go.uber.org/multierr" | ||
) | ||
|
||
// WriteAtomically is not atomic on windows, but writes the file directly. | ||
// | ||
// https://github.com/google/renameio/blob/v2.0.0/README.md#windows-support | ||
// https://github.com/google/renameio/pull/20 | ||
func WriteAtomically(fileName string, perm os.FileMode, write func(file io.Writer) error) error { | ||
file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return multierr.Append(write(file), file.Close()) | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.