|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.Immutable; |
| 4 | +using System.IO; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace TotalImage.FileSystems.ISO |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Represents a directory record within an ISO 9660 file system |
| 11 | + /// </summary> |
| 12 | + public class IsoDirectoryRecord : Directory |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Used to separate a file name and extension in a file identifier |
| 16 | + /// </summary> |
| 17 | + public const char IDENTIFIER_SEPARATOR_1 = '.'; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Used to separate a file extension and file version in a file identifier |
| 21 | + /// </summary> |
| 22 | + public const char IDENTIFIER_SEPARATOR_2 = ';'; |
| 23 | + |
| 24 | + #region Implemented Properties / Methods |
| 25 | + /// <inheritdoc /> |
| 26 | + public override string Name { get => FileIdentifier; set => throw new NotImplementedException(); } |
| 27 | + |
| 28 | + /// <inheritdoc /> |
| 29 | + public override FileAttributes Attributes { get => 0; set => throw new NotImplementedException(); } |
| 30 | + |
| 31 | + /// <inheritdoc /> |
| 32 | + public override DateTime? LastAccessTime { get => null; set => throw new NotImplementedException(); } |
| 33 | + |
| 34 | + /// <inheritdoc /> |
| 35 | + public override DateTime? LastWriteTime { get => RecordingDate?.LocalDateTime.ToLocalTime(); set => throw new NotImplementedException(); } |
| 36 | + |
| 37 | + /// <inheritdoc /> |
| 38 | + public override DateTime? CreationTime { get => null; set => throw new NotImplementedException(); } |
| 39 | + |
| 40 | + /// <inheritdoc /> |
| 41 | + public override ulong Length { get => DataLength; set => throw new NotImplementedException(); } |
| 42 | + |
| 43 | + public override Directory CreateSubdirectory(string path) |
| 44 | + { |
| 45 | + throw new NotImplementedException(); |
| 46 | + } |
| 47 | + |
| 48 | + public override void Delete() |
| 49 | + { |
| 50 | + throw new NotImplementedException(); |
| 51 | + } |
| 52 | + |
| 53 | + public override IEnumerable<FileSystemObject> EnumerateFileSystemObjects(bool showHidden, bool showDeleted) |
| 54 | + { |
| 55 | + return Array.Empty<FileSystemObject>(); |
| 56 | + } |
| 57 | + |
| 58 | + public override void MoveTo(string path) |
| 59 | + { |
| 60 | + throw new NotImplementedException(); |
| 61 | + } |
| 62 | + #endregion |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Length of directory record |
| 66 | + /// </summary> |
| 67 | + public byte RecordLength { get; } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Length of extended attribute record |
| 71 | + /// </summary> |
| 72 | + public byte ExtendedAttributeLength { get; } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Offset of extent |
| 76 | + /// </summary> |
| 77 | + public uint ExtentOffset { get; } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Length of data |
| 81 | + /// </summary> |
| 82 | + public uint DataLength { get; } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Time directory was recorded to disc |
| 86 | + /// </summary> |
| 87 | + public DateTimeOffset? RecordingDate { get; } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Flags indicating the characteristics of the directory |
| 91 | + /// </summary> |
| 92 | + public IsoFileFlags FileFlags { get; } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// File unit size if data is recorded in interleave mode |
| 96 | + /// </summary> |
| 97 | + public byte FileUnitSize { get; } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// The interleave gap size if data is recorded in interleave mode |
| 101 | + /// </summary> |
| 102 | + public byte InterleaveGapSize { get; } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// The ordinal number of the volume that this extent appears on within the volume set |
| 106 | + /// </summary> |
| 107 | + public ushort VolumeSequenceNumber { get; } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Length of the file identifier |
| 111 | + /// </summary> |
| 112 | + public byte FileIdentifierLength { get; } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// The identifier of the file |
| 116 | + /// </summary> |
| 117 | + public string FileIdentifier { get; } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Raw binary data reserved for system use |
| 121 | + /// </summary> |
| 122 | + public ImmutableArray<byte> SystemUseContent { get; } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Create an ISO 9660 directory record |
| 126 | + /// </summary> |
| 127 | + /// <param name="record">A span containing the directory record</param> |
| 128 | + /// <exception cref="ArgumentOutOfRangeException">Thrown if the span provided does not cover the entire record</exception> |
| 129 | + public IsoDirectoryRecord(in ReadOnlySpan<byte> record) : base(null, null) |
| 130 | + { |
| 131 | + if (record[0] > record.Length) |
| 132 | + { |
| 133 | + throw new ArgumentOutOfRangeException(nameof(record)); |
| 134 | + } |
| 135 | + |
| 136 | + RecordLength = record[0]; |
| 137 | + ExtendedAttributeLength = record[1]; |
| 138 | + ExtentOffset = IsoUtilities.ReadUInt32MultiEndian(record[2..10]); |
| 139 | + DataLength = IsoUtilities.ReadUInt32MultiEndian(record[10..18]); |
| 140 | + RecordingDate = IsoUtilities.FromIsoRecordingDateTime(record[18..25]); |
| 141 | + FileFlags = (IsoFileFlags)record[25]; |
| 142 | + FileUnitSize = record[26]; |
| 143 | + InterleaveGapSize = record[27]; |
| 144 | + VolumeSequenceNumber = IsoUtilities.ReadUInt16MultiEndian(record[28..32]); |
| 145 | + FileIdentifierLength = record[32]; |
| 146 | + |
| 147 | + char[] textBuffer = new char[FileIdentifierLength]; |
| 148 | + Encoding.ASCII.GetChars(record[33..(33 + FileIdentifierLength)], textBuffer); |
| 149 | + FileIdentifier = textBuffer.AsSpan().Trim('\0').ToString(); |
| 150 | + |
| 151 | + int systemUseStart = 33 + FileIdentifierLength; |
| 152 | + if (systemUseStart % 2 == 1) systemUseStart++; // account for padding field |
| 153 | + |
| 154 | + if (systemUseStart == RecordLength) |
| 155 | + { |
| 156 | + SystemUseContent = Array.Empty<byte>().ToImmutableArray(); |
| 157 | + } |
| 158 | + else |
| 159 | + { |
| 160 | + record[systemUseStart..RecordLength].ToArray().ToImmutableArray(); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments