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

北慕城南 提交于 2019-12-12 13:29:22

问题


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.

来源:https://stackoverflow.com/questions/8038697/android-2-1-file-i-o-pread-fails-with-errno-22-einval-invalid-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!