Fix typos.
[LameXP.git] / src / LockedFile.cpp
blob0e0947a91ab44b227803dd16a4a3957cc9b615c8
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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.
9 //
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"
23 #include "Global.h"
25 #include <QResource>
26 #include <QFile>
27 #include <QFileInfo>
28 #include <QDir>
29 #include <QCryptographicHash>
31 LockedFile::LockedFile(const QString &resourcePath, const QString &outPath, const QByteArray &expectedHash)
33 m_fileHandle = NULL;
35 QResource resource(resourcePath);
36 QFile outFile(outPath);
38 m_filePath = QFileInfo(outFile).absoluteFilePath();
39 outFile.open(QIODevice::WriteOnly);
41 //Write data to file
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).canonicalFilePath());
47 char error_msg[512];
48 strcpy_s(error_msg, 512, QString("File '%1' could not be written!").arg(QFileInfo(outFile).fileName()).toUtf8().constData());
49 throw error_msg;
51 outFile.close();
53 else
55 char error_msg[512];
56 strcpy_s(error_msg, 512, QString("File '%1' could not be created!").arg(QFileInfo(outFile).fileName()).toUtf8().constData());
57 throw error_msg;
60 //Now lock the file
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).canonicalFilePath());
66 char error_msg[512];
67 strcpy_s(error_msg, 512, QString("File '%1' could not be locked!").arg(QFileInfo(outFile).fileName()).toLatin1().constData());
68 throw error_msg;
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());
77 outFile.close();
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).canonicalFilePath());
85 char error_msg[512];
86 strcpy_s(error_msg, 512, QString("File '%1' is corruputed, take care!").arg(QFileInfo(outFile).fileName()).toLatin1().constData());
87 throw error_msg;
91 LockedFile::LockedFile(const QString &filePath)
93 m_fileHandle = NULL;
94 QFileInfo existingFile(filePath);
96 if(!existingFile.exists())
98 char error_msg[256];
99 strcpy_s(error_msg, 256, QString("File '%1' does not exist!").arg(existingFile.fileName()).toLatin1().constData());
100 throw error_msg;
103 //Remember file path
104 m_filePath = existingFile.canonicalFilePath();
106 //Now lock the file
107 m_fileHandle = CreateFileW(QWCHAR(QDir::toNativeSeparators(filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
109 if(m_fileHandle == INVALID_HANDLE_VALUE)
111 char error_msg[256];
112 strcpy_s(error_msg, 256, QString("File '%1' could not be locked!").arg(existingFile.fileName()).toLatin1().constData());
113 throw error_msg;
117 LockedFile::~LockedFile(void)
119 LAMEXP_CLOSE(m_fileHandle);
122 const QString &LockedFile::filePath()
124 return m_filePath;