OSDN Git Service

reworkd temporary file name generator
authorIvailo Monev <xakepa10@gmail.com>
Sun, 9 Aug 2020 02:24:03 +0000 (05:24 +0300)
committerIvailo Monev <xakepa10@gmail.com>
Sun, 9 Aug 2020 02:36:41 +0000 (05:36 +0300)
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>
src/core/io/qtemporaryfile.cpp

index 6579971..637677c 100644 (file)
 
 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);
+
+    static const char tmpnamechars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
-    QSystemError error;
+    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 (!createFileFromTemplate(d->fd, filename, phPos, phLength, error)) {
-        setError(QFile::OpenError, error.toString());
+    if (d->fd == -1) {
+        setError(QFile::OpenError, qt_error_string(errno));
         return false;
     }