ListInfo 实现 BackendProtocol.ListInfo
(ctx context.Context, path string)
| 35 | |
| 36 | // ListInfo 实现 BackendProtocol.ListInfo |
| 37 | func (b *StateBackend) ListInfo(ctx context.Context, path string) ([]FileInfo, error) { |
| 38 | b.mu.RLock() |
| 39 | defer b.mu.RUnlock() |
| 40 | |
| 41 | if path == "" { |
| 42 | path = "/" |
| 43 | } |
| 44 | |
| 45 | // 规范化路径 |
| 46 | path = filepath.Clean(path) |
| 47 | if !strings.HasSuffix(path, "/") && path != "/" { |
| 48 | path += "/" |
| 49 | } |
| 50 | |
| 51 | var results []FileInfo |
| 52 | seen := make(map[string]bool) |
| 53 | |
| 54 | for filePath, data := range b.files { |
| 55 | // 检查是否在目标目录下 |
| 56 | if !strings.HasPrefix(filePath, path) && path != "/" { |
| 57 | continue |
| 58 | } |
| 59 | |
| 60 | // 获取相对路径 |
| 61 | relPath := filePath |
| 62 | if path != "/" { |
| 63 | relPath = strings.TrimPrefix(filePath, path) |
| 64 | } |
| 65 | |
| 66 | // 检查是否是直接子项 |
| 67 | parts := strings.Split(strings.Trim(relPath, "/"), "/") |
| 68 | if len(parts) == 0 { |
| 69 | continue |
| 70 | } |
| 71 | |
| 72 | name := parts[0] |
| 73 | if name == "" { |
| 74 | continue |
| 75 | } |
| 76 | |
| 77 | fullPath := filepath.Join(path, name) |
| 78 | if seen[fullPath] { |
| 79 | continue |
| 80 | } |
| 81 | seen[fullPath] = true |
| 82 | |
| 83 | // 判断是文件还是目录 |
| 84 | isDir := len(parts) > 1 |
| 85 | |
| 86 | if isDir { |
| 87 | // 目录项 |
| 88 | results = append(results, FileInfo{ |
| 89 | Path: fullPath, |
| 90 | IsDirectory: true, |
| 91 | Size: 0, |
| 92 | }) |
| 93 | } else { |
| 94 | // 文件项 |