aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/format/tar.lux
blob: e8335ce9d535cabe9eb77be093654aab4751b700 (plain)
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
(.module:
  [library
   [lux {"-" [Mode Name and]}
    [abstract
     [monad {"+" [do]}]]
    [control
     [pipe {"+" [case>]}]
     ["." try {"+" [Try]}]
     ["." exception {"+" [exception:]}]
     ["<>" parser
      ["<.>" binary {"+" [Parser]}]]]
    [data
     ["." product]
     ["." binary {"+" [Binary]}]
     ["." text {"+" [Char]}
      ["%" format {"+" [format]}]
      [encoding
       ["." utf8]]]
     ["." format "_"
      ["#" binary {"+" [Writer]} ("#\." monoid)]]
     [collection
      ["." list ("#\." mix)]
      ["." row {"+" [Row]} ("#\." mix)]]]
    [math
     ["." number
      ["n" nat]
      ["." i64]]]
    [time
     ["." instant {"+" [Instant]}]
     ["." duration]]
    [world
     ["." file]]
    [type
     abstract]]])

(type: Size
  Nat)

(def: octal_size
  Size
  8)

(def: (octal_padding max_size number)
  (-> Size Text Text)
  (let [padding_size (n.- (text.size number)
                          max_size)
        padding (|> "0"
                    (list.repeated padding_size)
                    text.together)]
    (format padding number)))

(def: blank " ")
(def: null text.null)

(def: small_size Size 6)
(def: big_size Size 11)

