GlobInfo 实现 BackendProtocol.GlobInfo
(ctx context.Context, pattern, path string)
| 272 | |
| 273 | // GlobInfo 实现 BackendProtocol.GlobInfo |
| 274 | func (b *StateBackend) GlobInfo(ctx context.Context, pattern, path string) ([]FileInfo, error) { |
| 275 | b.mu.RLock() |
| 276 | defer b.mu.RUnlock() |
| 277 | |
| 278 | if path == "" { |
| 279 | path = "/" |
| 280 | } |
| 281 | |
| 282 | // 将 glob 模式转换为正则表达式 |
| 283 | regexPattern := globToRegex(pattern) |
| 284 | re, err := regexp.Compile(regexPattern) |
| 285 | if err != nil { |
| 286 | return nil, fmt.Errorf("invalid glob pattern: %w", err) |
| 287 | } |
| 288 | |
| 289 | var results []FileInfo |
| 290 | |
| 291 | for filePath, data := range b.files { |
| 292 | // 检查路径前缀 |
| 293 | if !strings.HasPrefix(filePath, path) && path != "/" { |
| 294 | continue |
| 295 | } |
| 296 | |
| 297 | // 匹配模式 |
| 298 | relPath := strings.TrimPrefix(filePath, path) |
| 299 | if re.MatchString(relPath) || re.MatchString(filePath) { |
| 300 | size := int64(0) |
| 301 | for _, line := range data.Lines { |
| 302 | size += int64(len(line) + 1) |
| 303 | } |
| 304 | |
| 305 | results = append(results, FileInfo{ |
| 306 | Path: filePath, |
| 307 | IsDirectory: false, |
| 308 | Size: size, |
| 309 | CreatedTime: data.CreatedAt, |
| 310 | ModifiedTime: data.ModifiedAt, |
| 311 | }) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return results, nil |
| 316 | } |
| 317 | |
| 318 | // globToRegex 将 glob 模式转换为正则表达式 |
| 319 | func globToRegex(glob string) string { |