//this was a bitch! //for nfs write to work we have to write chunked //otherwise this could crash on big files ssize_t CNFSFile::Write(const void* lpBuf, size_t uiBufSize) { size_t numberOfBytesWritten = 0; int writtenBytes = 0; size_t leftBytes = uiBufSize; //clamp max write chunksize to 32kb - fixme - this might be superfluious with future libnfs versions size_t chunkSize = gNfsConnection.GetMaxWriteChunkSize() > 32768 ? 32768 : (size_t)gNfsConnection.GetMaxWriteChunkSize(); CSingleLock lock(gNfsConnection); if (m_pFileHandle == NULL || m_pNfsContext == NULL) return -1; //write as long as some bytes are left to be written while( leftBytes ) { //the last chunk could be smalle than chunksize if(leftBytes < chunkSize) { chunkSize = leftBytes;//write last chunk with correct size } //write chunk writtenBytes = gNfsConnection.GetImpl()->nfs_write(m_pNfsContext, m_pFileHandle, chunkSize, (char *)lpBuf + numberOfBytesWritten); //decrease left bytes leftBytes-= writtenBytes; //increase overall written bytes numberOfBytesWritten += writtenBytes; //danger - something went wrong if (writtenBytes < 0) { CLog::Log(LOGERROR, "Failed to pwrite(%s) %s\n", m_url.GetFileName().c_str(), gNfsConnection.GetImpl()->nfs_get_error(m_pNfsContext)); if (numberOfBytesWritten == 0) return -1; break; } } //return total number of written bytes return numberOfBytesWritten; }
//this was a bitch! //for nfs write to work we have to write chunked //otherwise this could crash on big files int CFileNFS::Write(const void* lpBuf, int64_t uiBufSize) { int numberOfBytesWritten = 0; int writtenBytes = 0; int leftBytes = uiBufSize; int chunkSize = gNfsConnection.GetMaxWriteChunkSize(); CSingleLock lock(gNfsConnection); if (m_pFileHandle == NULL || gNfsConnection.GetNfsContext() == NULL) return -1; //write as long as some bytes are left to be written while( leftBytes ) { //the last chunk could be smalle than chunksize if(leftBytes < chunkSize) { chunkSize = leftBytes;//write last chunk with correct size } //write chunk writtenBytes = gNfsConnection.GetImpl()->nfs_write(gNfsConnection.GetNfsContext(), m_pFileHandle, (size_t)chunkSize, (char *)lpBuf + numberOfBytesWritten); //decrease left bytes leftBytes-= writtenBytes; //increase overall written bytes numberOfBytesWritten += writtenBytes; //danger - something went wrong if (writtenBytes < 0) { CLog::Log(LOGERROR, "Failed to pwrite(%s) %s\n", m_url.GetFileName().c_str(), gNfsConnection.GetImpl()->nfs_get_error(gNfsConnection.GetNfsContext())); break; } } //return total number of written bytes return numberOfBytesWritten; }