Keil Logo Arm Logo

RL-FlashFS - File overwrite?

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
Marc Crandall
Posted
2-Dec-2011 14:34 GMT
Toolset
ARM
New! RL-FlashFS - File overwrite?

Hello,

I'm wondering if it's possible, and if so if someone has an example, to overwrite a portion of a file using the RL-FlashFS library.

Basically I have a working setup and I have a small file system on NOR flash. I have a fixed size file and I want to overwrite a middle section of this file.

If I try something like:

            if(f = fopen("my_test_file", "a"))
            {
                int j = 38;
                rewind(f);
                fseek(f, header.head_index-1 * (4 * sizeof(uint32_t)), SEEK_SET);
                fwrite(&j, sizeof(uint32_t), 1, f);
                fwrite(&j, sizeof(uint32_t), 1, f);
                fwrite(&j, sizeof(uint32_t), 1, f);
                fwrite(&j, sizeof(uint32_t), 1, f);
                fseek (f, -1L, SEEK_END);
                fclose(f);
            }

The rewind doesn't seem to do anything and it just appends the 4 ints to the end of the file.

Any ideas? Am I doing something wrong?

Thanks.

M

Read-Only
Author
Tamir Michael
Posted
2-Dec-2011 14:53 GMT
Toolset
ARM
New! RE: RL-FlashFS - File overwrite?

It is very difficult to say - nothing of your program/expectations is really explained, but are you sure about this code

...head_index-1 * (4 *,...

Maybe you are missing brackets here?

Read-Only
Author
Marc Crandall
Posted
2-Dec-2011 15:39 GMT
Toolset
ARM
New! RE: RL-FlashFS - File overwrite?

Hi Tamir thanks for your reply.

Before I posted here I removed all the particulars of my program and tried on very simple test code. I am fairly confident in the multiplication - nonetheless I just re-tried it with a constant and still the same outcome. I went further and I tried this extremely simple test function:

    FILE * f;
    uint32_t i, j;

    if(f = fopen("tf.txt", "w"))
    {

        for(i = 0; i < 100; i++)
        {
            fputc(65,f);
        }
        fflush(f);
        fclose(f);
    }
    else
    {
        return;
    }

    if(f = fopen("tf.txt", "a"))
    {
        j = 38;
        rewind(f);
        fwrite(&j, sizeof(uint32_t), 1, f);
        fwrite(&j, sizeof(uint32_t), 1, f);
        fwrite(&j, sizeof(uint32_t), 1, f);
        fwrite(&j, sizeof(uint32_t), 1, f);
        fseek(f, -1L, SEEK_END);
        fclose(f);
    }
    else
    {
        return; //! \todo some form of error handling?
    }

My expectation for this test function would be to create a file with 100 'A' then overwrite the first 32 bytes with NULL and '&' characters.

Are my expectations incorrect?

The result is a file with 100 'A' characters followed by 4 '&' characters.

What am after is a simple example of overwriting a section of a file (using the RL-FlashFS lib). I have tried many combinations all without success in overwriting a section of a file. If I open the file with "a" I can only write to the end of the file.

Thanks

M

Read-Only
Author
Tamir Michael
Posted
2-Dec-2011 16:02 GMT
Toolset
ARM
New! RE: RL-FlashFS - File overwrite?

What you do seems correct. It could be interesting to test this with a SD card/NAND flash (assuming you use MDK 4.20+). Either way this is worth reporting to our friends at the support.
Did you try to replace the rewind with a fseek? What happens if you execute a fseek, followed by a ftell? does the file pointer even move?

Read-Only
Author
Marc Crandall
Posted
2-Dec-2011 16:23 GMT
Toolset
ARM
New! RE: RL-FlashFS - File overwrite?

I don't have an SD card or NAND on my hardware but I just tried it on USB and the same thing.

Interestingly enough the following:


...
    if(f = fopen("f:\tf.txt", "a"))
    {
        j = 38;
        printf("\r\n ftell = %d",ftell(f));
        rewind(f);
        printf("\r\n ftell = %d",ftell(f));
        fwrite(&j, sizeof(uint32_t), 1, f);
        fwrite(&j, sizeof(uint32_t), 1, f);
        fwrite(&j, sizeof(uint32_t), 1, f);
        fwrite(&j, sizeof(uint32_t), 1, f);
        fseek(f, -1L, SEEK_END);
        printf("\r\n ftell = %d",ftell(f));
        fclose(f);
    }
...

Outputs:
ftell = 100
ftell = 0
ftell = 115

I guess I will contact support.

Thanks.

M

Read-Only
Author
Marc Crandall
Posted
2-Dec-2011 21:40 GMT
Toolset
ARM
New! RE: RL-FlashFS - File overwrite?

The reply from support:

**********************
Thank you for using Keil software.

The file pointer only gets used for reading.

The C library does not allow you to open a file in a mode in which you can overwrite a portion of the file, you can either replace the contents of file, or append to it.

If you want to delete part of a file, you need to:

1) make a temporary copy of the file
2) open the original file with mode "w", and the copy in mode "r"
3) copy the portions you want to keep, from the temporary file to the original file
4) close both files
5) delete the temporary file

**********************

I followed up to see if there are any plans to implement other modes (like "r+")...

Read-Only
Author
Brendan Cassidy
Posted
2-May-2012 20:03 GMT
Toolset
ARM
New! RE: RL-FlashFS - File overwrite?

MDK-ARM Version 4.21
Release Date: 20 Jun, 2011
...
Added: File update mode (r+, w+, a+) support for FAT

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Keil logo

Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.