Shop OBEX P1 Docs P2 Docs Learn Events
FSRW: opendir with filesize, create time? — Parallax Forums

FSRW: opendir with filesize, create time?

SRLMSRLM Posts: 5,045
edited 2013-12-16 00:33 in Propeller 1
Is there any way to iterate through all the files on the SD card and get the filename, the creation time, and the file size? Does anybody have a path to add this information to nextfile()?

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2013-12-02 14:10
    Ok, this code I found in one of my projects:
            sdfat.opendir
            repeat while sdfat.nextfile(@dirbuf)<>-1
              i:=dirbuf[8]
              dirbuf[8]:=0
              term.str( @dirbuf )
              dirbuf[8]:=i
              term.str( string(".") )
              i:=dirbuf[11]
              dirbuf[11]:=0
              term.str( @dirbuf+8 )
              dirbuf[11]:=i
              term.tx( 13 )
    
    sdfat being the FSRW, term is a fullduplex serial, dirbuf is a byte array size of sdfat#DIRSIZE.
    If you have a closer look at sdfat#DIRSIZE, then you'll see that one directory-entry has a size of 32. The first 8 bytes are the filename, the next 3 bytes are the extension.
    The rest contains the file mode, start cluster, date, size and so on. The size is stored in the last 4 bytes in this buffer.

    term.dec( long[ @dirbuf ][ $1c ] )

    should do the job. The date is not usefull if you access files created by FSRW, as FSRW does write a fixed value. Kyes SD driver supports time information if an RTC is available - as far as I remember.
  • SRLMSRLM Posts: 5,045
    edited 2013-12-02 14:33
    Perfect, thanks! This is exactly what I was looking for. I won't be able to test this until next week ( I've just been told to drop this for the next week), but I'll let you know how it goes. I plan on adding this to the C++ version of FSRW that I have ported.
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-12-02 14:53
    Did your C++ version of FSRW come from the Spin code, or did you use the original C code to generate it? I'm just curious if you used spin2cpp to generate it. The original C code would be more readable, and possibly faster and smaller also.
  • SRLMSRLM Posts: 5,045
    edited 2013-12-02 16:21
    I converted the C++ from the Spin version using spin2cpp. I did this because I don't fully understand the FSRW source, and doing a "manual" port from the C code would have been error prone.

    That being said, I have gone through the source and done a bunch of cleanups and optimizations. It's pretty light weight now. You can see the source here: https://github.com/libpropeller/libpropeller/tree/master/libpropeller/sd

    The main addition that I made was the error handling. Unfortunately, C++ doesn't have a good equivalent to abort, and I couldn't get exceptions small enough. So I came up with an error code system. Not the best, and kind of clunky, but it works well.

    On a side note: much of libpropeller is based on the equivalent spin objects: FFDS1, PWM32, FSRW, MCP3208, and the Quadrature Encoder were all Spin objects. I'd like to think that I did a good job porting them (both in readability and performance), but I'm open to any suggestions.
  • Dave HeinDave Hein Posts: 6,347
    edited 2013-12-02 17:09
    Your C++ version of FSRW looks good. I like the error code system. I think it's better than the abort method that the Spin version uses.
  • SRLMSRLM Posts: 5,045
    edited 2013-12-02 17:43
    Thanks! It probably took me 20-30 hours to get that object sorted out, along with the error handling. For the error handling I had to go through the fsrw.spin source and mark each place where an abort was used, then figure out the call stack at that point. After that I had to go through each call to that function and add in the post call error check. It was pretty tedious, but in the end it works.

    Exceptions would be the best, but no matter what I did I couldn't get the code size down enough to be effective. I got it to fit, but just barely. My SD unit tests were somewhere around 30KB. Not to mention the speed cost, whatever that would turn out to be.
  • jazzedjazzed Posts: 11,803
    edited 2013-12-02 19:37
    Looks great Cody.

    How big is simple open, read, write, SD test code now in CMM mode?
  • SRLMSRLM Posts: 5,045
    edited 2013-12-02 20:38
    jazzed wrote: »
    How big is simple open, read, write, SD test code now in CMM mode?

    I don't have my system set up to test the simplelibraries tools, and I'm a bit pressed for time right now so I can't work on getting that set up. I did take the simpletools test from here and duplicate it to work with libpropeller. See the attached zip.

    Note: I changed "print" to "printf" to make the comparison fair.

    I compiled with the following:
    /propeller-elf-g++ -mcmm -I../  -Wall   -Os -ffunction-sections -fdata-sections -pipe -fno-exceptions -fno-rtti -m32bit-doubles -mno-fcache -fpermissive -fno-strict-aliasing -std=gnu++0x -Wl,--gc-sections  -o main.elf main.cpp
    

    As is (with printf) the downloaded size is 18096 bytes.

    Hmmm. Now that I read your post over again I see that it's not so much asking for a comparison with simpletools but a question about size of simple functionality. Still, I'm interested in size comparisons.

    If I comment out all the printfs the size drops to 8256 bytes. So, that's 8256 bytes to open a file, write to it, then read it back.
  • jazzedjazzed Posts: 11,803
    edited 2013-12-02 21:06
    SRLM wrote: »
    ...
    Hmmm. Now that I read your post over again I see that it's not so much asking for a comparison with simpletools but a question about size of simple functionality. Still, I'm interested in size comparisons.

    If I comment out all the printfs the size drops to 8256 bytes. So, that's 8256 bytes to open a file, write to it, then read it back.
    Ya, I'm just looking for smaller SD code, not comparisons between library sets. Andy wrote most of simpletools, so if he's happy, I'm happy.

    We've struggled to find an SD driver that was less than about 8KB. Amazingly, the DOSFS stuff in PropellerGCC is relatively competitive in code size, and it has a very good feature-set. Thanks to Dave Hein for adding DOSFS.
  • SRLMSRLM Posts: 5,045
    edited 2013-12-16 00:33
    @MagIO2

    Thanks! That was exactly what I needed. I've added this to my SD driver
    bool NextFile(char * filename,
                int & filesize,
                int & year, int & month, int & day, int & hour, int & minute, int & second) {
    
            ...
                    filesize = ReverseBytesInLong(at + 28);
                    ExtractDateTime(ReverseBytesInLong(at + 22), year, month, day, hour, minute, second);
                    
                    return true;
            ...
        }
    

    Now we can get the filesize (in bytes) and the last modification time. The last modification time is meaningful if the SetDate function is called before the file is created so that FSRW has the current time.

    https://github.com/libpropeller/libpropeller/blob/master/libpropeller/sd/sd.h#L534
Sign In or Register to comment.