FileFactory::FileError FileFactory::save(FileVersion version) { if(version == FileFactory::Version_Auto) { version = (FileVersion)mCurrentFileVersion; } else { mCurrentFileVersion = version; } //Don't save a file without at least 1 tab. if(mTabWidget->count() <= 0) return FileFactory::Err_NoTabsToSave; QTemporaryFile f; if(!f.open()) { //TODO: some nice dialog to warn the user. qWarning() << "Couldn't open file for writing..." << f.fileName(); return FileFactory::Err_OpeningFile; } QDataStream out(&f); // Write a header with a "magic number" and a version out << AppInfo::inst()->magicNumber; File *saveFile = 0; switch(version) { default: case FileFactory::Version_1_2: saveFile = new File_v2(mMainWindow, this); break; case FileFactory::Version_1_0: saveFile = new File_v1(mMainWindow, this); break; } int error = saveFile->save(&out); f.close(); if(error != FileFactory::No_Error) return (FileFactory::FileError)error; QDir d(QFileInfo(fileName).path()); //only try to delete the file if it exists. if(d.exists(fileName)) { if(!d.remove(fileName)) return FileFactory::Err_RemovingOrigFile; } if(!f.copy(fileName)) { qDebug() << "Could not write final output file." << f.fileName() << fileName; return FileFactory::Err_RenamingTempFile; } return FileFactory::No_Error; }
void ArchivBuilder::build( QByteArray stubData, QString fileName ) { QTemporaryFile tmpArchiv; tmpArchiv.open(); qDebug() << "Will create temporarty file: " << tmpArchiv.fileName(); QDataStream ds( &tmpArchiv ); ds.writeRawData( stubData, stubData.size() ); // Set dataMarker for( uint i = 0; i < ArchivBuilder::DATA_MARKER_LENGTH - 1; i++ ) { ds.writeRawData( ArchivBuilder::DATA_MARKER + i , 1 ); } // qDebug() << "Marker size: " << ArchivBuilder::DATA_MARKER_LENGTH; ds.writeRawData( ArchivBuilder::DATA_MARKER_REPLACECHAR + 0 , 1 ); QByteArray indexData = _index->compressedIndexData(); int indexSize = indexData.size(); qDebug() << "Index size: " << indexSize; QString indexSizeString; indexSizeString.setNum( indexSize ); ds.writeRawData( indexSizeString.toAscii(), indexSizeString.toAscii().size() ); ds.writeRawData( "\n", 1 ); // qDebug() << "Wrote size string: " << indexSizeString.toAscii(); ds.writeRawData( indexData, indexData.size() ); QByteArray archivData = _data->data(); ds.writeRawData( archivData, archivData.size() ); qDebug() << "Wrote data section of size: " << archivData.size(); if ( QFileInfo( fileName ).exists() ) { QFile( fileName ).remove(); // QFile( fileName ).rename( QString( "%1_back_%2" ).arg( fileName ).arg( RedBullPlayer::Tools::UuidCreator::create() ) ); } if( tmpArchiv.copy( fileName ) ) { qDebug() << "Created Archive at: " << QFileInfo( fileName ) .absoluteFilePath(); } else { qWarning() << "Could not copy archiv file to: " << QFileInfo( fileName ) .absoluteFilePath(); } qDebug() << "Will create temporarty file: " << tmpArchiv.fileName(); tmpArchiv.close(); tmpArchiv.remove(); }
void ImageManager::ThumbnailCache::save() const { m_timer->stop(); m_unsaved = 0; QTemporaryFile file; if ( !file.open() ) { qWarning("Failed to create temporary file"); return; } QDataStream stream(&file); stream << FILEVERSION << m_currentFile << m_currentOffset << m_map.count(); for( QMap<DB::FileName,CacheFileInfo>::ConstIterator it = m_map.begin(); it != m_map.end(); ++it ) { const CacheFileInfo& cacheInfo = it.value(); stream << it.key().relative() << cacheInfo.fileIndex << cacheInfo.offset << cacheInfo.size; } file.close(); const QString realFileName = thumbnailPath(QString::fromLatin1("thumbnailindex")); QFile::remove( realFileName ); if ( !file.copy( realFileName ) ) qWarning("Failed to copy the temporary file %s to %s", qPrintable( file.fileName() ), qPrintable( realFileName ) ); QFile realFile( realFileName ); realFile.open( QIODevice::ReadOnly ); realFile.setPermissions( QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::WriteGroup | QFile::ReadOther ); realFile.close(); }
void BootloaderInstallHex::installStage2(void) { emit logItem(tr("Adding bootloader to firmware file"), LOGINFO); QCoreApplication::processEvents(); // local temp file QTemporaryFile tempbin; tempbin.open(); QString tempbinName = tempbin.fileName(); tempbin.close(); // get temporary files filenames -- external tools need this. m_descrambled.open(); QString descrambledName = m_descrambled.fileName(); m_descrambled.close(); m_tempfile.open(); QString tempfileName = m_tempfile.fileName(); m_tempfile.close(); int origin = 0; switch(m_model) { case 3: origin = 0x3f0000; break; case 2: case 1: origin = 0x1f0000; break; default: origin = 0; break; } // iriver decode already done in stage 1 int result; if((result = mkboot_iriver(descrambledName.toLocal8Bit().constData(), tempfileName.toLocal8Bit().constData(), tempbinName.toLocal8Bit().constData(), origin)) < 0) { QString error; switch(result) { case -1: error = tr("could not open input file"); break; case -2: error = tr("reading header failed"); break; case -3: error = tr("reading firmware failed"); break; case -4: error = tr("can't open bootloader file"); break; case -5: error = tr("reading bootloader file failed"); break; case -6: error = tr("can't open output file"); break; case -7: error = tr("writing output file failed"); break; } emit logItem(tr("Error in patching: %1").arg(error), LOGERROR); emit done(true); return; } QTemporaryFile targethex; targethex.open(); QString targethexName = targethex.fileName(); if((result = iriver_encode(tempbinName.toLocal8Bit().constData(), targethexName.toLocal8Bit().constData(), FALSE)) < 0) { emit logItem(tr("Error in scramble: %1").arg(scrambleError(result)), LOGERROR); targethex.close(); emit done(true); return; } // finally check the md5sum of the created file QByteArray filedata; filedata = targethex.readAll(); targethex.close(); QString hash = QCryptographicHash::hash(filedata, QCryptographicHash::Md5).toHex(); qDebug() << "[BootloaderInstallHex] created hexfile hash:" << hash; emit logItem(tr("Checking modified firmware file"), LOGINFO); if(hash != QString(md5sums[m_hashindex].patched)) { emit logItem(tr("Error: modified file checksum wrong"), LOGERROR); targethex.remove(); emit done(true); return; } // finally copy file to player targethex.copy(m_blfile); emit logItem(tr("Success: modified firmware file created"), LOGINFO); logInstall(LogAdd); emit done(false); return; }