Commit f5a8e90a authored by Michael Reeves's avatar Michael Reeves

Use QTemporaryFile to manage temporary files

parent 84b87c9f
......@@ -37,6 +37,7 @@
#include <QDir>
#include <QFileInfo>
#include <QProcess>
#include <QTemporaryFile>
#include <QTextCodec>
#include <QTextStream>
......@@ -247,7 +248,8 @@ QStringList SourceData::setData(const QString& data)
// Create a temp file for preprocessing:
if(m_tempInputFileName.isEmpty())
{
m_tempInputFileName = FileAccess::tempFileName();
FileAccess::createTempFile(m_tempFile);
m_tempInputFileName = m_tempFile.fileName();
}
FileAccess f(m_tempInputFileName);
......@@ -626,6 +628,7 @@ static QString getArguments(QString cmd, QString& program, QStringList& args)
QStringList SourceData::readAndPreprocess(QTextCodec* pEncoding, bool bAutoDetectUnicode)
{
m_pEncoding = pEncoding;
QTemporaryFile fileIn1, fileOut1;
QString fileNameIn1;
QString fileNameOut1;
QString fileNameIn2;
......@@ -643,11 +646,12 @@ QStringList SourceData::readAndPreprocess(QTextCodec* pEncoding, bool bAutoDetec
}
else // File is not local: create a temporary local copy:
{
if(m_tempInputFileName.isEmpty()) {
m_tempInputFileName = FileAccess::tempFileName();
if(m_tempInputFileName.isEmpty())
{
m_fileAccess.createLocalCopy();
m_tempInputFileName = m_fileAccess.getTempName();
}
m_fileAccess.copyFile(m_tempInputFileName);
fileNameIn1 = m_tempInputFileName;
}
if(bAutoDetectUnicode)
......@@ -679,18 +683,21 @@ QStringList SourceData::readAndPreprocess(QTextCodec* pEncoding, bool bAutoDetec
}
else
{
QTemporaryFile tmpInPPFile;
QString fileNameInPP = fileNameIn1;
if(pEncoding1 != m_pOptions->m_pEncodingPP)
{
// Before running the preprocessor convert to the format that the preprocessor expects.
fileNameInPP = FileAccess::tempFileName();
FileAccess::createTempFile(tmpInPPFile);
fileNameInPP = tmpInPPFile.fileName();
pEncoding1 = m_pOptions->m_pEncodingPP;
convertFileEncoding(fileNameIn1, pEncoding, fileNameInPP, pEncoding1);
}
QString ppCmd = m_pOptions->m_PreProcessorCmd;
fileNameOut1 = FileAccess::tempFileName();
FileAccess::createTempFile(fileOut1);
fileNameOut1 = fileOut1.fileName();
QProcess ppProcess;
ppProcess.setStandardInputFile(fileNameInPP);
......@@ -718,28 +725,27 @@ QStringList SourceData::readAndPreprocess(QTextCodec* pEncoding, bool bAutoDetec
m_normalData.readFile(fileNameIn1);
pEncoding1 = m_pEncoding;
}
if(fileNameInPP != fileNameIn1)
{
FileAccess::removeTempFile(fileNameInPP);
}
}
// LineMatching Preprocessor
if(!m_pOptions->m_LineMatchingPreProcessorCmd.isEmpty())
{
QTemporaryFile tempOut2, fileInPP;
fileNameIn2 = fileNameOut1.isEmpty() ? fileNameIn1 : fileNameOut1;
QString fileNameInPP = fileNameIn2;
pEncoding2 = pEncoding1;
if(pEncoding2 != m_pOptions->m_pEncodingPP)
{
// Before running the preprocessor convert to the format that the preprocessor expects.
fileNameInPP = FileAccess::tempFileName();
FileAccess::createTempFile(fileInPP);
fileNameInPP = fileInPP.fileName();
pEncoding2 = m_pOptions->m_pEncodingPP;
convertFileEncoding(fileNameIn2, pEncoding1, fileNameInPP, pEncoding2);
}
QString ppCmd = m_pOptions->m_LineMatchingPreProcessorCmd;
fileNameOut2 = FileAccess::tempFileName();
FileAccess::createTempFile(tempOut2);
fileNameOut2 = tempOut2.fileName();
QProcess ppProcess;
ppProcess.setStandardInputFile(fileNameInPP);
ppProcess.setStandardOutputFile(fileNameOut2);
......@@ -764,11 +770,6 @@ QStringList SourceData::readAndPreprocess(QTextCodec* pEncoding, bool bAutoDetec
m_pOptions->m_LineMatchingPreProcessorCmd = "";
m_lmppData.readFile(fileNameIn2);
}
FileAccess::removeTempFile(fileNameOut2);
if(fileNameInPP != fileNameIn2)
{
FileAccess::removeTempFile(fileNameInPP);
}
}
else if(m_pOptions->m_bIgnoreComments || m_pOptions->m_bIgnoreCase)
{
......@@ -819,18 +820,6 @@ QStringList SourceData::readAndPreprocess(QTextCodec* pEncoding, bool bAutoDetec
}
}
}
// Remove unneeded temporary files. (A temp file from clipboard must not be deleted.)
if(!bTempFileFromClipboard && !m_tempInputFileName.isEmpty())
{
FileAccess::removeTempFile(m_tempInputFileName);
m_tempInputFileName = "";
}
if(!fileNameOut1.isEmpty())
{
FileAccess::removeTempFile(fileNameOut1);
fileNameOut1 = "";
}
return errors;
}
......
......@@ -21,6 +21,8 @@
#include <QPainter>
#include <QLinkedList>
#include <QVector>
#include <QTemporaryFile>
#include "common.h"
#include "fileaccess.h"
#include "options.h"
......@@ -285,6 +287,7 @@ private:
FileAccess m_fileAccess;
Options* m_pOptions;
QString m_tempInputFileName;
QTemporaryFile m_tempFile;//Created from clipboard content.
struct FileData
{
......
......@@ -101,40 +101,6 @@ class StatusInfo : public QDialog
}
};
class TempRemover
{
public:
TempRemover(const QString& origName, FileAccess& fa);
~TempRemover();
QString name() { return m_name; }
bool success() { return m_bSuccess; }
private:
QString m_name;
bool m_bTemp;
bool m_bSuccess;
};
TempRemover::TempRemover(const QString& origName, FileAccess& fa)
{
if(fa.isLocal())
{
m_name = origName;
m_bTemp = false;
m_bSuccess = true;
}
else
{
m_name = FileAccess::tempFileName();
m_bSuccess = fa.copyFile(m_name);
m_bTemp = m_bSuccess;
}
}
TempRemover::~TempRemover()
{
if(m_bTemp && !m_name.isEmpty())
FileAccess::removeTempFile(m_name);
}
enum Columns
{
s_NameCol = 0,
......@@ -841,14 +807,14 @@ bool DirectoryMergeWindow::Data::fastFileComparison(
QString fileName1 = fi1.absoluteFilePath();
QString fileName2 = fi2.absoluteFilePath();
TempRemover tr1(fileName1, fi1);
if(!tr1.success())
if(!fi2.createLocalCopy())
{
status = i18n("Creating temp copy of %1 failed.", fileName1);
return bEqual;
}
TempRemover tr2(fileName2, fi2);
if(!tr2.success())
if(!fi2.createLocalCopy())
{
status = i18n("Creating temp copy of %1 failed.", fileName2);
return bEqual;
......@@ -857,7 +823,7 @@ bool DirectoryMergeWindow::Data::fastFileComparison(
std::vector<char> buf1(100000);
std::vector<char> buf2(buf1.size());
QFile file1(tr1.name());
QFile file1(fi1.fileName());
if(!file1.open(QIODevice::ReadOnly))
{
......@@ -865,7 +831,7 @@ bool DirectoryMergeWindow::Data::fastFileComparison(
return bEqual;
}
QFile file2(tr2.name());
QFile file2(fi2.fileName());
if(!file2.open(QIODevice::ReadOnly))
{
......
......@@ -54,8 +54,6 @@ FileAccess::FileAccess()
reset();
}
void FileAccess::reset(void)
{
m_fileInfo = QFileInfo();
......@@ -76,22 +74,13 @@ void FileAccess::reset(void)
m_linkTarget = "";
//m_fileType = -1;
m_pParent = nullptr;
//Insure that old tempFile is removed we will recreate it as needed.
if(!m_localCopy.isEmpty())
{
removeTempFile(m_localCopy);
m_localCopy = "";
}
tmpFile.clear();
tmpFile = QSharedPointer<QTemporaryFile>(new QTemporaryFile());
}
FileAccess::~FileAccess()
{
//remove tempfile if any
if(!m_localCopy.isEmpty())
{
removeTempFile(m_localCopy);
m_localCopy = "";
}
tmpFile.clear();
}
// Two kinds of optimization are applied here:
......@@ -443,10 +432,10 @@ QString FileAccess::absoluteFilePath() const
} // Full abs path
// Just the name-part of the path, without parent directories
QString FileAccess::fileName() const
QString FileAccess::fileName(bool needTmp) const
{
if(!isLocal())
return m_name;
return (needTmp) ? m_localCopy : m_name;
else if(parent())
return m_filePath;
else
......@@ -610,21 +599,45 @@ bool FileAccess::listDir(t_DirectoryList* pDirList, bool bRecursive, bool bFindH
dirAntiPattern, bFollowDirLinks, bUseCvsIgnore);
}
QString FileAccess::tempFileName()
QString FileAccess::getTempName() const
{
return m_localCopy;
}
bool FileAccess::createLocalCopy()
{
if(isLocal() || !m_localCopy.isEmpty())
return true;
tmpFile->setAutoRemove(true);
tmpFile->setFileTemplate(QStringLiteral("XXXXXX-kdiff3tmp"));
tmpFile->open();
tmpFile->close();
m_localCopy = tmpFile->fileName();
return copyFile(tmpFile->fileName());
}
//static tempfile Generator
void FileAccess::createTempFile(QTemporaryFile& tmpFile)
{
tmpFile.setAutoRemove(true);
tmpFile.setFileTemplate(QStringLiteral("XXXXXX-kdiff3tmp"));
tmpFile.open();
tmpFile.close();
}
//TODO:Depricated to be removed
/*QString FileAccess::tempFileName()
{
QTemporaryFile tmpFile;
tmpFile.open();
//tmpFile.setAutoDelete( true ); // We only want the name. Delete the precreated file immediately.
QString name = tmpFile.fileName() + ".2";
//tmpFile.setAutoDelete( true );
QString name = tmpFile.fileName();
tmpFile.close();
return name;
}
}*/
bool FileAccess::removeTempFile(const QString& name) // static
{
if(name.endsWith(QLatin1String(".2")))
FileAccess(name.left(name.length() - 2)).removeFile();
{
return FileAccess(name).removeFile();
}
......@@ -661,7 +674,8 @@ qint64 FileAccess::sizeForReading()
if(!isLocal() && m_size == 0)
{
// Size couldn't be determined. Copy the file to a local temp place.
QString localCopy = tempFileName();
createLocalCopy();
QString localCopy = tmpFile->fileName();
bool bSuccess = copyFile(localCopy);
if(bSuccess)
{
......
......@@ -13,9 +13,11 @@
#include "progress.h"
#include <QUrl>
#include <QFileInfo>
#include <QDateTime>
#include <QSharedPointer>
#include <QTemporaryFile>
#include <QUrl>
#include <KIO/UDSEntry>
......@@ -61,7 +63,7 @@ public:
QDateTime lastModified() const;
QString fileName() const; // Just the name-part of the path, without parent directories
QString fileName(bool needTmp = false) const; // Just the name-part of the path, without parent directories
QString filePath() const; // The path-string that was used during construction
QString prettyAbsPath() const;
QUrl url() const;
......@@ -77,7 +79,9 @@ public:
bool copyFile( const QString& destUrl );
bool createBackup( const QString& bakExtension );
static QString tempFileName();
QString getTempName() const;
bool createLocalCopy();
static void createTempFile(QTemporaryFile& );
static bool removeTempFile( const QString& );
bool removeFile();
static bool removeFile( const QString& );
......@@ -111,6 +115,7 @@ private:
QString m_linkTarget;
QString m_name;
QString m_localCopy;
QSharedPointer<QTemporaryFile> tmpFile;
QString m_filePath; // might be absolute or relative if m_pParent!=0
qint64 m_size;
......
......@@ -23,6 +23,7 @@
#include <QFile>
#include <QProcess>
#include <QTemporaryFile>
#include <QTextStream>
#include <KAboutData>
......@@ -183,7 +184,9 @@ bool KDiff3Part::openFile()
{
// Normal patch
// patch -f -u --ignore-whitespace -i [inputfile] -o [outfile] [patchfile]
QString tempFileName = FileAccess::tempFileName();
QTemporaryFile tmpFile;
FileAccess::createTempFile(tmpFile);
QString tempFileName = tmpFile.fileName();
QString cmd = "patch -f -u --ignore-whitespace -i \"" + localFilePath() +
"\" -o \"" + tempFileName + "\" \"" + fileName1 + "\"";
......@@ -192,15 +195,15 @@ bool KDiff3Part::openFile()
process.waitForFinished(-1);
m_widget->slotFileOpen2(fileName1, tempFileName, "", "",
"", version2.isEmpty() ? fileName2 : "REV:" + version2 + ":" + fileName2, "", nullptr); // alias names
// std::cerr << "KDiff3: f1:" << fileName1.toLatin1() <<"<->"<<tempFileName.toLatin1()<< std::endl;
FileAccess::removeTempFile(tempFileName);
"", version2.isEmpty() ? fileName2 : "REV:" + version2 + ":" + fileName2, "", nullptr); // alias names // std::cerr << "KDiff3: f1:" << fileName1.toLatin1() <<"<->"<<tempFileName.toLatin1()<< std::endl;
}
else if(version2.isEmpty() && f2.exists())
{
// Reverse patch
// patch -f -u -R --ignore-whitespace -i [inputfile] -o [outfile] [patchfile]
QString tempFileName = FileAccess::tempFileName();
QTemporaryFile tmpFile;
FileAccess::createTempFile(tmpFile);
QString tempFileName = tmpFile.fileName();
QString cmd = "patch -f -u -R --ignore-whitespace -i \"" + localFilePath() +
"\" -o \"" + tempFileName + "\" \"" + fileName2 + "\"";
......@@ -211,7 +214,6 @@ bool KDiff3Part::openFile()
m_widget->slotFileOpen2(tempFileName, fileName2, "", "",
version1.isEmpty() ? fileName1 : "REV:" + version1 + ":" + fileName1, "", "", nullptr); // alias name
// std::cerr << "KDiff3: f2:" << fileName2.toLatin1() <<"<->"<<tempFileName.toLatin1()<< std::endl;
FileAccess::removeTempFile(tempFileName);
}
else if(!version1.isEmpty() && !version2.isEmpty())
{
......@@ -219,13 +221,17 @@ bool KDiff3Part::openFile()
// Assuming that files are on CVS: Try to get them
// cvs update -p -r [REV] [FILE] > [OUTPUTFILE]
QString tempFileName1 = FileAccess::tempFileName();
QTemporaryFile tmpFile1;
FileAccess::createTempFile(tmpFile1);
QString tempFileName1 = tmpFile1.fileName();
QString cmd1 = "cvs update -p -r " + version1 + " \"" + fileName1 + "\" >\"" + tempFileName1 + "\"";
QProcess process1;
process1.start(cmd1);
process1.waitForFinished(-1);
QString tempFileName2 = FileAccess::tempFileName();
QTemporaryFile tmpFile2;
FileAccess::createTempFile(tmpFile2);
QString tempFileName2 = tmpFile2.fileName();
QString cmd2 = "cvs update -p -r " + version2 + " \"" + fileName2 + "\" >\"" + tempFileName2 + "\"";
QProcess process2;
process2.start(cmd2);
......@@ -237,8 +243,6 @@ bool KDiff3Part::openFile()
"", nullptr);
// std::cerr << "KDiff3: f1/2:" << tempFileName1.toLatin1() <<"<->"<<tempFileName2.toLatin1()<< std::endl;
FileAccess::removeTempFile(tempFileName1);
FileAccess::removeTempFile(tempFileName2);
return true;
}
else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment