QFileSystemEntry QFileSystemEngine::currentPath() { QFileSystemEntry result; QT_STATBUF st; if (QT_STAT(".", &st) == 0) { #if defined(__GLIBC__) && !defined(PATH_MAX) char *currentName = ::get_current_dir_name(); if (currentName) { result = QFileSystemEntry(QByteArray(currentName), QFileSystemEntry::FromNativePath()); ::free(currentName); } #else char currentName[PATH_MAX+1]; if (::getcwd(currentName, PATH_MAX)) { #if defined(Q_OS_VXWORKS) && defined(VXWORKS_VXSIM) QByteArray dir(currentName); if (dir.indexOf(':') < dir.indexOf('/')) dir.remove(0, dir.indexOf(':')+1); qstrncpy(currentName, dir.constData(), PATH_MAX); #endif result = QFileSystemEntry(QByteArray(currentName), QFileSystemEntry::FromNativePath()); } # if defined(QT_DEBUG) if (result.isEmpty()) qWarning("QFileSystemEngine::currentPath: getcwd() failed"); # endif #endif } else { # if defined(QT_DEBUG) qWarning("QFileSystemEngine::currentPath: stat(\".\") failed"); # endif } return result; }
QString QFSFileEngine::fileName(FileName file) const { Q_D(const QFSFileEngine); if (file == BundleName) { return QFileSystemEngine::bundleName(d->fileEntry); } else if (file == BaseName) { return d->fileEntry.fileName(); } else if (file == PathName) { return d->fileEntry.path(); } else if (file == AbsoluteName || file == AbsolutePathName) { QFileSystemEntry entry(QFileSystemEngine::absoluteName(d->fileEntry)); if (file == AbsolutePathName) { return entry.path(); } return entry.filePath(); } else if (file == CanonicalName || file == CanonicalPathName) { QFileSystemEntry entry(QFileSystemEngine::canonicalName(d->fileEntry, d->metaData)); if (file == CanonicalPathName) return entry.path(); return entry.filePath(); } else if (file == LinkName) { if (d->isSymlink()) { QFileSystemEntry entry = QFileSystemEngine::getLinkTarget(d->fileEntry, d->metaData); return entry.filePath(); } return QString(); } return d->fileEntry.filePath(); }
QFileSystemEntry QFileSystemEngine::currentPath() { QFileSystemEntry result; QT_STATBUF st; if (QT_STAT(".", &st) == 0) { #if defined(__GLIBC__) && !defined(PATH_MAX) char *currentName = ::get_current_dir_name(); if (currentName) { result = QFileSystemEntry(QByteArray(currentName), QFileSystemEntry::FromNativePath()); ::free(currentName); } #else char currentName[PATH_MAX+1]; if (::getcwd(currentName, PATH_MAX)) result = QFileSystemEntry(QByteArray(currentName), QFileSystemEntry::FromNativePath()); # if defined(QT_DEBUG) if (result.isEmpty()) qWarning("QFSFileEngine::currentPath: getcwd() failed"); # endif #endif } else { # if defined(QT_DEBUG) qWarning("QFSFileEngine::currentPath: stat(\".\") failed"); # endif } return result; }
//static QFileSystemEntry QFileSystemEngine::absoluteName(const QFileSystemEntry &entry) { if (entry.isAbsolute() && entry.isClean()) return entry; QByteArray orig = entry.nativeFilePath(); QByteArray result; if (orig.isEmpty() || !orig.startsWith('/')) { QFileSystemEntry cur(currentPath()); result = cur.nativeFilePath(); } if (!orig.isEmpty() && !(orig.length() == 1 && orig[0] == '.')) { if (!result.isEmpty() && !result.endsWith('/')) result.append('/'); result.append(orig); } if (result.length() == 1 && result[0] == '/') return QFileSystemEntry(result, QFileSystemEntry::FromNativePath()); const bool isDir = result.endsWith('/'); /* as long as QDir::cleanPath() operates on a QString we have to convert to a string here. * ideally we never convert to a string since that loses information. Please fix after * we get a QByteArray version of QDir::cleanPath() */ QFileSystemEntry resultingEntry(result, QFileSystemEntry::FromNativePath()); QString stringVersion = QDir::cleanPath(resultingEntry.filePath()); if (isDir) stringVersion.append(QLatin1Char('/')); return QFileSystemEntry(stringVersion); }
//static bool QFileSystemEngine::renameFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error) { if (::rename(source.nativeFilePath().constData(), target.nativeFilePath().constData()) == 0) return true; error = QSystemError(errno, QSystemError::StandardLibraryError); return false; }
static bool isPackage(const QFileSystemMetaData &data, const QFileSystemEntry &entry) { if (!data.isDirectory()) return false; QFileInfo info(entry.filePath()); QString suffix = info.suffix(); if (suffix.length() > 0) { // First step: is the extension known ? CFStringRef extensionRef = QCFString::toCFStringRef(suffix); CFStringRef uniformTypeIdentifier = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extensionRef, NULL); if (UTTypeConformsTo(uniformTypeIdentifier, kUTTypeBundle)) return true; // Second step: check if an application knows the package type CFStringRef path = QCFString::toCFStringRef(entry.filePath()); QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, path, kCFURLPOSIXPathStyle, true); UInt32 type, creator; // Well created packages have the PkgInfo file if (CFBundleGetPackageInfoInDirectory(url, &type, &creator)) return true; // Find if an application other than Finder claims to know how to handle the package QCFType<CFURLRef> application; LSGetApplicationForURL(url, kLSRolesEditor|kLSRolesViewer|kLSRolesViewer, NULL, &application); if (application) { QCFType<CFBundleRef> bundle = CFBundleCreate(kCFAllocatorDefault, application); CFStringRef identifier = CFBundleGetIdentifier(bundle); QString applicationId = QCFString::toQString(identifier); if (applicationId != QLatin1String("com.apple.finder")) return true; } } // Third step: check if the directory has the package bit set FSRef packageRef; FSPathMakeRef((UInt8 *)entry.nativeFilePath().constData(), &packageRef, NULL); FSCatalogInfo catalogInfo; FSGetCatalogInfo(&packageRef, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL); FolderInfo *folderInfo = reinterpret_cast<FolderInfo *>(catalogInfo.finderInfo); return folderInfo->finderFlags & kHasBundle; }
QT_BEGIN_NAMESPACE QString QFileInfoPrivate::getFileName(QAbstractFileEngine::FileName name) const { if (cache_enabled && !fileNames[(int)name].isNull()) return fileNames[(int)name]; QString ret; if (fileEngine == 0) { // local file; use the QFileSystemEngine directly switch (name) { case QAbstractFileEngine::CanonicalName: case QAbstractFileEngine::CanonicalPathName: { QFileSystemEntry entry = QFileSystemEngine::canonicalName(fileEntry, metaData); if (cache_enabled) { // be smart and store both fileNames[QAbstractFileEngine::CanonicalName] = entry.filePath(); fileNames[QAbstractFileEngine::CanonicalPathName] = entry.path(); } if (name == QAbstractFileEngine::CanonicalName) ret = entry.filePath(); else ret = entry.path(); break; } case QAbstractFileEngine::LinkName: ret = QFileSystemEngine::getLinkTarget(fileEntry, metaData).filePath(); break; case QAbstractFileEngine::BundleName: ret = QFileSystemEngine::bundleName(fileEntry); break; case QAbstractFileEngine::AbsoluteName: case QAbstractFileEngine::AbsolutePathName: { QFileSystemEntry entry = QFileSystemEngine::absoluteName(fileEntry); if (cache_enabled) { // be smart and store both fileNames[QAbstractFileEngine::AbsoluteName] = entry.filePath(); fileNames[QAbstractFileEngine::AbsolutePathName] = entry.path(); } if (name == QAbstractFileEngine::AbsoluteName) ret = entry.filePath(); else ret = entry.path(); break; } default: break; } } else { ret = fileEngine->fileName(name); } if (ret.isNull()) ret = QLatin1String(""); if (cache_enabled) fileNames[(int)name] = ret; return ret; }
//static QByteArray QFileSystemEngine::id(const QFileSystemEntry &entry) { struct stat statResult; if (stat(entry.nativeFilePath().constData(), &statResult)) { qErrnoWarning("stat() failed for '%s'", entry.nativeFilePath().constData()); return QByteArray(); } QByteArray result = QByteArray::number(quint64(statResult.st_dev), 16); result += ':'; result += QByteArray::number(quint64(statResult.st_ino)); return result; }
//static QFileSystemEntry QFileSystemEngine::canonicalName(const QFileSystemEntry &entry, QFileSystemMetaData &data) { if (entry.isEmpty() || entry.isRoot()) return entry; QFileSystemEntry result = absoluteName(entry); if (!data.hasFlags(QFileSystemMetaData::ExistsAttribute)) fillMetaData(result, data, QFileSystemMetaData::ExistsAttribute); if (!data.exists()) { // file doesn't exist return QFileSystemEntry(); } else { return result; } }
QT_BEGIN_NAMESPACE #if defined(Q_OS_DARWIN) static inline bool hasResourcePropertyFlag(const QFileSystemMetaData &data, const QFileSystemEntry &entry, CFStringRef key) { QCFString path = CFStringCreateWithFileSystemRepresentation(0, entry.nativeFilePath().constData()); if (!path) return false; QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, path, kCFURLPOSIXPathStyle, data.hasFlags(QFileSystemMetaData::DirectoryType)); if (!url) return false; CFBooleanRef value; if (CFURLCopyResourcePropertyForKey(url, key, &value, NULL)) { if (value == kCFBooleanTrue) return true; } return false; }
//static bool QFileSystemEngine::setPermissions(const QFileSystemEntry &entry, QFile::Permissions permissions, QSystemError &error, QFileSystemMetaData *data) { mode_t mode = 0; if (permissions & (QFile::ReadOwner | QFile::ReadUser)) mode |= S_IRUSR; if (permissions & (QFile::WriteOwner | QFile::WriteUser)) mode |= S_IWUSR; if (permissions & (QFile::ExeOwner | QFile::ExeUser)) mode |= S_IXUSR; if (permissions & QFile::ReadGroup) mode |= S_IRGRP; if (permissions & QFile::WriteGroup) mode |= S_IWGRP; if (permissions & QFile::ExeGroup) mode |= S_IXGRP; if (permissions & QFile::ReadOther) mode |= S_IROTH; if (permissions & QFile::WriteOther) mode |= S_IWOTH; if (permissions & QFile::ExeOther) mode |= S_IXOTH; bool success = ::chmod(entry.nativeFilePath().constData(), mode) == 0; if (success && data) { data->entryFlags &= ~QFileSystemMetaData::Permissions; data->entryFlags |= QFileSystemMetaData::MetaDataFlag(uint(permissions)); data->knownFlagsMask |= QFileSystemMetaData::Permissions; } if (!success) error = QSystemError(errno, QSystemError::StandardLibraryError); return success; }
//static bool QFileSystemEngine::createDirectory(const QFileSystemEntry &entry, bool createParents) { QString dirName = entry.filePath(); if (createParents) { dirName = QDir::cleanPath(dirName); for (int oldslash = -1, slash=0; slash != -1; oldslash = slash) { slash = dirName.indexOf(QDir::separator(), oldslash+1); if (slash == -1) { if (oldslash == dirName.length()) break; slash = dirName.length(); } if (slash) { QByteArray chunk = QFile::encodeName(dirName.left(slash)); QT_STATBUF st; if (QT_STAT(chunk, &st) != -1) { if ((st.st_mode & S_IFMT) != S_IFDIR) return false; } else if (QT_MKDIR(chunk, 0777) != 0) { return false; } } } return true; } #if defined(Q_OS_DARWIN) // Mac X doesn't support trailing /'s if (dirName.endsWith(QLatin1Char('/'))) dirName.chop(1); #endif return (QT_MKDIR(QFile::encodeName(dirName), 0777) == 0); }
/*! \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; }
//static QFileSystemEntry QFileSystemEngine::canonicalName(const QFileSystemEntry &entry, QFileSystemMetaData &data) { if (entry.isEmpty() || entry.isRoot()) return entry; #if !defined(Q_OS_MAC) && _POSIX_VERSION < 200809L // realpath(X,0) is not supported Q_UNUSED(data); return QFileSystemEntry(slowCanonicalized(absoluteName(entry).filePath())); #else char *ret = 0; # if defined(Q_OS_MAC) # if !defined(QT_NO_CORESERVICES) // Mac OS X 10.5.x doesn't support the realpath(X,0) extension we use here. if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_6) { ret = realpath(entry.nativeFilePath().constData(), (char*)0); } else { // on 10.5 we can use FSRef to resolve the file path. QString path = QDir::cleanPath(entry.filePath()); FSRef fsref; if (FSPathMakeRef((const UInt8 *)path.toUtf8().data(), &fsref, 0) == noErr) { CFURLRef urlref = CFURLCreateFromFSRef(NULL, &fsref); CFStringRef canonicalPath = CFURLCopyFileSystemPath(urlref, kCFURLPOSIXPathStyle); QString ret = QCFString::toQString(canonicalPath); CFRelease(canonicalPath); CFRelease(urlref); return QFileSystemEntry(ret); } } # else ret = (char*)malloc(PATH_MAX); realpath(entry.nativeFilePath().constData(), (char*)ret); # endif //!defined(QT_NO_CORESERVICES) # else #ifdef Q_OS_ANDROID ret = (char*)malloc(PATH_MAX); memset(ret, 0, PATH_MAX); ret = realpath(entry.nativeFilePath().constData(), ret); #else ret = realpath(entry.nativeFilePath().constData(), (char*)0); #endif ret = realpath(entry.nativeFilePath().constData(), (char*)0); # endif //defined(Q_OS_MAC) if (ret) { data.knownFlagsMask |= QFileSystemMetaData::ExistsAttribute; data.entryFlags |= QFileSystemMetaData::ExistsAttribute; QString canonicalPath = QDir::cleanPath(QString::fromLocal8Bit(ret)); free(ret); return QFileSystemEntry(canonicalPath); } else if (errno == ENOENT) { // file doesn't exist data.knownFlagsMask |= QFileSystemMetaData::ExistsAttribute; data.entryFlags &= ~(QFileSystemMetaData::ExistsAttribute); return QFileSystemEntry(); } return entry; #endif }
//static bool QFileSystemEngine::removeFile(const QFileSystemEntry &entry, QSystemError &error) { if (unlink(entry.nativeFilePath().constData()) == 0) return true; error = QSystemError(errno, QSystemError::StandardLibraryError); return false; }
static bool isPackage(const QFileSystemMetaData &data, const QFileSystemEntry &entry) { if (!data.isDirectory()) return false; QFileInfo info(entry.filePath()); QString suffix = info.suffix(); if (suffix.length() > 0) { // First step: is the extension known ? QCFType<CFStringRef> extensionRef = QCFString::toCFStringRef(suffix); QCFType<CFStringRef> uniformTypeIdentifier = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extensionRef, NULL); if (UTTypeConformsTo(uniformTypeIdentifier, kUTTypeBundle)) return true; // Second step: check if an application knows the package type QCFType<CFStringRef> path = QCFString::toCFStringRef(entry.filePath()); QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, path, kCFURLPOSIXPathStyle, true); UInt32 type, creator; // Well created packages have the PkgInfo file if (CFBundleGetPackageInfoInDirectory(url, &type, &creator)) return true; #ifdef Q_OS_OSX // Find if an application other than Finder claims to know how to handle the package QCFType<CFURLRef> application; LSGetApplicationForURL(url, kLSRolesEditor|kLSRolesViewer|kLSRolesViewer, NULL, &application); if (application) { QCFType<CFBundleRef> bundle = CFBundleCreate(kCFAllocatorDefault, application); CFStringRef identifier = CFBundleGetIdentifier(bundle); QString applicationId = QCFString::toQString(identifier); if (applicationId != QLatin1String("com.apple.finder")) return true; } #endif } // Third step: check if the directory has the package bit set return hasResourcePropertyFlag(data, entry, kCFURLIsPackageKey); }
//static bool QFileSystemEngine::removeDirectory(const QFileSystemEntry &entry, bool removeEmptyParents) { if (removeEmptyParents) { QString dirName = QDir::cleanPath(entry.filePath()); for (int oldslash = 0, slash=dirName.length(); slash > 0; oldslash = slash) { QByteArray chunk = QFile::encodeName(dirName.left(slash)); QT_STATBUF st; if (QT_STAT(chunk, &st) != -1) { if ((st.st_mode & S_IFMT) != S_IFDIR) return false; if (::rmdir(chunk) != 0) return oldslash != 0; } else { return false; } slash = dirName.lastIndexOf(QDir::separator(), oldslash-1); } return true; } return rmdir(QFile::encodeName(entry.filePath())) == 0; }
//static QString QFileSystemEngine::bundleName(const QFileSystemEntry &entry) { QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, QCFString(entry.filePath()), kCFURLPOSIXPathStyle, true); if (QCFType<CFDictionaryRef> dict = CFBundleCopyInfoDictionaryForURL(url)) { if (CFTypeRef name = (CFTypeRef)CFDictionaryGetValue(dict, kCFBundleNameKey)) { if (CFGetTypeID(name) == CFStringGetTypeID()) return QCFString::toQString((CFStringRef)name); } } return QString(); }
/*! \internal */ void QDirIteratorPrivate::advance() { if (engine) { while (!fileEngineIterators.isEmpty()) { // Find the next valid iterator that matches the filters. QAbstractFileEngineIterator *it; while (it = fileEngineIterators.top(), it->hasNext()) { it->next(); if (entryMatches(it->currentFileName(), it->currentFileInfo())) return; } fileEngineIterators.pop(); delete it; } } else { #ifndef QT_NO_FILESYSTEMITERATOR QFileSystemEntry nextEntry; QFileSystemMetaData nextMetaData; while (!nativeIterators.isEmpty()) { // Find the next valid iterator that matches the filters. QFileSystemIterator *it; while (it = nativeIterators.top(), it->advance(nextEntry, nextMetaData)) { QFileInfo info(new QFileInfoPrivate(nextEntry, nextMetaData)); if (entryMatches(nextEntry.fileName(), info)) return; } nativeIterators.pop(); delete it; } #endif } currentFileInfo = nextFileInfo; nextFileInfo = QFileInfo(); }
QT_BEGIN_NAMESPACE QFileSystemIterator::QFileSystemIterator(const QFileSystemEntry &path, QDir::Filters filters, const QStringList &nameFilters, QDirIterator::IteratorFlags iteratorFlags) : lastError(KErrNone), entryIndex(-1) { RFs& fs = qt_s60GetRFs(); nativePath = path.nativeFilePath(); if (!nativePath.endsWith(QLatin1Char('\\'))) nativePath.append(QLatin1Char('\\')); QString absPath = QFileSystemEngine::absoluteName(path).nativeFilePath(); if (!absPath.endsWith(QLatin1Char('\\'))) absPath.append(QLatin1Char('\\')); int pathLen = absPath.length(); if (pathLen > KMaxFileName) { lastError = KErrBadName; return; } //set up server side filtering to reduce IPCs //RDir won't accept all valid name filters e.g. "*. bar" if (nameFilters.count() == 1 && !(filters & QDir::AllDirs) && iteratorFlags == QDirIterator::NoIteratorFlags && pathLen + nameFilters[0].length() <= KMaxFileName) { //server side supports one mask - skip this for recursive mode or if only files should be filtered absPath.append(nameFilters[0]); } TUint symbianMask = 0; if ((filters & QDir::Dirs) || (filters & QDir::AllDirs) || (iteratorFlags & QDirIterator::Subdirectories)) symbianMask |= KEntryAttDir; //include directories if (filters & QDir::Hidden) symbianMask |= KEntryAttHidden; if (filters & QDir::System) symbianMask |= KEntryAttSystem; //Do not use KEntryAttMatchExclusive to optimise to return only //directories for QDir::Dirs. There may be a file which is actually //a "mount point" for a file engine and needs to be returned so it //can be overriden to be a directory, see QTBUG-23688 if (symbianMask == 0 && ((filters & QDir::PermissionMask) == QDir::Writable)) { symbianMask = KEntryAttMatchExclude | KEntryAttReadOnly; } lastError = dirHandle.Open(fs, qt_QString2TPtrC(absPath), symbianMask); }
//static QFileSystemEntry QFileSystemEngine::absoluteName(const QFileSystemEntry &entry) { QString orig = entry.filePath(); const bool isAbsolute = entry.isAbsolute(); const bool isDirty = !entry.isClean(); if (isAbsolute && !isDirty) return entry; const bool isRelative = entry.isRelative(); const bool needsDrive = (!orig.isEmpty() && orig.at(0).unicode() == '/'); const bool isDriveLetter = !needsDrive && !isAbsolute && !isRelative && orig.length() == 2; const bool isDriveRelative = !needsDrive && !isAbsolute && !isRelative && orig.length() > 2; QString result; if (needsDrive || isDriveLetter || isDriveRelative || !isAbsolute || orig.isEmpty()) { QFileSystemEntry cur(currentPath()); if(needsDrive) result = cur.filePath().left(2); else if(isDriveRelative && cur.filePath().at(0) != orig.at(0)) result = orig.left(2); // for BC, see tst_QFileInfo::absolutePath(<not current drive>:my.dll) else result = cur.filePath(); if(isDriveLetter) { result[0] = orig.at(0); //copy drive letter orig.clear(); } if(isDriveRelative) { orig = orig.mid(2); //discard the drive specifier from orig } } if (!orig.isEmpty() && !(orig.length() == 1 && orig.at(0).unicode() == '.')) { if (!result.isEmpty() && !result.endsWith(QLatin1Char('/'))) result.append(QLatin1Char('/')); result.append(orig); } return QFileSystemEntry(symbianCleanAbsolutePath(result), QFileSystemEntry::FromInternalPath()); }
//static bool QFileSystemEngine::setCurrentPath(const QFileSystemEntry &entry) { QFileSystemMetaData meta; QFileSystemEntry absname = absoluteName(entry); fillMetaData(absname, meta, QFileSystemMetaData::ExistsAttribute | QFileSystemMetaData::DirectoryType); if(!(meta.exists() && meta.isDirectory())) return false; RFs& fs = qt_s60GetRFs(); QString abspath = absname.nativeFilePath(); if(!abspath.endsWith(QLatin1Char('\\'))) abspath.append(QLatin1Char('\\')); TInt r = fs.SetSessionPath(qt_QString2TPtrC(abspath)); //SetSessionPath succeeds for non existent directory, which is why it's checked above if (r == KErrNone) { __ASSERT_COMPILE(sizeof(wchar_t) == sizeof(unsigned short)); //attempt to set open C to the same path r = ::wchdir(reinterpret_cast<const wchar_t *>(absname.filePath().utf16())); if (r < 0) qWarning("failed to sync path to open C"); return true; } return false; }
static bool _q_resolveEntryAndCreateLegacyEngine_recursive(QFileSystemEntry &entry, QFileSystemMetaData &data, QAbstractFileEngine *&engine, bool resolvingEntry = false) { QString const &filePath = entry.filePath(); if ((engine = qt_custom_file_engine_handler_create(filePath))) return _q_checkEntry(engine, resolvingEntry); #if defined(QT_BUILD_CORE_LIB) for (int prefixSeparator = 0; prefixSeparator < filePath.size(); ++prefixSeparator) { QChar const ch = filePath[prefixSeparator]; if (ch == QLatin1Char('/')) break; if (ch == QLatin1Char(':')) { if (prefixSeparator == 0) { engine = new QResourceFileEngine(filePath); return _q_checkEntry(engine, resolvingEntry); } if (prefixSeparator == 1) break; const QStringList &paths = QDir::searchPaths(filePath.left(prefixSeparator)); for (int i = 0; i < paths.count(); i++) { entry = QFileSystemEntry(QDir::cleanPath(paths.at(i) % QLatin1Char('/') % filePath.mid(prefixSeparator + 1))); // Recurse! if (_q_resolveEntryAndCreateLegacyEngine_recursive(entry, data, engine, true)) return true; } // entry may have been clobbered at this point. return false; } // There's no need to fully validate the prefix here. Consulting the // unicode tables could be expensive and validation is already // performed in QDir::setSearchPaths. // // if (!ch.isLetterOrNumber()) // break; } #endif // defined(QT_BUILD_CORE_LIB) return _q_checkEntry(entry, data, resolvingEntry); }
//static bool QFileSystemEngine::createDirectory(const QFileSystemEntry &entry, bool createParents) { QString dirName = entry.filePath(); if (createParents) { dirName = QDir::cleanPath(dirName); for (int oldslash = -1, slash=0; slash != -1; oldslash = slash) { slash = dirName.indexOf(QDir::separator(), oldslash+1); if (slash == -1) { if (oldslash == dirName.length()) break; slash = dirName.length(); } if (slash) { const QByteArray chunk = QFile::encodeName(dirName.left(slash)); if (QT_MKDIR(chunk.constData(), 0777) != 0) { if (errno == EEXIST #if defined(Q_OS_QNX) // On QNX the QNet (VFS paths of other hosts mounted under a directory // such as /net) mountpoint returns ENOENT, despite existing. stat() // on the QNet mountpoint returns successfully and reports S_IFDIR. || errno == ENOENT #endif ) { QT_STATBUF st; if (QT_STAT(chunk.constData(), &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR) continue; } return false; } } } return true; } #if defined(Q_OS_DARWIN) // Mac X doesn't support trailing /'s if (dirName.endsWith(QLatin1Char('/'))) dirName.chop(1); #endif return (QT_MKDIR(QFile::encodeName(dirName).constData(), 0777) == 0); }
//static bool QFileSystemEngine::fillMetaData(const QFileSystemEntry &entry, QFileSystemMetaData &data, QFileSystemMetaData::MetaDataFlags what) { if (what & QFileSystemMetaData::SymbianTEntryFlags) { RFs& fs(qt_s60GetRFs()); TInt err; QFileSystemEntry absentry(absoluteName(entry)); if (entry.isEmpty()) { err = KErrNotFound; } else if (absentry.isRoot()) { //Root directories don't have an entry, and Entry() returns KErrBadName. //Therefore get information about the volume instead. TInt drive; err = RFs::CharToDrive(TChar(absentry.nativeFilePath().at(0).unicode()), drive); if (!err) { TVolumeInfo info; err = fs.Volume(info, drive); if (!err) data.fillFromVolumeInfo(info); } } else { TEntry ent; err = fs.Entry(qt_QString2TPtrC(absentry.nativeFilePath()), ent); if (!err) data.fillFromTEntry(ent); } if (err) { data.size_ = 0; data.modificationTime_ = TTime(0); data.entryFlags &= ~(QFileSystemMetaData::SymbianTEntryFlags); } //files in /sys/bin on any drive are executable, even though we don't normally have permission to check whether they exist or not if(absentry.filePath().midRef(1,10).compare(QLatin1String(":/sys/bin/"), Qt::CaseInsensitive) == 0) data.entryFlags |= QFileSystemMetaData::ExecutePermissions; } return data.hasFlags(what); }
void tst_QFileSystemEntry::defaultCtor() { QFileSystemEntry entry; QVERIFY(entry.filePath().isNull()); QVERIFY(entry.nativeFilePath().isNull()); QVERIFY(entry.fileName().isNull()); QCOMPARE(entry.path(), QString(".")); QVERIFY(entry.baseName().isNull()); QVERIFY(entry.completeBaseName().isNull()); QVERIFY(entry.suffix().isNull()); QVERIFY(entry.completeSuffix().isNull()); QVERIFY(!entry.isAbsolute()); QVERIFY(entry.isRelative()); QVERIFY(entry.isClean()); #if defined(Q_OS_WIN) QVERIFY(!entry.isDriveRoot()); #endif QVERIFY(!entry.isRoot()); QVERIFY(entry.isEmpty()); }
//static bool QFileSystemEngine::fillMetaData(const QFileSystemEntry &entry, QFileSystemMetaData &data, QFileSystemMetaData::MetaDataFlags what) { #if !defined(QWS) && !defined(Q_WS_QPA) && defined(Q_OS_MAC) if (what & QFileSystemMetaData::BundleType) { if (!data.hasFlags(QFileSystemMetaData::DirectoryType)) what |= QFileSystemMetaData::DirectoryType; } #endif #if !defined(QWS) && !defined(Q_WS_QPA) && defined(Q_OS_MAC) \ && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 if (what & QFileSystemMetaData::HiddenAttribute) { // Mac OS >= 10.5: st_flags & UF_HIDDEN what |= QFileSystemMetaData::PosixStatFlags; } #endif if (what & QFileSystemMetaData::PosixStatFlags) what |= QFileSystemMetaData::PosixStatFlags; if (what & QFileSystemMetaData::ExistsAttribute) { // FIXME: Would other queries being performed provide this bit? what |= QFileSystemMetaData::PosixStatFlags; } data.entryFlags &= ~what; const char * nativeFilePath; int nativeFilePathLength; { const QByteArray &path = entry.nativeFilePath(); nativeFilePath = path.constData(); nativeFilePathLength = path.size(); Q_UNUSED(nativeFilePathLength); } bool entryExists = true; // innocent until proven otherwise QT_STATBUF statBuffer; bool statBufferValid = false; if (what & QFileSystemMetaData::LinkType) { if (QT_LSTAT(nativeFilePath, &statBuffer) == 0) { if (S_ISLNK(statBuffer.st_mode)) { data.entryFlags |= QFileSystemMetaData::LinkType; } else { statBufferValid = true; data.entryFlags &= ~QFileSystemMetaData::PosixStatFlags; } } else { entryExists = false; } data.knownFlagsMask |= QFileSystemMetaData::LinkType; } if (statBufferValid || (what & QFileSystemMetaData::PosixStatFlags)) { if (entryExists && !statBufferValid) statBufferValid = (QT_STAT(nativeFilePath, &statBuffer) == 0); if (statBufferValid) data.fillFromStatBuf(statBuffer); else { entryExists = false; data.creationTime_ = 0; data.modificationTime_ = 0; data.accessTime_ = 0; data.size_ = 0; data.userId_ = (uint) -2; data.groupId_ = (uint) -2; } // reset the mask data.knownFlagsMask |= QFileSystemMetaData::PosixStatFlags | QFileSystemMetaData::ExistsAttribute; } #if !defined(QWS) && !defined(Q_WS_QPA) && defined(Q_OS_MAC) if (what & QFileSystemMetaData::AliasType) { if (entryExists) { FSRef fref; if (FSPathMakeRef((const UInt8 *)nativeFilePath, &fref, NULL) == noErr) { Boolean isAlias, isFolder; if (FSIsAliasFile(&fref, &isAlias, &isFolder) == noErr) { if (isAlias) data.entryFlags |= QFileSystemMetaData::AliasType; } } } data.knownFlagsMask |= QFileSystemMetaData::AliasType; } #endif if (what & QFileSystemMetaData::UserPermissions) { // calculate user permissions if (entryExists) { if (what & QFileSystemMetaData::UserReadPermission) { if (QT_ACCESS(nativeFilePath, R_OK) == 0) data.entryFlags |= QFileSystemMetaData::UserReadPermission; } if (what & QFileSystemMetaData::UserWritePermission) { if (QT_ACCESS(nativeFilePath, W_OK) == 0) data.entryFlags |= QFileSystemMetaData::UserWritePermission; } if (what & QFileSystemMetaData::UserExecutePermission) { if (QT_ACCESS(nativeFilePath, X_OK) == 0) data.entryFlags |= QFileSystemMetaData::UserExecutePermission; } } data.knownFlagsMask |= (what & QFileSystemMetaData::UserPermissions); } if (what & QFileSystemMetaData::HiddenAttribute && !data.isHidden()) { QString fileName = entry.fileName(); if ((fileName.size() > 0 && fileName.at(0) == QLatin1Char('.')) || (entryExists && _q_isMacHidden(nativeFilePath))) data.entryFlags |= QFileSystemMetaData::HiddenAttribute; data.knownFlagsMask |= QFileSystemMetaData::HiddenAttribute; } #if !defined(QWS) && !defined(Q_WS_QPA) && defined(Q_OS_MAC) if (what & QFileSystemMetaData::BundleType) { if (entryExists && data.isDirectory()) { QCFType<CFStringRef> path = CFStringCreateWithBytes(0, (const UInt8*)nativeFilePath, nativeFilePathLength, kCFStringEncodingUTF8, false); QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, path, kCFURLPOSIXPathStyle, true); UInt32 type, creator; if (CFBundleGetPackageInfoInDirectory(url, &type, &creator)) data.entryFlags |= QFileSystemMetaData::BundleType; } data.knownFlagsMask |= QFileSystemMetaData::BundleType; } #endif return data.hasFlags(what); }
bool QFileSystemEngine::setCurrentPath(const QFileSystemEntry &path) { int r; r = QT_CHDIR(path.nativeFilePath()); return r >= 0; }
//static bool QFileSystemEngine::fillMetaData(const QFileSystemEntry &entry, QFileSystemMetaData &data, QFileSystemMetaData::MetaDataFlags what) { #if defined(Q_OS_MACX) if (what & QFileSystemMetaData::BundleType) { if (!data.hasFlags(QFileSystemMetaData::DirectoryType)) what |= QFileSystemMetaData::DirectoryType; } if (what & QFileSystemMetaData::HiddenAttribute) { // Mac OS >= 10.5: st_flags & UF_HIDDEN what |= QFileSystemMetaData::PosixStatFlags; } #endif // defined(Q_OS_MACX) if (what & QFileSystemMetaData::PosixStatFlags) what |= QFileSystemMetaData::PosixStatFlags; if (what & QFileSystemMetaData::ExistsAttribute) { // FIXME: Would other queries being performed provide this bit? what |= QFileSystemMetaData::PosixStatFlags; } data.entryFlags &= ~what; const char * nativeFilePath; int nativeFilePathLength; { const QByteArray &path = entry.nativeFilePath(); nativeFilePath = path.constData(); nativeFilePathLength = path.size(); Q_UNUSED(nativeFilePathLength); } bool entryExists = true; // innocent until proven otherwise QT_STATBUF statBuffer; bool statBufferValid = false; if (what & QFileSystemMetaData::LinkType) { if (QT_LSTAT(nativeFilePath, &statBuffer) == 0) { if (S_ISLNK(statBuffer.st_mode)) { data.entryFlags |= QFileSystemMetaData::LinkType; } else { statBufferValid = true; data.entryFlags &= ~QFileSystemMetaData::PosixStatFlags; } } else { entryExists = false; } data.knownFlagsMask |= QFileSystemMetaData::LinkType; } if (statBufferValid || (what & QFileSystemMetaData::PosixStatFlags)) { if (entryExists && !statBufferValid) statBufferValid = (QT_STAT(nativeFilePath, &statBuffer) == 0); if (statBufferValid) data.fillFromStatBuf(statBuffer); else { entryExists = false; data.creationTime_ = 0; data.modificationTime_ = 0; data.accessTime_ = 0; data.size_ = 0; data.userId_ = (uint) -2; data.groupId_ = (uint) -2; } // reset the mask data.knownFlagsMask |= QFileSystemMetaData::PosixStatFlags | QFileSystemMetaData::ExistsAttribute; } #if defined(Q_OS_MACX) if (what & QFileSystemMetaData::AliasType) { if (entryExists) { FSRef fref; if (FSPathMakeRef((const UInt8 *)nativeFilePath, &fref, NULL) == noErr) { Boolean isAlias, isFolder; if (FSIsAliasFile(&fref, &isAlias, &isFolder) == noErr) { if (isAlias) data.entryFlags |= QFileSystemMetaData::AliasType; } } } data.knownFlagsMask |= QFileSystemMetaData::AliasType; } #endif if (what & QFileSystemMetaData::UserPermissions) { // calculate user permissions if (entryExists) { if (what & QFileSystemMetaData::UserReadPermission) { if (QT_ACCESS(nativeFilePath, R_OK) == 0) data.entryFlags |= QFileSystemMetaData::UserReadPermission; } if (what & QFileSystemMetaData::UserWritePermission) { if (QT_ACCESS(nativeFilePath, W_OK) == 0) data.entryFlags |= QFileSystemMetaData::UserWritePermission; } if (what & QFileSystemMetaData::UserExecutePermission) { if (QT_ACCESS(nativeFilePath, X_OK) == 0) data.entryFlags |= QFileSystemMetaData::UserExecutePermission; } } data.knownFlagsMask |= (what & QFileSystemMetaData::UserPermissions); } if (what & QFileSystemMetaData::HiddenAttribute && !data.isHidden()) { QString fileName = entry.fileName(); if ((fileName.size() > 0 && fileName.at(0) == QLatin1Char('.')) || (entryExists && _q_isMacHidden(nativeFilePath))) data.entryFlags |= QFileSystemMetaData::HiddenAttribute; data.knownFlagsMask |= QFileSystemMetaData::HiddenAttribute; } #if defined(Q_OS_MACX) if (what & QFileSystemMetaData::BundleType) { if (entryExists && isPackage(data, entry)) data.entryFlags |= QFileSystemMetaData::BundleType; data.knownFlagsMask |= QFileSystemMetaData::BundleType; } #endif if (!entryExists) { data.clearFlags(what); return false; } return data.hasFlags(what); }
//static QFileSystemEntry QFileSystemEngine::getLinkTarget(const QFileSystemEntry &link, QFileSystemMetaData &data) { #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); Q_CHECK_PTR(s); len = ::readlink(link.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(link.nativeFilePath().constData(), s, PATH_MAX); #endif if (len > 0) { QString ret; if (!data.hasFlags(QFileSystemMetaData::DirectoryType)) fillMetaData(link, data, QFileSystemMetaData::DirectoryType); if (data.isDirectory() && s[0] != '/') { QDir parent(link.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 (link.filePath().startsWith(QLatin1Char('/'))) { ret.prepend(link.filePath().left(link.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 QFileSystemEntry(ret); } #if !defined(QWS) && !defined(Q_WS_QPA) && defined(Q_OS_MAC) { FSRef fref; if (FSPathMakeRef((const UInt8 *)QFile::encodeName(QDir::cleanPath(link.filePath())).data(), &fref, 0) == noErr) { // TODO get the meta data info from the QFileSystemMetaData object Boolean isAlias, isFolder; if (FSResolveAliasFile(&fref, true, &isFolder, &isAlias) == noErr && isAlias) { AliasHandle alias; if (FSNewAlias(0, &fref, &alias) == noErr && alias) { QCFString cfstr; if (FSCopyAliasInfo(alias, 0, 0, &cfstr, 0, 0) == noErr) return QFileSystemEntry(QCFString::toQString(cfstr)); } } } } #endif return QFileSystemEntry(); }