1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "LockedFile.h"
29 #include <QCryptographicHash>
31 LockedFile::LockedFile(const QString
&resourcePath
, const QString
&outPath
, const QByteArray
&expectedHash
)
35 QResource
resource(resourcePath
);
36 QFile
outFile(outPath
);
38 m_filePath
= QFileInfo(outFile
).absoluteFilePath();
39 outFile
.open(QIODevice::WriteOnly
);
42 if(outFile
.isOpen() && outFile
.isWritable() && resource
.isValid())
44 if(outFile
.write(reinterpret_cast<const char*>(resource
.data()), resource
.size()) != resource
.size())
46 QFile::remove(QFileInfo(outFile
).absoluteFilePath());
48 strcpy_s(error_msg
, 256, QString("File '%1' could not be written!").arg(QFileInfo(outFile
).fileName()).toUtf8().constData());
56 strcpy_s(error_msg
, 256, QString("File '%1' could not be created!").arg(QFileInfo(outFile
).fileName()).toUtf8().constData());
61 m_fileHandle
= CreateFileW(QWCHAR(QDir::toNativeSeparators(m_filePath
)), GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, NULL
, NULL
);
63 if(m_fileHandle
== INVALID_HANDLE_VALUE
)
65 QFile::remove(QFileInfo(outFile
).absoluteFilePath());
67 strcpy_s(error_msg
, 256, QString("File '%1' could not be locked!").arg(QFileInfo(outFile
).fileName()).toLatin1().constData());
71 //Verify file contents
72 outFile
.open(QIODevice::ReadOnly
);
73 QCryptographicHash
fileHash(QCryptographicHash::Sha1
);
74 if(outFile
.isOpen() && outFile
.isReadable())
76 fileHash
.addData(outFile
.readAll());
80 if(_stricmp(fileHash
.result().toHex().constData(), expectedHash
.constData()))
82 qWarning("\nFile checksum error:\n Expected = %040s\n Detected = %040s\n", expectedHash
.constData(), fileHash
.result().toHex().constData());
83 LAMEXP_CLOSE(m_fileHandle
);
84 QFile::remove(QFileInfo(outFile
).absoluteFilePath());
86 strcpy_s(error_msg
, 256, QString("File '%1' is corruputed, take care!").arg(QFileInfo(outFile
).fileName()).toLatin1().constData());
91 LockedFile::~LockedFile(void)
93 LAMEXP_CLOSE(m_fileHandle
);
96 const QString
&LockedFile::filePath()