Skip to content

Commit bcc75df

Browse files
committed
Completed new file import dialog
Fixes and improvements to long filename support
1 parent 40f297b commit bcc75df

18 files changed

+414
-365
lines changed

DiskImageTool/CopyProtection.vb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Module Copy_Protection
3030

3131
'Softguard Superlok v2
3232
If Not ProtectionFound AndAlso BadSectors.Count >= 10 Then
33-
If CheckBadSectors(BadSectors, {108, 109, 110, 111, 112, 113, 114, 115, 116, 117}) AndAlso Disk.RootDirectory.GetFileIndex("CPC.COM", False) > -1 Then
33+
If CheckBadSectors(BadSectors, {108, 109, 110, 111, 112, 113, 114, 115, 116, 117}) AndAlso Disk.RootDirectory.FindShortFileName("CPC.COM", False) > -1 Then
3434
ProtectionFound = True
3535
ProtectionName = "Softguard Superlok v2"
3636
End If
@@ -85,14 +85,14 @@ Module Copy_Protection
8585
End If
8686

8787
'Softguard Superlok v2/v3
88-
If Not ProtectionFound AndAlso Disk.RootDirectory.GetFileIndex("CML0300.FCL", False) > -1 Then
88+
If Not ProtectionFound AndAlso Disk.RootDirectory.FindShortFileName("CML0300.FCL", False) > -1 Then
8989
ProtectionFound = True
9090
ProtectionName = "Softguard Superlok v2/v3"
9191
End If
9292

9393
'Xidex Magnetics (XEMAG) XELOK v2
9494
If Not ProtectionFound Then
95-
Dim Fileindex = Disk.RootDirectory.GetFileIndex("XEMAG.SYS", False)
95+
Dim Fileindex = Disk.RootDirectory.FindShortFileName("XEMAG.SYS", False)
9696
If Fileindex > -1 Then
9797
Dim DirectoryEntry = Disk.RootDirectory.GetFile(Fileindex)
9898
If Disk.BPB.ClusterToSector(DirectoryEntry.StartingCluster) = 162 Then
@@ -120,7 +120,7 @@ Module Copy_Protection
120120

121121
Private Function CheckFileList(Disk As DiskImage.Disk, FileList() As String) As Boolean
122122
For Each File In FileList
123-
If Disk.RootDirectory.GetFileIndex(File, False) > -1 Then
123+
If Disk.RootDirectory.FindShortFileName(File, False) > -1 Then
124124
Return True
125125
End If
126126
Next
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Namespace DiskImage
22
Public Class AddFileOptions
3-
Public Property CreatedDate As Boolean
4-
Public Property LastAccessedDate As Boolean
5-
Public Property LFN As Boolean
6-
Public Property NTExtensions As Boolean
3+
Public Property UseLFN As Boolean
4+
Public Property UseNTExtensions As Boolean
5+
Public Property UseCreatedDate As Boolean
6+
Public Property UseLastAccessedDate As Boolean
77
End Class
88
End Namespace

DiskImageTool/DiskImage/DirectoryBase.vb

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
End If
115115
End Sub
116116

117-
Public MustOverride Function AddDirectory(DirectoryData() As Byte, Options As AddFileOptions, LFNFileName As String, Optional Index As Integer = -1) As AddDirectoryData Implements IDirectory.AddDirectory
117+
Public MustOverride Function AddDirectory(EntryData() As Byte, Options As AddFileOptions, Filename As String, Optional Index As Integer = -1) As AddFileData Implements IDirectory.AddDirectory
118118

119119
Public MustOverride Function AddFile(FilePath As String, Options As AddFileOptions, Optional Index As Integer = -1) As Integer Implements IDirectory.AddFile
120120

@@ -137,6 +137,57 @@
137137
Return Index
138138
End Function
139139

