fix(local): add IsDir checks to Read/Write/Edit to prevent directory-as-file errors - #932
Open
luchun19921229 wants to merge 1 commit into
Open
fix(local): add IsDir checks to Read/Write/Edit to prevent directory-as-file errors#932luchun19921229 wants to merge 1 commit into
luchun19921229 wants to merge 1 commit into
Conversation
…a loss
Three methods accepted directory paths without returning an error:
- Read(): file.Stat() checked Size()==0 but not IsDir(), returning empty
FileContent{} for directories (silent data loss).
- Write(): os.OpenFile(dir, O_WRONLY|O_CREATE|O_TRUNC) returns EISDIR on
Unix with a confusing error message. Added explicit Stat+IsDir check.
- Edit(): os.ReadFile(dir) returns empty content, then strings.Count()
reports "string not found" — misleading error. Added explicit Stat+IsDir
check before ReadFile.
readAllBytes() already has the info.IsDir() guard, so MultiModalRead is
already protected. This aligns Read/Write/Edit with that pattern.
Author
|
@cloudwego/eino-maintainers 请 review 这个 PR。CI 全绿,改动很简单:在 |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a user passes a directory path to
Local.Read(),Local.Write(), orLocal.Edit(), the methods attempt to use the path as a file — opening, reading, or writing it — which produces confusing low-level errors (e.g.is a directoryfrom the OS) or silent failures instead of a clear explanation.Changes
Added a guard check in each of the three methods that returns a clear error message when the target path is a directory rather than a file:
Read()file.Stat()beforeinfo.Size() == 0check"path is a directory, not a file: <path>"Write()os.Stat(path)beforeMkdirAll/OpenFile"path is a directory, not a file: <path>"Edit()os.Stat(path)beforeos.ReadFile(path)"path is a directory, not a file: <path>"Testing