/// Reads "size" bytes from the file, and dumps data into "dst". /// The number of actual bytes read is returned in bytesRead /// @returns The status of the file File::Status read(U32 size, char *dst, U32 *bytesRead) { #ifdef DEBUG // fprintf(stdout,"reading %d bytes\n",size);fflush(stdout); #endif AssertFatal(Closed != currentStatus, "File::read: file closed"); AssertFatal(NULL != handle, "File::read: invalid file handle"); AssertFatal(NULL != dst, "File::read: NULL destination pointer"); AssertFatal(true == hasCapability(FileRead), "File::read: file lacks capability"); AssertWarn(0 != size, "File::read: size of zero"); /* show stats for this file */ #ifdef DEBUG //struct stat st; //fstat(*((int *)handle), &st); //fprintf(stdout,"file size = %d\n", st.st_size); #endif /****************************/ long lastBytes=0; File::Status lastStatus = File::Ok; if (Ok != currentStatus || 0 == size) { lastStatus = currentStatus; } else { long *bytes = &lastBytes; if ( (*((U32 *)bytes) = x86UNIXRead(*((int *)handle), dst, size)) == -1) { #ifdef DEBUG // fprintf(stdout,"unsuccessful: %d\n", *((U32 *)bytes));fflush(stdout); #endif setStatus(); // unsuccessful lastStatus = currentStatus; } else { // dst[*((U32 *)bytes)] = '\0'; if (*((U32 *)bytes) != size || *((U32 *)bytes) == 0) { #ifdef DEBUG // fprintf(stdout,"end of stream: %d\n", *((U32 *)bytes));fflush(stdout); #endif currentStatus = EOS; // end of stream lastStatus = currentStatus; } } } // dst[*bytesRead] = '\0'; #ifdef DEBUG //fprintf(stdout, "We read:\n"); //fprintf(stdout, "====================================================\n"); //fprintf(stdout, "%s\n",dst); //fprintf(stdout, "====================================================\n"); //fprintf(stdout,"read ok: %d\n", *bytesRead);fflush(stdout); #endif // if bytesRead is a valid pointer, put number of bytes read there. if(bytesRead) *bytesRead = lastBytes; currentStatus = lastStatus; return currentStatus; // successfully read size bytes }
//------------------------------------------------------------------------------ // copy a file from src to dest static bool CopyFile(const char* src, const char* dest) { S32 srcFd = x86UNIXOpen(src, O_RDONLY); S32 destFd = x86UNIXOpen(dest, O_WRONLY | O_CREAT | O_TRUNC); bool error = false; if (srcFd != -1 && destFd != -1) { const int BufSize = 8192; char buf[BufSize]; S32 bytesRead = 0; while ((bytesRead = x86UNIXRead(srcFd, buf, BufSize)) > 0) { // write data if (x86UNIXWrite(destFd, buf, bytesRead) == -1) { error = true; break; } } if (bytesRead == -1) error = true; } if (srcFd != -1) x86UNIXClose(srcFd); if (destFd != -1) x86UNIXClose(destFd); if (error) { Con::errorf("Error copying file: %s, %s", src, dest); remove(dest); } return error; }