7#include "qplatformdefs.h"
9#include "private/qtemporaryfile_p.h"
10#include "private/qfile_p.h"
11#include "private/qsystemerror_p.h"
14#include "private/qcore_unix_p.h"
18#if defined(QT_BUILD_CORE_LIB)
21#define tr(X) QString::fromLatin1(X)
54 if (qfilename[phPos] == u
'X') {
60 || qfilename[phPos] == u
'/') {
70 qfilename.
append(
".XXXXXX"_L1);
78 phPos = filename.
size();
116 Char *
const placeholderEnd = placeholderStart +
length;
130 enum { BitsPerCharacter = 10 };
132 Char *rIter = placeholderEnd;
133 while (rIter != placeholderStart) {
135 auto applyOne = [&]() {
136 quint32 v = rnd & ((1 << BitsPerCharacter) - 1);
137 rnd >>= BitsPerCharacter;
138 char ch = char((26 + 26) *
v / (1 << BitsPerCharacter));
146 if (rIter == placeholderStart)
150 if (rIter == placeholderStart)
160#ifndef QT_NO_TEMPORARYFILE
178 const int maxAttempts = 16;
179 for (
int attempt = 0; attempt < maxAttempts; ++attempt) {
186 ? 0u : (FILE_SHARE_READ | FILE_SHARE_WRITE);
188 file = CreateFile((
const wchar_t *)
path.constData(),
189 GENERIC_READ | GENERIC_WRITE,
190 shareMode, NULL, CREATE_NEW,
191 FILE_ATTRIBUTE_NORMAL, NULL);
193 if (
file != INVALID_HANDLE_VALUE)
196 DWORD err = GetLastError();
197 if (err == ERROR_ACCESS_DENIED) {
198 WIN32_FILE_ATTRIBUTE_DATA attributes;
199 if (!GetFileAttributesEx((
const wchar_t *)
path.constData(),
200 GetFileExInfoStandard, &attributes)
206 }
else if (err != ERROR_FILE_EXISTS) {
213 QT_OPEN_CREAT | QT_OPEN_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE,
214 static_cast<mode_t
>(
mode));
239#ifdef LINUX_UNNAMED_TMPFILE
247 if (lastSlash >= 0) {
250 tfn.
path[lastSlash] =
'\0';
254 file =
QT_OPEN(
p, O_TMPFILE | QT_OPEN_RDWR | QT_OPEN_LARGEFILE,
255 static_cast<mode_t
>(
mode));
259 if (errno == EOPNOTSUPP || errno == EISDIR) {
263 tfn.
path[lastSlash] =
'/';
291 if (!((
nullptr ==
d->fh) && (-1 ==
d->fd)
293 && (INVALID_HANDLE_VALUE ==
d->fileHandle)
310 std::optional<QFile::Permissions> permissions)
332 d->fileEntry.clear();
343#if !defined(Q_OS_WIN)
344 d->closeFileHandle =
true;
347 d->openMode = openMode;
348 d->lastFlushFailed =
false;
364 d->fileEntry.clear();
424#ifdef LINUX_UNNAMED_TMPFILE
428 return ::linkat(AT_FDCWD,
src, AT_FDCWD,
dst.nativeFilePath(), AT_SYMLINK_FOLLOW) == 0;
437 d_func()->fileEntry =
entry;
441 auto materializeAsTemplate = [=](
const QString &newName) {
443 static const int maxAttempts = 16;
444 for (
int attempt = 0; attempt < maxAttempts; ++attempt) {
447 if (materializeAt(
entry))
448 return success(
entry);
454 if (materializeAsTemplate(newName))
459 if (materializeAt(
dst))
480#ifdef LINUX_UNNAMED_TMPFILE
482 Q_ASSERT(d_func()->fileEntry.isEmpty());
498 : templateName(templateNameIn)
529#ifdef LINUX_UNNAMED_TMPFILE
541#if defined(QT_BUILD_CORE_LIB)
545 baseName =
"qt_temp"_L1;
704 if (!
d->fileName.isEmpty() &&
d->autoRemove)
734 return d->autoRemove;
774 if (tef && tef->isReallyOpen())
777 if (
d->fileName.isEmpty())
791 return d->templateName;
812 d->templateName =
name;
837 if (!tef || !tef->isReallyOpen() || !tef->filePathWasTemplate)
843 if (tef->rename(newName)) {
846 tef->setFileName(newName);
847 d->fileName = newName;
928 if (tef && tef->isReallyOpen()) {
938 d->resetFileEngine();
942 if (tef->isUnnamedFile())
956#include "moc_qtemporaryfile.cpp"
\inmodule QtCore \reentrant
void setError(QFile::FileError error, const QString &str)
Sets the error type to error, and the error string to errorString.
FileName
These values are used to request a file name in a particular format.
QFile::FileError error() const
Returns the QFile::FileError that resulted from the last failed operation.
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
qsizetype lastIndexOf(char c, qsizetype from=-1) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
QString applicationName
the name of this application
static QString fromNativeSeparators(const QString &pathName)
static QString tempPath()
Returns the absolute canonical path of the system's temporary directory.
QString fileName(FileName file) const override
\reimp
bool rename(const QString &newName) override
\reimp
bool renameOverwrite(const QString &newName) override
\reimp
bool close() override
\reimp
bool open(QIODevice::OpenMode openMode, std::optional< QFile::Permissions > permissions) override
\reimp
void setFileName(const QString &file) override
\reimp
bool seek(qint64) override
\reimp
bool remove() override
\reimp
std::unique_ptr< QAbstractFileEngine > fileEngine
qint64 pos() const override
\reimp
bool seek(qint64 offset) override
For random-access devices, this function sets the current position to pos, returning true on success,...
void unsetError()
Sets the file's error to QFileDevice::NoError.
FileError error() const
Returns the file error status.
void close() override
Calls QFileDevice::flush() and closes the file.
static QFileSystemEntry absoluteName(const QFileSystemEntry &entry)
Q_AUTOTEST_EXPORT NativePath nativeFilePath() const
bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
bool remove()
Removes the file specified by fileName().
bool rename(const QString &newName)
Renames the file currently specified by fileName() to newName.
void setOpenMode(QIODeviceBase::OpenMode openMode)
Sets the OpenMode of the device to openMode.
bool isOpen() const
Returns true if the device is open; otherwise returns false.
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read.
quint32 generate()
Generates a 32-bit random quantity and returns it.
static Q_DECL_CONST_FUNCTION QRandomGenerator * global()
\threadsafe
\macro QT_RESTRICTED_CAST_FROM_ASCII
qsizetype size() const
Returns the number of characters in this string.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
QString & append(QChar c)
const QString & templateName
bool rename(const QString &newName) override
\reimp
bool remove() override
\reimp
QString fileName(FileName file) const override
\reimp
bool close() override
\reimp
bool open(QIODevice::OpenMode flags, std::optional< QFile::Permissions > permissions) override
\reimp
bool renameOverwrite(const QString &newName) override
\reimp
bool isReallyOpen() const
bool materializeUnnamedFile(const QString &newName, MaterializationMode mode)
bool isUnnamedFile() const override final
void initialize(const QString &file, quint32 mode, bool nameIsTemplate=true)
void setFileName(const QString &file) override
\reimp
void resetFileEngine() const
void materializeUnnamedFile()
static QString defaultTemplateName()
QAbstractFileEngine * engine() const override
\inmodule QtCore \reentrant
~QTemporaryFile()
Destroys the temporary file object, the file is automatically closed if necessary and if in auto remo...
QTemporaryFile()
Constructs a QTemporaryFile using as file template the application name returned by QCoreApplication:...
QString fileName() const override
Returns the complete unique filename backing the QTemporaryFile object.
static QTemporaryFile * createNativeFile(const QString &fileName)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setFileTemplate(const QString &name)
Sets the static portion of the file name to name.
bool open()
A QTemporaryFile will always be opened in QIODevice::ReadWrite mode, this allows easy access to the d...
QString fileTemplate() const
Returns the set file template.
bool autoRemove() const
Returns true if the QTemporaryFile is in auto remove mode.
bool rename(const QString &newName)
Renames the current temporary file to newName and returns true if it succeeded.
void setAutoRemove(bool b)
Sets the QTemporaryFile into auto-remove mode if b is true.
Combined button and popup list for selecting options.
bool qt_haveLinuxProcfs()
DBusConnection const char DBusError * error
#define INVALID_FILE_ATTRIBUTES
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLenum GLuint GLenum GLsizei length
GLsizei const GLchar *const * path
static CreateUnnamedFileStatus createUnnamedFile(NativeFileHandle &file, QTemporaryFileName &tfn, quint32 mode, QSystemError *error)
static bool createFileFromTemplate(NativeFileHandle &file, QTemporaryFileName &templ, quint32 mode, int flags, QSystemError &error)
char * toString(const MyType &t)
[31]
QTemporaryFileName(const QString &templateName)
QFileSystemEntry::NativePath generateNext()
QFileSystemEntry::NativePath path
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent