/
TarHeader.Write.cs
1001 lines (824 loc) · 46 KB
/
TarHeader.Write.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Buffers.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace System.Formats.Tar
{
// Writes header attributes of a tar archive entry.
internal sealed partial class TarHeader
{
private static ReadOnlySpan<byte> UstarMagicBytes => "ustar\0"u8;
private static ReadOnlySpan<byte> UstarVersionBytes => "00"u8;
private static ReadOnlySpan<byte> GnuMagicBytes => "ustar "u8;
private static ReadOnlySpan<byte> GnuVersionBytes => " \0"u8;
// Predefined text for the Name field of a GNU long metadata entry. Applies for both LongPath ('L') and LongLink ('K').
private const string GnuLongMetadataName = "././@LongLink";
private const string ArgNameEntry = "entry";
internal void WriteAs(TarEntryFormat format, Stream archiveStream, Span<byte> buffer)
{
Debug.Assert(format > TarEntryFormat.Unknown && format <= TarEntryFormat.Gnu);
Debug.Assert(archiveStream.CanSeek || _dataStream == null || _dataStream.CanSeek);
if (archiveStream.CanSeek && _dataStream is { CanSeek: false })
{
WriteWithUnseekableDataStreamAs(format, archiveStream, buffer);
}
else // Seek status of archive does not matter
{
long bytesToWrite = GetTotalDataBytesToWrite();
WriteFieldsToBuffer(format, bytesToWrite, buffer);
archiveStream.Write(buffer);
if (_dataStream != null)
{
WriteData(archiveStream, _dataStream, _size);
}
}
}
internal async Task WriteAsAsync(TarEntryFormat format, Stream archiveStream, Memory<byte> buffer, CancellationToken cancellationToken)
{
Debug.Assert(format > TarEntryFormat.Unknown && format <= TarEntryFormat.Gnu);
Debug.Assert(archiveStream.CanSeek || _dataStream == null || _dataStream.CanSeek);
if (archiveStream.CanSeek && _dataStream is { CanSeek: false })
{
await WriteWithUnseekableDataStreamAsAsync(format, archiveStream, buffer, cancellationToken).ConfigureAwait(false);
}
else // seek status of archive does not matter
{
long bytesToWrite = GetTotalDataBytesToWrite();
WriteFieldsToBuffer(format, bytesToWrite, buffer.Span);
await archiveStream.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);
if (_dataStream != null)
{
await WriteDataAsync(archiveStream, _dataStream, _size, cancellationToken).ConfigureAwait(false);
}
}
}
private void WriteWithUnseekableDataStreamAs(TarEntryFormat format, Stream archiveStream, Span<byte> buffer)
{
// When the data stream is unseekable, the order in which we write the entry data changes
Debug.Assert(archiveStream.CanSeek);
Debug.Assert(_dataStream != null);
Debug.Assert(!_dataStream.CanSeek);
// Store the start of the current entry's header, it'll be used later
long headerStartPosition = archiveStream.Position;
ushort dataLocation = format switch
{
TarEntryFormat.V7 => FieldLocations.V7Data,
TarEntryFormat.Ustar or TarEntryFormat.Pax => FieldLocations.PosixData,
TarEntryFormat.Gnu => FieldLocations.GnuData,
_ => throw new ArgumentOutOfRangeException(nameof(format))
};
// We know the exact location where the data starts depending on the format
long dataStartPosition = headerStartPosition + dataLocation;
// Move to the data start location and write the data
archiveStream.Seek(dataLocation, SeekOrigin.Current);
_dataStream.CopyTo(archiveStream); // The data gets copied from the current position
// Get the new archive stream position, and the difference is the size of the data stream
long dataEndPosition = archiveStream.Position;
long actualLength = dataEndPosition - dataStartPosition;
// Write the padding now so that we can go back to writing the entry's header metadata
WriteEmptyPadding(archiveStream, actualLength);
// Store the end of the current header, we will write the next one after this position
long endOfHeaderPosition = archiveStream.Position;
// Go back to the start of the entry header to write the rest of the fields
archiveStream.Position = headerStartPosition;
WriteFieldsToBuffer(format, actualLength, buffer);
archiveStream.Write(buffer);
// Finally, move to the end of the header to continue with the next entry
archiveStream.Position = endOfHeaderPosition;
}
// Asynchronously writes the entry in the order required to be able to obtain the unseekable data stream size.
private async Task WriteWithUnseekableDataStreamAsAsync(TarEntryFormat format, Stream archiveStream, Memory<byte> buffer, CancellationToken cancellationToken)
{
// When the data stream is unseekable, the order in which we write the entry data changes
Debug.Assert(archiveStream.CanSeek);
Debug.Assert(_dataStream != null);
Debug.Assert(!_dataStream.CanSeek);
// Store the start of the current entry's header, it'll be used later
long headerStartPosition = archiveStream.Position;
ushort dataLocation = format switch
{
TarEntryFormat.V7 => FieldLocations.V7Data,
TarEntryFormat.Ustar or TarEntryFormat.Pax => FieldLocations.PosixData,
TarEntryFormat.Gnu => FieldLocations.GnuData,
_ => throw new ArgumentOutOfRangeException(nameof(format))
};
// We know the exact location where the data starts depending on the format
long dataStartPosition = headerStartPosition + dataLocation;
// Move to the data start location and write the data
archiveStream.Seek(dataLocation, SeekOrigin.Current);
await _dataStream.CopyToAsync(archiveStream, cancellationToken).ConfigureAwait(false); // The data gets copied from the current position
// Get the new archive stream position, and the difference is the size of the data stream
long dataEndPosition = archiveStream.Position;
long actualLength = dataEndPosition - dataStartPosition;
// Write the padding now so that we can go back to writing the entry's header metadata
await WriteEmptyPaddingAsync(archiveStream, actualLength, cancellationToken).ConfigureAwait(false);
// Store the end of the current header, we will write the next one after this position
long endOfHeaderPosition = archiveStream.Position;
// Go back to the start of the entry header to write the rest of the fields
archiveStream.Position = headerStartPosition;
WriteFieldsToBuffer(format, actualLength, buffer.Span);
await archiveStream.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);
// Finally, move to the end of the header to continue with the next entry
archiveStream.Position = endOfHeaderPosition;
}
// Writes the V7 header fields to the specified buffer, calculates and writes the checksum, then returns the final data length.
private void WriteV7FieldsToBuffer(long size, Span<byte> buffer)
{
_size = size;
TarEntryType actualEntryType = TarHelpers.GetCorrectTypeFlagForFormat(TarEntryFormat.V7, _typeFlag);
int tmpChecksum = WriteName(buffer);
tmpChecksum += WriteCommonFields(buffer, actualEntryType);
_checksum = WriteChecksum(tmpChecksum, buffer);
}
// Writes the Ustar header fields to the specified buffer, calculates and writes the checksum, then returns the final data length.
private void WriteUstarFieldsToBuffer(long size, Span<byte> buffer)
{
_size = size;
TarEntryType actualEntryType = TarHelpers.GetCorrectTypeFlagForFormat(TarEntryFormat.Ustar, _typeFlag);
int tmpChecksum = WriteUstarName(buffer);
tmpChecksum += WriteCommonFields(buffer, actualEntryType);
tmpChecksum += WritePosixMagicAndVersion(buffer);
tmpChecksum += WritePosixAndGnuSharedFields(buffer);
_checksum = WriteChecksum(tmpChecksum, buffer);
}
// Writes the current header as a PAX Global Extended Attributes entry into the archive stream.
internal void WriteAsPaxGlobalExtendedAttributes(Stream archiveStream, Span<byte> buffer, int globalExtendedAttributesEntryNumber)
{
VerifyGlobalExtendedAttributesDataIsValid(globalExtendedAttributesEntryNumber);
WriteAsPaxExtendedAttributes(archiveStream, buffer, ExtendedAttributes, isGea: true, globalExtendedAttributesEntryNumber);
}
// Writes the current header as a PAX Global Extended Attributes entry into the archive stream and returns the value of the final checksum.
internal Task WriteAsPaxGlobalExtendedAttributesAsync(Stream archiveStream, Memory<byte> buffer, int globalExtendedAttributesEntryNumber, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<int>(cancellationToken);
}
VerifyGlobalExtendedAttributesDataIsValid(globalExtendedAttributesEntryNumber);
return WriteAsPaxExtendedAttributesAsync(archiveStream, buffer, ExtendedAttributes, isGea: true, globalExtendedAttributesEntryNumber, cancellationToken);
}
// Verifies the data is valid for writing a Global Extended Attributes entry.
private void VerifyGlobalExtendedAttributesDataIsValid(int globalExtendedAttributesEntryNumber)
{
Debug.Assert(_typeFlag is TarEntryType.GlobalExtendedAttributes);
Debug.Assert(globalExtendedAttributesEntryNumber >= 0);
}
// Writes the current header as a PAX entry into the archive stream.
// Makes sure to add the preceding extended attributes entry before the actual entry.
internal void WriteAsPax(Stream archiveStream, Span<byte> buffer)
{
Debug.Assert(_typeFlag is not TarEntryType.GlobalExtendedAttributes);
// First, we write the preceding extended attributes header
TarHeader extendedAttributesHeader = new(TarEntryFormat.Pax);
// Fill the current header's dict
CollectExtendedAttributesFromStandardFieldsIfNeeded();
// And pass the attributes to the preceding extended attributes header for writing
extendedAttributesHeader.WriteAsPaxExtendedAttributes(archiveStream, buffer, ExtendedAttributes, isGea: false, globalExtendedAttributesEntryNumber: -1);
buffer.Clear(); // Reset it to reuse it
// Second, we write this header as a normal one
WriteAs(TarEntryFormat.Pax, archiveStream, buffer);
}
// Asynchronously writes the current header as a PAX entry into the archive stream.
// Makes sure to add the preceding exteded attributes entry before the actual entry.
internal async Task WriteAsPaxAsync(Stream archiveStream, Memory<byte> buffer, CancellationToken cancellationToken)
{
Debug.Assert(_typeFlag is not TarEntryType.GlobalExtendedAttributes);
cancellationToken.ThrowIfCancellationRequested();
// First, we write the preceding extended attributes header
TarHeader extendedAttributesHeader = new(TarEntryFormat.Pax);
// Fill the current header's dict
CollectExtendedAttributesFromStandardFieldsIfNeeded();
// And pass the attributes to the preceding extended attributes header for writing
await extendedAttributesHeader.WriteAsPaxExtendedAttributesAsync(archiveStream, buffer, ExtendedAttributes, isGea: false, globalExtendedAttributesEntryNumber: -1, cancellationToken).ConfigureAwait(false);
buffer.Span.Clear(); // Reset it to reuse it
// Second, we write this header as a normal one
await WriteAsAsync(TarEntryFormat.Pax, archiveStream, buffer, cancellationToken).ConfigureAwait(false);
}
// Writes the current header as a Gnu entry into the archive stream.
// Makes sure to add the preceding LongLink and/or LongPath entries if necessary, before the actual entry.
internal void WriteAsGnu(Stream archiveStream, Span<byte> buffer)
{
// First, we determine if we need a preceding LongLink, and write it if needed
if (_linkName != null && Encoding.UTF8.GetByteCount(_linkName) > FieldLengths.LinkName)
{
TarHeader longLinkHeader = GetGnuLongMetadataHeader(TarEntryType.LongLink, _linkName);
longLinkHeader.WriteAs(TarEntryFormat.Gnu, archiveStream, buffer);
buffer.Clear(); // Reset it to reuse it
}
// Second, we determine if we need a preceding LongPath, and write it if needed
if (Encoding.UTF8.GetByteCount(_name) > FieldLengths.Name)
{
TarHeader longPathHeader = GetGnuLongMetadataHeader(TarEntryType.LongPath, _name);
longPathHeader.WriteAs(TarEntryFormat.Gnu, archiveStream, buffer);
buffer.Clear(); // Reset it to reuse it
}
// Third, we write this header as a normal one
WriteAs(TarEntryFormat.Gnu, archiveStream, buffer);
}
// Writes the current header as a Gnu entry into the archive stream.
// Makes sure to add the preceding LongLink and/or LongPath entries if necessary, before the actual entry.
internal async Task WriteAsGnuAsync(Stream archiveStream, Memory<byte> buffer, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
// First, we determine if we need a preceding LongLink, and write it if needed
if (_linkName != null && Encoding.UTF8.GetByteCount(_linkName) > FieldLengths.LinkName)
{
TarHeader longLinkHeader = GetGnuLongMetadataHeader(TarEntryType.LongLink, _linkName);
await longLinkHeader.WriteAsAsync(TarEntryFormat.Gnu, archiveStream, buffer, cancellationToken).ConfigureAwait(false);
buffer.Span.Clear(); // Reset it to reuse it
}
// Second, we determine if we need a preceding LongPath, and write it if needed
if (Encoding.UTF8.GetByteCount(_name) > FieldLengths.Name)
{
TarHeader longPathHeader = GetGnuLongMetadataHeader(TarEntryType.LongPath, _name);
await longPathHeader.WriteAsAsync(TarEntryFormat.Gnu, archiveStream, buffer, cancellationToken).ConfigureAwait(false);
buffer.Span.Clear(); // Reset it to reuse it
}
// Third, we write this header as a normal one
await WriteAsAsync(TarEntryFormat.Gnu, archiveStream, buffer, cancellationToken).ConfigureAwait(false);
}
// Creates and returns a GNU long metadata header, with the specified long text written into its data stream.
private static TarHeader GetGnuLongMetadataHeader(TarEntryType entryType, string longText)
{
Debug.Assert(entryType is TarEntryType.LongPath or TarEntryType.LongLink);
TarHeader longMetadataHeader = new(TarEntryFormat.Gnu);
longMetadataHeader._name = GnuLongMetadataName; // Same name for both longpath or longlink
longMetadataHeader._mode = TarHelpers.GetDefaultMode(entryType);
longMetadataHeader._uid = 0;
longMetadataHeader._gid = 0;
longMetadataHeader._mTime = DateTimeOffset.MinValue; // 0
longMetadataHeader._typeFlag = entryType;
longMetadataHeader._dataStream = new MemoryStream(Encoding.UTF8.GetBytes(longText));
return longMetadataHeader;
}
// Shared checksum and data length calculations for GNU entry writing.
private void WriteGnuFieldsToBuffer(long size, Span<byte> buffer)
{
_size = size;
int tmpChecksum = WriteName(buffer);
tmpChecksum += WriteCommonFields(buffer, TarHelpers.GetCorrectTypeFlagForFormat(TarEntryFormat.Gnu, _typeFlag));
tmpChecksum += WriteGnuMagicAndVersion(buffer);
tmpChecksum += WritePosixAndGnuSharedFields(buffer);
tmpChecksum += WriteGnuFields(buffer);
_checksum = WriteChecksum(tmpChecksum, buffer);
}
// Writes the current header as a PAX Extended Attributes entry into the archive stream.
private void WriteAsPaxExtendedAttributes(Stream archiveStream, Span<byte> buffer, Dictionary<string, string> extendedAttributes, bool isGea, int globalExtendedAttributesEntryNumber)
{
WriteAsPaxExtendedAttributesShared(isGea, globalExtendedAttributesEntryNumber, extendedAttributes);
WriteAs(TarEntryFormat.Pax, archiveStream, buffer);
}
// Asynchronously writes the current header as a PAX Extended Attributes entry into the archive stream and returns the value of the final checksum.
private Task WriteAsPaxExtendedAttributesAsync(Stream archiveStream, Memory<byte> buffer, Dictionary<string, string> extendedAttributes, bool isGea, int globalExtendedAttributesEntryNumber, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
WriteAsPaxExtendedAttributesShared(isGea, globalExtendedAttributesEntryNumber, extendedAttributes);
return WriteAsAsync(TarEntryFormat.Pax, archiveStream, buffer, cancellationToken);
}
// Initializes the name, mode and type flag of a PAX extended attributes entry.
private void WriteAsPaxExtendedAttributesShared(bool isGea, int globalExtendedAttributesEntryNumber, Dictionary<string, string> extendedAttributes)
{
Debug.Assert(isGea && globalExtendedAttributesEntryNumber >= 0 || !isGea && globalExtendedAttributesEntryNumber < 0);
_dataStream = GenerateExtendedAttributesDataStream(extendedAttributes);
_name = isGea ?
GenerateGlobalExtendedAttributeName(globalExtendedAttributesEntryNumber) :
GenerateExtendedAttributeName();
_mode = TarHelpers.GetDefaultMode(_typeFlag);
_typeFlag = isGea ? TarEntryType.GlobalExtendedAttributes : TarEntryType.ExtendedAttributes;
}
// Shared checksum and data length calculations for PAX entry writing.
private void WritePaxFieldsToBuffer(long size, Span<byte> buffer)
{
_size = size;
int tmpChecksum = WriteName(buffer);
tmpChecksum += WriteCommonFields(buffer, TarHelpers.GetCorrectTypeFlagForFormat(TarEntryFormat.Pax, _typeFlag));
tmpChecksum += WritePosixMagicAndVersion(buffer);
tmpChecksum += WritePosixAndGnuSharedFields(buffer);
_checksum = WriteChecksum(tmpChecksum, buffer);
}
// Writes the format-specific fields of the current entry, as well as the entry data length, into the specified buffer.
private void WriteFieldsToBuffer(TarEntryFormat format, long bytesToWrite, Span<byte> buffer)
{
switch (format)
{
case TarEntryFormat.V7:
WriteV7FieldsToBuffer(bytesToWrite, buffer);
break;
case TarEntryFormat.Ustar:
WriteUstarFieldsToBuffer(bytesToWrite, buffer);
break;
case TarEntryFormat.Pax:
WritePaxFieldsToBuffer(bytesToWrite, buffer);
break;
case TarEntryFormat.Gnu:
WriteGnuFieldsToBuffer(bytesToWrite, buffer);
break;
}
}
// Gnu and pax save in the name byte array only the UTF8 bytes that fit.
// V7 does not support more than 100 bytes so it throws.
private int WriteName(Span<byte> buffer)
{
ReadOnlySpan<char> name = _name;
int encodedLength = GetUtf8TextLength(name);
if (encodedLength > FieldLengths.Name)
{
if (_format is TarEntryFormat.V7)
{
throw new ArgumentException(SR.Format(SR.TarEntryFieldExceedsMaxLength, nameof(TarEntry.Name)), ArgNameEntry);
}
int utf16NameTruncatedLength = GetUtf16TruncatedTextLength(name, FieldLengths.Name);
name = name.Slice(0, utf16NameTruncatedLength);
}
return WriteAsUtf8String(name, buffer.Slice(FieldLocations.Name, FieldLengths.Name));
}
// 'https://www.freebsd.org/cgi/man.cgi?tar(5)'
// If the path name is too long to fit in the 100 bytes provided by the standard format,
// it can be split at any / character with the first portion going into the prefix field.
private int WriteUstarName(Span<byte> buffer)
{
// We can have a path name as big as 256, prefix + '/' + name,
// the separator in between can be neglected as the reader will append it when it joins both fields.
const int MaxPathName = FieldLengths.Prefix + 1 + FieldLengths.Name;
if (GetUtf8TextLength(_name) > MaxPathName)
{
throw new ArgumentException(SR.Format(SR.TarEntryFieldExceedsMaxLength, nameof(TarEntry.Name)), ArgNameEntry);
}
Span<byte> encodingBuffer = stackalloc byte[MaxPathName];
int encoded = Encoding.UTF8.GetBytes(_name, encodingBuffer);
ReadOnlySpan<byte> pathNameBytes = encodingBuffer.Slice(0, encoded);
// If the pathname is able to fit in Name, we can write it down there and avoid calculating Prefix.
if (pathNameBytes.Length <= FieldLengths.Name)
{
return WriteLeftAlignedBytesAndGetChecksum(pathNameBytes, buffer.Slice(FieldLocations.Name, FieldLengths.Name));
}
int lastIdx = pathNameBytes.LastIndexOfAny(PathInternal.Utf8DirectorySeparators);
scoped ReadOnlySpan<byte> name;
scoped ReadOnlySpan<byte> prefix;
if (lastIdx < 1) // splitting at the root is not allowed.
{
name = pathNameBytes;
prefix = default;
}
else
{
name = pathNameBytes.Slice(lastIdx + 1);
prefix = pathNameBytes.Slice(0, lastIdx);
}
// At this point path name is > 100.
// Attempt to split it in a way it can use prefix.
while (prefix.Length - name.Length > FieldLengths.Prefix)
{
lastIdx = prefix.LastIndexOfAny(PathInternal.Utf8DirectorySeparators);
if (lastIdx < 1)
{
break;
}
name = pathNameBytes.Slice(lastIdx + 1);
prefix = pathNameBytes.Slice(0, lastIdx);
}
if (prefix.Length <= FieldLengths.Prefix && name.Length <= FieldLengths.Name)
{
Debug.Assert(prefix.Length != 1 || !PathInternal.Utf8DirectorySeparators.Contains(prefix[0]));
int checksum = WriteLeftAlignedBytesAndGetChecksum(prefix, buffer.Slice(FieldLocations.Prefix, FieldLengths.Prefix));
checksum += WriteLeftAlignedBytesAndGetChecksum(name, buffer.Slice(FieldLocations.Name, FieldLengths.Name));
return checksum;
}
else
{
throw new ArgumentException(SR.Format(SR.TarEntryFieldExceedsMaxLength, nameof(TarEntry.Name)), ArgNameEntry);
}
}
// Writes all the common fields shared by all formats into the specified spans.
private int WriteCommonFields(Span<byte> buffer, TarEntryType actualEntryType)
{
// Don't write an empty LinkName if the entry is a hardlink or symlink
Debug.Assert(!string.IsNullOrEmpty(_linkName) ^ (_typeFlag is not TarEntryType.SymbolicLink and not TarEntryType.HardLink));
int checksum = 0;
if (_mode > 0)
{
checksum += FormatOctal(_mode, buffer.Slice(FieldLocations.Mode, FieldLengths.Mode));
}
if (_uid > 0)
{
checksum += FormatOctal(_uid, buffer.Slice(FieldLocations.Uid, FieldLengths.Uid));
}
if (_gid > 0)
{
checksum += FormatOctal(_gid, buffer.Slice(FieldLocations.Gid, FieldLengths.Gid));
}
if (_size > 0)
{
if (_size <= TarHelpers.MaxSizeLength)
{
checksum += FormatOctal(_size, buffer.Slice(FieldLocations.Size, FieldLengths.Size));
}
else if (_format is not TarEntryFormat.Pax)
{
throw new ArgumentException(SR.Format(SR.TarSizeFieldTooLargeForEntryFormat, _format));
}
else
{
Debug.Assert(_typeFlag is not TarEntryType.ExtendedAttributes and not TarEntryType.GlobalExtendedAttributes);
Debug.Assert(Convert.ToInt64(ExtendedAttributes[PaxEaSize]) > TarHelpers.MaxSizeLength);
}
}
checksum += WriteAsTimestamp(_mTime, buffer.Slice(FieldLocations.MTime, FieldLengths.MTime));
char typeFlagChar = (char)actualEntryType;
buffer[FieldLocations.TypeFlag] = (byte)typeFlagChar;
checksum += typeFlagChar;
if (!string.IsNullOrEmpty(_linkName))
{
ReadOnlySpan<char> linkName = _linkName;
if (GetUtf8TextLength(linkName) > FieldLengths.LinkName)
{
if (_format is not TarEntryFormat.Pax and not TarEntryFormat.Gnu)
{
throw new ArgumentException(SR.Format(SR.TarEntryFieldExceedsMaxLength, nameof(TarEntry.LinkName)), ArgNameEntry);
}
int truncatedLength = GetUtf16TruncatedTextLength(linkName, FieldLengths.LinkName);
linkName = linkName.Slice(0, truncatedLength);
}
checksum += WriteAsUtf8String(linkName, buffer.Slice(FieldLocations.LinkName, FieldLengths.LinkName));
}
return checksum;
}
// Calculates how many data bytes should be written, depending on the position pointer of the stream.
// Only works if the stream is seekable.
private long GetTotalDataBytesToWrite()
{
if (_dataStream == null)
{
return 0;
}
long length = _dataStream.Length;
long position = _dataStream.Position;
return position < length ? length - position : 0;
}
// Writes the magic and version fields of a ustar or pax entry into the specified spans.
private static int WritePosixMagicAndVersion(Span<byte> buffer)
{
int checksum = WriteLeftAlignedBytesAndGetChecksum(UstarMagicBytes, buffer.Slice(FieldLocations.Magic, FieldLengths.Magic));
checksum += WriteLeftAlignedBytesAndGetChecksum(UstarVersionBytes, buffer.Slice(FieldLocations.Version, FieldLengths.Version));
return checksum;
}
// Writes the magic and vresion fields of a gnu entry into the specified spans.
private static int WriteGnuMagicAndVersion(Span<byte> buffer)
{
int checksum = WriteLeftAlignedBytesAndGetChecksum(GnuMagicBytes, buffer.Slice(FieldLocations.Magic, FieldLengths.Magic));
checksum += WriteLeftAlignedBytesAndGetChecksum(GnuVersionBytes, buffer.Slice(FieldLocations.Version, FieldLengths.Version));
return checksum;
}
// Writes the posix fields shared by ustar, pax and gnu, into the specified spans.
private int WritePosixAndGnuSharedFields(Span<byte> buffer)
{
int checksum = 0;
if (!string.IsNullOrEmpty(_uName))
{
ReadOnlySpan<char> uName = _uName;
if (GetUtf8TextLength(uName) > FieldLengths.UName)
{
if (_format is not TarEntryFormat.Pax)
{
throw new ArgumentException(SR.Format(SR.TarEntryFieldExceedsMaxLength, nameof(PaxTarEntry.UserName)), ArgNameEntry);
}
int truncatedLength = GetUtf16TruncatedTextLength(uName, FieldLengths.UName);
uName = uName.Slice(0, truncatedLength);
}
checksum += WriteAsUtf8String(uName, buffer.Slice(FieldLocations.UName, FieldLengths.UName));
}
if (!string.IsNullOrEmpty(_gName))
{
ReadOnlySpan<char> gName = _gName;
if (GetUtf8TextLength(gName) > FieldLengths.GName)
{
if (_format is not TarEntryFormat.Pax)
{
throw new ArgumentException(SR.Format(SR.TarEntryFieldExceedsMaxLength, nameof(PaxTarEntry.GroupName)), ArgNameEntry);
}
int truncatedLength = GetUtf16TruncatedTextLength(gName, FieldLengths.GName);
gName = gName.Slice(0, truncatedLength);
}
checksum += WriteAsUtf8String(gName, buffer.Slice(FieldLocations.GName, FieldLengths.GName));
}
if (_devMajor > 0)
{
checksum += FormatOctal(_devMajor, buffer.Slice(FieldLocations.DevMajor, FieldLengths.DevMajor));
}
if (_devMinor > 0)
{
checksum += FormatOctal(_devMinor, buffer.Slice(FieldLocations.DevMinor, FieldLengths.DevMinor));
}
return checksum;
}
// Saves the gnu-specific fields into the specified spans.
private int WriteGnuFields(Span<byte> buffer)
{
int checksum = WriteAsTimestamp(_aTime, buffer.Slice(FieldLocations.ATime, FieldLengths.ATime));
checksum += WriteAsTimestamp(_cTime, buffer.Slice(FieldLocations.CTime, FieldLengths.CTime));
if (_gnuUnusedBytes != null)
{
checksum += WriteLeftAlignedBytesAndGetChecksum(_gnuUnusedBytes, buffer.Slice(FieldLocations.GnuUnused, FieldLengths.AllGnuUnused));
}
return checksum;
}
// Writes the current header's data stream into the archive stream.
private static void WriteData(Stream archiveStream, Stream dataStream, long actualLength)
{
dataStream.CopyTo(archiveStream); // The data gets copied from the current position
WriteEmptyPadding(archiveStream, actualLength);
}
// Calculates the padding for the current entry and writes it after the data.
private static void WriteEmptyPadding(Stream archiveStream, long actualLength)
{
int paddingAfterData = TarHelpers.CalculatePadding(actualLength);
if (paddingAfterData != 0)
{
Debug.Assert(paddingAfterData <= TarHelpers.RecordSize);
Span<byte> zeros = stackalloc byte[TarHelpers.RecordSize];
zeros = zeros.Slice(0, paddingAfterData);
zeros.Clear();
archiveStream.Write(zeros);
}
}
// Calculates the padding for the current entry and asynchronously writes it after the data.
private static ValueTask WriteEmptyPaddingAsync(Stream archiveStream, long actualLength, CancellationToken cancellationToken)
{
int paddingAfterData = TarHelpers.CalculatePadding(actualLength);
if (paddingAfterData != 0)
{
Debug.Assert(paddingAfterData <= TarHelpers.RecordSize);
byte[] zeros = new byte[paddingAfterData];
return archiveStream.WriteAsync(zeros, cancellationToken);
}
return ValueTask.CompletedTask;
}
// Asynchronously writes the current header's data stream into the archive stream.
private static async Task WriteDataAsync(Stream archiveStream, Stream dataStream, long actualLength, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
await dataStream.CopyToAsync(archiveStream, cancellationToken).ConfigureAwait(false); // The data gets copied from the current position
int paddingAfterData = TarHelpers.CalculatePadding(actualLength);
if (paddingAfterData != 0)
{
byte[] buffer = ArrayPool<byte>.Shared.Rent(paddingAfterData);
Array.Clear(buffer, 0, paddingAfterData);
await archiveStream.WriteAsync(buffer.AsMemory(0, paddingAfterData), cancellationToken).ConfigureAwait(false);
ArrayPool<byte>.Shared.Return(buffer);
}
}
// Dumps into the archive stream an extended attribute entry containing metadata of the entry it precedes.
private static MemoryStream? GenerateExtendedAttributesDataStream(Dictionary<string, string> extendedAttributes)
{
MemoryStream? dataStream = null;
byte[]? buffer = null;
Span<byte> span = stackalloc byte[512];
if (extendedAttributes.Count > 0)
{
dataStream = new MemoryStream();
foreach ((string attribute, string value) in extendedAttributes)
{
// Generates an extended attribute key value pair string saved into a byte array, following the ISO/IEC 10646-1:2000 standard UTF-8 encoding format.
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html
// The format is:
// "XX attribute=value\n"
// where "XX" is the number of characters in the entry, including those required for the count itself.
// If prepending the length digits increases the number of digits, we need to expand.
int length = 3 + Encoding.UTF8.GetByteCount(attribute) + Encoding.UTF8.GetByteCount(value);
int originalDigitCount = CountDigits(length), newDigitCount;
length += originalDigitCount;
while ((newDigitCount = CountDigits(length)) != originalDigitCount)
{
length += newDigitCount - originalDigitCount;
originalDigitCount = newDigitCount;
}
Debug.Assert(length == CountDigits(length) + 3 + Encoding.UTF8.GetByteCount(attribute) + Encoding.UTF8.GetByteCount(value));
// Get a large enough buffer if we don't already have one.
if (span.Length < length)
{
if (buffer is not null)
{
ArrayPool<byte>.Shared.Return(buffer);
}
span = buffer = ArrayPool<byte>.Shared.Rent(length);
}
// Format the contents.
bool formatted = Utf8Formatter.TryFormat(length, span, out int bytesWritten);
Debug.Assert(formatted);
span[bytesWritten++] = (byte)' ';
bytesWritten += Encoding.UTF8.GetBytes(attribute, span.Slice(bytesWritten));
span[bytesWritten++] = (byte)'=';
bytesWritten += Encoding.UTF8.GetBytes(value, span.Slice(bytesWritten));
span[bytesWritten++] = (byte)'\n';
// Write it to the stream.
dataStream.Write(span.Slice(0, bytesWritten));
}
dataStream.Position = 0; // Ensure it gets written into the archive from the beginning
}
if (buffer is not null)
{
ArrayPool<byte>.Shared.Return(buffer);
}
return dataStream;
static int CountDigits(int value)
{
Debug.Assert(value >= 0);
int digits = 1;
while (true)
{
value /= 10;
if (value == 0) break;
digits++;
}
return digits;
}
}
// Some fields that have a reserved spot in the header, may not fit in such field anymore, but they can fit in the
// extended attributes. They get collected and saved in that dictionary, with no restrictions.
private void CollectExtendedAttributesFromStandardFieldsIfNeeded()
{
ExtendedAttributes[PaxEaName] = _name;
ExtendedAttributes[PaxEaMTime] = TarHelpers.GetTimestampStringFromDateTimeOffset(_mTime);
TryAddStringField(ExtendedAttributes, PaxEaGName, _gName, FieldLengths.GName);
TryAddStringField(ExtendedAttributes, PaxEaUName, _uName, FieldLengths.UName);
if (!string.IsNullOrEmpty(_linkName))
{
Debug.Assert(_typeFlag is TarEntryType.SymbolicLink or TarEntryType.HardLink);
ExtendedAttributes[PaxEaLinkName] = _linkName;
}
if (_size > TarHelpers.MaxSizeLength)
{
ExtendedAttributes[PaxEaSize] = _size.ToString();
}
else
{
ExtendedAttributes.Remove(PaxEaSize);
}
// Sets the specified string to the dictionary if it's longer than the specified max byte length; otherwise, remove it.
static void TryAddStringField(Dictionary<string, string> extendedAttributes, string key, string? value, int maxLength)
{
if (string.IsNullOrEmpty(value) || GetUtf8TextLength(value) <= maxLength)
{
extendedAttributes.Remove(key);
}
else
{
extendedAttributes[key] = value;
}
}
}
// The checksum accumulator first adds up the byte values of eight space chars, then the final number
// is written on top of those spaces on the specified span as ascii.
// At the end, it's saved in the header field and the final value returned.
internal static int WriteChecksum(int checksum, Span<byte> buffer)
{
// The checksum field is also counted towards the total sum
// but as an array filled with spaces
checksum += (byte)' ' * 8;
Span<byte> converted = stackalloc byte[FieldLengths.Checksum];
converted.Clear();
FormatOctal(checksum, converted);
Span<byte> destination = buffer.Slice(FieldLocations.Checksum, FieldLengths.Checksum);
// Checksum field ends with a null and a space
destination[^1] = (byte)' ';
destination[^2] = (byte)'\0';
int i = destination.Length - 3;
int j = converted.Length - 1;
while (i >= 0)
{
if (j >= 0)
{
destination[i] = converted[j];
j--;
}
else
{
destination[i] = (byte)'0'; // Leading zero chars
}
i--;
}
return checksum;
}
// Writes the specified bytes into the specified destination, aligned to the left. Returns the sum of the value of all the bytes that were written.
private static int WriteLeftAlignedBytesAndGetChecksum(ReadOnlySpan<byte> bytesToWrite, Span<byte> destination)
{
Debug.Assert(destination.Length > 1);
// Copy as many bytes as will fit
int numToCopy = Math.Min(bytesToWrite.Length, destination.Length);
bytesToWrite = bytesToWrite.Slice(0, numToCopy);
bytesToWrite.CopyTo(destination);
return Checksum(bytesToWrite);
}
// Writes the specified bytes aligned to the right, filling all the leading bytes with the zero char 0x30,
// ensuring a null terminator is included at the end of the specified span.
private static int WriteRightAlignedBytesAndGetChecksum(ReadOnlySpan<byte> bytesToWrite, Span<byte> destination)
{
Debug.Assert(destination.Length > 1);
// Null terminated
destination[^1] = (byte)'\0';
// Copy as many input bytes as will fit
int numToCopy = Math.Min(bytesToWrite.Length, destination.Length - 1);
bytesToWrite = bytesToWrite.Slice(0, numToCopy);
int copyPos = destination.Length - 1 - bytesToWrite.Length;
bytesToWrite.CopyTo(destination.Slice(copyPos));
// Fill all leading bytes with zeros
destination.Slice(0, copyPos).Fill((byte)'0');
return Checksum(destination);
}
private static int Checksum(ReadOnlySpan<byte> bytes)
{
int checksum = 0;
foreach (byte b in bytes)
{
checksum += b;
}
return checksum;
}
// Writes the specified decimal number as a right-aligned octal number and returns its checksum.
internal static int FormatOctal(long value, Span<byte> destination)
{
ulong remaining = (ulong)value;
Span<byte> digits = stackalloc byte[32]; // longer than any possible octal formatting of a ulong
int i = digits.Length - 1;
while (true)
{
digits[i] = (byte)('0' + (remaining % 8));
remaining /= 8;
if (remaining == 0) break;
i--;
}
return WriteRightAlignedBytesAndGetChecksum(digits.Slice(i), destination);
}
// Writes the specified DateTimeOffset's Unix time seconds as a right-aligned octal number, and returns its checksum.
private static int WriteAsTimestamp(DateTimeOffset timestamp, Span<byte> destination)
{
long unixTimeSeconds = timestamp.ToUnixTimeSeconds();
return FormatOctal(unixTimeSeconds, destination);
}
// Writes the specified text as an UTF8 string aligned to the left, and returns its checksum.
private static int WriteAsUtf8String(ReadOnlySpan<char> text, Span<byte> buffer)
{
int encoded = Encoding.UTF8.GetBytes(text, buffer);
return WriteLeftAlignedBytesAndGetChecksum(buffer.Slice(0, encoded), buffer);
}
// Gets the special name for the 'name' field in an extended attribute entry.
// Format: "%d/PaxHeaders.%p/%f"
// - %d: The directory name of the file, equivalent to the result of the dirname utility on the translated pathname.
// - %p: The current process ID.
// - %f: The filename of the file, equivalent to the result of the basename utility on the translated pathname.
private string GenerateExtendedAttributeName()
{
ReadOnlySpan<char> dirName = Path.GetDirectoryName(_name.AsSpan());
dirName = dirName.IsEmpty ? "." : dirName;
ReadOnlySpan<char> fileName = Path.GetFileName(_name.AsSpan());
fileName = fileName.IsEmpty ? "." : fileName;
return _typeFlag is TarEntryType.Directory or TarEntryType.DirectoryList ?
$"{dirName}/PaxHeaders.{Environment.ProcessId}/{fileName}{Path.DirectorySeparatorChar}" :
$"{dirName}/PaxHeaders.{Environment.ProcessId}/{fileName}";
}
// Gets the special name for the 'name' field in a global extended attribute entry.
// Format: "%d/GlobalHead.%p.%n"
// - %d: The path of the $TMPDIR variable, if found. Otherwise, the value is '/tmp'.
// - %p: The current process ID.
// - %n: The sequence number of the global extended header record of the archive, starting at 1.
// If the path of $TMPDIR makes the final string too long to fit in the 'name' field,
// then the TMPDIR='/tmp' is used.
private static string GenerateGlobalExtendedAttributeName(int globalExtendedAttributesEntryNumber)
{
Debug.Assert(globalExtendedAttributesEntryNumber >= 1);
ReadOnlySpan<char> tmp = Path.TrimEndingDirectorySeparator(Path.GetTempPath());
string result = $"{tmp}/GlobalHead.{Environment.ProcessId}.{globalExtendedAttributesEntryNumber}";
return result.Length >= FieldLengths.Name ?
string.Concat("/tmp", result.AsSpan(tmp.Length)) :
result;
}
private static int GetUtf8TextLength(ReadOnlySpan<char> text)
=> Encoding.UTF8.GetByteCount(text);
// Returns the text's utf16 length truncated at the specified utf8 max length.
private static int GetUtf16TruncatedTextLength(ReadOnlySpan<char> text, int utf8MaxLength)
{
Debug.Assert(GetUtf8TextLength(text) > utf8MaxLength);
int utf8Length = 0;
int utf16TruncatedLength = 0;
foreach (Rune rune in text.EnumerateRunes())
{
utf8Length += rune.Utf8SequenceLength;
if (utf8Length <= utf8MaxLength)
{
utf16TruncatedLength += rune.Utf16SequenceLength;
}
else
{
break;
}
}
return utf16TruncatedLength;
}
}