📄 qtemporaryfile.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qtemporaryfile.h"#ifndef QT_NO_TEMPORARYFILE#include "qplatformdefs.h"#include "qabstractfileengine.h"#include "private/qfile_p.h"#include "private/qabstractfileengine_p.h"#include "private/qfsfileengine_p.h"#include <stdlib.h>#include <errno.h>#include <sys/stat.h>#include <sys/types.h>#include <stdlib.h>#include <time.h>#include <ctype.h>#include <sys/types.h>#ifdef Q_OS_WIN#include <process.h>#if defined(_MSC_VER) && _MSC_VER >= 1400#include <share.h>#endif#endif/* * 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. */static int _gettemp(char *path, int *doopen, int domkdir, int slen){ char *start, *trv, *suffp; struct stat sbuf; int rval;#ifdef Q_OS_WIN int pid;#else pid_t pid;#endif if (doopen && domkdir) { errno = EINVAL; return(0); } for (trv = path; *trv; ++trv) ; trv -= slen; suffp = trv; --trv; if (trv < path) { errno = EINVAL; return (0); }#if defined(Q_OS_WIN) && defined(_MSC_VER) && _MSC_VER >= 1400 pid = _getpid();#else pid = getpid();#endif while (trv >= path && *trv == 'X' && pid != 0) { *trv-- = (pid % 10) + '0'; pid /= 10; }#ifndef S_ISDIR#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)#endif while (trv >= path && *trv == 'X') { char c; // CHANGE arc4random() -> random() pid = (qrand() & 0xffff) % (26+26); if (pid < 26) c = pid + 'A'; else c = (pid - 26) + 'a'; *trv-- = c; } start = trv + 1; /* * check the target directory; if you have six X's and it * doesn't exist this runs for a *very* long time. */ if (doopen || domkdir) { for (;; --trv) { if (trv <= path) break; if (*trv == '/') { *trv = '\0'; rval = stat(path, &sbuf); *trv = '/'; if (rval != 0) return(0); if (!S_ISDIR(sbuf.st_mode)) { errno = ENOTDIR; return(0); } break; } } } for (;;) { if (doopen) {#if defined(Q_OS_WIN) && defined(_MSC_VER) && _MSC_VER >= 1400 if (_sopen_s(doopen, path, QT_OPEN_CREAT|O_EXCL|QT_OPEN_RDWR|QT_OPEN_BINARY#ifdef QT_LARGEFILE_SUPPORT |QT_OPEN_LARGEFILE#endif , _SH_DENYNO, _S_IREAD | _S_IWRITE)== 0)#else if ((*doopen = open(path, QT_OPEN_CREAT|O_EXCL|QT_OPEN_RDWR#ifdef QT_LARGEFILE_SUPPORT |QT_OPEN_LARGEFILE#endif# if defined(Q_OS_WIN) |O_BINARY# endif , 0600)) >= 0)#endif return(1); if (errno != EEXIST) return(0); } else if (domkdir) {#ifdef Q_OS_WIN if (QT_MKDIR(path) == 0)#else if (mkdir(path, 0700) == 0)#endif return(1); if (errno != EEXIST) return(0); }#ifndef Q_OS_WIN else if (lstat(path, &sbuf)) return(errno == ENOENT ? 1 : 0);#endif /* tricky little algorwwithm for backward compatibility */ for (trv = start;;) { if (!*trv) return (0); if (*trv == 'Z') { if (trv == suffp) return (0); *trv++ = 'a'; } else { if (isdigit(*trv)) *trv = 'a'; else if (*trv == 'z') /* inc from z to A */ *trv = 'A'; else { if (trv == suffp) return (0); ++*trv; } break; } } } /*NOTREACHED*/}static int qt_mkstemps(char *path, int slen){ int fd; return (_gettemp(path, &fd, 0, slen) ? fd : -1);}//************* QTemporaryFileEngineclass QTemporaryFileEngine : public QFSFileEngine{ Q_DECLARE_PRIVATE(QFSFileEngine)public: QTemporaryFileEngine(const QString &file) : QFSFileEngine(file) { } ~QTemporaryFileEngine(); bool open(QIODevice::OpenMode flags); bool remove(); bool close();};QTemporaryFileEngine::~QTemporaryFileEngine(){}bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode){ Q_D(QFSFileEngine); QString qfilename = d->filePath; if(!qfilename.contains(QLatin1String("XXXXXX"))) qfilename += QLatin1String(".XXXXXX"); int suffixLength = qfilename.length() - (qfilename.lastIndexOf(QLatin1String("XXXXXX"), -1, Qt::CaseSensitive) + 6); d->closeFileHandle = true; char *filename = qstrdup(qfilename.toLocal8Bit()); int fd = qt_mkstemps(filename, suffixLength); if (fd != -1) { // First open the fd as an external file descriptor to // initialize the engine properly. QFSFileEngine::open(openMode, fd); // Allow the engine to close the handle even if it's "external". d->closeFileHandle = true; // Restore the file names (open() resets them). d->filePath = QString::fromLocal8Bit(filename); //changed now! d->nativeInitFileName(); delete [] filename; return true; } delete [] filename; setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(errno)); return false;}bool QTemporaryFileEngine::remove(){ Q_D(QFSFileEngine); // Since the QTemporaryFileEngine::close() does not really close the file, // we must explicitly call QFSFileEngine::close() before we remove it. QFSFileEngine::close(); bool removed = QFSFileEngine::remove(); d->filePath.clear(); return removed;}bool QTemporaryFileEngine::close(){ // Don't close the file, just seek to the front. seek(0); setError(QFile::UnspecifiedError, QString()); return true;}//************* QTemporaryFilePrivateclass QTemporaryFilePrivate : public QFilePrivate{ Q_DECLARE_PUBLIC(QTemporaryFile)protected: QTemporaryFilePrivate(); ~QTemporaryFilePrivate(); bool autoRemove; QString templateName; mutable QTemporaryFileEngine *fileEngine;};QTemporaryFilePrivate::QTemporaryFilePrivate() : autoRemove(true), fileEngine(0){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -