OSDN Git Service

first commit
[lamexp/LameXP.git] / src / LockedFile.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "LockedFile.h"
23 #include "Global.h"
24
25 #include <QResource>
26 #include <QFile>
27 #include <QFileInfo>
28 #include <QDir>
29 #include <QCryptographicHash>
30
31 LockedFile::LockedFile(const QString &resourcePath, const QString &outPath, const QByteArray &expectedHash)
32 {
33         m_fileHandle = NULL;
34         
35         QResource resource(resourcePath);
36         QFile outFile(outPath);
37         
38         m_filePath = QFileInfo(outFile).absoluteFilePath();
39         outFile.open(QIODevice::WriteOnly);
40         
41         //Write data to file
42         if(outFile.isOpen() && outFile.isWritable() && resource.isValid())
43         {
44                 if(outFile.write(reinterpret_cast<const char*>(resource.data()), resource.size()) != resource.size())
45                 {
46                         QFile::remove(QFileInfo(outFile).absoluteFilePath());
47                         char error_msg[256];
48                         strcpy_s(error_msg, 256, QString("File '%1' could not be written!").arg(QFileInfo(outFile).fileName()).toLatin1().constData());
49                         throw error_msg;
50                 }
51                 outFile.close();
52         }
53         else
54         {
55                 char error_msg[256];
56                 strcpy_s(error_msg, 256, QString("File '%1' could not be created!").arg(QFileInfo(outFile).fileName()).toLatin1().constData());
57                 throw error_msg;
58         }
59
60         //Now lock the file
61         m_fileHandle = CreateFileW(QWCHAR(QDir::toNativeSeparators(m_filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
62
63         if(m_fileHandle == INVALID_HANDLE_VALUE)
64         {
65                 QFile::remove(QFileInfo(outFile).absoluteFilePath());
66                 char error_msg[256];
67                 strcpy_s(error_msg, 256, QString("File '%1' could not be locked!").arg(QFileInfo(outFile).fileName()).toLatin1().constData());
68                 throw error_msg;
69         }
70
71         //Verify file contents
72         outFile.open(QIODevice::ReadOnly);
73         QCryptographicHash fileHash(QCryptographicHash::Sha1);
74         if(outFile.isOpen() && outFile.isReadable())
75         {
76                 fileHash.addData(outFile.readAll());
77                 outFile.close();
78         }
79
80         if(_stricmp(fileHash.result().toHex().constData(), expectedHash.constData()))
81         {
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());
85                 char error_msg[256];
86                 strcpy_s(error_msg, 256, QString("File '%1' is corruputed, take care!").arg(QFileInfo(outFile).fileName()).toLatin1().constData());
87                 throw error_msg;
88         }
89 }
90
91 LockedFile::~LockedFile(void)
92 {
93         LAMEXP_CLOSE(m_fileHandle);
94 }
95
96 const QString &LockedFile::filePath()
97 {
98         return m_filePath;
99 }