Skip to content

Commit a9a1327

Browse files
committed
ISO-9660: Add directory / file enumeration
1 parent 22d083c commit a9a1327

9 files changed

Lines changed: 250 additions & 53 deletions

File tree

‎Docs/status.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ This is the list of all currently supported media types, image containers, parti
1010
| Name | Read | Write | Create |
1111
| --- | --- | --- | --- |
1212
| Floppy disks | ✔ Yes | ❌ No | ✔ Yes |
13+
| CD-ROM images | ✔ Yes | ❌ No | ❌ No |
1314
| Hard disks | ✔ Yes | ❌ No | ❌ No |
1415

1516
Read and Create functionality is limited to supported features below.
1617

1718
## Image containers
1819
| Name | Common file extensions | Read | Write | Create |
1920
| --- | --- | --- | --- | --- |
20-
| Raw sector image | IMG, IMA, FLP, VFD, DSK | ✔ Yes | ❌ No | ✔ Yes |
21+
| Raw sector image | IMG, ISO, IMA, FLP, VFD, DSK | ✔ Yes | ❌ No | ✔ Yes |
2122
| Microsoft VHD | VHD | ⚠ Partial | ❌ No | ❌ No |
2223

2324
Read and Create functionality is limited to supported features below.
@@ -57,5 +58,6 @@ Only FAT12, FAT16 and FAT32 primary partitions are currently supported (see belo
5758
| FAT12 | ✔ Yes | ❌ No | ✔ Yes |
5859
| FAT16 | ✔ Yes | ❌ No | ❌ No |
5960
| FAT32 | ✔ Yes | ❌ No | ❌ No |
61+
| ISO-9660 | ⚠ Partial | ❌ No | ❌ No |
6062

6163
Long file names (LFN) are supported on all supported FAT variants.

‎TotalImage.IO/FileSystems/ISO/Iso9660FileSystem.cs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public class Iso9660FileSystem : FileSystem
2323
/// <summary>
2424
/// The first primary volume descriptor
2525
/// </summary>
26-
public IsoPrimaryVolumeDescriptor? PrimaryVolumeDescriptor
26+
public IsoPrimaryVolumeDescriptor PrimaryVolumeDescriptor
2727
=> VolumeDescriptors
2828
.OfType<IsoPrimaryVolumeDescriptor>()
29-
.FirstOrDefault();
29+
.First();
3030

3131
/// <summary>
3232
/// Create an ISO 9660 file system
@@ -43,7 +43,7 @@ public Iso9660FileSystem(Stream containerStream) : base(containerStream)
4343
{
4444
containerStream.Read(recordBytes);
4545

46-
var record = IsoVolumeDescriptor.ReadVolumeDescriptor(recordBytes);
46+
var record = IsoVolumeDescriptor.ReadVolumeDescriptor(recordBytes, this);
4747
if (!record.IsValid())
4848
{
4949
throw new InvalidDataException();
@@ -55,14 +55,16 @@ public Iso9660FileSystem(Stream containerStream) : base(containerStream)
5555

5656
VolumeDescriptors = volumeDescriptors.ToImmutable();
5757

58-
IsoPrimaryVolumeDescriptor? primaryDescriptor = PrimaryVolumeDescriptor;
58+
IsoPrimaryVolumeDescriptor? primaryDescriptor = VolumeDescriptors
59+
.OfType<IsoPrimaryVolumeDescriptor>()
60+
.FirstOrDefault();
5961
if (primaryDescriptor == null)
6062
{
6163
throw new InvalidDataException("No primary volume descriptor");
6264
}
6365

6466
VolumeLabel = primaryDescriptor.VolumeSetIdentifier;
65-
RootDirectory = primaryDescriptor.RootDirectory;
67+
RootDirectory = new IsoDirectory(primaryDescriptor.RootDirectory, this);
6668
TotalSize = primaryDescriptor.LogicalBlockSize * primaryDescriptor.VolumeSpace;
6769
}
6870

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace TotalImage.FileSystems.ISO
6+
{
7+
/// <summary>
8+
/// Represents a directory on an ISO 9660 file system
9+
/// </summary>
10+
public class IsoDirectory : Directory
11+
{
12+
/// <summary>
13+
/// The internal file system record for the directory
14+
/// </summary>
15+
protected IsoFileSystemObject Record { get; }
16+
17+
/// <inheritdoc />
18+
public override string Name { get => Record.FileIdentifier; set => throw new NotImplementedException(); }
19+
20+
/// <inheritdoc />
21+
public override FileAttributes Attributes { get => FileAttributes.Directory; set => throw new NotImplementedException(); }
22+
23+
/// <inheritdoc />
24+
public override DateTime? LastAccessTime { get => null; set => throw new NotImplementedException(); }
25+
26+
/// <inheritdoc />
27+
public override DateTime? LastWriteTime { get => Record.RecordingDate?.LocalDateTime.ToLocalTime(); set => throw new NotImplementedException(); }
28+
29+
/// <inheritdoc />
30+
public override DateTime? CreationTime { get => null; set => throw new NotImplementedException(); }
31+
32+
/// <inheritdoc />
33+
public override ulong Length { get => Record.DataLength; set => throw new NotImplementedException(); }
34+
35+
/// <inheritdoc />
36+
public override Directory CreateSubdirectory(string path)
37+
{
38+
throw new NotImplementedException();
39+
}
40+
41+
/// <inheritdoc />
42+
public override void Delete()
43+
{
44+
throw new NotImplementedException();
45+
}
46+
47+
/// <inheritdoc />
48+
public override IEnumerable<FileSystemObject> EnumerateFileSystemObjects(bool showHidden, bool showDeleted)
49+
{
50+
var fileSystem = (Iso9660FileSystem)FileSystem;
51+
var stream = FileSystem.GetStream();
52+
53+
stream.Seek(fileSystem.PrimaryVolumeDescriptor.LogicalBlockSize * Record.ExtentOffset, SeekOrigin.Begin);
54+
55+
byte[] records = new byte[Record.DataLength];
56+
stream.Read(records);
57+
58+
int nextRecord = 0;
59+
while (nextRecord < Record.DataLength)
60+
{
61+
byte recordLength = records[nextRecord];
62+
var record = new IsoFileSystemObject(records[nextRecord..(nextRecord + recordLength)]);
63+
if (record.FileIdentifier != "" && record.FileIdentifier[0] != (char)1)
64+
{
65+
if (record.FileFlags.HasFlag(IsoFileFlags.Directory))
66+
{
67+
yield return new IsoDirectory(record, fileSystem, this);
68+
}
69+
else
70+
{
71+
yield return new IsoFile(record, fileSystem, this);
72+
}
73+
}
74+
nextRecord += recordLength;
75+
}
76+
}
77+
78+
/// <inheritdoc />
79+
public override void MoveTo(string path)
80+
{
81+
throw new NotImplementedException();
82+
}
83+
84+
/// <summary>
85+
/// Create a directory on an ISO 9660 file system from a file record
86+
/// </summary>
87+
/// <param name="fso">The file system record</param>
88+
/// <param name="fileSystem">The file system containing the directory</param>
89+
/// <param name="parent">The parent directory, if any</param>
90+
public IsoDirectory(IsoFileSystemObject fso, FileSystem fileSystem, Directory? parent = null) : base(fileSystem, parent)
91+
{
92+
Record = fso;
93+
}
94+
}
95+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace TotalImage.FileSystems.ISO
5+
{
6+
/// <summary>
7+
/// Represents a file on an ISO 9660 file system
8+
/// </summary>
9+
public class IsoFile : File
10+
{
11+
/// <summary>
12+
/// The internal file system record for the file
13+
/// </summary>
14+
protected IsoFileSystemObject Record { get; }
15+
16+
/// <inheritdoc />
17+
public override string Name
18+
{
19+
get => Record.FileIdentifier;
20+
set => throw new NotImplementedException();
21+
}
22+
23+
/// <inheritdoc />
24+
public override FileAttributes Attributes
25+
{
26+
get => 0;
27+
set => throw new NotImplementedException();
28+
}
29+
30+
/// <inheritdoc />
31+
public override DateTime? LastAccessTime
32+
{
33+
get => null;
34+
set => throw new NotImplementedException();
35+
}
36+
37+
/// <inheritdoc />
38+
public override DateTime? LastWriteTime
39+
{
40+
get => Record.RecordingDate?.LocalDateTime.ToLocalTime();
41+
set => throw new NotImplementedException();
42+
}
43+
44+
/// <inheritdoc />
45+
public override DateTime? CreationTime
46+
{
47+
get => null;
48+
set => throw new NotImplementedException();
49+
}
50+
51+
/// <inheritdoc />
52+
public override ulong Length
53+
{
54+
get => Record.DataLength;
55+
set => throw new NotImplementedException();
56+
}
57+
58+
/// <inheritdoc />
59+
public override void Delete()
60+
{
61+
throw new NotImplementedException();
62+
}
63+
64+
/// <inheritdoc />
65+
public override void MoveTo(string path)
66+
{
67+
throw new NotImplementedException();
68+
}
69+
70+
/// <inheritdoc />
71+
public override Stream GetStream()
72+
{
73+
throw new NotImplementedException();
74+
}
75+
76+
/// <summary>
77+
/// Create a file on an ISO 9660 file system from a file record
78+
/// </summary>
79+
/// <param name="fso">The file system record</param>
80+
/// <param name="fileSystem">The file system containing the file</param>
81+
/// <param name="parent">The parent directory, if any</param>
82+
public IsoFile(IsoFileSystemObject fso, FileSystem fileSystem, Directory? parent = null) : base(fileSystem, parent)
83+
{
84+
Record = fso;
85+
}
86+
}
87+
}

TotalImage.IO/FileSystems/ISO/IsoDirectoryRecord.cs renamed to TotalImage.IO/FileSystems/ISO/IsoFileSystemObject.cs

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
namespace TotalImage.FileSystems.ISO
88
{
99
/// <summary>
10-
/// Represents a directory record within an ISO 9660 file system
10+
/// Represents a file system object within an ISO 9660 file system
1111
/// </summary>
12-
public class IsoDirectoryRecord : Directory
12+
public class IsoFileSystemObject
1313
{
1414
/// <summary>
1515
/// Used to separate a file name and extension in a file identifier
@@ -21,46 +21,6 @@ public class IsoDirectoryRecord : Directory
2121
/// </summary>
2222
public const char IDENTIFIER_SEPARATOR_2 = ';';
2323

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-
6424
/// <summary>
6525
/// Length of directory record
6626
/// </summary>
@@ -126,7 +86,7 @@ public override void MoveTo(string path)
12686
/// </summary>
12787
/// <param name="record">A span containing the directory record</param>
12888
/// <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)
89+
public IsoFileSystemObject(in ReadOnlySpan<byte> record)
13090
{
13191
if (record[0] > record.Length)
13292
{
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Buffers.Binary;
3+
4+
namespace TotalImage.FileSystems.ISO
5+
{
6+
/// <summary>
7+
/// Represents an entry in the path table of an ISO 9660 file system
8+
/// </summary>
9+
public class IsoPathTableEntry
10+
{
11+
/// <summary>
12+
/// The length of the directory identifier
13+
/// </summary>
14+
public byte DirectoryIdentifierLength { get; }
15+
16+
/// <summary>
17+
/// The length of the extended attribute record
18+
/// </summary>
19+
public byte ExtendedAttributeRecordLength { get; }
20+
21+
/// <summary>
22+
/// The offset of the extent
23+
/// </summary>
24+
public uint ExtentOffset { get; }
25+
26+
/// <summary>
27+
/// The index number of the parent directory in the path table
28+
/// </summary>
29+
public ushort ParentDirectoryIndex { get; }
30+
31+
/// <summary>
32+
/// Create a path table entry from a span of bytes
33+
/// </summary>
34+
/// <param name="entry">The bytes for the entry</param>
35+
/// <param name="useMsb">If true, treat multi-byte values as big-endian</param>
36+
public IsoPathTableEntry(in ReadOnlySpan<byte> entry, bool useMsb = false)
37+
{
38+
DirectoryIdentifierLength = entry[0];
39+
ExtendedAttributeRecordLength = entry[1];
40+
41+
ExtentOffset = useMsb
42+
? BinaryPrimitives.ReadUInt32BigEndian(entry[2..5])
43+
: BinaryPrimitives.ReadUInt32LittleEndian(entry[2..5]);
44+
45+
ParentDirectoryIndex = useMsb
46+
? BinaryPrimitives.ReadUInt16BigEndian(entry[6..8])
47+
: BinaryPrimitives.ReadUInt16LittleEndian(entry[6..8]);
48+
}
49+
}
50+
}

‎TotalImage.IO/FileSystems/ISO/IsoPrimaryVolumeDescriptor.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class IsoPrimaryVolumeDescriptor : IsoVolumeDescriptor
7171
/// <summary>
7272
/// The root directory record of the file system
7373
/// </summary>
74-
public IsoDirectoryRecord RootDirectory { get; }
74+
public IsoFileSystemObject RootDirectory { get; }
7575

7676
/// <summary>
7777
/// The identifier for the volume set
@@ -170,7 +170,7 @@ public IsoPrimaryVolumeDescriptor(in ReadOnlySpan<byte> record, in IsoVolumeDesc
170170
MPathTableOffset = BinaryPrimitives.ReadUInt32BigEndian(record[148..152]);
171171
MPathTableOffset = BinaryPrimitives.ReadUInt32BigEndian(record[152..156]);
172172

173-
RootDirectory = new IsoDirectoryRecord(record[156..190]);
173+
RootDirectory = new IsoFileSystemObject(record[156..190]);
174174

175175
textBuffer = new char[128];
176176

‎TotalImage.IO/FileSystems/ISO/IsoUtilities.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static class IsoUtilities
6969
}
7070

7171
TimeSpan offset = TimeSpan.FromMinutes((sbyte)date[6] * 15);
72-
return new DateTimeOffset(date[0], date[1], date[2], date[3], date[4], date[5], offset);
72+
return new DateTimeOffset(1900 + date[0], date[1], date[2], date[3], date[4], date[5], offset);
7373
}
7474

7575
/// <summary>

0 commit comments

Comments
 (0)