diff options
Diffstat (limited to 'src/corelib/io/qfsfileengine_unix.cpp')
-rw-r--r-- | src/corelib/io/qfsfileengine_unix.cpp | 228 |
1 files changed, 134 insertions, 94 deletions
diff --git a/src/corelib/io/qfsfileengine_unix.cpp b/src/corelib/io/qfsfileengine_unix.cpp index e152b035e2..4617d09874 100644 --- a/src/corelib/io/qfsfileengine_unix.cpp +++ b/src/corelib/io/qfsfileengine_unix.cpp @@ -65,46 +65,6 @@ QT_BEGIN_NAMESPACE /*! \internal - Returns the stdlib open string corresponding to a QIODevice::OpenMode. -*/ -static inline QByteArray openModeToFopenMode(QIODevice::OpenMode flags, const QFileSystemEntry &fileEntry, - QFileSystemMetaData &metaData) -{ - QByteArray mode; - if ((flags & QIODevice::ReadOnly) && !(flags & QIODevice::Truncate)) { - mode = "rb"; - if (flags & QIODevice::WriteOnly) { - metaData.clearFlags(QFileSystemMetaData::FileType); - if (!fileEntry.isEmpty() - && QFileSystemEngine::fillMetaData(fileEntry, metaData, QFileSystemMetaData::FileType) - && metaData.isFile()) { - mode += '+'; - } else { - mode = "wb+"; - } - } - } else if (flags & QIODevice::WriteOnly) { - mode = "wb"; - if (flags & QIODevice::ReadOnly) - mode += '+'; - } - if (flags & QIODevice::Append) { - mode = "ab"; - if (flags & QIODevice::ReadOnly) - mode += '+'; - } - -#if defined(__GLIBC__) && (__GLIBC__ * 0x100 + __GLIBC_MINOR__) >= 0x0207 - // must be glibc >= 2.7 - mode += 'e'; -#endif - - return mode; -} - -/*! - \internal - Returns the stdio open flags corresponding to a QIODevice::OpenMode. */ static inline int openModeToOpenFlags(QIODevice::OpenMode mode) @@ -130,17 +90,6 @@ static inline int openModeToOpenFlags(QIODevice::OpenMode mode) return oflags; } -/*! - \internal - - Sets the file descriptor to close on exec. That is, the file - descriptor is not inherited by child processes. -*/ -static inline bool setCloseOnExec(int fd) -{ - return fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) != -1; -} - static inline QString msgOpenDirectory() { const char message[] = QT_TRANSLATE_NOOP("QIODevice", "file to open is a directory"); @@ -151,6 +100,44 @@ static inline QString msgOpenDirectory() #endif } +#if !QT_CONFIG(futimens) && (QT_CONFIG(futimes) || QT_CONFIG(futimesat)) +namespace { +namespace GetFileTimes { + +template <typename T> +static inline typename QtPrivate::QEnableIf<(&T::st_atim, &T::st_mtim, true)>::Type get(const T *p, struct timeval *access, struct timeval *modification) +{ + access->tv_sec = p->st_atim.tv_sec; + access->tv_usec = p->st_atim.tv_nsec / 1000; + + modification->tv_sec = p->st_mtim.tv_sec; + modification->tv_usec = p->st_mtim.tv_nsec / 1000; +} + +template <typename T> +static inline typename QtPrivate::QEnableIf<(&T::st_atimespec, &T::st_mtimespec, true)>::Type get(const T *p, struct timeval *access, struct timeval *modification) +{ + access->tv_sec = p->st_atimespec.tv_sec; + access->tv_usec = p->st_atimespec.tv_nsec / 1000; + + modification->tv_sec = p->st_mtimespec.tv_sec; + modification->tv_usec = p->st_mtimespec.tv_nsec / 1000; +} + +template <typename T> +static inline typename QtPrivate::QEnableIf<(&T::st_atimensec, &T::st_mtimensec, true)>::Type get(const T *p, struct timeval *access, struct timeval *modification) +{ + access->tv_sec = p->st_atime; + access->tv_usec = p->st_atimensec / 1000; + + modification->tv_sec = p->st_mtime; + modification->tv_usec = p->st_mtimensec / 1000; +} + +} +} +#endif + /*! \internal */ @@ -158,6 +145,8 @@ bool QFSFileEnginePrivate::nativeOpen(QIODevice::OpenMode openMode) { Q_Q(QFSFileEngine); + Q_ASSERT_X(openMode & QIODevice::Unbuffered, "QFSFileEngine::open", + "QFSFileEngine no longer supports buffered mode; upper layer must buffer"); if (openMode & QIODevice::Unbuffered) { int flags = openModeToOpenFlags(openMode); @@ -199,49 +188,6 @@ bool QFSFileEnginePrivate::nativeOpen(QIODevice::OpenMode openMode) } fh = 0; - } else { - QByteArray fopenMode = openModeToFopenMode(openMode, fileEntry, metaData); - - // Try to open the file in buffered mode. - do { - fh = QT_FOPEN(fileEntry.nativeFilePath().constData(), fopenMode.constData()); - } while (!fh && errno == EINTR); - - // On failure, return and report the error. - if (!fh) { - q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, - qt_error_string(int(errno))); - return false; - } - - if (!(openMode & QIODevice::WriteOnly)) { - // we don't need this check if we tried to open for writing because then - // we had received EISDIR anyway. - if (QFileSystemEngine::fillMetaData(QT_FILENO(fh), metaData) - && metaData.isDirectory()) { - q->setError(QFile::OpenError, msgOpenDirectory()); - fclose(fh); - return false; - } - } - - setCloseOnExec(fileno(fh)); // ignore failure - - // Seek to the end when in Append mode. - if (openMode & QIODevice::Append) { - int ret; - do { - ret = QT_FSEEK(fh, 0, SEEK_END); - } while (ret == -1 && errno == EINTR); - - if (ret == -1) { - q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, - qt_error_string(int(errno))); - return false; - } - } - - fd = -1; } closeFileHandle = true; @@ -676,6 +622,83 @@ bool QFSFileEngine::setSize(qint64 size) return ret; } +bool QFSFileEngine::setFileTime(const QDateTime &newDate, FileTime time) +{ + Q_D(QFSFileEngine); + + if (d->openMode == QIODevice::NotOpen) { + setError(QFile::PermissionsError, qt_error_string(EACCES)); + return false; + } + + if (!newDate.isValid() || time == QAbstractFileEngine::CreationTime) { + setError(QFile::UnspecifiedError, qt_error_string(EINVAL)); + return false; + } + +#if QT_CONFIG(futimens) + struct timespec ts[2]; + + ts[0].tv_sec = ts[1].tv_sec = 0; + ts[0].tv_nsec = ts[1].tv_nsec = UTIME_OMIT; + + const qint64 msecs = newDate.toMSecsSinceEpoch(); + + if (time == QAbstractFileEngine::AccessTime) { + ts[0].tv_sec = msecs / 1000; + ts[0].tv_nsec = (msecs % 1000) * 1000000; + } else if (time == QAbstractFileEngine::ModificationTime) { + ts[1].tv_sec = msecs / 1000; + ts[1].tv_nsec = (msecs % 1000) * 1000000; + } + + if (futimens(d->nativeHandle(), ts) == -1) { + setError(QFile::PermissionsError, qt_error_string()); + return false; + } + + d->metaData.clearFlags(QFileSystemMetaData::Times); + + return true; +#elif QT_CONFIG(futimes) || QT_CONFIG(futimesat) + struct timeval tv[2]; + QT_STATBUF st; + + if (QT_FSTAT(d->nativeHandle(), &st) == -1) { + setError(QFile::PermissionsError, qt_error_string()); + return false; + } + + GetFileTimes::get(&st, &tv[0], &tv[1]); + + const qint64 msecs = newDate.toMSecsSinceEpoch(); + + if (time == QAbstractFileEngine::AccessTime) { + tv[0].tv_sec = msecs / 1000; + tv[0].tv_usec = (msecs % 1000) * 1000; + } else if (time == QAbstractFileEngine::ModificationTime) { + tv[1].tv_sec = msecs / 1000; + tv[1].tv_usec = (msecs % 1000) * 1000; + } + +#if QT_CONFIG(futimes) + if (futimes(d->nativeHandle(), tv) == -1) { +#else + if (futimesat(d->nativeHandle(), NULL, tv) == -1) { +#endif + setError(QFile::PermissionsError, qt_error_string()); + return false; + } + + d->metaData.clearFlags(QFileSystemMetaData::Times); + + return true; +#else + setError(QFile::UnspecifiedError, qt_error_string(ENOSYS)); + return false; +#endif +} + QDateTime QFSFileEngine::fileTime(FileTime time) const { Q_D(const QFSFileEngine); @@ -793,6 +816,23 @@ bool QFSFileEnginePrivate::unmap(uchar *ptr) #endif } +/*! + \reimp +*/ +bool QFSFileEngine::clone(int sourceHandle) +{ +#if defined(Q_OS_LINUX) + Q_D(QFSFileEngine); +# if !defined FICLONE +# define FICLONE _IOW (0x94, 9, int) +# endif + return ::ioctl(d->fd, FICLONE, sourceHandle) == 0; +#else + Q_UNUSED(sourceHandle); + return false; +#endif +} + QT_END_NAMESPACE #endif // QT_NO_FSFILEENGINE |