SDFileSystem 4.0.9 Dir command

General discussion relating to the library modules supplied with the compiler

Moderators: Jerry Messina, David Barker

Post Reply
MichaelL
Posts: 16
Joined: Fri Jan 16, 2009 3:52 pm
Location: Germany

SDFileSystem 4.0.9 Dir command

Post by MichaelL » Wed Mar 02, 2016 10:51 pm

I noticed that the DIR command when using it to search for all files in a directory becomes extremly slow. With a little over 1000 files it could take a couple of minutes to complete. When trying to figure out whats wrong I realized that the 'FindRootDirEntry' function always starts by reading in the FAT start sector and then works it way down to the desired next file (obviously using the 'dirnext' parameter). Reading a sector is slow. The file struct contains two variables (File.FileEntryDirectorySector and File.FileEntryDirectorySectorPos) which keep the last searched sector and position. Using that I replaced the original command in 'FindRootDirEntry':
SectorInitRead(File.CurrentSector) // Read sector to buffer
File.CurrentSectorPos = $000

with

If Repeated = false Then
SectorInitRead(File.CurrentSector) // Read sector to buffer
File.CurrentSectorPos = $000
Else
File.CurrentSector = File.FileEntryDirectorySector
File.CurrentSectorPos = File.FileEntryDirectorySectorPos
Inc(File.CurrentSectorPos,32)
If File.CurrentSectorPos = 512 Then
Inc(File.CurrentSector)
SectorInitRead(File.CurrentSector) // Read sector to buffer
File.CurrentSectorPos = $000
EndIf
RootDirFileNum = Disk.FileSearchNumber - 1
EndIf
Repeated is a Public boolean which I set to true if I am searching through a directory for all files. So essentially, I search the current FAT sector to the end. The existing routine keeps track of the currentsectorpos and loads a new sector only when its required.
There might be a better way, but it works now VERY fast. Searching through a directory with ~ 1200 files takes now less than 2 seconds in contrast to the original couple of minutes.
I also modified the routine that it will return the size of a file without the requirement to open the file first.
I thought that maybe somebody might be interested in that.
Finally, let me state that this is a great module which I use a lot.
Cheers, Michael

Jerry Messina
Swordfish Developer
Posts: 1119
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: SDFileSystem 4.0.9 Dir command

Post by Jerry Messina » Thu Mar 03, 2016 10:18 am

Thanks for that.
I also modified the routine that it will return the size of a file without the requirement to open the file first
Could you post those changes too?

User avatar
David Barker
Swordfish Developer
Posts: 1151
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: SDFileSystem 4.0.9 Dir command

Post by David Barker » Thu Mar 03, 2016 10:25 am

Thanks for posting these changes. They look like they could be really useful. If you email me the new, modified library, I could post on the wiki for others to share..

User avatar
David Barker
Swordfish Developer
Posts: 1151
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: SDFileSystem 4.0.9 Dir command

Post by David Barker » Thu Mar 03, 2016 1:24 pm

I've posted the file here:

http://www.sfcompiler.co.uk/wiki/pmwiki ... ardLibrary

Thanks again for sharing...

Post Reply