(template [<exception> <limit> <size>
           <type> <in> <out> <writer> <suffix>
           <coercion>]
  [(def: .public <limit>
     Nat
     (|> ..octal_size
         (list.repeated <size>)
         (list\mix n.* 1)
         ++))

   (exception: .public (<exception> {value Nat})
     (exception.report
      ["Value" (%.nat value)]
      ["Maximum" (%.nat (-- <limit>))]))

   (abstract: .public <type>
     {}
     
     Nat

     (def: .public (<in> value)
       (-> Nat (Try <type>))
       (if (n.< <limit> value)
         (#try.Success (:abstraction value))
         (exception.except <exception> [value])))

     (def: .public <out>
       (-> <type> Nat)
       (|>> :representation))

     (def: <writer>
       (Writer <type>)
       (let [suffix <suffix>
             padded_size (n.+ (text.size suffix) <size>)]
         (|>> :representation
              (\ n.octal encoded)
              (..octal_padding <size>)
              (text.suffix suffix)
              (\ utf8.codec encoded)
              (format.segment padded_size))))

     (def: <coercion>
       (-> Nat <type>)
       (|>> (n.% <limit>)
            :abstraction))
     )]

  [not_a_small_number small_limit ..small_size
   Small small from_small
   small_writer (format ..blank ..null)
   as_small]
  [not_a_big_number big_limit ..big_size
   Big big from_big
   big_writer ..blank
   as_big]
  )

(exception: .public (wrong_character {expected Char} {actual Char})
  (exception.report
   ["Expected" (%.nat expected)]
   ["Actual" (%.nat actual)]))

(def: small_suffix
  (Parser Any)
  (do <>.monad
    [pre_end <binary>.bits/8
     end <binary>.bits/8
     _ (let [expected (`` (char (~~ (static ..blank))))]
         (<>.assertion (exception.error ..wrong_character [expected pre_end])
                       (n.= expected pre_end)))
     _ (let [expected (`` (char (~~ (static ..null))))]
         (<>.assertion (exception.error ..wrong_character [expected end])
                       (n.= expected end)))]
    (in [])))

(def: small_parser
  (Parser Small)
  (do <>.monad
    [digits (<binary>.segment ..small_size)
     digits (<>.lifted (\ utf8.codec decoded digits))
     _ ..small_suffix]
    (<>.lifted
     (do {! try.monad}
       [value (\ n.octal decoded digits)]
       (..small value)))))

(def: big_parser
  (Parser Big)
  (do <>.monad
    [digits (<binary>.segment ..big_size)
     digits (<>.lifted (\ utf8.codec decoded digits))
     end <binary>.bits/8
     _ (let [expected (`` (char (~~ (static ..blank))))]
         (<>.assertion (exception.error ..wrong_character [expected end])
                       (n.= expected end)))]
    (<>.lifted
     (do {! try.monad}
       [value (\ n.octal decoded digits)]
       (..big value)))))

(abstract: Checksum
  {}
  
  Text

  (def: from_checksum
    (-> Checksum Text)
    (|>> :representation))

  (def: dummy_checksum
    Checksum
    (:abstraction "        "))

  (def: checksum_suffix
    (format ..blank ..null))

  (def: checksum
    (-> Binary Nat)
    (binary.aggregate n.+ 0))

  (def: checksum_checksum
    (|> ..dummy_checksum
        :representation
        (\ utf8.codec encoded)
        ..checksum))

  (def: checksum_code
    (-> Binary Checksum)
    (|>> ..checksum
         ..as_small
         ..from_small
         (\ n.octal encoded)
         (..octal_padding ..small_size)
         (text.suffix ..checksum_suffix)
         :abstraction))

  (def: checksum_writer
    (Writer Checksum)
    (let [padded_size (n.+ (text.size ..checksum_suffix)
                           ..small_size)]
      (|>> :representation
           (\ utf8.codec encoded)
           (format.segment padded_size))))

  (def: checksum_parser
    (Parser [Nat Checksum])
    (do <>.monad
      [ascii (<binary>.segment ..small_size)
       digits (<>.lifted (\ utf8.codec decoded ascii))
       _ ..small_suffix
       value (<>.lifted
              (\ n.octal decoded digits))]
      (in [value
           (:abstraction (format digits ..checksum_suffix))])))
  )

(def: last_ascii
  Char
  (number.hex "007F"))

(def: ascii?
  (-> Text Bit)
  (|>> (\ utf8.codec encoded)
       (binary.aggregate (function (_ char verdict)
                           (.and verdict
                                 (n.<= ..last_ascii char)))
                         true)))

(exception: .public (not_ascii {text Text})
  (exception.report
   ["Text" (%.text text)]))

(def: .public name_size Size 31)
(def: .public path_size Size 99)

(def: (un_padded string)
  (-> Binary (Try Binary))
  (case (binary.size string)
    0 (#try.Success string)
    size (loop [end (-- size)]
           (case end
             0 (#try.Success (\ utf8.codec encoded ""))
             _ (do try.monad
                 [last_char (binary.read/8! end string)]
                 (`` (case (.nat last_char)
                       (^ (char (~~ (static ..null))))
                       (recur (-- end))

                       _
                       (binary.slice 0 (++ end) string))))))))

(template [<type> <representation> <size> <exception> <in> <out> <writer> <parser> <none>]
  [(abstract: .public <type>
     {}
     
     <representation>

     (exception: .public (<exception> {value Text})
       (exception.report
        ["Value" (%.text value)]
        ["Size" (%.nat (text.size value))]
        ["Maximum" (%.nat <size>)]))

     (def: .public (<in> value)
       (-> <representation> (Try <type>))
       (if (..ascii? value)
         (if (|> value
                 (\ utf8.codec encoded)
                 binary.size
                 (n.> <size>))
           (exception.except <exception> [value])
           (#try.Success (:abstraction value)))
         (exception.except ..not_ascii [value])))

     (def: .public <out>
       (-> <type> <representation>)
       (|>> :representation))

     (def: <writer>
       (Writer <type>)
       (let [suffix ..null
             padded_size (n.+ (text.size suffix) <size>)]
         (|>> :representation
              (text.suffix suffix)
              (\ utf8.codec encoded)
              (format.segment padded_size))))

     (def: <parser>
       (Parser <type>)
       (do <>.monad
         [string (<binary>.segment <size>)
          end <binary>.bits/8
          .let [expected (`` (char (~~ (static ..null))))]
          _ (<>.assertion (exception.error ..wrong_character [expected end])
                          (n.= expected end))]
         (<>.lifted
          (do {! try.monad}
            [ascii (..un_padded string)
             text (\ utf8.codec decoded ascii)]
            (<in> text)))))

     (def: .public <none>
       <type>
       (try.trusted (<in> "")))
     )]

  [Name Text      ..name_size name_is_too_long name from_name name_writer name_parser anonymous]
  [Path file.Path ..path_size path_is_too_long path from_path path_writer path_parser no_path]
  )

(def: magic_size Size 7)

(abstract: Magic
  {}
  
  Text

  (def: ustar (:abstraction "ustar  "))

  (def: from_magic
    (-> Magic Text)
    (|>> :representation))

  (def: magic_writer
    (Writer Magic)
    (let [padded_size (n.+ (text.size ..null)
                           ..magic_size)]
      (|>> :representation
           (\ utf8.codec encoded)
           (format.segment padded_size))))

  (def: magic_parser
    (Parser Magic)
    (do <>.monad
      [string (<binary>.segment ..magic_size)
       end <binary>.bits/8
       .let [expected (`` (char (~~ (static ..null))))]
       _ (<>.assertion (exception.error ..wrong_character [expected end])
                       (n.= expected end))]
      (<>.lifted
       (\ try.monad each (|>> :abstraction)
          (\ utf8.codec decoded string)))))
  )

(def: block_size Size 512)

(def: owner_id_size ..small_size)

(def: blank_size Size (text.size ..blank))
(def: null_size Size (text.size ..null))
(def: mode_size Size ..small_size)
(def: content_size Size ..big_size)
(def: modification_time_size Size ..big_size)
(def: checksum_size Size ..small_size)
(def: link_flag_size Size 1)
(def: device_size Size ..small_size)

(def: small_number
  (-> Size Size)
  (|>> ($_ n.+ ..blank_size ..null_size)))

(def: big_number
  (-> Size Size)
  (|>> ($_ n.+ ..blank_size)))

(def: string
  (-> Size Size)
  (|>> ($_ n.+ ..null_size)))

(def: header_size
  ($_ n.+
      ... name
      (..string ..path_size)
      ... mode
      (..small_number ..mode_size)
      ... uid
      (..small_number ..owner_id_size)
      ... gid
      (..small_number ..owner_id_size)
      ... size
      (..big_number ..content_size)
      ... mtime
      (..big_number ..modification_time_size)
      ... chksum
      (..small_number ..checksum_size)
      ... linkflag
      ..link_flag_size
      ... linkname
      (..string ..path_size)
      ... magic
      (..string ..magic_size)
      ... uname
      (..string ..name_size)
      ... gname
      (..string ..name_size)
      ... devmajor
      (..small_number ..device_size)
      ... devminor
      (..small_number ..device_size)))

(abstract: Link_Flag
  {}
  
  Char

  (def: link_flag
    (-> Link_Flag Char)
    (|>> :representation))

  (def: link_flag_writer
    (Writer Link_Flag)
    (|>> :representation
         format.bits/8))

  (with_expansions [<options> (as_is [0 old_normal]
                                     [(char "0") normal]
                                     [(char "1") link]
                                     [(char "2") symbolic_link]
                                     [(char "3") character]
                                     [(char "4") block]
                                     [(char "5") directory]
                                     [(char "6") fifo]
                                     [(char "7") contiguous])]
    (template [<flag> <name>]
      [(def: <name>
         Link_Flag
         (:abstraction <flag>))]

      <options>
      )

    (exception: .public (invalid_link_flag {value Nat})
      (exception.report
       ["Value" (%.nat value)]))

    (def: link_flag_parser
      (Parser Link_Flag)
      (do <>.monad
        [linkflag <binary>.bits/8]
        (case (.nat linkflag)
          (^template [<value> <link_flag>]
            [(^ <value>)
             (in <link_flag>)])
          (<options>)

          _
          (<>.lifted
           (exception.except ..invalid_link_flag [(.nat linkflag)]))))))
  )

(abstract: .public Mode
  {}
  
  Nat

  (def: .public mode
    (-> Mode Nat)
    (|>> :representation))

  (def: .public (and left right)
    (-> Mode Mode Mode)
    (:abstraction
     (i64.or (:representation left)
             (:representation right))))

  (def: mode_writer
    (Writer Mode)
    (|>> :representation
         ..small
         try.trusted
         ..small_writer))

  (exception: .public (invalid_mode {value Nat})
    (exception.report
     ["Value" (%.nat value)]))

  (with_expansions [<options> (as_is ["0000" none]
                                     
                                     ["0001" execute_by_other]
                                     ["0002" write_by_other]
                                     ["0004" read_by_other]

                                     ["0010" execute_by_group]
                                     ["0020" write_by_group]
                                     ["0040" read_by_group]

                                     ["0100" execute_by_owner]
                                     ["0200" write_by_owner]
                                     ["0400" read_by_owner]

                                     ["1000" save_text]
                                     ["2000" set_group_id_on_execution]
                                     ["4000" set_user_id_on_execution])]
    (template [<code> <name>]
      [(def: .public <name>
         Mode
         (:abstraction (number.oct <code>)))]

      <options>
      )

    (def: maximum_mode
      Mode
      ($_ and
          ..none
          
          ..execute_by_other
          ..write_by_other
          ..read_by_other

          ..execute_by_group
          ..write_by_group
          ..read_by_group

          ..execute_by_owner
          ..write_by_owner
          ..read_by_owner

          ..save_text
          ..set_group_id_on_execution
          ..set_user_id_on_execution
          ))

    (def: mode_parser
      (Parser Mode)
      (do {! <>.monad}
        [value (\ ! each ..from_small ..small_parser)]
        (if (n.> (:representation ..maximum_mode)
                 value)
          (<>.lifted
           (exception.except ..invalid_mode [value]))
          (in (:abstraction value))))))
  )

(def: maximum_content_size
  Nat
  (|> ..octal_size
      (list.repeated ..content_size)
      (list\mix n.* 1)))

(abstract: .public Content
  {}
  
  [Big Binary]

  (def: .public (content content)
    (-> Binary (Try Content))
    (do try.monad
      [size (..big (binary.size content))]
      (in (:abstraction [size content]))))

  (def: from_content
    (-> Content [Big Binary])
    (|>> :representation))

  (def: .public data
    (-> Content Binary)
    (|>> :representation product.right))
  )

(type: .public ID
  Small)

(def: .public no_id
  ID
  (..as_small 0))

(type: .public Owner
  (Record
   [#name Name
    #id ID]))

(type: .public Ownership
  (Record
   [#user Owner
    #group Owner]))

(type: .public File
  [Path Instant Mode Ownership Content])

(type: .public Normal File)
(type: .public Symbolic_Link Path)
(type: .public Directory Path)
(type: .public Contiguous File)

(type: .public Entry
  (Variant
   (#Normal ..Normal)
   (#Symbolic_Link ..Symbolic_Link)
   (#Directory ..Directory)
   (#Contiguous ..Contiguous)))

(type: Device
  Small)

(def: no_device
  Device
  (try.trusted (..small 0)))

(type: .public Tar
  (Row Entry))

(def: (blocks size)
  (-> Big Nat)
  (n.+ (n./ ..block_size
            (..from_big size))
       (case (n.% ..block_size (..from_big size))
         0 0
         _ 1)))

(def: rounded_content_size
  (-> Big Nat)
  (|>> ..blocks
       (n.* ..block_size)))

(type: Header
  (Record
   [#path Path
    #mode Mode
    #user_id ID
    #group_id ID
    #size Big
    #modification_time Big
    #checksum Checksum
    #link_flag Link_Flag
    #link_name Path
    #magic Magic
    #user_name Name
    #group_name Name
    #major_device Device
    #minor_device Device]))

(def: header_writer'
  (Writer Header)
  ($_ format.and
      ..path_writer
      ..mode_writer
      ..small_writer
      ..small_writer
      ..big_writer
      ..big_writer
      ..checksum_writer
      ..link_flag_writer
      ..path_writer
      ..magic_writer
      ..name_writer
      ..name_writer
      ..small_writer
      ..small_writer
      ))

(def: (header_writer header)
  (Writer Header)
  (let [checksum (|> header
                     (with@ #checksum ..dummy_checksum)
                     (format.result ..header_writer')
                     ..checksum_code)]
    (|> header
        (with@ #checksum checksum)
        (format.result ..header_writer')
        (format.segment ..block_size))))

(def: modification_time
  (-> Instant Big)
  (|>> instant.relative
       (duration.ticks duration.second)
       .nat
       ..as_big))

(def: (file_writer link_flag)
  (-> Link_Flag (Writer File))
  (function (_ [path modification_time mode ownership content])
    (let [[size content] (..from_content content)
          writer ($_ format.and
                     ..header_writer
                     (format.segment (..rounded_content_size size)))]
      (writer [[#path path
                #mode mode
                #user_id (value@ [#user #id] ownership)
                #group_id (value@ [#group #id] ownership)
                #size size
                #modification_time (..modification_time modification_time)
                #checksum ..dummy_checksum
                #link_flag link_flag
                #link_name ..no_path
                #magic ..ustar
                #user_name (value@ [#user #name] ownership)
                #group_name (value@ [#group #name] ownership)
                #major_device ..no_device
                #minor_device ..no_device]
               content]))))

(def: normal_file_writer
  (Writer File)
  (..file_writer ..normal))

(def: contiguous_file_writer
  (Writer File)
  (..file_writer ..contiguous))

(def: (symbolic_link_writer path)
  (Writer Path)
  (..header_writer
   [#path ..no_path
    #mode ..none
    #user_id ..no_id
    #group_id ..no_id
    #size (..as_big 0)
    #modification_time (..as_big 0)
    #checksum ..dummy_checksum
    #link_flag ..symbolic_link
    #link_name path
    #magic ..ustar
    #user_name ..anonymous
    #group_name ..anonymous
    #major_device ..no_device
    #minor_device ..no_device]))

(def: (directory_writer path)
  (Writer Path)
  (..header_writer
   [#path path
    #mode ..none
    #user_id ..no_id
    #group_id ..no_id
    #size (..as_big 0)
    #modification_time (..as_big 0)
    #checksum ..dummy_checksum
    #link_flag ..directory
    #link_name ..no_path
    #magic ..ustar
    #user_name ..anonymous
    #group_name ..anonymous
    #major_device ..no_device
    #minor_device ..no_device]))

(def: entry_writer
  (Writer Entry)
  (|>> (case> (#Normal value) (..normal_file_writer value)
              (#Symbolic_Link value) (..symbolic_link_writer value)
              (#Directory value) (..directory_writer value)
              (#Contiguous value) (..contiguous_file_writer value))))

(def: end_of_archive_size
  Size
  (n.* 2 ..block_size))

(def: .public writer
  (Writer Tar)
  (let [end_of_archive (binary.empty ..end_of_archive_size)]
    (function (_ tar)
      (format\composite (row\mix (function (_ next total)
                                   (format\composite total (..entry_writer next)))
                                 format\identity
                                 tar)
                        (format.segment ..end_of_archive_size end_of_archive)))))

(exception: .public (wrong_checksum {expected Nat} {actual Nat})
  (exception.report
   ["Expected" (%.nat expected)]
   ["Actual" (%.nat actual)]))

(def: header_padding_size
  (n.- header_size block_size))

... When the checksum gets originally calculated, the assumption is that all the characters in the checksum field
... of the header will be spaces.
... This means that just calculating the checksum of the 512 bytes of the header, when reading them, would yield
... an incorrect result, as the contents of the checksum field would be an actual checksum, instead of just spaces.
... To correct for this, it is necessary to calculate the checksum of just the checksum field, subtract that, and then
... add-in the checksum of the spaces.
(def: (expected_checksum checksum header)
  (-> Checksum Binary Nat)
  (let [|checksum| (|> checksum
                       ..from_checksum
                       (\ utf8.codec encoded)
                       ..checksum)]
    (|> (..checksum header)
        (n.- |checksum|)
        (n.+ ..checksum_checksum))))

(def: header_parser
  (Parser Header)
  (do <>.monad
    [binary_header (<>.speculative (<binary>.segment block_size))
     path ..path_parser
     mode ..mode_parser
     user_id ..small_parser
     group_id ..small_parser
     size ..big_parser
     modification_time ..big_parser
     [actual checksum_code] ..checksum_parser
     _ (let [expected (expected_checksum checksum_code binary_header)]
         (<>.lifted
          (exception.assertion ..wrong_checksum [expected actual]
                               (n.= expected actual))))
     link_flag ..link_flag_parser
     link_name ..path_parser
     magic ..magic_parser
     user_name ..name_parser
     group_name ..name_parser
     major_device ..small_parser
     minor_device ..small_parser
     _ (<binary>.segment ..header_padding_size)]
    (in [#path path
         #mode mode
         #user_id user_id
         #group_id group_id
         #size size
         #modification_time modification_time
         #checksum checksum_code
         #link_flag link_flag
         #link_name link_name
         #magic magic
         #user_name user_name
         #group_name group_name
         #major_device major_device
         #minor_device minor_device])))

(exception: .public (wrong_link_flag {expected Link_Flag} {actual Link_Flag})
  (exception.report
   ["Expected" (%.nat (..link_flag expected))]
   ["Actual" (%.nat (..link_flag actual))]))

(def: (file_parser expected)
  (-> Link_Flag (Parser File))
  (do <>.monad
    [header ..header_parser
     _ (<>.assertion (exception.error ..wrong_link_flag [expected (value@ #link_flag header)])
                     (same? expected (value@ #link_flag header)))
     .let [size (value@ #size header)
           rounded_size (..rounded_content_size size)]
     content (<binary>.segment (..from_big size))
     content (<>.lifted (..content content))
     _ (<binary>.segment (n.- (..from_big size) rounded_size))]
    (in [(value@ #path header)
         (|> header
             (value@ #modification_time)
             ..from_big
             .int
             duration.of_millis
             (duration.up (|> duration.second duration.millis .nat))
             instant.absolute)
         (value@ #mode header)
         [#user [#name (value@ #user_name header)
                 #id (value@ #user_id header)]
          #group [#name (value@ #group_name header)
                  #id (value@ #group_id header)]]
         content])))

(def: (file_name_parser expected extractor)
  (-> Link_Flag (-> Header Path) (Parser Path))
  (do <>.monad
    [header ..header_parser
     _ (<>.lifted
        (exception.assertion ..wrong_link_flag [expected (value@ #link_flag header)]
                             (n.= (..link_flag expected)
                                  (..link_flag (value@ #link_flag header)))))]
    (in (extractor header))))

(def: entry_parser
  (Parser Entry)
  ($_ <>.either
      (\ <>.monad each (|>> #..Normal)
         (<>.either (..file_parser ..normal)
                    (..file_parser ..old_normal)))
      (\ <>.monad each (|>> #..Symbolic_Link)
         (..file_name_parser ..symbolic_link (value@ #link_name)))
      (\ <>.monad each (|>> #..Directory)
         (..file_name_parser ..directory (value@ #path)))
      (\ <>.monad each (|>> #..Contiguous)
         (..file_parser ..contiguous))))

... It's safe to implement the parser this way because the range of values for Nat is 2^64
... Whereas the maximum possible value for the checksum of a 512 block is (256 × 512) = 131,072
(def: end_of_archive_block_parser
  (Parser Any)
  (do <>.monad
    [block (<binary>.segment ..block_size)]
    (let [actual (..checksum block)]
      (<>.lifted
       (exception.assertion ..wrong_checksum [0 actual]
                            (n.= 0 actual))))))

(exception: .public invalid_end_of_archive)

(def: end_of_archive_parser
  (Parser Any)
  (do <>.monad
    [_ (<>.at_most 2 end_of_archive_block_parser)
     done? <binary>.end?]
    (<>.lifted
     (exception.assertion ..invalid_end_of_archive []
                          done?))))

(def: .public parser
  (Parser Tar)
  (|> (<>.some entry_parser)
      (\ <>.monad each row.of_list)
      (<>.before ..end_of_archive_parser)))