140+
Public Function FindFileName(Filename As String, IncludeDirectories As Boolean, Optional SkipIndex As Integer = -1) As Integer Implements IDirectory.FindFileName
141+
If _DirectoryData.EntryCount > 0 Then
142+
For Counter As UInteger = 0 To _DirectoryData.EntryCount - 1
143+
If Counter <> SkipIndex Then
144+
Dim File = _DirectoryEntries.Item(Counter)
145+
If Not File.IsDeleted And Not File.IsVolumeName And (IncludeDirectories Or Not File.IsDirectory) Then
146+
If File.GetFullFileName = Filename Then
147+
Return Counter
148+
End If
149+
End If
150+
End If
151+
Next
152+
End If
153+
154+
Return -1
155+
End Function
156+
157+
Public Function FindShortFileName(FileBytes() As Byte, IncludeDirectories As Boolean, Optional SkipIndex As Integer = -1) As Integer Implements IDirectory.FindShortFileName
158+
If _DirectoryData.EntryCount > 0 Then
159+
For Counter As UInteger = 0 To _DirectoryData.EntryCount - 1
160+
If Counter <> SkipIndex Then
161+
Dim File = _DirectoryEntries.Item(Counter)
162+
If Not File.IsDeleted And Not File.IsVolumeName And (IncludeDirectories Or Not File.IsDirectory) Then
163+
If FileBytes.CompareTo(File.FileNameWithExtension) Then
164+
Return Counter
165+
End If
166+
End If
167+
End If
168+
Next
169+
End If
170+
171+
Return -1
172+
End Function
173+
174+
Public Function FindShortFileName(Filename As String, IncludeDirectories As Boolean, Optional SkipIndex As Integer = -1) As Integer Implements IDirectory.FindShortFileName
175+
If _DirectoryData.EntryCount > 0 Then
176+
For Counter As UInteger = 0 To _DirectoryData.EntryCount - 1
177+
If Counter <> SkipIndex Then
178+
Dim File = _DirectoryEntries.Item(Counter)
179+
If Not File.IsDeleted And Not File.IsVolumeName And (IncludeDirectories Or Not File.IsDirectory) Then
180+
If File.GetShortFileName = Filename Then
181+
Return Counter
182+
End If
183+
End If
184+
End If
185+
Next
186+
End If
187+
188+
Return -1
189+
End Function
190+
140191
Public Function GetAvailableEntry() As DirectoryEntry Implements IDirectory.GetAvailableEntry
141192
Dim Buffer(10) As Byte
142193

@@ -157,7 +208,21 @@
157208
Return Nothing
158209
End Function
159210

160-
Public Function GetAvailableFileName(FileName As String, UseNTExtensions As Boolean, Optional CurrentIndex As Integer = -1) As String Implements IDirectory.GetAvailableFileName
211+
Public Function GetAvailableFileName(FileName As String, Optional CurrentIndex As Integer = -1) As String Implements IDirectory.GetAvailableFileName
212+
Dim FileParts = SplitFilename(FileName)
213+
214+
Dim NewFileName As String = FileName
215+
Dim Index As UInteger = 1
216+
217+
Do While FindFileName(NewFileName, True, CurrentIndex) > -1
218+
NewFileName = CombineFileParts(FileParts.Name & " (" & Index & ")", FileParts.Extension)
219+
Index += 1
220+
Loop
221+
222+
Return NewFileName
223+
End Function
224+
225+
Public Function GetAvailableShortFileName(FileName As String, UseNTExtensions As Boolean, Optional CurrentIndex As Integer = -1) As String Implements IDirectory.GetAvailableShortFileName
161226
Dim FileParts = SplitFilename(FileName)
162227

163228
Dim CleanFileName = DOSCleanFileName(FileParts.Name)
@@ -179,7 +244,7 @@
179244
NewFileName = CombineFileParts(CleanFileName, CleanExtension)
180245
End If
181246

