barecat 0.3.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. barecat/__init__.py +108 -0
  2. barecat/__main__.py +5 -0
  3. barecat/_api.py +143 -0
  4. barecat/_version.py +24 -0
  5. barecat/cli/__init__.py +0 -0
  6. barecat/cli/commands.py +540 -0
  7. barecat/cli/completions.py +27 -0
  8. barecat/cli/impl.py +609 -0
  9. barecat/cli/legacy.py +361 -0
  10. barecat/cli/main.py +1355 -0
  11. barecat/cli/shell.py +827 -0
  12. barecat/completions/__init__.py +1 -0
  13. barecat/completions/barecat.bash +246 -0
  14. barecat/completions/barecat.zsh +268 -0
  15. barecat/core/__init__.py +0 -0
  16. barecat/core/barecat.py +1209 -0
  17. barecat/core/index.py +1983 -0
  18. barecat/core/paths.py +61 -0
  19. barecat/core/sharder.py +328 -0
  20. barecat/core/types.py +340 -0
  21. barecat/exceptions.py +92 -0
  22. barecat/formats/__init__.py +0 -0
  23. barecat/formats/archive_formats.py +234 -0
  24. barecat/formats/from_tar_stream.py +53 -0
  25. barecat/formats/to_tar_stream.py +54 -0
  26. barecat/io/__init__.py +5 -0
  27. barecat/io/codecs.py +622 -0
  28. barecat/io/copyfile.py +550 -0
  29. barecat/io/fileobj.py +556 -0
  30. barecat/io/open.py +41 -0
  31. barecat/maintenance/__init__.py +0 -0
  32. barecat/maintenance/complete_database.py +79 -0
  33. barecat/maintenance/defrag.py +541 -0
  34. barecat/maintenance/merge.py +613 -0
  35. barecat/maintenance/reshard.py +153 -0
  36. barecat/maintenance/rsync.py +1282 -0
  37. barecat/maintenance/upgrade_database.py +200 -0
  38. barecat/maintenance/upgrade_database2.py +74 -0
  39. barecat/man/barecat.1 +284 -0
  40. barecat/sql/indexes.sql +6 -0
  41. barecat/sql/schema.sql +55 -0
  42. barecat/sql/triggers.sql +172 -0
  43. barecat/tui/__init__.py +0 -0
  44. barecat/tui/browse.py +448 -0
  45. barecat/tui/ncdu.py +318 -0
  46. barecat/util/__init__.py +0 -0
  47. barecat/util/consumed_threadpool.py +119 -0
  48. barecat/util/glob_helper.py +584 -0
  49. barecat/util/glob_to_regex.py +353 -0
  50. barecat/util/list_files.py +12 -0
  51. barecat/util/misc.py +174 -0
  52. barecat/util/physical_order.py +77 -0
  53. barecat/util/progbar.py +40 -0
  54. barecat/util/threading.py +59 -0
  55. barecat-0.3.2.dist-info/METADATA +276 -0
  56. barecat-0.3.2.dist-info/RECORD +60 -0
  57. barecat-0.3.2.dist-info/WHEEL +5 -0
  58. barecat-0.3.2.dist-info/entry_points.txt +15 -0
  59. barecat-0.3.2.dist-info/licenses/LICENSE +21 -0
  60. barecat-0.3.2.dist-info/top_level.txt +1 -0
