Updated Ukrainian translation.
[LameXP.git] / src / LockedFile.cpp
blob38144b641d978b17940e5ee14e573bb0ca3c2ff4
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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 #include <stdio.h>
32 #include <io.h>
33 #include <fcntl.h>
35 ///////////////////////////////////////////////////////////////////////////////
37 #define THROW(STR) \
38 { \
39 char error_msg[512]; \
40 strcpy_s(error_msg, 512, STR); \
41 throw error_msg; \
44 // WARNING: Passing file descriptors into Qt does NOT work with dynamically linked CRT!
45 #ifdef QT_NODLL
46 static const bool g_useFileDescr = 1;
47 #else
48 static const bool g_useFileDescr = 0;
49 #endif
51 ///////////////////////////////////////////////////////////////////////////////
53 static const char *g_seed = "b14bf0ed8d5a62311b366907a6450ea2d52ad8bbc7d520b70400f1dd8ddfa9339011a473";
54 static const char *g_salt = "80e0cce7af513880065488568071342e5889d6c5ee96190c303b7260e5c8356c350fbe44";
56 static QByteArray fileHash(QFile &file)
58 QByteArray hash = QByteArray::fromHex(g_seed);
59 QByteArray salt = QByteArray::fromHex(g_salt);
61 if(file.isOpen() && file.reset())
63 QByteArray data = file.readAll();
64 QCryptographicHash sha(QCryptographicHash::Sha1);
65 QCryptographicHash md5(QCryptographicHash::Md5);
67 for(int r = 0; r < 8; r++)
69 sha.reset(); md5.reset();
70 sha.addData(hash); md5.addData(hash);
71 sha.addData(data); md5.addData(data);
72 sha.addData(salt); md5.addData(salt);
73 hash = sha.result() + md5.result();
77 return hash;
80 ///////////////////////////////////////////////////////////////////////////////
82 LockedFile::LockedFile(const QString &resourcePath, const QString &outPath, const QByteArray &expectedHash)
84 m_fileHandle = NULL;
85 QResource resource(resourcePath);
87 //Make sure the resource is valid
88 if(!resource.isValid())
90 THROW(QString("Resource '%1' is invalid!").arg(QFileInfo(resourcePath).absoluteFilePath().replace(QRegExp("^:/"), QString())).toUtf8().constData());
93 QFile outFile(outPath);
94 m_filePath = QFileInfo(outFile).absoluteFilePath();
96 //Open output file
97 for(int i = 0; i < 64; i++)
99 if(outFile.open(QIODevice::WriteOnly)) break;
100 if(!i) qWarning("Failed to open file on first attemp, retrying...");
101 Sleep(100);
104 //Write data to file
105 if(outFile.isOpen() && outFile.isWritable())
107 if(outFile.write(reinterpret_cast<const char*>(resource.data()), resource.size()) != resource.size())
109 QFile::remove(QFileInfo(outFile).canonicalFilePath());
110 THROW(QString("File '%1' could not be written!").arg(QFileInfo(outFile).fileName()).toUtf8().constData());
112 outFile.close();
114 else
116 THROW(QString("File '%1' could not be created!").arg(QFileInfo(outFile).fileName()).toUtf8().constData());
119 //Now lock the file!
120 for(int i = 0; i < 64; i++)
122 m_fileHandle = CreateFileW(QWCHAR(QDir::toNativeSeparators(m_filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
123 if((m_fileHandle != NULL) && (m_fileHandle != INVALID_HANDLE_VALUE)) break;
124 if(!i) qWarning("Failed to lock file on first attemp, retrying...");
125 Sleep(100);
128 //Locked successfully?
129 if((m_fileHandle == NULL) || (m_fileHandle == INVALID_HANDLE_VALUE))
131 QFile::remove(QFileInfo(outFile).canonicalFilePath());
132 char error_msg[512];
133 strcpy_s(error_msg, 512, QString("File '%1' could not be locked!").arg(QFileInfo(outFile).fileName()).toLatin1().constData());
134 throw error_msg;
137 //Open file for reading
138 if(g_useFileDescr)
140 int fd = _open_osfhandle(reinterpret_cast<intptr_t>(m_fileHandle), _O_RDONLY | _O_BINARY);
141 if(fd >= 0) outFile.open(fd, QIODevice::ReadOnly);
143 else
145 for(int i = 0; i < 64; i++)
147 if(outFile.open(QIODevice::ReadOnly)) break;
148 if(!i) qWarning("Failed to re-open file on first attemp, retrying...");
149 Sleep(100);
153 //Verify file contents
154 QByteArray hash;
155 if(outFile.isOpen())
157 hash = fileHash(outFile).toHex();
158 outFile.close();
160 else
162 QFile::remove(m_filePath);
163 THROW(QString("File '%1' could not be read!").arg(QFileInfo(outFile).fileName()).toLatin1().constData());
166 //Compare hashes
167 if(hash.isNull() || _stricmp(hash.constData(), expectedHash.constData()))
169 qWarning("\nFile checksum error:\n A = %s\n B = %s\n", expectedHash.constData(), hash.constData());
170 LAMEXP_CLOSE(m_fileHandle);
171 QFile::remove(m_filePath);
172 THROW(QString("File '%1' is corruputed, take care!").arg(QFileInfo(resourcePath).absoluteFilePath().replace(QRegExp("^:/"), QString())).toLatin1().constData());
176 LockedFile::LockedFile(const QString &filePath)
178 m_fileHandle = NULL;
179 QFileInfo existingFile(filePath);
180 existingFile.setCaching(false);
182 //Make sure the file exists, before we try to lock it
183 if(!existingFile.exists())
185 char error_msg[256];
186 strcpy_s(error_msg, 256, QString("File '%1' does not exist!").arg(existingFile.fileName()).toLatin1().constData());
187 throw error_msg;
190 //Remember file path
191 m_filePath = existingFile.canonicalFilePath();
193 //Now lock the file
194 for(int i = 0; i < 64; i++)
196 m_fileHandle = CreateFileW(QWCHAR(QDir::toNativeSeparators(filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
197 if((m_fileHandle != NULL) && (m_fileHandle != INVALID_HANDLE_VALUE)) break;
198 if(!i) qWarning("Failed to lock file on first attemp, retrying...");
199 Sleep(100);
202 //Locked successfully?
203 if((m_fileHandle == NULL) || (m_fileHandle == INVALID_HANDLE_VALUE))
205 THROW(QString("File '%1' could not be locked!").arg(existingFile.fileName()).toLatin1().constData());
209 LockedFile::~LockedFile(void)
211 LAMEXP_CLOSE(m_fileHandle);
214 const QString &LockedFile::filePath()
216 return m_filePath;