mirror of
https://bitbucket.org/smil3y/katie.git
synced 2025-02-25 03:12:56 +00:00
reworkd temporary file name generator
while at it, do not open it with O_LARGEFILE since most files are small and even tho it is used as method for copying files for an examples it should not be used to copy such big files because there are far more optimal platform specific solutions for doing that Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
95bf73d5f3
commit
1d3024fc1f
1 changed files with 13 additions and 75 deletions
|
@ -46,78 +46,6 @@
|
|||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
|
||||
/*
|
||||
* Copyright (c) 1987, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*!
|
||||
\internal
|
||||
|
||||
Generates a unique file path and returns a native handle to the open file.
|
||||
\a path is used as a template when generating unique paths, \a pos
|
||||
identifies the position of the first character that will be replaced in the
|
||||
template and \a length the number of characters that may be substituted.
|
||||
|
||||
Returns an open handle to the newly created file if successful, an invalid
|
||||
handle otherwise. In both cases, the string in \a path will be changed and
|
||||
contain the generated path name.
|
||||
*/
|
||||
static bool createFileFromTemplate(int &file,
|
||||
QFileSystemEntry::NativePath &path, int pos, int length,
|
||||
QSystemError &error)
|
||||
{
|
||||
Q_ASSERT(length != 0);
|
||||
Q_ASSERT(pos < path.length());
|
||||
Q_ASSERT(length <= path.length() - pos);
|
||||
|
||||
char *data = path.data();
|
||||
for (int i = 0; i < length; i++) {
|
||||
char ch = char((qrand() & 0xffff) % (26 + 26));
|
||||
if (ch < 26)
|
||||
data[i + pos] = char(ch + 'A');
|
||||
else
|
||||
data[i + pos] = char(ch - 26 + 'a');
|
||||
}
|
||||
|
||||
// Atomically create file and obtain handle
|
||||
file = QT_OPEN(data,
|
||||
QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE,
|
||||
0600);
|
||||
|
||||
if (file == -1) {
|
||||
error = QSystemError(errno, QSystemError::StandardLibraryError);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//************* QTemporaryFileEngine
|
||||
class QTemporaryFileEngine : public QFSFileEngine
|
||||
{
|
||||
|
@ -239,11 +167,21 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
|
|||
}
|
||||
|
||||
Q_ASSERT(phLength >= 6);
|
||||
Q_ASSERT(phPos < filename.length());
|
||||
Q_ASSERT(phLength <= filename.length() - phPos);
|
||||
|
||||
QSystemError error;
|
||||
static const char tmpnamechars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
if (!createFileFromTemplate(d->fd, filename, phPos, phLength, error)) {
|
||||
setError(QFile::OpenError, error.toString());
|
||||
char *data = filename.data();
|
||||
for (int i = 0; i < phLength; i++) {
|
||||
data[i + phPos] = tmpnamechars[qrand() % 52];
|
||||
}
|
||||
|
||||
// Atomically create file and obtain handle
|
||||
d->fd = QT_OPEN(data, QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR, 0600);
|
||||
|
||||
if (d->fd == -1) {
|
||||
setError(QFile::OpenError, qt_error_string(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue