Search for files matching pattern. Uses subprocess list args to prevent injection.
(pattern: str, path: str = ".")
| 68 | return f"[ERROR] {e}" |
| 69 | |
| 70 | def search_files(pattern: str, path: str = ".") -> str: |
| 71 | """Search for files matching pattern. Uses subprocess list args to prevent injection.""" |
| 72 | try: |
| 73 | result = subprocess.run( |
| 74 | ["find", path, "-name", pattern], |
| 75 | capture_output=True, text=True, timeout=15 |
| 76 | ) |
| 77 | lines = (result.stdout + result.stderr).strip().splitlines() |
| 78 | lines = [l for l in lines if l.strip()][:50] |
| 79 | return "\n".join(lines) if lines else "(no matches)" |
| 80 | except subprocess.TimeoutExpired: |
| 81 | return "[ERROR] Search timed out" |
| 82 | except FileNotFoundError: |
| 83 | return "[ERROR] 'find' command not available" |
| 84 | except Exception as e: |
| 85 | return f"[ERROR] {e}" |