182-
Do While GetFileIndex(NewFileName, True, CurrentIndex) > -1
247+
Do While FindShortFileName(NewFileName, True, CurrentIndex) > -1
183248
NewFileName = TruncateFileName(CleanFileName, CleanExtension, Checksum, Index, UseNTExtensions)
184249
Index += 1
185250
Loop
@@ -202,41 +267,6 @@
202267
Public Function GetFile(Index As UInteger) As DirectoryEntry Implements IDirectory.GetFile
203268
Return _DirectoryEntries.Item(Index)
204269
End Function
205-
206-
Public Function GetFileIndex(FileBytes() As Byte, IncludeDirectories As Boolean, Optional SkipIndex As Integer = -1) As Integer Implements IDirectory.GetFileIndex
207-
If _DirectoryData.EntryCount > 0 Then
208-
For Counter As UInteger = 0 To _DirectoryData.EntryCount - 1
209-
If Counter <> SkipIndex Then
210-
Dim File = _DirectoryEntries.Item(Counter)
211-
If Not File.IsDeleted And Not File.IsVolumeName And (IncludeDirectories Or Not File.IsDirectory) Then
212-
If FileBytes.CompareTo(File.FileNameWithExtension) Then
213-
Return Counter
214-
End If
215-
End If
216-
End If
217-
Next
218-
End If
219-
220-
Return -1
221-
End Function
222-
223-
Public Function GetFileIndex(Filename As String, IncludeDirectories As Boolean, Optional SkipIndex As Integer = -1) As Integer Implements IDirectory.GetFileIndex
224-
If _DirectoryData.EntryCount > 0 Then
225-
For Counter As UInteger = 0 To _DirectoryData.EntryCount - 1
226-
If Counter <> SkipIndex Then
227-
Dim File = _DirectoryEntries.Item(Counter)
228-
If Not File.IsDeleted And Not File.IsVolumeName And (IncludeDirectories Or Not File.IsDirectory) Then
229-
If File.GetShortFileName = Filename Then
230-
Return Counter
231-
End If
232-
End If
233-
End If
234-
Next
235-
End If
236-
237-
Return -1
238-
End Function
239-
240270
Public Function GetIndex(DirectoryEntry As DirectoryEntry) As Integer Implements IDirectory.GetIndex
241271
Return _DirectoryEntries.IndexOf(DirectoryEntry)
242272
End Function

DiskImageTool/DiskImage/DirectoryEntry.vb

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -79,66 +79,6 @@
7979
End Get
8080
End Property
8181

82-
Public Function AddFile(FilePath As String, ShortFileName As String, UseCreationDate As Boolean, UseLastAccessDate As Boolean, Optional StartingCluster As UShort = 2) As Boolean
83-
Return AddFile(FilePath, ShortFileName, UseCreationDate, UseLastAccessDate, Disk.FAT.FreeClusters, StartingCluster)
84-
End Function
85-
86-
Public Function AddFile(FilePath As String, ShortFileName As String, UseCreationDate As Boolean, UseLastAccessDate As Boolean, ClusterList As SortedSet(Of UShort), Optional StartingCluster As UShort = 2) As Boolean
87-
Dim FileInfo = New IO.FileInfo(FilePath)
88-
Dim ClusterSize = Disk.BPB.BytesPerCluster
89-
90-
If FileInfo.Length > ClusterList.Count * ClusterSize Then
91-
Return False
92-
End If
93-
94-
Dim UseTransaction As Boolean = _Disk.BeginTransaction
95-
96-
Dim FirstCluster As UShort = 0
97-
98-
If FileInfo.Length > 0 Then
99-
'Load file into buffer, padding with empty space if needed
100-
Dim FileSize = Math.Ceiling(FileInfo.Length / ClusterSize) * ClusterSize
101-
Dim FileBuffer = ReadFileIntoBuffer(FileInfo, FileSize, 0)
102-
103-
Dim LastCluster As UShort = 0
104-
105-
For Counter As Integer = 0 To FileBuffer.Length - 1 Step ClusterSize
106-
Dim Cluster = Disk.FAT.GetNextFreeCluster(ClusterList, True, StartingCluster)
107-
If Cluster > 0 Then
108-
If Counter = 0 Then
109-
FirstCluster = Cluster
110-
Else
111-
Disk.FATTables.UpdateTableEntry(LastCluster, Cluster)
112-
End If
113-
Dim ClusterOffset = Disk.BPB.ClusterToOffset(Cluster)
114-
Dim Buffer = Disk.Image.GetBytes(ClusterOffset, ClusterSize)
115-
Array.Copy(FileBuffer, Counter, Buffer, 0, ClusterSize)
116-
Disk.Image.SetBytes(Buffer, ClusterOffset)
117-
LastCluster = Cluster
118-
StartingCluster = Cluster + 1
119-
End If
120-
Next
121-
122-
If LastCluster > 0 Then
123-
Disk.FATTables.UpdateTableEntry(LastCluster, FAT12.FAT_LAST_CLUSTER_END)
124-
End If
125-
End If
126-
127-
Dim NewEntry = New DirectoryEntryBase
128-
NewEntry.SetFileInfo(FileInfo, ShortFileName, UseCreationDate, UseLastAccessDate)
129-
NewEntry.StartingCluster = FirstCluster
130-
131-
Data = NewEntry.Data
132-
133-
InitFatChain()
134-
135-
If UseTransaction Then
136-
_Disk.EndTransaction()
137-
End If
138-
139-
Return True
140-
End Function
141-
14282
Public Function CanUnDelete() As Boolean
14383
If FileSize = 0 Or HasInvalidFileSize() Or HasInvalidStartingCluster() Then
14484
Return False

DiskImageTool/DiskImage/DirectoryEntryBase.vb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,7 @@ Namespace DiskImage
675675
Attributes = Attrib
676676
End Sub
677677

678-
Public Sub SetFileInfo(FolderInfo As DirectoryInfo, NewFileName As String, UseCreationDate As Boolean, UseLastAccessDate As Boolean)
679-
SetFileName(NewFileName)
680-
678+
Public Sub SetFileInfo(FolderInfo As DirectoryInfo, UseCreationDate As Boolean, UseLastAccessDate As Boolean)
681679
FileSize = 0
682680

683681
SetLastWriteDate(FolderInfo.LastWriteTime)

DiskImageTool/DiskImage/Interfaces/IDirectory.vb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
ReadOnly Property IsRootDirectory As Boolean
88
ReadOnly Property ParentEntry As DirectoryEntry
99
ReadOnly Property SectorChain As List(Of UInteger)
10-
Function AddDirectory(DirectoryData() As Byte, Options As AddFileOptions, LFNFileName As String, Optional Index As Integer = -1) As AddDirectoryData
10+
Function AddDirectory(EntryData() As Byte, Options As AddFileOptions, Filename As String, Optional Index As Integer = -1) As AddFileData
1111
Function AddFile(FilePath As String, Options As AddFileOptions, Optional Index As Integer = -1) As Integer
1212
Function AddFile(FileInfo As IO.FileInfo, Options As AddFileOptions, Optional Index As Integer = -1) As Integer
13-
Function GetAvailableFileName(FileName As String, UseNTExtensions As Boolean, Optional CurrentIndex As Integer = -1) As String
13+
Function FindFileName(Filename As String, IncludeDirectories As Boolean, Optional SkipIndex As Integer = -1) As Integer
14+
Function FindShortFileName(FileBytes() As Byte, IncludeDirectories As Boolean, Optional SkipIndex As Integer = -1) As Integer
15+
Function FindShortFileName(Filename As String, IncludeDirectories As Boolean, Optional SkipIndex As Integer = -1) As Integer
16+
Function GetAvailableEntry() As DirectoryEntry
17+
Function GetAvailableFileName(FileName As String, Optional CurrentIndex As Integer = -1) As String
18+
Function GetAvailableShortFileName(FileName As String, UseNTExtensions As Boolean, Optional CurrentIndex As Integer = -1) As String
1419
Function GetContent() As Byte()
1520
Function GetFile(Index As UInteger) As DirectoryEntry
1621
Function GetIndex(DirectoryEntry As DirectoryEntry) As Integer
17-
Function GetAvailableEntry() As DirectoryEntry
18-
Function GetFileIndex(FileBytes() As Byte, IncludeDirectories As Boolean, Optional SkipIndex As Integer = -1) As Integer
19-
Function GetFileIndex(Filename As String, IncludeDirectories As Boolean, Optional SkipIndex As Integer = -1) As Integer
2022
Function RemoveEntry(Index As UInteger) As Boolean
2123
Function RemoveLFN(Index As UInteger) As Boolean
2224
Sub UpdateEntryCounts()

0 commit comments

Comments
 (0)