-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.py
More file actions
1611 lines (1415 loc) · 60.5 KB
/
Copy pathreader.py
File metadata and controls
1611 lines (1415 loc) · 60.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
High-performance Linux filesystem reader for ext2, ext3, and ext4 filesystems.
Dual-engine:
1. Pure Python ext4 engine for fast in-memory traversal, streaming, and metadata.
2. debugfs CLI engine fallback for full compatibility with all Linux kernel features.
"""
import os
import io
import stat
import time
import zipfile
import datetime
import mimetypes
import subprocess
import fcntl
import struct
import uuid
import re
from typing import List, Dict, Any, Optional, Generator, Tuple, Set
import ext4
from scan_index import (
DiskScanIndex,
normalize_exts,
entry_matches_filters,
query_matches_text,
parse_date_filter_to_mtime,
BINARY_EXTENSIONS
)
DEBUGFS_BIN = "/opt/homebrew/opt/e2fsprogs/sbin/debugfs"
def format_bytes(size: int) -> str:
"""Format bytes into human-readable string (e.g. 1.2 MB)."""
if size < 1024:
return f"{size} B"
for unit in ["KB", "MB", "GB", "TB", "PB"]:
size /= 1024.0
if size < 1024.0 or unit == "PB":
return f"{size:.1f} {unit}"
return f"{size:.1f} B"
def format_hex_dump(data: bytes, base_offset: int = 0) -> List[Dict[str, str]]:
"""Generate a clean hex dump structure."""
lines = []
for i in range(0, len(data), 16):
chunk = data[i:i+16]
hex_bytes = " ".join(f"{b:02x}" for b in chunk)
# Pad hex part to 48 chars (16 * 3 - 1)
hex_part = f"{hex_bytes:<48}"
ascii_part = "".join(chr(b) if 32 <= b <= 126 else "." for b in chunk)
lines.append({
"offset": f"{base_offset + i:08x}",
"hex": hex_part,
"ascii": ascii_part
})
return lines
class BlockDeviceStream(io.RawIOBase):
"""
High-compatibility stream wrapper for raw disk block devices (/dev/disk*, /dev/rdisk*)
and disk images on macOS and Linux.
Background:
On macOS, calling lseek(fd, 0, SEEK_END) on block devices (/dev/diskX) or raw character
devices (/dev/rdiskX) returns 0. The ext4 library computes volume length using:
stream.seek(0, io.SEEK_END)
return stream.tell() - offset
When this returns 0, any subsequent seek (e.g. to superblock at offset 1024) triggers:
if seek < 0 or seek > len(self):
raise OSError(errno.EINVAL, "Invalid argument")
resulting in [Errno 22] Invalid argument.
This wrapper solves that by:
1. Detecting the true partition/disk capacity via macOS ioctls (DKIOCGETBLOCKSIZE &
DKIOCGETBLOCKCOUNT), Linux BLKGETSIZE64 ioctls, ext4 superblock geometry, or file seeks.
2. Virtualizing seek(offset, SEEK_END) to accurately report disk/partition length.
3. Providing automatic sector-aligned read fallback for raw /dev/rdisk character devices.
"""
def __init__(self, raw_file: io.BufferedReader, device_path: str = "", offset: int = 0, size: Optional[int] = None):
self.raw = raw_file
self.device_path = device_path or getattr(raw_file, "name", "")
self.offset = offset
self.pos = 0
if size is not None and size > 0:
self._size = size
else:
self._size = self._detect_size()
def _detect_size(self) -> int:
# 1. Try macOS ioctl (DKIOCGETBLOCKSIZE = 0x40046418, DKIOCGETBLOCKCOUNT = 0x40086419)
try:
DKIOCGETBLOCKSIZE = 0x40046418
DKIOCGETBLOCKCOUNT = 0x40086419
fd = self.raw.fileno()
buf4 = bytearray(4)
fcntl.ioctl(fd, DKIOCGETBLOCKSIZE, buf4)
bs = struct.unpack("I", buf4)[0]
buf8 = bytearray(8)
fcntl.ioctl(fd, DKIOCGETBLOCKCOUNT, buf8)
bc = struct.unpack("Q", buf8)[0]
total = bs * bc
if total > 0:
return total
except Exception:
pass
# 2. Try Linux ioctl (BLKGETSIZE64 = 0x80081272)
try:
BLKGETSIZE64 = 0x80081272
fd = self.raw.fileno()
buf8 = bytearray(8)
fcntl.ioctl(fd, BLKGETSIZE64, buf8)
sz = struct.unpack("Q", buf8)[0]
if sz > 0:
return sz
except Exception:
pass
# 3. Try standard file seek (works for disk images .img, .iso, .raw)
try:
curr = self.raw.tell()
self.raw.seek(0, io.SEEK_END)
s = self.raw.tell()
self.raw.seek(curr)
if s > 0:
return s
except Exception:
pass
# 4. Fallback: inspect ext4 superblock directly at self.offset + 1024
try:
self.raw.seek(self.offset + 1024)
sb = self.raw.read(1024)
if len(sb) >= 120 and sb[56:58] == b"\x53\xef":
s_blocks_count_lo = struct.unpack("<I", sb[4:8])[0]
s_log_block_size = struct.unpack("<I", sb[24:28])[0]
block_size = 1024 << s_log_block_size
s_blocks_count_hi = 0
if len(sb) >= 340:
s_blocks_count_hi = struct.unpack("<I", sb[336:340])[0]
total_blocks = (s_blocks_count_hi << 32) | s_blocks_count_lo
if total_blocks > 0:
return self.offset + (total_blocks * block_size)
except Exception:
pass
# 5. Fallback safe upper bound (16 TB)
return self.offset + (16 * 1024 * 1024 * 1024 * 1024)
def seekable(self) -> bool:
return True
def readable(self) -> bool:
return True
def writable(self) -> bool:
return False
def tell(self) -> int:
return self.pos
def seek(self, offset: int, whence: int = io.SEEK_SET) -> int:
if whence == io.SEEK_SET:
self.pos = offset
elif whence == io.SEEK_CUR:
self.pos += offset
elif whence == io.SEEK_END:
self.pos = self._size + offset
else:
raise ValueError(f"Invalid whence argument: {whence}")
self.pos = max(0, self.pos)
return self.pos
def read(self, size: int = -1) -> bytes:
if size == -1:
size = max(0, self._size - self.pos)
if size <= 0 or self.pos >= self._size:
return b""
size = min(size, max(0, self._size - self.pos))
try:
self.raw.seek(self.pos)
data = self.raw.read(size)
except OSError as e:
# Handle sector alignment requirement if raw device /dev/rdisk is used
if getattr(e, "errno", None) == 22:
sector_size = 4096
start_aligned = (self.pos // sector_size) * sector_size
end_aligned = ((self.pos + size + sector_size - 1) // sector_size) * sector_size
self.raw.seek(start_aligned)
chunk = self.raw.read(end_aligned - start_aligned)
rel = self.pos - start_aligned
data = chunk[rel:rel + size]
else:
raise
self.pos += len(data)
return data
def readinto(self, b) -> int:
data = self.read(len(b))
b[:len(data)] = data
return len(data)
def peek(self, size: int = 1) -> bytes:
curr_pos = self.pos
data = self.read(size)
self.pos = curr_pos
return data
def close(self):
if self.raw and not self.raw.closed:
self.raw.close()
@property
def closed(self) -> bool:
return self.raw.closed if self.raw else True
def fileno(self) -> int:
return self.raw.fileno()
def flush(self):
pass
class LinuxFileSystem:
def __init__(self, device_path: str, offset: int = 0):
# On macOS, block devices (/dev/diskX) provide standard seekable random read caching
if device_path.startswith("/dev/rdisk"):
block_dev = device_path.replace("/dev/rdisk", "/dev/disk")
if os.path.exists(block_dev):
device_path = block_dev
self.device_path = device_path
self.offset = offset
self.file_obj: Optional[BlockDeviceStream] = None
self.volume: Optional[ext4.Volume] = None
self.has_debugfs: bool = os.path.exists(DEBUGFS_BIN)
self.open()
def open(self):
"""Open the device or image file and initialize ext4 volume."""
try:
raw_file = open(self.device_path, "rb")
self.file_obj = BlockDeviceStream(raw_file, device_path=self.device_path, offset=self.offset)
except PermissionError as e:
raise PermissionError(
f"Cannot open '{self.device_path}': [Errno 13] Permission denied: '{self.device_path}'. "
f"macOS requires administrator privileges to access raw disk block devices."
) from e
except Exception as e:
raise RuntimeError(f"Cannot open '{self.device_path}': {e}")
# Validate superblock magic bytes before proceeding
try:
self.file_obj.seek(self.offset + 1024 + 56)
magic = self.file_obj.read(2)
if magic != b"\x53\xef":
from disk_detector import read_device_bytes
magic = read_device_bytes(self.device_path, self.offset + 1024 + 56, 2)
if magic != b"\x53\xef":
self.close()
hex_str = f"0x{magic.hex()}" if magic else "none"
raise ValueError(
f"Superblock magic {hex_str} at offset {self.offset} does not match ext4 magic 0xef53. "
f"'{self.device_path}' is not an ext2/3/4 filesystem."
)
except ValueError:
raise
except Exception as e:
self.close()
raise RuntimeError(f"Error inspecting filesystem header on '{self.device_path}': {e}")
try:
self.volume = ext4.Volume(
self.file_obj,
offset=self.offset,
ignore_magic=False,
ignore_checksum=True,
ignore_flags=True
)
except Exception as e:
self.close()
raise RuntimeError(f"Failed to initialize ext4 volume: {e}")
if not self.volume or not getattr(self.volume, "root", None):
self.close()
raise RuntimeError(f"ext4 volume failed to initialize on '{self.device_path}'")
# Initialize lightweight scan index
self.index: Optional[DiskScanIndex] = None
try:
sb = getattr(self.volume, "superblock", None)
uuid_bytes = bytes(sb.s_uuid) if sb and getattr(sb, "s_uuid", None) else None
uuid_str = str(uuid.UUID(bytes=uuid_bytes)) if uuid_bytes else os.path.basename(self.device_path)
wtime_str = str(getattr(sb, "s_wtime", "")) if sb else ""
vol_name = bytes(sb.s_volume_name).decode("latin1", errors="ignore").rstrip("\x00") if sb and getattr(sb, "s_volume_name", None) else "Linux Ext4"
self.index = DiskScanIndex(volume_id=uuid_str, last_write_time=wtime_str, volume_name=vol_name)
except Exception:
self.index = None
def close(self):
"""Close opened file handles."""
if self.file_obj and not self.file_obj.closed:
self.file_obj.close()
self.file_obj = None
self.volume = None
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def get_info(self) -> Dict[str, Any]:
"""Get filesystem superblock information."""
if self.volume and self.volume.superblock:
sb = self.volume.superblock
vol_name = bytes(sb.s_volume_name).decode("latin1", errors="ignore").rstrip("\x00")
block_size = self.volume.block_size
total_blocks = int(sb.s_blocks_count_lo)
free_blocks = int(sb.s_free_blocks_count_lo)
total_inodes = int(sb.s_inodes_count)
free_inodes = int(sb.s_free_inodes_count)
total_bytes = total_blocks * block_size
free_bytes = free_blocks * block_size
used_bytes = max(0, total_bytes - free_bytes)
percent_used = round((used_bytes / total_bytes * 100), 1) if total_bytes > 0 else 0
return {
"volume_name": vol_name or "Linux Filesystem",
"uuid": self.volume.uuid,
"block_size": block_size,
"total_blocks": total_blocks,
"free_blocks": free_blocks,
"total_bytes": total_bytes,
"free_bytes": free_bytes,
"used_bytes": used_bytes,
"percent_used": percent_used,
"total_human": format_bytes(total_bytes),
"free_human": format_bytes(free_bytes),
"used_human": format_bytes(used_bytes),
"total_inodes": total_inodes,
"free_inodes": free_inodes,
"used_inodes": total_inodes - free_inodes,
"inodes_percent": round(((total_inodes - free_inodes) / total_inodes * 100), 1) if total_inodes > 0 else 0,
"last_mount_time": datetime.datetime.fromtimestamp(sb.s_mtime, tz=datetime.timezone.utc).isoformat() if sb.s_mtime else None,
"last_write_time": datetime.datetime.fromtimestamp(sb.s_wtime, tz=datetime.timezone.utc).isoformat() if sb.s_wtime else None,
"mount_count": sb.s_mnt_count,
"max_mount_count": sb.s_max_mnt_count,
"filesystem_state": "Clean" if sb.s_state == 1 else "Has Errors",
"engine": "python-ext4"
}
# Fallback to direct inspection
from disk_detector import get_volume_info
info = get_volume_info(self.device_path, self.offset)
info["engine"] = "direct-superblock"
return info
def _resolve_inode(self, path: str) -> Tuple[Any, str]:
"""Resolve path to ext4 Inode and return (inode, normalized_path)."""
if not self.volume or not getattr(self.volume, "root", None):
raise RuntimeError(f"Filesystem volume not initialized for '{self.device_path}'. Please ensure a valid ext2/ext3/ext4 partition is selected.")
norm_path = "/" + "/".join([p for p in path.strip("/").split("/") if p and p != "."])
if norm_path == "/":
return self.volume.root, "/"
# Traverse path components
parts = [p for p in norm_path.split("/") if p]
curr_inode = self.volume.root
for part in parts:
if not isinstance(curr_inode, ext4.Directory):
raise FileNotFoundError(f"Not a directory: {part}")
found = False
for dirent, ft in curr_inode.opendir():
if dirent.name_str == part:
curr_inode = self.volume.inodes[dirent.inode]
found = True
break
if not found:
raise FileNotFoundError(f"Path component not found: {part}")
return curr_inode, norm_path
def listdir(self, path: str = "/") -> List[Dict[str, Any]]:
"""List directory contents with complete metadata."""
inode, norm_path = self._resolve_inode(path)
if not isinstance(inode, ext4.Directory):
raise NotADirectoryError(f"'{path}' is not a directory")
entries = []
for dirent, ft in inode.opendir():
name = dirent.name_str
if name in (".", ".."):
continue
try:
child_inode = self.volume.inodes[dirent.inode]
mode_int = child_inode.i_mode
mode_str = stat.filemode(mode_int)
# Determine file type
file_type = "file"
if stat.S_ISDIR(mode_int):
file_type = "directory"
elif stat.S_ISLNK(mode_int):
file_type = "symlink"
elif stat.S_ISCHR(mode_int):
file_type = "char_device"
elif stat.S_ISBLK(mode_int):
file_type = "block_device"
elif stat.S_ISFIFO(mode_int):
file_type = "fifo"
elif stat.S_ISSOCK(mode_int):
file_type = "socket"
# Symlink target
target = None
if file_type == "symlink" and isinstance(child_inode, ext4.SymbolicLink):
try:
target = child_inode.readlink().decode("utf-8", errors="replace")
except Exception:
pass
# Size
file_size = getattr(child_inode, "i_size", 0) if file_type != "directory" else 4096
# Timestamps
mtime_iso = None
atime_iso = None
ctime_iso = None
if child_inode.i_mtime:
mtime_iso = datetime.datetime.fromtimestamp(child_inode.i_mtime, tz=datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
if child_inode.i_atime:
atime_iso = datetime.datetime.fromtimestamp(child_inode.i_atime, tz=datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
if child_inode.i_ctime:
ctime_iso = datetime.datetime.fromtimestamp(child_inode.i_ctime, tz=datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
item_path = f"{norm_path.rstrip('/')}/{name}"
entries.append({
"name": name,
"path": item_path,
"type": file_type,
"size": file_size,
"size_human": format_bytes(file_size) if file_type != "directory" else "-",
"mode": mode_str,
"uid": child_inode.i_uid,
"gid": child_inode.i_gid,
"inode": dirent.inode,
"mtime": mtime_iso,
"atime": atime_iso,
"ctime": ctime_iso,
"target": target
})
except Exception as e:
# Still list entry even if inode parsing hit error
item_path = f"{norm_path.rstrip('/')}/{name}"
entries.append({
"name": name,
"path": item_path,
"type": "unknown",
"size": 0,
"size_human": "-",
"mode": "?????????",
"uid": 0,
"gid": 0,
"inode": dirent.inode,
"mtime": None,
"error": str(e)
})
# Sort: directories first, then alphabetical
entries.sort(key=lambda x: (0 if x["type"] == "directory" else 1, x["name"].lower()))
if self.index:
self.index.save_entries(entries, path)
return entries
def read_file_by_inode(self, inode_no: int, offset: int = 0, limit: Optional[int] = None) -> bytes:
"""Directly read bytes from an inode without re-traversing directory trees."""
if not self.volume or not getattr(self.volume, "inodes", None):
return b""
try:
inode = self.volume.inodes[inode_no]
if not isinstance(inode, ext4.File):
return b""
with inode.open() as f:
if offset > 0:
f.seek(offset)
if limit is not None:
return f.read(limit)
return f.read()
except Exception:
return b""
def read_file(self, path: str, offset: int = 0, limit: Optional[int] = None) -> bytes:
"""Read bytes from a file."""
inode, _ = self._resolve_inode(path)
if not isinstance(inode, ext4.File):
raise IsADirectoryError(f"'{path}' is not a regular file")
with inode.open() as f:
if offset > 0:
f.seek(offset)
if limit is not None:
return f.read(limit)
return f.read()
def stream_file(self, path: str, chunk_size: int = 65536) -> Generator[bytes, None, None]:
"""Stream file contents in chunks."""
inode, _ = self._resolve_inode(path)
if not isinstance(inode, ext4.File):
raise IsADirectoryError(f"'{path}' is not a regular file")
with inode.open() as f:
while True:
chunk = f.read(chunk_size)
if not chunk:
break
yield chunk
def get_preview(self, path: str, max_bytes: int = 262144) -> Dict[str, Any]:
"""Read preview data: text with syntax detection, image preview, or hex dump."""
inode, norm_path = self._resolve_inode(path)
file_size = getattr(inode, "i_size", 0)
mime, _ = mimetypes.guess_type(path)
data = b""
with inode.open() as f:
data = f.read(max_bytes)
is_truncated = file_size > max_bytes
is_binary = b"\x00" in data[:1024]
# Check if text
content_text = None
if not is_binary:
try:
content_text = data.decode("utf-8")
except UnicodeDecodeError:
try:
content_text = data.decode("latin-1")
except Exception:
is_binary = True
import base64
content_base64 = None
if is_binary and mime and mime.startswith("image/"):
content_base64 = f"data:{mime};base64," + base64.b64encode(data).decode("ascii")
hex_lines = format_hex_dump(data[:4096])
mode_str = stat.filemode(inode.i_mode)
mtime_iso = datetime.datetime.fromtimestamp(inode.i_mtime, tz=datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S") if inode.i_mtime else None
return {
"name": os.path.basename(norm_path),
"path": norm_path,
"size": file_size,
"size_human": format_bytes(file_size),
"is_binary": is_binary,
"is_truncated": is_truncated,
"mime_type": mime or ("text/plain" if not is_binary else "application/octet-stream"),
"content_text": content_text,
"content_base64": content_base64,
"hex_dump": hex_lines,
"metadata": {
"inode": inode.i_no,
"mode": mode_str,
"uid": inode.i_uid,
"gid": inode.i_gid,
"mtime": mtime_iso,
"links": inode.i_links_count,
"flags": hex(inode.i_flags) if hasattr(inode, "i_flags") else "0x0"
}
}
def generate_zip_stream(self, folder_path: str = "/") -> Generator[bytes, None, None]:
"""Generate a streaming zip archive of a directory."""
buffer = io.BytesIO()
zf = zipfile.ZipFile(buffer, mode="w", compression=zipfile.ZIP_DEFLATED)
def add_dir(dir_path: str, rel_prefix: str):
entries = self.listdir(dir_path)
for entry in entries:
rel_path = os.path.join(rel_prefix, entry["name"])
if entry["type"] == "directory":
zf.writestr(f"{rel_path}/", b"")
add_dir(entry["path"], rel_path)
elif entry["type"] == "file":
try:
content = self.read_file(entry["path"])
zf.writestr(rel_path, content)
except Exception:
pass
root_name = os.path.basename(folder_path.rstrip("/")) or "root"
add_dir(folder_path, root_name)
zf.close()
buffer.seek(0)
while True:
chunk = buffer.read(65536)
if not chunk:
break
yield chunk
def search(
self,
query: str,
root_path: str = "/",
max_results: int = 150,
include_exts: Optional[Any] = None,
exclude_exts: Optional[Any] = None,
type_filter: str = "all",
min_size: Optional[int] = None,
max_size: Optional[int] = None,
date_filter: Optional[str] = None,
case_sensitive: bool = False,
whole_word: bool = False,
use_regex: bool = False
) -> List[Dict[str, Any]]:
"""Recursively search for matching filenames with filter criteria."""
mtime_after = parse_date_filter_to_mtime(date_filter)
if getattr(self, "index", None) and self.index.is_scan_complete(root_path):
return self.index.search_names(
query=query,
root_path=root_path,
max_results=max_results,
include_exts=include_exts,
exclude_exts=exclude_exts,
type_filter=type_filter,
min_size=min_size,
max_size=max_size,
mtime_after=mtime_after,
case_sensitive=case_sensitive,
whole_word=whole_word,
use_regex=use_regex
)
clean_q = query.strip() if query else ""
inc_set = normalize_exts(include_exts)
exc_set = normalize_exts(exclude_exts)
if not clean_q and not inc_set and not exc_set and type_filter == "all" and min_size is None and max_size is None and not mtime_after:
return []
results = []
compiled_rgx = None
if use_regex and clean_q:
try:
flags = 0 if case_sensitive else re.IGNORECASE
compiled_rgx = re.compile(clean_q, flags)
except Exception:
compiled_rgx = None
def scan_dir(dir_path: str):
if len(results) >= max_results:
return
try:
entries = self.listdir(dir_path)
for entry in entries:
name = entry["name"]
p = entry["path"]
t = entry.get("type", "file")
sz = entry.get("size", 0)
mt = entry.get("mtime")
if t == "directory" and exc_set:
if name.lower() in exc_set:
continue
if not entry_matches_filters(
name=name,
path=p,
item_type=t,
size=sz,
mtime=mt,
include_exts=inc_set,
exclude_exts=exc_set,
type_filter=type_filter,
min_size=min_size,
max_size=max_size,
mtime_after=mtime_after
):
if t == "directory":
scan_dir(p)
continue
if not clean_q or query_matches_text(
name, clean_q,
case_sensitive=case_sensitive,
whole_word=whole_word,
use_regex=use_regex,
compiled_regex=compiled_rgx
):
results.append(entry)
if len(results) >= max_results:
return
if t == "directory":
scan_dir(p)
except Exception:
pass
scan_dir(root_path)
return results
def grep_content(
self,
query: str,
root_path: str = "/",
recursive: bool = False,
case_sensitive: bool = False,
whole_word: bool = False,
use_regex: bool = False,
include_exts: Optional[Any] = None,
exclude_exts: Optional[Any] = None,
min_size: Optional[int] = None,
max_size: Optional[int] = None,
date_filter: Optional[str] = None,
max_file_size: int = 5 * 1024 * 1024,
max_results: int = 100
) -> List[Dict[str, Any]]:
"""Search inside text files for a query string with filter options."""
results = []
if not query:
return results
mtime_after = parse_date_filter_to_mtime(date_filter)
inc_set = normalize_exts(include_exts)
exc_set = normalize_exts(exclude_exts)
target = query if case_sensitive else query.lower()
target_bytes = query.encode("utf-8") if case_sensitive else query.lower().encode("utf-8")
rgx_pattern = None
if use_regex:
try:
flags = 0 if case_sensitive else re.IGNORECASE
rgx_pattern = re.compile(query, flags)
except Exception:
rgx_pattern = None
elif whole_word:
try:
flags = 0 if case_sensitive else re.IGNORECASE
rgx_pattern = re.compile(r'(?:\b|_|^)' + re.escape(query) + r'(?:\b|_|$)', flags)
except Exception:
rgx_pattern = None
def check_file(entry: Dict[str, Any]):
if len(results) >= max_results:
return
file_path = entry["path"]
file_name = entry["name"]
size = entry.get("size", 0)
# Check filters
if not entry_matches_filters(
name=file_name,
path=file_path,
item_type="file",
size=size,
mtime=entry.get("mtime"),
include_exts=inc_set,
exclude_exts=exc_set,
type_filter="file",
min_size=min_size,
max_size=max_size or max_file_size,
mtime_after=mtime_after
):
return
ext = os.path.splitext(file_name)[1].lower()
if not inc_set and (ext in BINARY_EXTENSIONS or size > max_file_size):
return
try:
inode_no = entry.get("inode")
if inode_no:
chunk = self.read_file_by_inode(inode_no, limit=min(size, 2 * 1024 * 1024))
else:
chunk = self.read_file(file_path, limit=min(size, 2 * 1024 * 1024))
if not chunk or b"\x00" in chunk[:1024]:
return
if not rgx_pattern:
compare_bytes = chunk if case_sensitive else chunk.lower()
if target_bytes not in compare_bytes:
return
try:
text = chunk.decode("utf-8")
except UnicodeDecodeError:
try:
text = chunk.decode("latin-1")
except Exception:
return
lines = text.splitlines()
matching_lines = []
for line_idx, line in enumerate(lines, 1):
compare_line = line if case_sensitive else line.lower()
matched = False
start_idx = 0
match_len = len(query)
if rgx_pattern:
m = rgx_pattern.search(line)
if m:
matched = True
start_idx = m.start()
match_len = max(1, m.end() - m.start())
else:
idx = compare_line.find(target)
if idx >= 0:
matched = True
start_idx = idx
if matched:
snippet = line.strip()
if len(snippet) > 160:
start = max(0, start_idx - 40)
end = min(len(snippet), start_idx + match_len + 80)
snippet = ("..." if start > 0 else "") + snippet[start:end] + ("..." if end < len(snippet) else "")
matching_lines.append({
"line": line_idx,
"snippet": snippet
})
if len(matching_lines) >= 5:
break
if matching_lines:
results.append({
"name": file_name,
"path": file_path,
"type": "file",
"size": size,
"size_human": format_bytes(size),
"mode": entry.get("mode", "-rw-r--r--"),
"uid": entry.get("uid", 0),
"gid": entry.get("gid", 0),
"inode": entry.get("inode", 0),
"mtime": entry.get("mtime"),
"match_count": len(matching_lines),
"matches": matching_lines
})
except Exception:
pass
if getattr(self, "index", None) and self.index.is_scan_complete(root_path):
candidate_files = self.index.get_candidate_grep_files(
root_path=root_path,
max_size=max_size or max_file_size,
min_size=min_size,
include_exts=inc_set,
exclude_exts=exc_set,
mtime_after=mtime_after
)
for entry in candidate_files:
check_file(entry)
if len(results) >= max_results:
break
return results
def scan_dir(dir_path: str):
if len(results) >= max_results:
return
try:
entries = self.listdir(dir_path)
for entry in entries:
if entry["type"] == "file":
check_file(entry)
if len(results) >= max_results:
return
if recursive:
for entry in entries:
if entry["type"] == "directory":
if exc_set and entry["name"].lower() in exc_set:
continue
if entry["name"] not in ("proc", "sys", "dev", "lost+found"):
scan_dir(entry["path"])
if len(results) >= max_results:
return
except Exception:
pass
scan_dir(root_path)
return results
def grep_content_stream(
self,
query: str,
root_path: str = "/",
recursive: bool = False,
case_sensitive: bool = False,
whole_word: bool = False,
use_regex: bool = False,
include_exts: Optional[Any] = None,
exclude_exts: Optional[Any] = None,
min_size: Optional[int] = None,
max_size: Optional[int] = None,
date_filter: Optional[str] = None,
max_file_size: int = 5 * 1024 * 1024,
max_results: int = 250,
stop_event: Optional[threading.Event] = None
) -> Generator[Dict[str, Any], None, None]:
"""Stream search progress and matches as a generator with live cancellation support."""
if not query:
yield {"type": "done", "scanned": 0, "total": 0, "matches_count": 0, "elapsed": 0.0, "speed": 0}
return
mtime_after = parse_date_filter_to_mtime(date_filter)
inc_set = normalize_exts(include_exts)
exc_set = normalize_exts(exclude_exts)
target = query if case_sensitive else query.lower()
target_bytes = query.encode("utf-8") if case_sensitive else query.lower().encode("utf-8")
t0 = time.time()
rgx_pattern = None
if use_regex:
try:
flags = 0 if case_sensitive else re.IGNORECASE
rgx_pattern = re.compile(query, flags)
except Exception:
rgx_pattern = None
elif whole_word:
try:
flags = 0 if case_sensitive else re.IGNORECASE
rgx_pattern = re.compile(r'(?:\b|_|^)' + re.escape(query) + r'(?:\b|_|$)', flags)
except Exception:
rgx_pattern = None
# Pre-fetch initial directory to avoid double listdir on root_path
initial_entries = None
total_files = None
try:
initial_entries = self.listdir(root_path)
if not recursive:
total_files = len([e for e in initial_entries if e.get("type") == "file"])
except Exception:
initial_entries = None
yield {
"type": "start",
"query": query,
"root_path": root_path,
"recursive": recursive,
"total": total_files,
"current_file": f"Scanning {root_path}..."
}
scanned = 0
matches_count = 0
last_yield_time = t0
def check_file(entry: Dict[str, Any]) -> Optional[Dict[str, Any]]:
file_path = entry["path"]
file_name = entry["name"]
size = entry.get("size", 0)
# Check filters
if not entry_matches_filters(
name=file_name,
path=file_path,
item_type="file",
size=size,
mtime=entry.get("mtime"),
include_exts=inc_set,
exclude_exts=exc_set,
type_filter="file",
min_size=min_size,
max_size=max_size or max_file_size,
mtime_after=mtime_after
):
return None
ext = os.path.splitext(file_name)[1].lower()
if not inc_set and (ext in BINARY_EXTENSIONS or size > max_file_size):
return None
try:
inode_no = entry.get("inode")