barecat/cli/main.py ADDED
@@ -0,0 +1,1355 @@
1
+ """Unified CLI for barecat with subcommands."""
2
+
3
+ import argparse
4
+ import csv
5
+ import logging
6
+ import re
7
+ import sys
8
+
9
+ from ..util.glob_to_regex import glob_to_regex
10
+
11
+ import barecat
12
+ from ..cli import impl as impl
13
+ from ..util import misc
14
+ from ..util import physical_order as barecat_physical_order
15
+ from ..core.types import Order, SHARD_SIZE_UNLIMITED
16
+ from ..core.paths import resolve_index_path
17
+ from ..maintenance.defrag import BarecatDefragger
18
+ from ..util.misc import parse_size
19
+
20
+
21
+ def parse_shard_size(value):
22
+ """Parse shard size, defaulting to unlimited if None."""
23
+ if value is None:
24
+ return SHARD_SIZE_UNLIMITED
25
+ return parse_size(value)
26
+
27
+
28
+ def _extract_directory_from_argv():
29
+ """Extract -C/--directory value from sys.argv before argparse parsing.
30
+
31
+ This allows -C to appear after positional arguments (like tar), which
32
+ argparse doesn't support natively. We remove the -C flag and its value
33
+ from sys.argv so argparse doesn't see it twice.
34
+
35
+ Returns:
36
+ The directory path if -C/--directory was found, None otherwise.
37
+ """
38
+ argv = sys.argv
39
+ directory = None
40
+ indices_to_remove = []
41
+ for i, arg in enumerate(argv):
42
+ if arg in ('-C', '--directory') and i + 1 < len(argv):
43
+ directory = argv[i + 1]
44
+ indices_to_remove.extend([i, i + 1])
45
+ elif arg.startswith('--directory='):
46
+ directory = arg.split('=', 1)[1]
47
+ indices_to_remove.append(i)
48
+ # Remove from argv in reverse order to preserve indices
49
+ for i in sorted(indices_to_remove, reverse=True):
50
+ del sys.argv[i]
51
+ return directory
52
+
53
+
54
+ def _print_defrag_stats(stats):
55
+ """Print defragmentation statistics."""
56
+
57
+ def fmt(size):
58
+ return _format_size(size, human_readable=True)
59
+
60
+ print(f"Physical size: {fmt(stats['physical_size'])}")
61
+ print(f"Logical size: {fmt(stats['logical_size'])}")
62
+ print(f"Freeable space: {fmt(stats['total_gap_size'])}")
63
+ print(f"Number of gaps: {stats['num_gaps']}")
64
+ print(f"Fragmentation ratio: {stats['fragmentation_ratio']:.3f}")
65
+
66
+ if stats['gap_sizes']:
67
+ gap_sizes = sorted(stats['gap_sizes'])
68
+ n = len(gap_sizes)
69
+
70
+ print('\nGap size distribution:')
71
+ print(f' Min: {fmt(gap_sizes[0])}')
72
+ print(f' p25: {fmt(gap_sizes[n // 4])}')
73
+ print(f' p50: {fmt(gap_sizes[n // 2])}')
74
+ print(f' p75: {fmt(gap_sizes[3 * n // 4])}')
75
+ print(f' p90: {fmt(gap_sizes[int(n * 0.9)])}')
76
+ print(f' p99: {fmt(gap_sizes[int(n * 0.99)])}')
77
+ print(f' Max: {fmt(gap_sizes[-1])}')
78
+ print(f' Mean: {fmt(sum(gap_sizes) / n)}')
79
+
80
+ print('\nGaps by shard:')
81
+ for shard, gaps in sorted(stats['gaps_by_shard'].items()):
82
+ total = sum(size for _, size in gaps)
83
+ print(f' Shard {shard}: {len(gaps)} gaps, {fmt(total)} total')
84
+
85
+
86
+ def main():
87
+ parser = argparse.ArgumentParser(
88
+ prog='barecat',
89
+ description='Scalable archive format for storing millions of small files.',
90
+ )
91
+ subparsers = parser.add_subparsers(dest='command', title='commands')
92
+
93
+ # create - create new archive
94
+ p = subparsers.add_parser(
95
+ 'create',
96
+ aliases=['c'],
97
+ help='Create new archive (error if exists, use -f to overwrite)',
98
+ )
99
+ p.add_argument('archive', type=str, help='Barecat archive path')
100
+ p.add_argument('paths', type=str, nargs='*', help='Files/directories to add')
101
+ p.add_argument(
102
+ '-T',
103
+ '--files-from',
104
+ type=str,
105
+ metavar='FILE',
106
+ help='Read paths from FILE (use - for stdin)',
107
+ )
108
+ p.add_argument(
109
+ '-0',
110
+ '--null',
111
+ action='store_true',
112
+ help='Paths are null-separated (for use with find -print0)',
113
+ )
114
+ p.add_argument(
115
+ '-C',
116
+ '--directory',
117
+ type=str,
118
+ default=None,
119
+ metavar='DIR',
120
+ help='Change to DIR before adding files',
121
+ )
122
+ p.add_argument(
123
+ '--exclude',
124
+ action='append',
125
+ default=[],
126
+ metavar='PATTERN',
127
+ help='Exclude files matching glob pattern, use **/ for recursive (repeatable)',
128
+ )
129
+ p.add_argument(
130
+ '-i',
131
+ '--include',
132
+ action='append',
133
+ default=[],
134
+ metavar='PATTERN',
135
+ help='Only include files matching glob pattern, use **/ for recursive (repeatable)',
136
+ )
137
+ p.add_argument('-j', '--workers', type=int, default=None, help='Number of worker threads')
138
+ p.add_argument('-f', '--force', action='store_true', help='Overwrite existing archive')
139
+ p.add_argument(
140
+ '-s',
141
+ '--shard-size-limit',
142
+ type=str,
143
+ default=None,
144
+ metavar='SIZE',
145
+ help='Shard size limit (e.g., 1G, 500M)',
146
+ )
147
+ p.add_argument(
148
+ '--physical-order', action='store_true', help='Add files in physical disk order (for HDDs)'
149
+ )
150
+
151
+ # add - add to existing archive
152
+ p = subparsers.add_parser(
153
+ 'add',
154
+ aliases=['a'],
155
+ help='Add files to existing archive (use -c to create if missing)',
156
+ )
157
+ p.add_argument('archive', type=str, help='Barecat archive path')
158
+ p.add_argument('paths', type=str, nargs='*', help='Files/directories to add')
159
+ p.add_argument(
160
+ '-T',
161
+ '--files-from',
162
+ type=str,
163
+ metavar='FILE',
164
+ help='Read paths from FILE (use - for stdin)',
165
+ )
166
+ p.add_argument(
167
+ '-0',
168
+ '--null',
169
+ action='store_true',
170
+ help='Paths are null-separated (for use with find -print0)',
171
+ )
172
+ p.add_argument(
173
+ '-C',
174
+ '--directory',
175
+ type=str,
176
+ default=None,
177
+ metavar='DIR',
178
+ help='Change to DIR before adding files',
179
+ )
180
+ p.add_argument(
181
+ '--exclude',
182
+ action='append',
183
+ default=[],
184
+ metavar='PATTERN',
185
+ help='Exclude files matching glob pattern, use **/ for recursive (repeatable)',
186
+ )
187
+ p.add_argument(
188
+ '-i',
189
+ '--include',
190
+ action='append',
191
+ default=[],
192
+ metavar='PATTERN',
193
+ help='Only include files matching glob pattern, use **/ for recursive (repeatable)',
194
+ )
195
+ p.add_argument('-j', '--workers', type=int, default=None, help='Number of worker threads')
196
+ p.add_argument(
197
+ '-c', '--create', action='store_true', help='Create archive if it does not exist'
198
+ )
199
+ p.add_argument(
200
+ '-s',
201
+ '--shard-size-limit',
202
+ type=str,
203
+ default=None,
204
+ metavar='SIZE',
205
+ help='Shard size limit for new archive (e.g., 1G, 500M)',
206
+ )
207
+ p.add_argument(
208
+ '--physical-order', action='store_true', help='Add files in physical disk order (for HDDs)'
209
+ )
210
+
211
+ # extract
212
+ p = subparsers.add_parser(
213
+ 'extract',
214
+ aliases=['x'],
215
+ help='Extract files from archive',
216
+ )
217
+ p.add_argument('archive', type=str, help='Barecat archive path')
218
+ p.add_argument('paths', type=str, nargs='*', help='Paths to extract (default: all)')
219
+ p.add_argument(
220
+ '-C',
221
+ '--directory',
222
+ type=str,
223
+ default='.',
224
+ help='Extract to directory (default: current dir)',
225
+ )
226
+ p.add_argument(
227
+ '--pattern',
228
+ type=str,
229
+ default=None,
230
+ metavar='GLOB',
231
+ help='Only include files matching glob pattern (incompatible with -i/-x)',
232
+ )
233
+ p.add_argument(
234
+ '--exclude',
235
+ '-x',
236
+ action='append',
237
+ default=[],
238
+ metavar='PATTERN',
239
+ help='Exclude files matching glob pattern (repeatable)',
240
+ )
241
+ p.add_argument(
242
+ '--include',
243
+ '-i',
244
+ action='append',
245
+ default=[],
246
+ metavar='PATTERN',
247
+ help='Only include files matching glob pattern (repeatable)',
248
+ )
249
+
250
+ # list
251
+ p = subparsers.add_parser(
252
+ 'list',
253
+ aliases=['ls', 'l', 't'],
254
+ help='List archive contents',
255
+ )
256
+ p.add_argument('archive', type=str, help='Barecat archive path')
257
+ p.add_argument(
258
+ 'paths', type=str, nargs='*', help='Paths or glob patterns to list (default: root)'
259
+ )
260
+ p.add_argument('-l', '--long', action='store_true', help='Long listing with sizes')
261
+ p.add_argument('-R', '--recursive', action='store_true', help='List recursively')
262
+ p.add_argument(
263
+ '--jsonl', action='store_true', help='Output as JSON lines (one JSON object per line)'
264
+ )
265
+
266
+ # cat - print file to stdout
267
+ p = subparsers.add_parser(
268
+ 'cat',
269
+ help='Print file contents to stdout',
270
+ )
271
+ p.add_argument('archive', type=str, help='Barecat archive path')
272
+ p.add_argument('path', type=str, help='Path within archive')
273
+
274
+ # find - search for files
275
+ p = subparsers.add_parser(
276
+ 'find',
277
+ help='Search for files in archive (like /usr/bin/find)',
278
+ )
279
+ p.add_argument('archive', type=str, help='Barecat archive path')
280
+ p.add_argument('path', type=str, nargs='?', default='', help='Starting path (default: root)')
281
+ p.add_argument(
282
+ '-name', type=str, metavar='PATTERN', help='Match basename against glob pattern'
283
+ )
284
+ p.add_argument(
285
+ '-path',
286
+ '-wholename',
287
+ type=str,
288
+ metavar='PATTERN',
289
+ dest='pathpattern',
290
+ help='Match full path against glob pattern',
291
+ )
292
+ p.add_argument(
293
+ '-type', type=str, choices=['f', 'd'], dest='ftype', help='File type: f=file, d=directory'
294
+ )
295
+ p.add_argument(
296
+ '-size',
297
+ type=str,
298
+ metavar='[+-]N[kMG]',
299
+ help='Size filter: +N (larger), -N (smaller), N (exact)',
300
+ )
301
+ p.add_argument('-maxdepth', type=int, metavar='N', help='Maximum depth to descend')
302
+ p.add_argument('-print0', action='store_true', help='Print paths separated by null character')
303
+
304
+ # shell
305
+ p = subparsers.add_parser('shell', help='Interactive shell for exploring archives')
306
+ p.add_argument('archive', type=str, help='Path to the barecat archive')
307
+ p.add_argument('-c', '--cmd', type=str, dest='shell_cmd', help='Execute command and exit')
308
+ p.add_argument('-w', '--write', action='store_true', help='Open in write mode')
309
+
310
+ # browse
311
+ p = subparsers.add_parser('browse', help='Ranger-like file browser for archives')
312
+ p.add_argument('archive', type=str, help='Path to the barecat archive')
313
+
314
+ # du
315
+ p = subparsers.add_parser('du', help='Show disk usage (like du)')
316
+ p.add_argument('archive', type=str, help='Path to the barecat archive')
317
+ p.add_argument(
318
+ 'path', type=str, nargs='?', default='', help='Path within archive (default: root)'
319
+ )
320
+ p.add_argument('-a', '--all', action='store_true', help='Show all files, not just directories')
321
+ p.add_argument(
322
+ '-s', '--summarize', action='store_true', help='Show only total for each argument'
323
+ )
324
+ p.add_argument(
325
+ '-H', '--human-readable', action='store_true', help='Print sizes in human readable format'
326
+ )
327
+ p.add_argument(
328
+ '-d', '--max-depth', type=int, default=None, metavar='N', help='Max depth to show'
329
+ )
330
+
331
+ # ncdu
332
+ p = subparsers.add_parser('ncdu', help='ncdu-like disk usage viewer (TUI)')
333
+ p.add_argument('archive', type=str, help='Path to the barecat archive')
334
+
335
+ # tree
336
+ p = subparsers.add_parser('tree', help='Display directory tree')
337
+ p.add_argument('archive', type=str, help='Path to the barecat archive')
338
+ p.add_argument(
339
+ 'path', type=str, nargs='?', default='', help='Path within archive (default: root)'
340
+ )
341
+ p.add_argument(
342
+ '-L', '--level', type=int, default=None, metavar='N', help='Limit depth to N levels'
343
+ )
344
+ p.add_argument('-d', '--dirs-only', action='store_true', help='List directories only')
345
+
346
+ # verify
347
+ p = subparsers.add_parser('verify', help='Verify archive integrity')
348
+ p.add_argument('archive', type=str, help='Path to the archive')
349
+ p.add_argument('--quick', action='store_true', help='Quick check (index only, no CRC)')
350
+
351
+ # defrag
352
+ p = subparsers.add_parser('defrag', help='Defragment archive')
353
+ p.add_argument('archive', type=str, help='Path to the archive')
354
+ p.add_argument('--quick', action='store_true', help='Quick defrag (time-limited, partial)')
355
+ p.add_argument(
356
+ '--smart',
357
+ action='store_true',
358
+ help='Smart defrag (copies contiguous chunks, more efficient)',
359
+ )
360
+ p.add_argument(
361
+ '--max-seconds',
362
+ type=float,
363
+ default=300,
364
+ metavar='SEC',
365
+ help='Time limit for --quick mode (default: 300)',
366
+ )
367
+ p.add_argument(
368
+ '-n',
369
+ '--dry-run',
370
+ action='store_true',
371
+ help='Show statistics about freeable space without defragmenting',
372
+ )
373
+
374
+ # reshard
375
+ p = subparsers.add_parser('reshard', help='Reshard archive with a new shard size limit')
376
+ p.add_argument('archive', type=str, help='Path to the archive')
377
+ p.add_argument(
378
+ '-s',
379
+ '--shard-size-limit',
380
+ type=str,
381
+ required=True,
382
+ metavar='SIZE',
383
+ help='New shard size limit (e.g., 1G, 500M)',
384
+ )
385
+
386
+ # subset - create filtered copy of archive
387
+ p = subparsers.add_parser('subset', help='Create new archive with subset of files')
388
+ p.add_argument('archive', type=str, help='Source archive')
389
+ p.add_argument('-o', '--output', required=True, help='Output archive')
390
+ p.add_argument(
391
+ '-s',
392
+ '--shard-size-limit',
393
+ type=str,
394
+ default=None,
395
+ metavar='SIZE',
396
+ help='Shard size limit (e.g., 1G, 500M)',
397
+ )
398
+ p.add_argument('-f', '--force', action='store_true', help='Overwrite if output exists')
399
+ p.add_argument(
400
+ '--pattern',
401
+ type=str,
402
+ default=None,
403
+ metavar='GLOB',
404
+ help='Only include files matching glob pattern (incompatible with -i/-x)',
405
+ )
406
+ p.add_argument(
407
+ '-i',
408
+ '--include',
409
+ action='append',
410
+ default=[],
411
+ metavar='PATTERN',
412
+ help='Include files matching glob pattern, rsync-style (repeatable)',
413
+ )
414
+ p.add_argument(
415
+ '-x',
416
+ '--exclude',
417
+ action='append',
418
+ default=[],
419
+ metavar='PATTERN',
420
+ help='Exclude files matching glob pattern, rsync-style (repeatable)',
421
+ )
422
+
423
+ # merge
424
+ p = subparsers.add_parser(
425
+ 'merge', help='Merge multiple archives (barecat, tar, zip) into one barecat'
426
+ )
427
+ p.add_argument('archives', metavar='ARCHIVE', type=str, nargs='+', help='Archives to merge')
428
+ p.add_argument('-o', '--output', required=True, help='Output archive')
429
+ p.add_argument(
430
+ '-s', '--shard-size-limit', type=str, default=None, metavar='SIZE', help='Shard size limit'
431
+ )
432
+ p.add_argument('-f', '--force', action='store_true', help='Overwrite if output exists')
433
+ p.add_argument(
434
+ '-a',
435
+ '--append',
436
+ action='store_true',
437
+ help='Append to output if it exists (not supported with --symlink)',
438
+ )
439
+ p.add_argument(
440
+ '--symlink',
441
+ action='store_true',
442
+ help='Create symlinks to original shards instead of copying data',
443
+ )
444
+ p.add_argument(
445
+ '--ignore-duplicates', action='store_true', help='Skip files that already exist in output'
446
+ )
447
+ p.add_argument(
448
+ '--as-subdirs',
449
+ action='store_true',
450
+ help='Put each archive in a subdir named after its basename (minus extension)',
451
+ )
452
+ p.add_argument(
453
+ '--prefix',
454
+ type=str,
455
+ default=None,
456
+ metavar='PATH',
457
+ help='Prefix path for all merged files (combines with --as-subdirs)',
458
+ )
459
+ p.add_argument(
460
+ '--pattern',
461
+ type=str,
462
+ default=None,
463
+ metavar='GLOB',
464
+ help='Only include files matching glob pattern (incompatible with --include/--exclude)',
465
+ )
466
+ p.add_argument(
467
+ '-i',
468
+ '--include',
469
+ action='append',
470
+ default=[],
471
+ metavar='PATTERN',
472
+ help='Include files matching glob pattern, rsync-style first-match-wins (repeatable)',
473
+ )
474
+ p.add_argument(
475
+ '-x',
476
+ '--exclude',
477
+ action='append',
478
+ default=[],
479
+ metavar='PATTERN',
480
+ help='Exclude files matching glob pattern, rsync-style first-match-wins (repeatable)',
481
+ )
482
+
483
+ # index-to-csv
484
+ p = subparsers.add_parser('index-to-csv', help='Dump index as CSV')
485
+ p.add_argument('archive', type=str, help='Path to the archive')
486
+
487
+ # to-ncdu-json
488
+ p = subparsers.add_parser('to-ncdu-json', help='Print as ncdu JSON')
489
+ p.add_argument('archive', type=str, help='Path to the archive')
490
+
491
+ # convert - convert between barecat and tar/zip
492
+ p = subparsers.add_parser(
493
+ 'convert', help='Convert between barecat and tar/zip (auto-detects direction)'
494
+ )
495
+ p.add_argument(
496
+ 'input', type=str, help='Input file or format (tar.gz, tar.bz2, etc. with --stdin)'
497
+ )
498
+ p.add_argument(
499
+ 'output', type=str, help='Output file or format (tar.gz, tar.bz2, etc. with --stdout)'
500
+ )
501
+ p.add_argument(
502
+ '-s',
503
+ '--shard-size-limit',
504
+ type=str,
505
+ default=None,
506
+ metavar='SIZE',
507
+ help='Shard size limit (only for tar/zip → barecat)',
508
+ )
509
+ p.add_argument('-f', '--force', action='store_true', help='Overwrite existing output')
510
+ p.add_argument(
511
+ '--stdin',
512
+ action='store_true',
513
+ help='Read input from stdin (input arg specifies format: tar, tar.gz, tar.bz2, tar.xz)',
514
+ )
515
+ p.add_argument(
516
+ '--stdout',
517
+ action='store_true',
518
+ help='Write output to stdout (output arg specifies format: tar, tar.gz, tar.bz2, tar.xz)',
519
+ )
520
+ p.add_argument(
521
+ '--root-dir',
522
+ type=str,
523
+ metavar='NAME',
524
+ help='Wrap all files in a root directory (barecat → tar only)',
525
+ )
526
+ p.add_argument(
527
+ '--wrap',
528
+ action='store_true',
529
+ help='Zero-copy: create index over existing tar/zip (symlinks to original, uncompressed only)',
530
+ )
531
+
532
+ # upgrade
533
+ p = subparsers.add_parser('upgrade', help='Upgrade archive to new schema version')
534
+ p.add_argument('archive', type=str, help='Path to the barecat')
535
+ p.add_argument('-j', '--workers', type=int, default=8)
536
+ p.add_argument(
537
+ '--preserve-backup',
538
+ action=misc.BoolAction,
539
+ default=True,
540
+ help='Keep the .old backup file after upgrade (default: True)',
541
+ )
542
+
543
+ # completion-script
544
+ p = subparsers.add_parser('completion-script', help='Print shell completion script path')
545
+ p.add_argument('shell', choices=['bash', 'zsh'], help='Shell type')
546
+
547
+ # mount - FUSE filesystem
548
+ p = subparsers.add_parser(
549
+ 'mount',
550
+ help='Mount archive as FUSE filesystem (requires barecat[mount])',
551
+ description='Mount a barecat archive as a FUSE filesystem. '
552
+ 'Use -o for mount options (like mount/sshfs).',
553
+ )
554
+ p.add_argument('archive', type=str, help='Barecat archive path')
555
+ p.add_argument('mountpoint', type=str, help='Mount point')
556
+ p.add_argument(
557
+ '-o',
558
+ '--options',
559
+ type=str,
560
+ metavar='OPTIONS',
561
+ help='Comma-separated mount options: ro (default), rw, fg/foreground, '
562
+ 'mmap, defrag, overwrite, append_only, shard_size_limit=SIZE',
563
+ )
564
+
565
+ # rsync - rsync-like interface
566
+ p = subparsers.add_parser(
567
+ 'rsync',
568
+ help='rsync-like sync between local and archive',
569
+ description='Sync files between local filesystem and barecat archives. '
570
+ 'Use archive.barecat:path/ syntax for archive paths.',
571
+ )
572
+ p.add_argument('paths', nargs='+', help='Source(s) and destination (last is dest)')
573
+ p.add_argument('-n', '--dry-run', action='store_true', help='Show what would be done')
574
+ p.add_argument('-v', '--verbose', action='store_true', help='Verbose output')
575
+ p.add_argument('--progress', action='store_true', help='Show progress')
576
+ p.add_argument('--delete', action='store_true', help='Delete extraneous files from dest')
577
+ p.add_argument(
578
+ '-c', '--checksum', action='store_true', help='Compare by checksum not size/mtime'
579
+ )
580
+ p.add_argument('-u', '--update', action='store_true', help='Skip newer files on dest')
581
+ p.add_argument('--include', action='append', default=[], metavar='PAT', help='Include pattern')
582
+ p.add_argument('--exclude', action='append', default=[], metavar='PAT', help='Exclude pattern')
583
+
584
+ # Preprocess sys.argv: extract -C/--directory before argparse sees it,
585
+ # so it works regardless of position (like tar's -C flag).
586
+ directory_override = _extract_directory_from_argv()
587
+
588
+ logging.basicConfig(format='%(message)s', level=logging.WARNING)
589
+
590
+ args = parser.parse_args()
591
+
592
+ # Apply -C override if it was found in argv preprocessing
593
+ if directory_override is not None and hasattr(args, 'directory'):
594
+ args.directory = directory_override
595
+
596
+ if args.command is None:
597
+ parser.print_help()
598
+ sys.exit(0)
599
+
600
+ # Dispatch to handlers
601
+ if args.command in ('create', 'c'):
602
+ # create: error if exists (unless -f)
603
+ _handle_add(args, overwrite=args.force, exist_ok=args.force)
604
+
605
+ elif args.command in ('add', 'a'):
606
+ # add: error if doesn't exist (unless -c)
607
+ if not barecat.util.misc.exists(args.archive) and not args.create:
608
+ print(
609
+ f"Error: Archive '{args.archive}' does not exist. "
610
+ "Use 'barecat create' or 'barecat add -c'.",
611
+ file=sys.stderr,
612
+ )
613
+ sys.exit(1)
614
+ _handle_add(args, overwrite=False, exist_ok=True)
615
+
616
+ elif args.command in ('extract', 'x'):
617
+ _handle_extract(args)
618
+
619
+ elif args.command in ('list', 'ls', 'l', 't'):
620
+ _handle_list(args)
621
+
622
+ elif args.command == 'cat':
623
+ with barecat.Barecat(args.archive) as bc:
624
+ sys.stdout.buffer.write(bc[args.path])
625
+
626
+ elif args.command == 'find':
627
+ _handle_find(args)
628
+
629
+ elif args.command == 'shell':
630
+ from .shell import BarecatShell
631
+
632
+ shell = BarecatShell(args.archive, readonly=not args.write)
633
+ if args.shell_cmd:
634
+ shell.onecmd(args.shell_cmd)
635
+ else:
636
+ shell.cmdloop()
637
+ shell.close()
638
+
639
+ elif args.command == 'browse':
640
+ import curses
641
+ from ..tui.browse import BarecatBrowser
642
+
643
+ browser = BarecatBrowser(args.archive)
644
+ curses.wrapper(browser.run)
645
+
646
+ elif args.command == 'du':
647
+ _handle_du(args)
648
+
649
+ elif args.command == 'ncdu':
650
+ import curses
651
+ from ..tui.ncdu import BarecatDu
652
+
653
+ du = BarecatDu(args.archive)
654
+ curses.wrapper(du.run)
655
+
656
+ elif args.command == 'tree':
657
+ _handle_tree(args)
658
+
659
+ elif args.command == 'verify':
660
+ from .legacy import verify_crc_parallel
661
+
662
+ with barecat.Barecat(args.archive, threadsafe=True) as bc:
663
+ if args.quick:
664
+ if not bc.verify_integrity(quick=True):
665
+ print('Integrity verification failed.', file=sys.stderr)
666
+ sys.exit(1)
667
+ print(f'Verified {bc.num_files} files (quick check), all OK.')
668
+ else:
669
+ if not verify_crc_parallel(bc, workers=1):
670
+ print('CRC32C verification failed.', file=sys.stderr)
671
+ sys.exit(1)
672
+ if not bc.index.verify_integrity():
673
+ print('Index integrity errors were found.', file=sys.stderr)
674
+ sys.exit(1)
675
+ total_size = _format_size(bc.total_size, human_readable=True)
676
+ print(
677
+ f'Verified {bc.num_files} files ({total_size}), '
678
+ f'all checksums OK.'
679
+ )
680
+
681
+ elif args.command == 'defrag':
682
+ readonly = args.dry_run
683
+ with barecat.Barecat(args.archive, readonly=readonly, append_only=False) as bc:
684
+ defragger = BarecatDefragger(bc)
685
+ if args.dry_run:
686
+ stats = defragger.get_gap_stats()
687
+ _print_defrag_stats(stats)
688
+ elif defragger.needs_defrag():
689
+ if args.quick:
690
+ freed = defragger.defrag_quick(time_max_seconds=args.max_seconds)
691
+ elif args.smart:
692
+ freed = defragger.defrag_smart()
693
+ else:
694
+ freed = defragger.defrag()
695
+ print(f'Freed {_format_size(freed, human_readable=True)}')
696
+
697
+ elif args.command == 'reshard':
698
+ from ..maintenance.reshard import reshard
699
+
700
+ with barecat.Barecat(args.archive, readonly=False, append_only=False) as bc:
701
+ reshard(bc, target_shard_size_limit=parse_size(args.shard_size_limit))
702
+
703
+ elif args.command == 'subset':
704
+ if barecat.util.misc.exists(args.output) and not args.force:
705
+ print(
706
+ f"Error: Output '{args.output}' already exists. Use -f to overwrite.",
707
+ file=sys.stderr,
708
+ )
709
+ sys.exit(1)
710
+
711
+ if args.pattern and (args.include or args.exclude):
712
+ print('Error: --pattern is incompatible with --include/--exclude', file=sys.stderr)
713
+ sys.exit(1)
714
+
715
+ filter_rules = _build_filter_rules_from_argv() if (args.include or args.exclude) else None
716
+ pattern = _normalize_pattern(args.pattern) if args.pattern else None
717
+
718
+ # Use merge into empty archive
719
+ impl.merge(
720
+ source_paths=[args.archive],
721
+ target_path=args.output,
722
+ shard_size_limit=parse_shard_size(args.shard_size_limit),
723
+ overwrite=args.force,
724
+ ignore_duplicates=False,
725
+ as_subdirs=False,
726
+ prefix=None,
727
+ pattern=pattern,
728
+ filter_rules=filter_rules,
729
+ )
730
+
731
+ elif args.command == 'merge':
732
+ output_exists = barecat.util.misc.exists(args.output)
733
+
734
+ # Validate flag combinations
735
+ if args.symlink and args.append:
736
+ print('Error: --append is not supported with --symlink', file=sys.stderr)
737
+ sys.exit(1)
738
+
739
+ if args.pattern and (args.include or args.exclude):
740
+ print('Error: --pattern is incompatible with --include/--exclude', file=sys.stderr)
741
+ sys.exit(1)
742
+
743
+ # Check if any inputs are tar/zip (not supported with --symlink)
744
+ has_traditional_archives = any(impl.is_traditional_archive(a) for a in args.archives)
745
+ if args.symlink and has_traditional_archives:
746
+ print('Error: --symlink is not supported with tar/zip inputs', file=sys.stderr)
747
+ sys.exit(1)
748
+
749
+ if output_exists and not args.force and not args.append:
750
+ print(
751
+ f"Error: Output '{args.output}' already exists. "
752
+ 'Use -f to overwrite or -a to append.',
753
+ file=sys.stderr,
754
+ )
755
+ sys.exit(1)
756
+
757
+ # Build filter rules for rsync-style filtering
758
+ filter_rules = _build_filter_rules_from_argv() if (args.include or args.exclude) else None
759
+
760
+ pattern = _normalize_pattern(args.pattern) if args.pattern else None
761
+
762
+ try:
763
+ if args.symlink:
764
+ impl.merge_symlink(
765
+ source_paths=args.archives,
766
+ target_path=args.output,
767
+ overwrite=args.force,
768
+ ignore_duplicates=args.ignore_duplicates,
769
+ as_subdirs=args.as_subdirs,
770
+ prefix=args.prefix,
771
+ pattern=pattern,
772
+ filter_rules=filter_rules,
773
+ )
774
+ else:
775
+ # For append mode, we don't overwrite but allow existing
776
+ impl.merge(
777
+ source_paths=args.archives,
778
+ target_path=args.output,
779
+ shard_size_limit=parse_shard_size(args.shard_size_limit),
780
+ overwrite=args.force,
781
+ ignore_duplicates=args.ignore_duplicates or args.append,
782
+ as_subdirs=args.as_subdirs,
783
+ prefix=args.prefix,
784
+ pattern=pattern,
785
+ filter_rules=filter_rules,
786
+ )
787
+ except barecat.FileExistsBarecatError as e:
788
+ print(
789
+ f'Error: Duplicate path in archive: {e}\n'
790
+ 'To ignore duplicates and keep the first occurrence, '
791
+ 'use --ignore-duplicates.',
792
+ file=sys.stderr,
793
+ )
794
+ sys.exit(1)
795
+
796
+ elif args.command == 'index-to-csv':
797
+ writer = csv.writer(sys.stdout, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
798
+ writer.writerow(['path', 'shard', 'offset', 'size', 'crc32c'])
799
+ with barecat.Index(resolve_index_path(args.archive)) as index:
800
+ for f in index.iter_all_fileinfos(order=Order.PATH):
801
+ writer.writerow([f.path, f.shard, f.offset, f.size, f.crc32c])
802
+
803
+ elif args.command == 'to-ncdu-json':
804
+ impl.print_ncdu_json(resolve_index_path(args.archive))
805
+
806
+ elif args.command == 'convert':
807
+ tar_formats = ('tar', 'tar.gz', 'tar.bz2', 'tar.xz')
808
+
809
+ # With --stdin, input arg is format; with --stdout, output arg is format
810
+ if args.stdin:
811
+ if args.input not in tar_formats:
812
+ print(
813
+ f"Error: With --stdin, input must be a tar format: {', '.join(tar_formats)}",
814
+ file=sys.stderr,
815
+ )
816
+ sys.exit(1)
817
+ input_is_archive = True
818
+ else:
819
+ input_is_archive = impl.is_traditional_archive(args.input)
820
+
821
+ if args.stdout:
822
+ if args.output not in tar_formats:
823
+ print(
824
+ f"Error: With --stdout, output must be a tar format: {', '.join(tar_formats)}",
825
+ file=sys.stderr,
826
+ )
827
+ sys.exit(1)
828
+ output_is_archive = True
829
+ else:
830
+ output_is_archive = impl.is_traditional_archive(args.output)
831
+
832
+ # Validate conversion direction
833
+ if input_is_archive and output_is_archive:
834
+ print(
835
+ 'Error: Both input and output are tar/zip. One must be a barecat path.',
836
+ file=sys.stderr,
837
+ )
838
+ sys.exit(1)
839
+ if not input_is_archive and not output_is_archive:
840
+ print(
841
+ 'Error: Neither input nor output is tar/zip. '
842
+ 'One must have extension like .tar, .tar.gz, .zip (or use --stdin/--stdout)',
843
+ file=sys.stderr,
844
+ )
845
+ sys.exit(1)
846
+
847
+ if input_is_archive:
848
+ # tar/zip → barecat
849
+ if barecat.util.misc.exists(args.output) and not args.force:
850
+ print(
851
+ f"Error: Output '{args.output}' already exists. Use -f to overwrite.",
852
+ file=sys.stderr,
853
+ )
854
+ sys.exit(1)
855
+ if args.wrap:
856
+ # Zero-copy: create index pointing into original archive
857
+ compressed_exts = ('.tar.gz', '.tgz', '.tar.bz2', '.tbz2', '.tar.xz', '.txz')
858
+ if args.input.lower().endswith(compressed_exts):
859
+ print(
860
+ 'Error: --wrap only works with uncompressed .tar or .zip files',
861
+ file=sys.stderr,
862
+ )
863
+ sys.exit(1)
864
+ if args.stdin:
865
+ print('Error: --wrap cannot be used with --stdin', file=sys.stderr)
866
+ sys.exit(1)
867
+ try:
868
+ impl.wrap_archive(
869
+ src_path=args.input,
870
+ target_path=args.output,
871
+ overwrite=args.force,
872
+ )
873
+ except ValueError as e:
874
+ print(f'Error: {e}', file=sys.stderr)
875
+ sys.exit(1)
876
+ elif args.stdin:
877
+ impl.stdin_tar2barecat(
878
+ tar_format=args.input,
879
+ target_path=args.output,
880
+ shard_size_limit=parse_shard_size(args.shard_size_limit),
881
+ overwrite=args.force,
882
+ )
883
+ else:
884
+ impl.archive2barecat(
885
+ src_path=args.input,
886
+ target_path=args.output,
887
+ shard_size_limit=parse_shard_size(args.shard_size_limit),
888
+ overwrite=args.force,
889
+ )
890
+ else:
891
+ # barecat → tar/zip
892
+ import os.path as osp
893
+
894
+ if not args.stdout and osp.exists(args.output) and not args.force:
895
+ print(
896
+ f"Error: Output '{args.output}' already exists. Use -f to overwrite.",
897
+ file=sys.stderr,
898
+ )
899
+ sys.exit(1)
900
+ if args.stdout:
901
+ impl.barecat2stdout_tar(
902
+ src_path=args.input, tar_format=args.output, root_dir=args.root_dir
903
+ )
904
+ else:
905
+ impl.barecat2archive(
906
+ src_path=args.input, target_path=args.output, root_dir=args.root_dir
907
+ )
908
+
909
+ elif args.command == 'upgrade':
910
+ from ..maintenance.upgrade_database import upgrade
911
+
912
+ upgrade(args.archive, workers=args.workers, preserve_backup=args.preserve_backup)
913
+
914
+ elif args.command == 'completion-script':
915
+ print(barecat.get_completion_script(args.shell))
916
+
917
+ elif args.command == 'mount':
918
+ _handle_mount(args)
919
+
920
+ elif args.command == 'rsync':
921
+ from ..maintenance.rsync import rsync, RsyncOptions
922
+
923
+ if len(args.paths) < 2:
924
+ print('Error: rsync requires at least source and destination', file=sys.stderr)
925
+ sys.exit(1)
926
+
927
+ sources = args.paths[:-1]
928
+ dest = args.paths[-1]
929
+ options = RsyncOptions(
930
+ delete=args.delete,
931
+ dry_run=args.dry_run,
932
+ verbose=args.verbose,
933
+ progress=args.progress,
934
+ checksum=args.checksum,
935
+ update=args.update,
936
+ include=args.include,
937
+ exclude=args.exclude,
938
+ )
939
+ rsync(sources, dest, options)
940
+
941
+
942
+ def _normalize_pattern(pattern):
943
+ """Normalize pattern: trailing / becomes /** (rsync convention)."""
944
+ if pattern.endswith('/'):
945
+ return pattern + '**'
946
+ return pattern
947
+
948
+
949
+ def _build_filter_rules_from_argv():
950
+ """Build filter rules from sys.argv, preserving order of -i/-x flags.
951
+
952
+ We parse sys.argv directly because argparse's append action doesn't preserve
953
+ interleaving order between -i/--include and -x/--exclude flags, which matters
954
+ for first-match-wins rsync-style filtering.
955
+ """
956
+ filter_rules = []
957
+ for i, arg in enumerate(sys.argv):
958
+ if arg in ('-i', '--include') and i + 1 < len(sys.argv):
959
+ filter_rules.append(('+', _normalize_pattern(sys.argv[i + 1])))
960
+ elif arg.startswith('--include='):
961
+ filter_rules.append(('+', _normalize_pattern(arg.split('=', 1)[1])))
962
+ elif arg in ('-x', '--exclude') and i + 1 < len(sys.argv):
963
+ filter_rules.append(('-', _normalize_pattern(sys.argv[i + 1])))
964
+ elif arg.startswith('--exclude='):
965
+ filter_rules.append(('-', _normalize_pattern(arg.split('=', 1)[1])))
966
+ return filter_rules
967
+
968
+
969
+ def _handle_add(args, overwrite=False, exist_ok=True):
970
+ """Handle create/add commands."""
971
+ import itertools
972
+ import os
973
+ import os.path as osp
974
+
975
+ from ..util.progbar import progressbar
976
+
977
+ # Change directory if -C specified (like tar)
978
+ if args.directory:
979
+ os.chdir(args.directory)
980
+
981
+ def matches_patterns(path, patterns, compiled_patterns={}):
982
+ """Check if path matches any of the patterns.
983
+
984
+ Uses Python glob syntax with ** support:
985
+ - '*.pyc' matches only at root level
986
+ - '**/*.pyc' matches .pyc files at any depth
987
+ - 'tests/**' matches everything under tests/
988
+ """
989
+ if not patterns:
990
+ return True
991
+ for p in patterns:
992
+ if p not in compiled_patterns:
993
+ # Patterns with ** need recursive=True
994
+ recursive = '**' in p
995
+ compiled_patterns[p] = re.compile(
996
+ glob_to_regex(p, recursive=recursive, include_hidden=True)
997
+ )
998
+ if compiled_patterns[p].match(path):
999
+ return True
1000
+ return False
1001
+
1002
+ def should_include(path):
1003
+ """Check if path should be included based on include/exclude patterns."""
1004
+ # If exclude patterns given and path matches, exclude it
1005
+ if args.exclude and matches_patterns(path, args.exclude):
1006
+ return False
1007
+ # If include patterns given, path must match one of them
1008
+ if args.include and not matches_patterns(path, args.include):
1009
+ return False
1010
+ return True
1011
+
1012
+ def generate_paths():
1013
+ """Generate (filesystem_path, store_path) pairs."""
1014
+ # From positional arguments (files/directories)
1015
+ if args.paths:
1016
+ for root in args.paths:
1017
+ if osp.isfile(root):
1018
+ if should_include(root):
1019
+ yield root, root
1020
+ elif osp.isdir(root):
1021
+ # Add the directory itself
1022
+ if should_include(root):
1023
+ yield root, root
1024
+
1025
+ for dirpath, subdirnames, filenames in os.walk(root):
1026
+ # Filter subdirs in-place to prevent descending into excluded dirs
1027
+ subdirnames[:] = [
1028
+ d for d in subdirnames if should_include(osp.join(dirpath, d))
1029
+ ]
1030
+
1031
+ for entryname in itertools.chain(filenames, subdirnames):
1032
+ full_path = osp.join(dirpath, entryname)
1033
+ if not should_include(full_path):
1034
+ continue
1035
+ # Store path same as filesystem path (like tar)
1036
+ yield full_path, full_path
1037
+
1038
+ # From file list (-T FILE or -T -)
1039
+ if args.files_from:
1040
+ if args.files_from == '-':
1041
+ # Read from stdin
1042
+ if args.null:
1043
+ for path in impl.iterate_zero_terminated(sys.stdin.buffer):
1044
+ if should_include(path):
1045
+ yield path, path
1046
+ else:
1047
+ for line in sys.stdin:
1048
+ path = line.rstrip('\n')
1049
+ if path and should_include(path):
1050
+ yield path, path
1051
+ else:
1052
+ # Read from file
1053
+ if args.null:
1054
+ with open(args.files_from, 'rb') as f:
1055
+ for path in impl.iterate_zero_terminated(f):
1056
+ if should_include(path):
1057
+ yield path, path
1058
+ else:
1059
+ with open(args.files_from) as f:
1060
+ for line in f:
1061
+ path = line.rstrip('\n')
1062
+ if path and should_include(path):
1063
+ yield path, path
1064
+
1065
+ # Validate: must have some input
1066
+ if not args.paths and not args.files_from:
1067
+ print('Error: No input specified. Provide paths or -T FILE.', file=sys.stderr)
1068
+ sys.exit(1)
1069
+
1070
+ iterator = generate_paths()
1071
+
1072
+ if args.physical_order:
1073
+ iterator = iter(
1074
+ sorted(iterator, key=lambda x: barecat_physical_order.get_physical_offset(x[0]))
1075
+ )
1076
+
1077
+ iterator = progressbar(iterator, desc='Packing files', unit=' files')
1078
+
1079
+ shard_size = getattr(args, 'shard_size_limit', None)
1080
+ impl.create(
1081
+ iterator,
1082
+ target_path=args.archive,
1083
+ shard_size_limit=parse_shard_size(shard_size),
1084
+ overwrite=overwrite,
1085
+ exist_ok=exist_ok,
1086
+ workers=args.workers,
1087
+ )
1088
+
1089
+
1090
+ def _handle_extract(args):
1091
+ """Handle extract command."""
1092
+ import os
1093
+ import os.path as osp
1094
+ from ..io.copyfile import copy
1095
+ from ..util.progbar import progressbar
1096
+
1097
+ if args.pattern and (args.include or args.exclude):
1098
+ print('Error: --pattern is incompatible with --include/--exclude', file=sys.stderr)
1099
+ sys.exit(1)
1100
+
1101
+ def extract_file(bc, fpath):
1102
+ target = osp.join(args.directory, fpath)
1103
+ os.makedirs(osp.dirname(target), exist_ok=True)
1104
+ with open(target, 'wb') as out:
1105
+ copy(bc.open(fpath, 'rb'), out)
1106
+
1107
+ with barecat.Barecat(args.archive) as bc:
1108
+ if args.paths:
1109
+ # Extract specific paths (filtering not applied to explicit paths)
1110
+ for path in args.paths:
1111
+ if path in bc:
1112
+ sys.stdout.buffer.write(bc[path])
1113
+ elif bc.index.isdir(path):
1114
+ for dirpath, dirnames, filenames in bc.walk(path):
1115
+ for fname in progressbar(filenames):
1116
+ fpath = osp.join(dirpath, fname)
1117
+ extract_file(bc, fpath)
1118
+ else:
1119
+ print(f'Error: {path} not found in archive', file=sys.stderr)
1120
+ sys.exit(1)
1121
+ elif args.pattern:
1122
+ # Use optimized glob path
1123
+ pattern = _normalize_pattern(args.pattern)
1124
+ recursive = '**' in pattern
1125
+ for info in progressbar(
1126
+ bc.index.iterglob_infos(pattern, recursive=recursive, only_files=True),
1127
+ desc='Extracting',
1128
+ unit=' files',
1129
+ ):
1130
+ extract_file(bc, info.path)
1131
+ elif args.include or args.exclude:
1132
+ # Build filter rules preserving order
1133
+ filter_rules = _build_filter_rules_from_argv()
1134
+ for info in progressbar(
1135
+ bc.index.iterglob_infos_incl_excl(filter_rules, only_files=True),
1136
+ desc='Extracting',
1137
+ unit=' files',
1138
+ ):
1139
+ extract_file(bc, info.path)
1140
+ else:
1141
+ # Extract everything
1142
+ for fpath in progressbar(bc, desc='Extracting', unit=' files'):
1143
+ extract_file(bc, fpath)
1144
+
1145
+
1146
+ def _format_mode(mode):
1147
+ """Format mode bits as ls-style string (e.g., -rw-r--r-- or drwxr-xr-x)."""
1148
+ import stat
1149
+
1150
+ if mode is None:
1151
+ return '----------'
1152
+
1153
+ # File type
1154
+ if stat.S_ISDIR(mode):
1155
+ s = 'd'
1156
+ elif stat.S_ISLNK(mode):
1157
+ s = 'l'
1158
+ else:
1159
+ s = '-'
1160
+
1161
+ # Owner permissions
1162
+ s += 'r' if mode & stat.S_IRUSR else '-'
1163
+ s += 'w' if mode & stat.S_IWUSR else '-'
1164
+ s += 'x' if mode & stat.S_IXUSR else '-'
1165
+
1166
+ # Group permissions
1167
+ s += 'r' if mode & stat.S_IRGRP else '-'
1168
+ s += 'w' if mode & stat.S_IWGRP else '-'
1169
+ s += 'x' if mode & stat.S_IXGRP else '-'
1170
+
1171
+ # Other permissions
1172
+ s += 'r' if mode & stat.S_IROTH else '-'
1173
+ s += 'w' if mode & stat.S_IWOTH else '-'
1174
+ s += 'x' if mode & stat.S_IXOTH else '-'
1175
+
1176
+ return s
1177
+
1178
+
1179
+ def _format_time(mtime_ns):
1180
+ """Format mtime like ls (e.g., 'Dec 23 14:30' or 'Dec 23 2023')."""
1181
+ from datetime import datetime
1182
+
1183
+ if mtime_ns is None:
1184
+ return ' '
1185
+
1186
+ dt = datetime.fromtimestamp(mtime_ns / 1e9)
1187
+ now = datetime.now()
1188
+
1189
+ # If within last 6 months, show time; otherwise show year
1190
+ six_months_ago = now.timestamp() - 180 * 24 * 3600
1191
+ if dt.timestamp() > six_months_ago:
1192
+ return dt.strftime('%b %e %H:%M')
1193
+ else:
1194
+ return dt.strftime('%b %e %Y')
1195
+
1196
+
1197
+ def _get_user_group(uid, gid, cache={}):
1198
+ """Get user and group names from uid/gid, with caching."""
1199
+ import pwd
1200
+ import grp
1201
+
1202
+ if uid not in cache:
1203
+ try:
1204
+ cache[uid] = pwd.getpwuid(uid).pw_name
1205
+ except (KeyError, TypeError):
1206
+ cache[uid] = str(uid) if uid is not None else '-'
1207
+
1208
+ if gid not in cache:
1209
+ try:
1210
+ cache[gid] = grp.getgrgid(gid).gr_name
1211
+ except (KeyError, TypeError):
1212
+ cache[gid] = str(gid) if gid is not None else '-'
1213
+
1214
+ return cache[uid], cache[gid]
1215
+
1216
+
1217
+ def _handle_list(args):
1218
+ """Handle list command."""
1219
+ from .commands import list_entries
1220
+
1221
+ with barecat.Barecat(args.archive) as bc:
1222
+ list_entries(
1223
+ bc.index,
1224
+ paths=args.paths,
1225
+ long_format=args.long,
1226
+ recursive=args.recursive,
1227
+ jsonl=getattr(args, 'jsonl', False),
1228
+ )
1229
+
1230
+
1231
+ def _handle_find(args):
1232
+ """Handle find command."""
1233
+ from .commands import find_entries
1234
+
1235
+ with barecat.Barecat(args.archive) as bc:
1236
+ find_entries(
1237
+ bc.index,
1238
+ path=args.path,
1239
+ name=args.name,
1240
+ pathpattern=args.pathpattern,
1241
+ ftype=args.ftype,
1242
+ size=args.size,
1243
+ maxdepth=args.maxdepth,
1244
+ print0=args.print0,
1245
+ )
1246
+
1247
+
1248
+ def _format_size(size, human_readable=False):
1249
+ """Format size for du output."""
1250
+ if not human_readable:
1251
+ return str(size)
1252
+
1253
+ for unit in ['', 'K', 'M', 'G', 'T']:
1254
+ if abs(size) < 1024:
1255
+ if unit == '':
1256
+ return str(int(round(size)))
1257
+ return f'{size:.1f}{unit}'
1258
+ size /= 1024
1259
+ return f'{size:.1f}P'
1260
+
1261
+
1262
+ def _handle_du(args):
1263
+ """Handle du command."""
1264
+ from .commands import du_entries
1265
+
1266
+ with barecat.Barecat(args.archive) as bc:
1267
+ du_entries(
1268
+ bc.index,
1269
+ path=args.path,
1270
+ all_files=args.all,
1271
+ summarize=args.summarize,
1272
+ human_readable=args.human_readable,
1273
+ max_depth=args.max_depth,
1274
+ )
1275
+
1276
+
1277
+ def _handle_tree(args):
1278
+ """Handle tree command."""
1279
+ from .commands import tree_entries
1280
+
1281
+ with barecat.Barecat(args.archive) as bc:
1282
+ tree_entries(
1283
+ bc.index,
1284
+ path=args.path,
1285
+ level=args.level,
1286
+ dirs_only=args.dirs_only,
1287
+ )
1288
+
1289
+
1290
+ def _handle_mount(args):
1291
+ """Handle mount command with standard mount-style -o options."""
1292
+ try:
1293
+ from barecat_mount import mount
1294
+ except ImportError:
1295
+ print(
1296
+ 'Error: Mount support not installed. Install with: pip install barecat[mount]',
1297
+ file=sys.stderr,
1298
+ )
1299
+ sys.exit(1)
1300
+
1301
+ # Parse -o options (comma-separated, like mount/sshfs)
1302
+ # Defaults
1303
+ readonly = True
1304
+ foreground = False
1305
+ mmap = False
1306
+ overwrite = False
1307
+ append_only = False
1308
+ enable_defrag = False
1309
+ shard_size_limit = None
1310
+
1311
+ if args.options:
1312
+ for opt in args.options.split(','):
1313
+ opt = opt.strip()
1314
+ if opt == 'ro':
1315
+ readonly = True
1316
+ elif opt == 'rw':
1317
+ readonly = False
1318
+ elif opt in ('fg', 'foreground'):
1319
+ foreground = True
1320
+ elif opt == 'mmap':
1321
+ mmap = True
1322
+ elif opt == 'overwrite':
1323
+ overwrite = True
1324
+ elif opt == 'append_only':
1325
+ append_only = True
1326
+ elif opt == 'defrag':
1327
+ enable_defrag = True
1328
+ elif opt.startswith('shard_size_limit='):
1329
+ shard_size_limit = opt.split('=', 1)[1]
1330
+ elif opt == '':
1331
+ pass # ignore empty
1332
+ else:
1333
+ print(f'Error: Unknown mount option: {opt}', file=sys.stderr)
1334
+ print(
1335
+ 'Valid options: ro, rw, fg/foreground, mmap, defrag, overwrite, '
1336
+ 'append_only, shard_size_limit=SIZE',
1337
+ file=sys.stderr,
1338
+ )
1339
+ sys.exit(1)
1340
+
1341
+ mount(
1342
+ barecat_path=args.archive,
1343
+ mountpoint=args.mountpoint,
1344
+ readonly=readonly,
1345
+ foreground=foreground,
1346
+ mmap=mmap,
1347
+ overwrite=overwrite,
1348
+ append_only=append_only,
1349
+ enable_defrag=enable_defrag,
1350
+ shard_size_limit=shard_size_limit,
1351
+ )
1352
+
1353
+
1354
+ if __name__ == '__main__':
1355
+ main()