Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Improve file system probe content and missing file diagnostics https://github.com/siyuan-note/siyuan/issues/20472

Daniel8bd98ded4f47
diff --git a/kernel/util/runtime.go b/kernel/util/runtime.go
index f12a709a0c..d7c7e1fe9f 100644
--- a/kernel/util/runtime.go
+++ b/kernel/util/runtime.go
@@ -19,7 +19,6 @@ package util
import (
"bytes"
"context"
- "crypto/rand"
"fmt"
"net/url"
"os"
@@ -379,8 +378,7 @@ func checkFileSysStatus() {
for range 7 {
tmp := filepath.Join(dir, "check_consistency")
- data := make([]byte, 1024*4)
- _, err := rand.Read(data)
+ data, err := newFileSysStatusCheckData()
if err != nil {
ReportFileSysFatalError(err)
return
@@ -396,7 +394,7 @@ func checkFileSysStatus() {
for range 32 {
renamed := tmp + "_renamed"
if err = os.Rename(tmp, renamed); err != nil {
- ReportFileSysFatalError(err)
+ ReportFileSysFatalError(fileSysStatusCheckError(err))
break
}
@@ -404,7 +402,7 @@ func checkFileSysStatus() {
f, err := os.Open(renamed)
if err != nil {
- ReportFileSysFatalError(err)
+ ReportFileSysFatalError(fileSysStatusCheckError(err))
return
}
@@ -414,7 +412,7 @@ func checkFileSysStatus() {
}
if err = os.Rename(renamed, tmp); err != nil {
- ReportFileSysFatalError(err)
+ ReportFileSysFatalError(fileSysStatusCheckError(err))
return
}
diff --git a/kernel/util/runtime_filesys.go b/kernel/util/runtime_filesys.go
new file mode 100644
index 0000000000..e531e2ce6c
--- /dev/null
+++ b/kernel/util/runtime_filesys.go
@@ -0,0 +1,47 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package util
+
+import (
+ "crypto/rand"
+ "errors"
+ "fmt"
+ "os"
+)
+
+func newFileSysStatusCheckData() ([]byte, error) {
+ // 每批使用独立的随机标识,其余内容填充为普通文本,保持探测文件大小不变。
+ var id [16]byte
+ if _, err := rand.Read(id[:]); err != nil {
+ return nil, err
+ }
+ data := make([]byte, 4096)
+ offset := copy(data, fmt.Sprintf("SiYuan file system consistency check\nCheck ID: %x\n", id))
+ const line = "SiYuan uses this temporary file to check file system consistency.\n"
+ for offset < len(data) {
+ offset += copy(data[offset:], line)
+ }
+ return data, nil
+}
+
+func fileSysStatusCheckError(err error) error {
+ if !errors.Is(err, os.ErrNotExist) {
+ return err
+ }
+ // 保留原始操作和完整路径;不存在错误也可能来自父目录,不能据此断定文件被隔离。
+ return fmt.Errorf("file system check file or parent directory is missing; it may have been removed by another program or quarantined by security software; check the paths and quarantine records: %w", err)
+}
diff --git a/kernel/util/runtime_filesys_test.go b/kernel/util/runtime_filesys_test.go
new file mode 100644
index 0000000000..e26edff283
--- /dev/null
+++ b/kernel/util/runtime_filesys_test.go
@@ -0,0 +1,234 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package util
+
+import (
+ "bytes"
+ "compress/gzip"
+ "context"
+ "errors"
+ "fmt"
+ "go/ast"
+ "go/format"
+ "go/parser"
+ "go/token"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "regexp"
+ "strings"
+ "syscall"
+ "testing"
+ "time"
+
+ "github.com/siyuan-note/logging"
+)
+
+func TestFileSysStatusCheckData(t *testing.T) {
+ ids := map[string]bool{}
+ pattern := regexp.MustCompile(`(?m)^Check ID: ([0-9a-f]{32})$`)
+ for range 32 {
+ data, err := newFileSysStatusCheckData()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(data) != 4096 {
+ t.Fatalf("data length = %d, want 4096", len(data))
+ }
+ for _, b := range data {
+ if b != '\n' && (b < ' ' || b > '~') {
+ t.Fatalf("non-text byte: %x", b)
+ }
+ }
+ match := pattern.FindSubmatch(data)
+ if match == nil || ids[string(match[1])] {
+ t.Fatalf("missing or repeated check ID: %q", match)
+ }
+ ids[string(match[1])] = true
+ var compressed bytes.Buffer
+ writer := gzip.NewWriter(&compressed)
+ if _, err = writer.Write(data); err != nil {
+ t.Fatal(err)
+ }
+ if err = writer.Close(); err != nil {
+ t.Fatal(err)
+ }
+ if compressed.Len() >= len(data)/4 {
+ t.Fatalf("check data is not mostly repeated text: compressed length %d", compressed.Len())
+ }
+ }
+}
+
+func TestFileSysStatusCheckMissingFileDiagnostics(t *testing.T) {
+ for _, operation := range []string{"open", "rename", "rename-back", "rename-target-parent", "open-parent"} {
+ t.Run(operation, func(t *testing.T) {
+ root := t.TempDir()
+ dir := filepath.Join(root, "filesys_status_check")
+ if err := os.Mkdir(dir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ source := filepath.Join(dir, "check_consistency")
+ target := source + "_renamed"
+ if operation == "rename-back" || operation == "open" || operation == "open-parent" {
+ source, target = target, source
+ }
+ if err := os.WriteFile(source, []byte("check"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ if operation == "rename-target-parent" {
+ parent := filepath.Join(root, "removed")
+ if err := os.Mkdir(parent, 0755); err != nil {
+ t.Fatal(err)
+ }
+ target = filepath.Join(parent, "check_consistency_renamed")
+ if err := os.Remove(parent); err != nil {
+ t.Fatal(err)
+ }
+ } else if operation == "open-parent" {
+ if err := os.RemoveAll(dir); err != nil {
+ t.Fatal(err)
+ }
+ } else if err := os.Remove(source); err != nil {
+ t.Fatal(err)
+ }
+ var err error
+ if strings.HasPrefix(operation, "open") {
+ var f *os.File
+ f, err = os.Open(source)
+ if f != nil {
+ f.Close()
+ }
+ } else {
+ err = os.Rename(source, target)
+ }
+ if !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("fault injection did not produce a missing-path error: %v", err)
+ }
+ wrapped := fileSysStatusCheckError(fmt.Errorf("probe operation: %w", err))
+ if !errors.Is(wrapped, os.ErrNotExist) || !strings.Contains(wrapped.Error(), source) {
+ t.Fatalf("missing original error or source path: %v", wrapped)
+ }
+ for _, text := range []string{"file or parent directory is missing", "may have been", "quarantined by security software"} {
+ if !strings.Contains(wrapped.Error(), text) {
+ t.Fatalf("missing diagnostic %q: %v", text, wrapped)
+ }
+ }
+ if strings.HasPrefix(operation, "open") {
+ var pathErr *os.PathError
+ if !errors.As(wrapped, &pathErr) || pathErr.Path != source {
+ t.Fatalf("lost PathError: %v", wrapped)
+ }
+ } else {
+ var linkErr *os.LinkError
+ if !errors.As(wrapped, &linkErr) || linkErr.Old != source || linkErr.New != target || !strings.Contains(wrapped.Error(), target) {
+ t.Fatalf("lost rename paths: %v", wrapped)
+ }
+ }
+ })
+ }
+}
+
+func TestFileSysStatusCheckOtherErrorsUnchanged(t *testing.T) {
+ for _, cause := range []error{os.ErrPermission, syscall.EIO, syscall.EBUSY, syscall.Errno(32)} {
+ err := &os.PathError{Op: "open", Path: "check_consistency_renamed", Err: cause}
+ if got := fileSysStatusCheckError(err); got != err {
+ t.Fatalf("non-missing error changed: %v", got)
+ }
+ }
+ if got := fileSysStatusCheckError(nil); got != nil {
+ t.Fatalf("nil error changed: %v", got)
+ }
+}
+
+func TestFileSysStatusCheckMissingFileStillExits(t *testing.T) {
+ const rootEnv = "SIYUAN_TEST_FILESYS_CHECK_ROOT"
+ if root := os.Getenv(rootEnv); root != "" {
+ WorkspaceDir = root
+ DataDir = filepath.Join(root, "data")
+ logging.SetLogPath(filepath.Join(root, "check.log"))
+ path := filepath.Join(DataDir, ".siyuan", "filesys_status_check", "check_consistency")
+ go func() {
+ // 等待探测文件创建并关闭写句柄,兼容不允许删除已打开文件的平台。
+ var lastErr error
+ for range 10000 {
+ if lastErr = os.Remove(path); lastErr == nil {
+ return
+ }
+ time.Sleep(time.Millisecond)
+ }
+ t.Errorf("could not remove probe: %v", lastErr)
+ }()
+ checkFileSysStatus()
+ t.Fatal("missing probe did not terminate the process")
+ }
+ root := t.TempDir()
+ ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
+ defer cancel()
+ cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestFileSysStatusCheckMissingFileStillExits$")
+ cmd.Env = append(os.Environ(), rootEnv+"="+root)
+ output, err := cmd.CombinedOutput()
+ var exitErr *exec.ExitError
+ if !errors.As(err, &exitErr) || exitErr.ExitCode() != logging.ExitCodeFileSysErr {
+ t.Fatalf("expected fatal file-system exit, got %v\n%s", err, output)
+ }
+ for _, text := range []string{"check_consistency", "check_consistency_renamed", "quarantined by security software"} {
+ if !strings.Contains(string(output), text) {
+ t.Fatalf("missing fatal diagnostic %q:\n%s", text, output)
+ }
+ }
+}
+
+func TestFileSysStatusCheckProtectionStructure(t *testing.T) {
+ fileSet := token.NewFileSet()
+ source, err := parser.ParseFile(fileSet, "runtime.go", nil, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var body string
+ for _, decl := range source.Decls {
+ if function, ok := decl.(*ast.FuncDecl); ok && function.Name.Name == "checkFileSysStatus" {
+ var formatted bytes.Buffer
+ if err = format.Node(&formatted, fileSet, function.Body); err != nil {
+ t.Fatal(err)
+ }
+ body = formatted.String()
+ }
+ }
+ for _, fragment := range []string{
+ "for range 7", "for range 32", "time.Sleep(5 * time.Second)", "RandomSleep(500, 1000)",
+ `filepath.Join(dir, "check_consistency")`, `tmp + "_renamed"`,
+ `!entry.IsDir() && strings.Contains(entry.Name(), "check_")`, "if 1 < len(lines)",
+ "if IsCloudDrivePath(WorkspaceDir)", "checkFileSysStatusLock.TryLock()",
+ } {
+ if !strings.Contains(body, fragment) {
+ t.Errorf("missing protection: %s", fragment)
+ }
+ }
+ if strings.Count(body, "ReportFileSysFatalError(fileSysStatusCheckError(err))") != 3 || strings.Count(body, "ReportFileSysFatalError(") != 12 {
+ t.Fatal("fatal error branches changed")
+ }
+ if strings.Count(body, "os.Rename(") != 2 || strings.Count(body, "os.Open(") != 1 || strings.Count(body, "newFileSysStatusCheckData()") != 1 {
+ t.Fatal("probe operations changed")
+ }
+ var formatted bytes.Buffer
+ if err = format.Node(&formatted, fileSet, source); err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(formatted.String(), "thirdPartySyncCheckTicker = time.NewTicker(time.Minute * 10)") {
+ t.Fatal("probe interval changed")
+ }
+}