📄 qfsfileengine_unix.cpp
字号:
QString QFSFileEngine::tempPath(){ QString temp = QFile::decodeName(qgetenv("TMPDIR")); if (temp.isEmpty()) temp = QString::fromLatin1("/tmp/"); return temp;}QFileInfoList QFSFileEngine::drives(){ QFileInfoList ret; ret.append(rootPath()); return ret;}bool QFSFileEnginePrivate::doStat() const{ if (tried_stat == 0) { QFSFileEnginePrivate *that = const_cast<QFSFileEnginePrivate*>(this); if (fh && nativeFilePath.isEmpty()) { // ### actually covers two cases: d->fh and when the file is not open that->could_stat = (QT_FSTAT(fileno(fh), &st) == 0); } else if (fd == -1) { // ### actually covers two cases: d->fh and when the file is not open that->could_stat = (QT_STAT(nativeFilePath.constData(), &st) == 0); } else { that->could_stat = (QT_FSTAT(fd, &st) == 0); } that->tried_stat = 1; } return could_stat;}bool QFSFileEnginePrivate::isSymlink() const{ if (need_lstat) { QFSFileEnginePrivate *that = const_cast<QFSFileEnginePrivate *>(this); that->need_lstat = false; QT_STATBUF st; // don't clobber our main one that->is_link = (QT_LSTAT(nativeFilePath.constData(), &st) == 0) ? S_ISLNK(st.st_mode) : false; } return is_link;}#if !defined(QWS) && defined(Q_OS_MAC)static bool _q_isMacHidden(const QString &path){ OSErr err = noErr; FSRef fsRef;#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4) if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_4) { err = FSPathMakeRefWithOptions(reinterpret_cast<const UInt8 *>(QFile::encodeName(path).constData()), kFSPathMakeRefDoNotFollowLeafSymlink, &fsRef, 0); } else#endif { QFileInfo fi(path); FSRef parentRef; err = FSPathMakeRef(reinterpret_cast<const UInt8 *>(fi.absoluteDir().canonicalPath().toUtf8().constData()), &parentRef, 0); if (err == noErr) { QString fileName = fi.fileName(); err = FSMakeFSRefUnicode(&parentRef, fileName.length(), reinterpret_cast<const UniChar *>(fileName.unicode()), kTextEncodingUnknown, &fsRef); } } if (err != noErr) return false; FSCatalogInfo catInfo; err = FSGetCatalogInfo(&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL); if (err != noErr) return false; FileInfo * const fileInfo = reinterpret_cast<FileInfo*>(&catInfo.finderInfo); bool result = (fileInfo->finderFlags & kIsInvisible); return result;}#endif/*! \reimp*/QAbstractFileEngine::FileFlags QFSFileEngine::fileFlags(FileFlags type) const{ Q_D(const QFSFileEngine); // Force a stat, so that we're guaranteed to get up-to-date results if (type & QAbstractFileEngine::FileFlag(QAbstractFileEngine::Refresh)) { d->tried_stat = 0; d->need_lstat = 1; } QAbstractFileEngine::FileFlags ret = 0; bool exists = d->doStat(); if (!exists && !d->isSymlink()) return ret; if (exists && (type & PermsMask)) { if (d->st.st_mode & S_IRUSR) ret |= ReadOwnerPerm; if (d->st.st_mode & S_IWUSR) ret |= WriteOwnerPerm; if (d->st.st_mode & S_IXUSR) ret |= ExeOwnerPerm; if (d->st.st_mode & S_IRUSR) ret |= ReadUserPerm; if (d->st.st_mode & S_IWUSR) ret |= WriteUserPerm; if (d->st.st_mode & S_IXUSR) ret |= ExeUserPerm; if (d->st.st_mode & S_IRGRP) ret |= ReadGroupPerm; if (d->st.st_mode & S_IWGRP) ret |= WriteGroupPerm; if (d->st.st_mode & S_IXGRP) ret |= ExeGroupPerm; if (d->st.st_mode & S_IROTH) ret |= ReadOtherPerm; if (d->st.st_mode & S_IWOTH) ret |= WriteOtherPerm; if (d->st.st_mode & S_IXOTH) ret |= ExeOtherPerm; } if (type & TypesMask) {#if !defined(QWS) && defined(Q_OS_MAC) bool foundAlias = false; { FSRef fref; if (FSPathMakeRef((const UInt8 *)QFile::encodeName(QDir::cleanPath(d->filePath)).data(), &fref, NULL) == noErr) { Boolean isAlias, isFolder; if (FSIsAliasFile(&fref, &isAlias, &isFolder) == noErr && isAlias) { foundAlias = true; ret |= LinkType; } } } if (!foundAlias)#endif { if ((type & LinkType) && d->isSymlink()) ret |= LinkType; if (exists && (d->st.st_mode & S_IFMT) == S_IFREG) ret |= FileType; else if (exists && (d->st.st_mode & S_IFMT) == S_IFDIR) ret |= DirectoryType;#if !defined(QWS) && defined(Q_OS_MAC) if((ret & DirectoryType) && (type & BundleType)) { QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, QCFString(d->filePath), kCFURLPOSIXPathStyle, true); UInt32 type, creator; if(CFBundleGetPackageInfoInDirectory(url, &type, &creator)) ret |= BundleType; }#endif } } if (type & FlagsMask) { ret |= LocalDiskFlag; if (exists) ret |= ExistsFlag; if (fileName(BaseName)[0] == QLatin1Char('.')#if !defined(QWS) && defined(Q_OS_MAC) || _q_isMacHidden(d->filePath)#endif ) ret |= HiddenFlag; if (d->filePath == QLatin1String("/")) ret |= RootFlag; } return ret;}QString QFSFileEngine::fileName(FileName file) const{ Q_D(const QFSFileEngine); if (file == BundleName) {#if !defined(QWS) && defined(Q_OS_MAC) QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, QCFString(d->filePath), kCFURLPOSIXPathStyle, true); if(CFDictionaryRef dict = CFBundleCopyInfoDictionaryForURL(url)) { if(CFTypeRef name = (CFTypeRef)CFDictionaryGetValue(dict, kCFBundleNameKey)) { if(CFGetTypeID(name) == CFStringGetTypeID()) return QCFString::toQString((CFStringRef)name); } }#endif return QString(); } else if (file == BaseName) { int slash = d->filePath.lastIndexOf(QLatin1Char('/')); if (slash != -1) return d->filePath.mid(slash + 1); } else if (file == PathName) { int slash = d->filePath.lastIndexOf(QLatin1Char('/')); if (slash == -1) return QLatin1String("."); else if (!slash) return QLatin1String("/"); return d->filePath.left(slash); } else if (file == AbsoluteName || file == AbsolutePathName) { QString ret; if (d->filePath.isEmpty() || !d->filePath.startsWith(QLatin1Char('/'))) ret = QDir::currentPath(); if (!d->filePath.isEmpty() && d->filePath != QLatin1String(".")) { if (!ret.isEmpty() && !ret.endsWith(QLatin1Char('/'))) ret += QLatin1Char('/'); ret += d->filePath; } if (ret == QLatin1String("/")) return ret; bool isDir = ret.endsWith(QLatin1Char('/')); ret = QDir::cleanPath(ret); if (isDir) ret += QLatin1String("/"); if (file == AbsolutePathName) { int slash = ret.lastIndexOf(QLatin1Char('/')); if (slash == -1) return QDir::currentPath(); else if (!slash) return QLatin1String("/"); return ret.left(slash); } return ret; } else if (file == CanonicalName || file == CanonicalPathName) {#if defined(__GLIBC__) && !defined(PATH_MAX) char *cur = ::get_current_dir_name(); if (cur) { QString ret; char *tmp = ::canonicalize_file_name(d->nativeFilePath.constData()); if (tmp) { ret = QFile::decodeName(tmp); ::free(tmp); } ::chdir(cur); // always make sure we go back to the current dir ::free(cur);#else char cur[PATH_MAX+1]; if (::getcwd(cur, PATH_MAX)) { QString ret;# ifndef Q_OS_INTEGRITY // INTEGRITY has no realpath char real[PATH_MAX+1]; // need the cast for old solaris versions of realpath that doesn't take // a const char*. if (::realpath(d->nativeFilePath.constData(), real)) ret = QFile::decodeName(QByteArray(real));# endif ::chdir(cur); // always make sure we go back to the current dir#endif //check it QT_STATBUF st; if (QT_STAT(QFile::encodeName(ret), &st) != 0) ret = QString(); if (!ret.isEmpty() && file == CanonicalPathName) { int slash = ret.lastIndexOf(QLatin1Char('/')); if (slash == -1) return QDir::currentPath(); else if (!slash) return QLatin1String("/"); return ret.left(slash); } return ret; } if (file == CanonicalPathName) return fileName(AbsolutePathName); return fileName(AbsoluteName); } else if (file == LinkName) { if (d->isSymlink()) {#if defined(__GLIBC__) && !defined(PATH_MAX)#define PATH_CHUNK_SIZE 256 char *s = 0; int len = -1; int size = PATH_CHUNK_SIZE; while (1) { s = (char *) ::realloc(s, size); if (s == 0) { len = -1; break; } len = ::readlink(d->nativeFilePath.constData(), s, size); if (len < 0) { ::free(s); break; } if (len < size) { break; } size *= 2; }#else char s[PATH_MAX+1]; int len = readlink(d->nativeFilePath.constData(), s, PATH_MAX);#endif if (len > 0) { QString ret; if (S_ISDIR(d->st.st_mode) && s[0] != '/') { QDir parent(d->filePath); parent.cdUp(); ret = parent.path(); if (!ret.isEmpty() && !ret.endsWith(QLatin1Char('/'))) ret += QLatin1Char('/'); } s[len] = '\0'; ret += QFile::decodeName(QByteArray(s));#if defined(__GLIBC__) && !defined(PATH_MAX) ::free(s);#endif if (!ret.startsWith(QLatin1Char('/'))) { if (d->filePath.startsWith(QLatin1Char('/'))) { ret.prepend(d->filePath.left(d->filePath.lastIndexOf(QLatin1Char('/'))) + QLatin1Char('/')); } else { ret.prepend(QDir::currentPath() + QLatin1Char('/')); } } ret = QDir::cleanPath(ret); if (ret.size() > 1 && ret.endsWith(QLatin1Char('/'))) ret.chop(1); return ret; } }#if !defined(QWS) && defined(Q_OS_MAC) { FSRef fref; if (FSPathMakeRef((const UInt8 *)QFile::encodeName(QDir::cleanPath(d->filePath)).data(), &fref, 0) == noErr) { Boolean isAlias, isFolder; if (FSResolveAliasFile(&fref, true, &isFolder, &isAlias) == noErr && isAlias) { AliasHandle alias; if (FSNewAlias(0, &fref, &alias) == noErr && alias) { CFStringRef cfstr; if (FSCopyAliasInfo(alias, 0, 0, &cfstr, 0, 0) == noErr) return QCFString::toQString(cfstr); } } } }#endif return QString(); } return d->filePath;}bool QFSFileEngine::isRelativePath() const{ Q_D(const QFSFileEngine); int len = d->filePath.length(); if (len == 0) return true; return d->filePath[0] != QLatin1Char('/');}uint QFSFileEngine::ownerId(FileOwner own) const{ Q_D(const QFSFileEngine); static const uint nobodyID = (uint) -2; if (d->doStat()) { if (own == OwnerUser) return d->st.st_uid; else return d->st.st_gid; } return nobodyID;}QString QFSFileEngine::owner(FileOwner own) const{#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) QVarLengthArray<char, 1024> buf(sysconf(_SC_GETPW_R_SIZE_MAX));#endif if (own == OwnerUser) { struct passwd *pw = 0;#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) struct passwd entry; getpwuid_r(ownerId(own), &entry, buf.data(), buf.size(), &pw);#else pw = getpwuid(ownerId(own));#endif if (pw) return QFile::decodeName(QByteArray(pw->pw_name)); } else if (own == OwnerGroup) { struct group *gr = 0;#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) buf.resize(sysconf(_SC_GETGR_R_SIZE_MAX)); struct group entry; getgrgid_r(ownerId(own), &entry, buf.data(), buf.size(), &gr);#else gr = getgrgid(ownerId(own));#endif if (gr) return QFile::decodeName(QByteArray(gr->gr_name)); } return QString();}bool QFSFileEngine::setPermissions(uint perms){ Q_D(QFSFileEngine); mode_t mode = 0; if (perms & ReadOwnerPerm) mode |= S_IRUSR; if (perms & WriteOwnerPerm) mode |= S_IWUSR; if (perms & ExeOwnerPerm) mode |= S_IXUSR; if (perms & ReadUserPerm) mode |= S_IRUSR; if (perms & WriteUserPerm) mode |= S_IWUSR; if (perms & ExeUserPerm) mode |= S_IXUSR; if (perms & ReadGroupPerm) mode |= S_IRGRP; if (perms & WriteGroupPerm) mode |= S_IWGRP; if (perms & ExeGroupPerm) mode |= S_IXGRP; if (perms & ReadOtherPerm) mode |= S_IROTH; if (perms & WriteOtherPerm) mode |= S_IWOTH; if (perms & ExeOtherPerm) mode |= S_IXOTH; if (d->fd != -1) return !fchmod(d->fd, mode); return !::chmod(d->nativeFilePath.constData(), mode);}bool QFSFileEngine::setSize(qint64 size){ Q_D(QFSFileEngine); if (d->fd != -1) return !QT_FTRUNCATE(d->fd, size); return !QT_TRUNCATE(d->nativeFilePath.constData(), size);}QDateTime QFSFileEngine::fileTime(FileTime time) const{ Q_D(const QFSFileEngine); QDateTime ret; if (d->doStat()) { if (time == CreationTime) ret.setTime_t(d->st.st_ctime ? d->st.st_ctime : d->st.st_mtime); else if (time == ModificationTime) ret.setTime_t(d->st.st_mtime); else if (time == AccessTime) ret.setTime_t(d->st.st_atime); } return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -