统一 frontend、backend、test、test-log 和 out 目录,更新构建、部署及测试入口;纳入配置管理兼容与首次安全引导修复;排除本机配置、密钥和测试产物。
This commit is contained in:
1 parent
28cd9d3406
commit
f60d3b0880
603 files changed
+2351
-660
No files matched your search
@@ -0,0 +1,98 @@
|
||||
package atomicfile
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func Write(path string, data []byte, perm os.FileMode) error {
|
||||
directory := filepath.Dir(path)
|
||||
root, err := os.OpenRoot(directory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer root.Close()
|
||||
return WriteRoot(root, filepath.Base(path), data, perm)
|
||||
}
|
||||
|
||||
func WriteRoot(root *os.Root, name string, data []byte, perm os.FileMode) error {
|
||||
if root == nil || name == "" || name == "." || filepath.IsAbs(name) {
|
||||
return fmt.Errorf("invalid atomic file path")
|
||||
}
|
||||
directory := filepath.Dir(name)
|
||||
base := filepath.Base(name)
|
||||
token := make([]byte, 16)
|
||||
if _, err := rand.Read(token); err != nil {
|
||||
return err
|
||||
}
|
||||
temporaryName := filepath.Join(directory, "."+base+"-"+hex.EncodeToString(token))
|
||||
temporary, err := root.OpenFile(temporaryName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
closed := false
|
||||
committed := false
|
||||
defer func() {
|
||||
if !closed {
|
||||
_ = temporary.Close()
|
||||
}
|
||||
if !committed {
|
||||
_ = root.Remove(temporaryName)
|
||||
}
|
||||
}()
|
||||
|
||||
if err = temporary.Chmod(perm); err != nil {
|
||||
return err
|
||||
}
|
||||
written, err := temporary.Write(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if written != len(data) {
|
||||
return fmt.Errorf("short atomic file write: wrote %d of %d bytes", written, len(data))
|
||||
}
|
||||
if err = temporary.Sync(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = temporary.Close(); err != nil {
|
||||
closed = true
|
||||
return err
|
||||
}
|
||||
closed = true
|
||||
if err = root.Rename(temporaryName, name); err != nil {
|
||||
return err
|
||||
}
|
||||
committed = true
|
||||
if err = root.Chmod(name, perm); err != nil {
|
||||
return err
|
||||
}
|
||||
return syncRootDirectory(root, directory)
|
||||
}
|
||||
|
||||
func SyncDirectory(path string) error {
|
||||
if runtime.GOOS == "windows" {
|
||||
return nil
|
||||
}
|
||||
root, err := os.OpenRoot(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer root.Close()
|
||||
return syncRootDirectory(root, ".")
|
||||
}
|
||||
|
||||
func syncRootDirectory(root *os.Root, name string) error {
|
||||
if runtime.GOOS == "windows" {
|
||||
return nil
|
||||
}
|
||||
directory, err := root.Open(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer directory.Close()
|
||||
return directory.Sync()
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package atomicfile
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWriteReplacesFileAtomically(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
path := filepath.Join(directory, "config.json")
|
||||
if err := os.WriteFile(path, []byte("old"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Write(path, []byte("new"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(data) != "new" {
|
||||
t.Fatalf("atomic file content = %q, want new", data)
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if runtime.GOOS != "windows" && info.Mode().Perm() != 0600 {
|
||||
t.Fatalf("atomic file permissions = %o, want 600", info.Mode().Perm())
|
||||
}
|
||||
entries, err := os.ReadDir(directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if strings.HasPrefix(entry.Name(), ".config.json-") {
|
||||
t.Fatalf("temporary file was left behind: %s", entry.Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteCreatesEmptyFile(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "empty")
|
||||
if err := Write(path, nil, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Size() != 0 {
|
||||
t.Fatalf("empty atomic file size = %d, want 0", info.Size())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user