From 0e9a6015e9935615e59cdf45db60005cc9f69d6f Mon Sep 17 00:00:00 2001 From: Laurence Tan Date: Mon, 24 Nov 2025 18:52:55 +1100 Subject: [PATCH] handle invalid-path errors (#1701) --- server/disk_space_primitives.go | 32 ++++++++++++++++++++++++++++ server/disk_space_primitives_test.go | 17 +++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/server/disk_space_primitives.go b/server/disk_space_primitives.go index 8fc6d73f..097b51e7 100644 --- a/server/disk_space_primitives.go +++ b/server/disk_space_primitives.go @@ -199,6 +199,12 @@ func (d *DiskSpacePrimitives) GetFileMeta(path string) (FileMeta, error) { if os.IsNotExist(err) { return FileMeta{}, ErrNotFound } + // Check if this is a filesystem syntax error (e.g., invalid characters) + // This typically happens with virtual pages like "tag:Something" which contain + // characters that are invalid in filesystem paths + if isSyntaxError(err) { + return FileMeta{}, ErrNotFound + } return FileMeta{}, fmt.Errorf("%w: %s", ErrCouldNotGetMeta, path) } @@ -218,12 +224,20 @@ func (d *DiskSpacePrimitives) ReadFile(path string) ([]byte, FileMeta, error) { if os.IsNotExist(err) { return nil, FileMeta{}, ErrNotFound } + // Check if this is a filesystem syntax error (e.g., invalid characters) + if isSyntaxError(err) { + return nil, FileMeta{}, ErrNotFound + } return nil, FileMeta{}, fmt.Errorf("failed to stat file %s: %w", path, err) } // Read file content data, err := os.ReadFile(localPath) if err != nil { + // Check if this is a filesystem syntax error + if isSyntaxError(err) { + return nil, FileMeta{}, ErrNotFound + } return nil, FileMeta{}, fmt.Errorf("failed to read file %s: %w", path, err) } @@ -304,3 +318,21 @@ func getCreationTime(info os.FileInfo) time.Time { return t.ChangeTime() } } + +// isSyntaxError checks if the error is due to invalid filename syntax +// This typically happens on virtual filesystems when trying to access files with characters +// like colons (e.g., "tag:Something") which are valid in SilverBullet virtual pages +// but not valid in some virtual filesystem paths +func isSyntaxError(err error) bool { + if err == nil { + return false + } + // Check for specific syntax errors in the error message + // We need to check the entire error chain, not just the top-level error + errMsg := err.Error() + return strings.Contains(errMsg, "syntax is incorrect") || + strings.Contains(errMsg, "syntax incorrect") || + (strings.Contains(errMsg, "syntax") && strings.Contains(errMsg, "incorrect")) || + strings.Contains(errMsg, "invalid argument") || + strings.Contains(errMsg, "bad file descriptor") +} diff --git a/server/disk_space_primitives_test.go b/server/disk_space_primitives_test.go index cbc0c705..5aeb4f72 100644 --- a/server/disk_space_primitives_test.go +++ b/server/disk_space_primitives_test.go @@ -255,3 +255,20 @@ func TestSymlinksAndInfiniteLoops(t *testing.T) { assert.NoError(t, err, "Should be able to read original file") assert.Equal(t, []byte("new content"), content, "Original file should be updated through symlink") } + +// Test that virtual pages (like tag:Something) return ErrNotFound instead of possible filesystem errors +func TestVirtualPages(t *testing.T) { + space, err := NewDiskSpacePrimitives(t.TempDir(), "") + assert.NoError(t, err, "Failed to create DiskSpacePrimitives") + + // Virtual pages with colons should return ErrNotFound, not a filesystem error + _, err = space.GetFileMeta("tag:Todo.md") + assert.Equal(t, ErrNotFound, err, "Virtual page should return ErrNotFound") + + _, err = space.GetFileMeta("search:this") + assert.Equal(t, ErrNotFound, err, "Virtual page should return ErrNotFound") + + _, _, err = space.ReadFile("tag:Todo.md") + assert.Equal(t, ErrNotFound, err, "Reading virtual page should return ErrNotFound") + +}