maFileRead reaturns 0 (success) on Android on EOF

3 posts / 0 new
Last post
DI (FH) Ludwig Ertl
ert@csp.at's picture
Offline
Mobile Conjurer
Joined: 22 Feb 2010
Posts:
maFileRead reaturns 0 (success) on Android on EOF

Hi,
Before posting this to the bugtracker I wanted to ensure that you confirm that this is not working as intended.
The docs about maFileRead

state:
Returns 0 on success, or < 0 on error.

Now for me, EOF is an error, but your Android runtime doesn't think so, as it seems:

	int maFileRead(int file, int dst, int len)
	{
		log("maFileRead ("+file+")");
		MoSyncFileHandle fileHandle = mFileHandles.get(file);
		if (null == fileHandle)
		{
			logerr("maFileRead Error: MA_FERR_NOTFOUND ("+file+")");
			return MA_FERR_NOTFOUND;
		}
		// Create a sliced buffer which we can send to file
		mMoSyncThread.mMemDataSection.position(dst);
		ByteBuffer slicedBuffer = mMoSyncThread.mMemDataSection.slice();
		slicedBuffer.limit(len);
		readFileToByteBuffer(fileHandle, slicedBuffer);
		return 0;
	}

So in the following simple test case, readFileToByteBuffer returns -1 but maReadFile returns 0.
I checked against the other runtimes where EOF seems to be handled accordingly:

	bool LimitedFileStream::read(void* dst, int size) {
		int curPos;
		TEST(FileStream::tell(curPos));
		if(curPos + size > mEndPos) {
			FAIL;
		}
		TEST(FileStream::read(dst, size));
		return true;
	}
	int Syscall::maFileRead(MAHandle file, void* dst, int len) {
		LOGF("maFileRead(%i, 0x%"PFP", %i)\n", file, dst, len);
		FileHandle& fh(getFileHandle(file));
		if(!fh.fs)
			FILE_FAIL(MA_FERR_GENERIC);
		bool res = fh.fs->read(dst, len);
		if(!res)
			FILE_FAIL(MA_FERR_GENERIC);
		return 0;
	}

The following simple code can reproduce the behaviour:

#include <ma.h>
#include <conprint.h>

void maFileReadEOFBug()
{
	char szFile[256]={0}, Buffer[32];
	MAHandle file;
	maGetSystemProperty("mosync.path.local", szFile, sizeof(szFile));
	strcat (szFile, "testfile.txt");
	if ((file = maFileOpen(szFile, MA_ACCESS_READ_WRITE))<0)
	{
		lprintfln ("Error opening file %s\n", szFile);
		return;
	}
	if (maFileCreate(file)<0)
	{
		lprintfln ("Error on maFileCreate %s\n", szFile);
		maFileClose(file);
		return;
	}
	if (maFileRead(file, Buffer, sizeof(Buffer))<0)
	{
		lprintfln ("maFileRead failed. This is GOOD!\n");
	}
	else
	{
		lprintfln ("maFileRead succeeded on an empty file. This is BAD!\n");
	}
	maFileDelete(file);
	maFileClose(file);
}

int MAMain() {
	maFileReadEOFBug();
}

On the emulator, it works as expected:

[78] Emulator connected.
[78] maFileRead failed. This is GOOD!

On the Android device, however this outputs:

11-15 11:53:05.327 16225 16232 I MoSyncFile: maFileOpen (/data/data/com.mosync.a
pp_bug/files/testfile.txt, 3): 1
11-15 11:53:05.335 16225 16232 I MoSyncFile: maFileCreate (1)
11-15 11:53:05.335 16225 16232 I MoSyncFile: maFileRead (1)
11-15 11:53:05.335 16225 16232 I MoSyncFile: readFileToByteBuffer bytes read: -1
11-15 11:53:05.335 16225 16232 I maWriteLog: maFileRead succeeded on an empty fi
le. This is BAD!
11-15 11:53:05.335 16225 16232 I MoSyncFile: maFileDelete (1)
11-15 11:53:05.335 16225 16232 I MoSyncFile: maFileDelete success (1)
11-15 11:53:05.335 16225 16232 I MoSyncFile: maFileClose (1)

I guess I should file a bug report, right? Seems that the behaviour of the Android runtime is faulty.

Regards,
Ludwig 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anders Malm
andersmalm's picture
Offline
Mobile Conjurer
Joined: 28 Aug 2008
Posts:

Hi Ludwig!

We have now fixed that issue and it will be merged into to the nightly build branch today. The behavior is now consistent with the rest of our runtimes! Thanks for finding this!

Abi Waqas
abi's picture
Offline
Mobile Wizard
Joined: 3 May 2010
Posts:

The bug is now fixed and verified Ludwig. Thankyou for pointing it out for us. You can follow the progress here:

http://jira.mosync.com/browse/MOSYNC-237