/// Writes "size" bytes into the file from the pointer "src". /// The number of actual bytes written is returned in bytesWritten /// @returns The status of the file virtual File::Status write(U32 size, const char *src, U32 *bytesWritten) { // JMQ: despite the U32 parameters, the maximum filesize supported by this // function is probably the max value of S32, due to the unix syscall // api. AssertFatal(Closed != currentStatus, "File::write: file closed"); AssertFatal(NULL != handle, "File::write: invalid file handle"); AssertFatal(NULL != src, "File::write: NULL source pointer"); AssertFatal(true == hasCapability(FileWrite), "File::write: file lacks capability"); AssertWarn(0 != size, "File::write: size of zero"); long lastBytes=0; File::Status lastStatus = File::Ok; if ((Ok != currentStatus && EOS != currentStatus) || 0 == size) { lastStatus = currentStatus; } else { lastBytes = x86UNIXWrite(*((int *)handle), src, size); if (lastBytes < 0) { lastBytes = 0; setStatus(); lastStatus = currentStatus; } else { lastStatus = Ok; } } // if bytesWritten is a valid pointer, put number of bytes read there. if(bytesWritten) *bytesWritten = lastBytes; currentStatus = lastStatus; return currentStatus; }
//------------------------------------------------------------------------------ // 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; }
//----------------------------------------------------------------------------- // Write to a file. // The number of bytes to write is passed in size, the data is passed in src. // The number of bytes written is available in bytesWritten if a non-Null // pointer is provided. //----------------------------------------------------------------------------- File::FileStatus File::write(U32 size, const char *src, U32 *bytesWritten) { // JMQ: despite the U32 parameters, the maximum filesize supported by this // function is probably the max value of S32, due to the unix syscall // api. AssertFatal(Closed != currentStatus, "File::write: file closed"); AssertFatal(NULL != handle, "File::write: invalid file handle"); AssertFatal(NULL != src, "File::write: NULL source pointer"); AssertFatal(true == hasCapability(FileWrite), "File::write: file lacks capability"); AssertWarn(0 != size, "File::write: size of zero"); if ((Ok != currentStatus && EOS != currentStatus) || 0 == size) return currentStatus; else { S32 numWritten = x86UNIXWrite(*((int *)handle), src, size); if (numWritten < 0) return setStatus(); if (bytesWritten) *bytesWritten = static_cast<U32>(numWritten); return currentStatus = Ok; } }