ListInfo 实现 BackendProtocol.ListInfo
(ctx context.Context, path string)
| 27 | |
| 28 | // ListInfo 实现 BackendProtocol.ListInfo |
| 29 | func (b *FilesystemBackend) ListInfo(ctx context.Context, path string) ([]FileInfo, error) { |
| 30 | if path == "" { |
| 31 | path = "." |
| 32 | } |
| 33 | |
| 34 | // 解析为绝对路径 |
| 35 | absPath := b.fs.Resolve(path) |
| 36 | |
| 37 | // 检查路径是否在沙箱内 |
| 38 | if !b.fs.IsInside(absPath) { |
| 39 | return nil, fmt.Errorf("path outside sandbox: %s", path) |
| 40 | } |
| 41 | |
| 42 | // 读取目录 |
| 43 | entries, err := os.ReadDir(absPath) |
| 44 | if err != nil { |
| 45 | return nil, fmt.Errorf("read dir: %w", err) |
| 46 | } |
| 47 | |
| 48 | var results []FileInfo |
| 49 | for _, entry := range entries { |
| 50 | info, err := entry.Info() |
| 51 | if err != nil { |
| 52 | continue |
| 53 | } |
| 54 | |
| 55 | results = append(results, FileInfo{ |
| 56 | Path: filepath.Join(path, entry.Name()), |
| 57 | IsDirectory: entry.IsDir(), |
| 58 | Size: info.Size(), |
| 59 | ModifiedTime: info.ModTime(), |
| 60 | CreatedTime: info.ModTime(), // Go 的 FileInfo 不提供创建时间 |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | return results, nil |
| 65 | } |
| 66 | |
| 67 | // Read 实现 BackendProtocol.Read |
| 68 | func (b *FilesystemBackend) Read(ctx context.Context, path string, offset, limit int) (string, error) { |