Android 2.1 file i/o: pread fails with errno 22 (EINVAL, Invalid argument)

1.3k Views Asked by At

I'm doing simple file-copy operation using open(2), pread(2) and pwrite(2) as seen below (code simplified a bit). My problem is that the ::pread(2) functions fails returning -1, with [errno=22]. Note that both source and destination files are placed on SD card. This problem occurs on Android 2.1 (API level <=7, both emulator and real device), no problem when running it on Android 2.3 (API level 9). Is there a problem in my code or is it a bug in kernel/stdlib?

fileSource = ::open(pcSource, O_RDONLY);
fileDest = ::open(pcDest, O_RDWR|O_TRUNC|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO)

unsigned uiCopyLen = 0;
unsigned uiCopied = 0;
struct stat fileStatus;
fileStatus.st_size = 0;

::fstat(fileSource, &fileStatus);

char * cBuffer = new char[fileStatus.st_blksize];

// ... <in loop>
    ::pread(fileSource, cBuffer, fileStatus.st_blksize, uiCopied);  // errno 22, Invalid argument
    ::pwrite(fileDest, cBuffer, iLen, uiCopied);
// ... </in loop>

Also note that if using read(2), write(2) or switching to streamed API (fread(3), fwrite(3)) works correctly. Strange.

0

There are 0 best solutions below