An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Avoid false-positive iCloud workspace detection on macOS
Daniel67662449334b
diff --git a/kernel/util/file.go b/kernel/util/file.goindex c41713b942..bcd50e87a9 100644--- a/kernel/util/file.go+++ b/kernel/util/file.go@@ -492,59 +492,3 @@ func CeilSize(size int64) int64 {func IsReservedFilename(baseName string) bool {return "assets" == baseName || "templates" == baseName || "widgets" == baseName || "emojis" == baseName || ".siyuan" == baseName || strings.HasPrefix(baseName, ".")}--func WalkWithSymlinks(root string, fn fs.WalkDirFunc) error {- // 感谢 https://github.com/edwardrf/symwalk/blob/main/symwalk.go-- rr, err := filepath.EvalSymlinks(root) // Find real base if there is any symlinks in the path- if err != nil {- return err- }-- visitedDirs := make(map[string]struct{})- return filelock.Walk(rr, getWalkFn(visitedDirs, fn))-}--func getWalkFn(visitedDirs map[string]struct{}, fn fs.WalkDirFunc) fs.WalkDirFunc {- return func(path string, d fs.DirEntry, err error) error {- if err != nil {- return fn(path, d, err)- }-- if d.IsDir() {- if _, ok := visitedDirs[path]; ok {- return filepath.SkipDir- }- visitedDirs[path] = struct{}{}- }-- if err := fn(path, d, err); err != nil {- return err- }-- info, err := d.Info()- if nil != err {- return err- }- if info.Mode()&os.ModeSymlink == 0 {- return nil- }-- // path is a symlink- rp, err := filepath.EvalSymlinks(path)- if err != nil {- return err- }-- ri, err := os.Stat(rp)- if err != nil {- return err- }-- if ri.IsDir() {- return filelock.Walk(rp, getWalkFn(visitedDirs, fn))- }-- return nil- }-}diff --git a/kernel/util/icloud_darwin.go b/kernel/util/icloud_darwin.gonew file mode 100644index 0000000000..891dea3944--- /dev/null+++ b/kernel/util/icloud_darwin.go@@ -0,0 +1,63 @@+// 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/>.++//go:build darwin && !ios++package util++/*+#cgo CFLAGS: -x objective-c+#cgo LDFLAGS: -framework Foundation+#include <stdlib.h>+#import <Foundation/Foundation.h>++// siyuanIsUbiquitousItem 通过系统文件资源属性判断路径是否位于 iCloud 存储中。+// 参考 https://developer.apple.com/documentation/foundation/urlresourcevalues/isubiquitousitem+static int siyuanIsUbiquitousItem(const char *path) {+ @autoreleasepool {+ NSString *filePath = [NSString stringWithUTF8String:path];+ if (!filePath) return -2;++ NSURL *url = [NSURL fileURLWithPath:filePath];+ NSNumber *value = nil;+ NSError *error = nil;+ if (![url getResourceValue:&value forKey:NSURLIsUbiquitousItemKey error:&error]) return -1;+ return [value boolValue] ? 1 : 0;+ }+}+*/+import "C"++import (+ "errors"+ "unsafe"+)++func isUbiquitousItem(path string) (bool, error) {+ cPath := C.CString(path)+ defer C.free(unsafe.Pointer(cPath))++ switch C.siyuanIsUbiquitousItem(cPath) {+ case 1:+ return true, nil+ case 0:+ return false, nil+ case -2:+ return false, errors.New("invalid UTF-8 path")+ default:+ return false, errors.New("failed to read iCloud file resource status")+ }+}diff --git a/kernel/util/icloud_other.go b/kernel/util/icloud_other.gonew file mode 100644index 0000000000..fb147f4e5a--- /dev/null+++ b/kernel/util/icloud_other.go@@ -0,0 +1,23 @@+// 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/>.++//go:build !darwin || ios++package util++func isUbiquitousItem(string) (bool, error) {+ return false, nil+}diff --git a/kernel/util/icloud_test.go b/kernel/util/icloud_test.gonew file mode 100644index 0000000000..d1c05dd910--- /dev/null+++ b/kernel/util/icloud_test.go@@ -0,0 +1,152 @@+// 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 (+ "os"+ "path/filepath"+ "testing"+)++func TestIsSameOrSubPath(t *testing.T) {+ root := filepath.Join(t.TempDir(), "iCloud")+ tests := []struct {+ name string+ target string+ want bool+ }{+ {name: "same", target: root, want: true},+ {name: "same cleaned", target: root + string(os.PathSeparator) + ".", want: true},+ {name: "child", target: filepath.Join(root, "workspace"), want: true},+ {name: "sibling prefix", target: root + "-other", want: false},+ {name: "parent", target: filepath.Dir(root), want: false},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ if got := isSameOrSubPath(root, test.target); got != test.want {+ t.Fatalf("isSameOrSubPath(%q, %q) = %v, want %v", root, test.target, got, test.want)+ }+ })+ }+}++func TestLongestExistingPath(t *testing.T) {+ existing := filepath.Join(t.TempDir(), "existing")+ if err := os.MkdirAll(existing, 0755); nil != err {+ t.Fatal(err)+ }+ path := filepath.Join(existing, "missing", "workspace")+ if got := longestExistingPath(path); got != existing {+ t.Fatalf("longestExistingPath(%q) = %q, want %q", path, got, existing)+ }+}++func TestMatchICloudRoot(t *testing.T) {+ home := t.TempDir()+ iCloudRoot := filepath.Join(home, "Library", "Mobile Documents")+ if err := os.MkdirAll(iCloudRoot, 0755); nil != err {+ t.Fatal(err)+ }++ tests := []struct {+ name string+ workspace string+ want bool+ }{+ {name: "same", workspace: iCloudRoot, want: true},+ {name: "child", workspace: filepath.Join(iCloudRoot, "workspace"), want: true},+ {name: "outside", workspace: filepath.Join(home, "workspace"), want: false},+ {name: "sibling prefix", workspace: iCloudRoot + "-other", want: false},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ _, got := matchICloudRoot(home, iCloudRoot, test.workspace)+ if got != test.want {+ t.Fatalf("matchICloudRoot(%q, %q, %q) = %v, want %v", home, iCloudRoot, test.workspace, got, test.want)+ }+ })+ }+}++func TestMatchICloudRootRejectsBroadRoots(t *testing.T) {+ home := t.TempDir()+ workspace := filepath.Join(home, "workspace")+ if _, matched := matchICloudRoot(home, filepath.VolumeName(home)+string(os.PathSeparator), workspace); matched {+ t.Fatal("partition root must not be treated as an iCloud root")+ }+ if _, matched := matchICloudRoot(home, home, workspace); matched {+ t.Fatal("home directory must not be treated as an iCloud root")+ }++ otherHome := t.TempDir()+ otherRoot := filepath.Join(otherHome, "Library", "Mobile Documents")+ if err := os.MkdirAll(otherRoot, 0755); nil != err {+ t.Fatal(err)+ }+ if _, matched := matchICloudRoot(home, otherRoot, filepath.Join(otherRoot, "workspace")); matched {+ t.Fatal("a directory outside home must not be treated as an iCloud root")+ }+}++func TestMatchICloudRootResolvesWorkspaceSymlinks(t *testing.T) {+ home := t.TempDir()+ iCloudRoot := filepath.Join(home, "Library", "Mobile Documents")+ iCloudWorkspace := filepath.Join(iCloudRoot, "workspace")+ localWorkspace := filepath.Join(home, "local-workspace")+ if err := os.MkdirAll(iCloudWorkspace, 0755); nil != err {+ t.Fatal(err)+ }+ if err := os.MkdirAll(localWorkspace, 0755); nil != err {+ t.Fatal(err)+ }++ linkIntoICloud := filepath.Join(home, "link-into-icloud")+ if err := os.Symlink(iCloudWorkspace, linkIntoICloud); nil != err {+ t.Skipf("create symlink failed: %s", err)+ }+ if _, matched := matchICloudRoot(home, iCloudRoot, linkIntoICloud); !matched {+ t.Fatal("workspace symlink pointing into iCloud must be detected")+ }++ linkOutOfICloud := filepath.Join(iCloudRoot, "link-out-of-icloud")+ if err := os.Symlink(localWorkspace, linkOutOfICloud); nil != err {+ t.Fatal(err)+ }+ if _, matched := matchICloudRoot(home, iCloudRoot, linkOutOfICloud); matched {+ t.Fatal("workspace symlink pointing out of iCloud must not be detected")+ }+}++func TestMatchICloudRootIgnoresNestedRootSymlink(t *testing.T) {+ home := t.TempDir()+ iCloudRoot := filepath.Join(home, "Library", "Mobile Documents")+ workspace := filepath.Join(home, "workspace")+ if err := os.MkdirAll(iCloudRoot, 0755); nil != err {+ t.Fatal(err)+ }+ if err := os.MkdirAll(workspace, 0755); nil != err {+ t.Fatal(err)+ }++ rootLink := filepath.Join(iCloudRoot, "root-link")+ if err := os.Symlink(filepath.VolumeName(home)+string(os.PathSeparator), rootLink); nil != err {+ t.Skipf("create symlink failed: %s", err)+ }+ if _, matched := matchICloudRoot(home, iCloudRoot, workspace); matched {+ t.Fatal("a symlink to the partition root inside iCloud must not affect detection")+ }+}diff --git a/kernel/util/runtime.go b/kernel/util/runtime.goindex 28fdcdc91e..71547b41bd 100644--- a/kernel/util/runtime.go+++ b/kernel/util/runtime.go@@ -20,8 +20,6 @@ import ("bytes""crypto/rand""fmt"- "io"- "io/fs""os""path/filepath""runtime"@@ -394,28 +392,69 @@ func isKnownCloudDrivePath(workspaceAbsPath string) bool {strings.Contains(workspaceAbsPathLower, "天翼云")}-func isICloudPath(workspaceAbsPath string) (ret bool) {- if !gulu.OS.IsDarwin() {+func isICloudPath(workspaceAbsPath string) bool {+ if !gulu.OS.IsDarwin() || !filepath.IsAbs(workspaceAbsPath) {return false}- workspaceAbsPathLower := strings.ToLower(workspaceAbsPath)+ workspacePath := ResolveLongestExistingParent(workspaceAbsPath)+ if existingPath := longestExistingPath(workspacePath); "" != existingPath {+ isUbiquitous, err := isUbiquitousItem(existingPath)+ if nil != err {+ logging.LogDebugf("check iCloud status for path [%s] failed: %s", existingPath, err)+ } else if isUbiquitous {+ logging.LogWarnf("workspace [%s] is in iCloud path [%s], detected by system metadata", workspaceAbsPath, existingPath)+ return true+ }+ }// macOS 端对工作空间放置在 iCloud 路径下做检查 https://github.com/siyuan-note/siyuan/issues/7747iCloudRoot := filepath.Join(HomeDir, "Library", "Mobile Documents")- WalkWithSymlinks(iCloudRoot, func(path string, d fs.DirEntry, err error) error {- if !d.IsDir() {- return nil+ if resolvedRoot, matched := matchICloudRoot(HomeDir, iCloudRoot, workspacePath); matched {+ logging.LogWarnf("workspace [%s] is in iCloud path [%s]", workspaceAbsPath, resolvedRoot)+ return true+ }+ return false+}++func longestExistingPath(path string) string {+ path = filepath.Clean(path)+ for {+ if _, err := os.Stat(path); nil == err {+ return path}- if strings.HasPrefix(workspaceAbsPathLower, strings.ToLower(path)) {- ret = true- logging.LogWarnf("workspace [%s] is in iCloud path [%s]", workspaceAbsPath, path)- return io.EOF+ parent := filepath.Dir(path)+ if parent == path {+ return ""}- return nil- })- return+ path = parent+ }+}++func matchICloudRoot(homeDir, iCloudRoot, workspacePath string) (resolvedRoot string, matched bool) {+ resolvedHome, err := filepath.EvalSymlinks(filepath.Clean(homeDir))+ if nil != err || !filepath.IsAbs(resolvedHome) {+ return+ }++ resolvedRoot, err = filepath.EvalSymlinks(filepath.Clean(iCloudRoot))+ if nil != err || !filepath.IsAbs(resolvedRoot) || IsPartitionRootPath(resolvedRoot) || resolvedHome == resolvedRoot ||+ !gulu.File.IsSubPath(resolvedHome, resolvedRoot) {+ return "", false+ }++ resolvedWorkspace := ResolveLongestExistingParent(workspacePath)+ if !filepath.IsAbs(resolvedWorkspace) {+ return "", false+ }+ return resolvedRoot, isSameOrSubPath(resolvedRoot, resolvedWorkspace)+}++func isSameOrSubPath(root, target string) bool {+ root = filepath.Clean(root)+ target = filepath.Clean(target)+ return root == target || gulu.File.IsSubPath(root, target)}func existAvailabilityStatus(workspaceAbsPath string) bool {