Missing unlink / Bug on 0 byte files?

2 posts / 0 new
Last post
csp
csp's picture
Offline
Joined: 6 Apr 2010
Posts:
Missing unlink / Bug on 0 byte files?

Hi,

I'm using Build r1000.
It seems to me, that the MAfs Library is missing unlink() to delete a file.
Additionally, fflush() is referenced in the .h file but not implemented, as it seems (at least a stub that returns 0 would be desirable).

So I implemented by own version of unlink, which basically just truncates the file (any better ideas?):
// MoSync doesn't have unlink, so we truncate the file by opening it.

int unlink(const char *pszFile)
{
	FILE *fp = fopen(pszFile, "w");
	if (fp)
	{
		fclose(fp);
		return 0;
	}
	return 1;
}

Now it seems to me that the FS library has a bug on handling 0 byte files and gets stuck on fwrite.
Here are my assumptions about the things happening:

File.c:

static MA_FILE* openReadWrite(const char *filename, int modeFlags) {
...
        if(modeFlags&(MODE_READ|MODE_APPEND)) {
                data = maCreatePlaceholder();
                maReadStore(store, data);
                realLength = size = maGetDataSize(data);    // size = 0 on 0 byte files?
        }
...
        file->bufferStart = 0;
        file->bufferSize = size;
}

Now on fwrite:

size_t MA_fwrite ( const void * ptr, size_t size, size_t count, MA_FILE * stream) {
...
                int lastBufferSize = stream->bufferSize;
                while(stream->filePtr+bytesToWrite >  vol->dataOffset+stream->bufferSize) stream->bufferSize<<=1;
...
}

0<<1 = 0<<1 = 0<<1 = 0<<1 = 0...

Seems buggy to me. Is this a real bug or are my assumptions wrong? Nevertheless, it hangs here in fwrite and loops forever.

Regards,
Ludwig

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Niklas Nummelin
niklas's picture
Offline
Mobile Wizard
Joined: 18 Dec 2007
Posts:

The latter thing is for sure a bug, will fix it as soon as possible. I added an issue for it: https://github.com/MoSync/MoSync

'unlink' could probably be implemented, but as it isn't a real file system there's no such things as 'symbolic links' (yet) and unlink would have no effect really except for removing a file. Please note that the MAFS library is mostly made for easily porting applications that use standard file operations, so that they work even if the platform hasn't got native file system access. We have a real native file system API in the works that may be better suited for your needs.

// Niklas