void QFSFileEngineIterator::advance()
{
    currentEntry = platform->dirEntry ? QFile::decodeName(QByteArray(platform->dirEntry->d_name)) : QString();

    if (!platform->dir)
        return;

#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_CYGWIN) && !defined(Q_OS_SYMBIAN)
    if (QT_READDIR_R(platform->dir, platform->mt_file, &platform->dirEntry) != 0)
        platform->done = true;
#else
    // ### add local lock to prevent breaking reentrancy
    platform->dirEntry = QT_READDIR(platform->dir);
#endif // _POSIX_THREAD_SAFE_FUNCTIONS
    if (!platform->dirEntry) {
        QT_CLOSEDIR(platform->dir);
        platform->dir = 0;
        platform->done = true;
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_CYGWIN) && !defined(Q_OS_SYMBIAN)
        delete [] platform->mt_file;
        platform->mt_file = 0;
#endif
    }
}
bool SynchronizerDirList::load(const QString &urlIn, bool wait)
{
    if (busy)
        return false;

    currentUrl = urlIn;
    QUrl url = QUrl::fromUserInput(urlIn, QString(), QUrl::AssumeLocalFile);

    QHashIterator< QString, vfile *> lit(*this);
    while (lit.hasNext())
        delete lit.next().value();
    clear();
    if (fileIterator) {
        delete fileIterator;
        fileIterator = 0;
    }

    if (url.isLocalFile()) {
        QString path = url.adjusted(QUrl::StripTrailingSlash).path();
        QT_DIR* dir = QT_OPENDIR(path.toLocal8Bit());
        if (!dir)  {
            KMessageBox::error(parentWidget, i18n("Cannot open the %1 directory.", path), i18n("Error"));
            emit finished(result = false);
            return false;
        }

        QT_DIRENT* dirEnt;
        QString name;

        while ((dirEnt = QT_READDIR(dir)) != NULL) {
            name = QString::fromLocal8Bit(dirEnt->d_name);

            if (name == "." || name == "..") continue;
            if (ignoreHidden && name.startsWith('.')) continue;

            QString fullName = path + '/' + name;

            QT_STATBUF stat_p;
            QT_LSTAT(fullName.toLocal8Bit(), &stat_p);

            QString perm = KRpermHandler::mode2QString(stat_p.st_mode);

            bool symLink = S_ISLNK(stat_p.st_mode);
            QString symlinkDest;
            bool brokenLink = false;

            if (symLink) {  // who the link is pointing to ?
                char symDest[256];
                memset(symDest, 0, 256);
                int endOfName = 0;
                endOfName = readlink(fullName.toLocal8Bit(), symDest, 256);
                if (endOfName != -1) {
                    QString absSymDest = symlinkDest = QString::fromLocal8Bit(symDest);

                    if (!absSymDest.startsWith('/'))
                        absSymDest = QDir::cleanPath(path + '/' + absSymDest);

                    if (QDir(absSymDest).exists())
                        perm[0] = 'd';
                    if (!QDir(path).exists(absSymDest))
                        brokenLink = true;
                }
            }

            QString mime;

            QUrl fileURL = QUrl::fromLocalFile(fullName);

            vfile* item = new vfile(name, stat_p.st_size, perm, stat_p.st_mtime, symLink, brokenLink, stat_p.st_uid,
                                    stat_p.st_gid, mime, symlinkDest, stat_p.st_mode);
            item->vfile_setUrl(fileURL);

            insert(name, item);
        }

        QT_CLOSEDIR(dir);
        emit finished(result = true);
        return true;
    } else {
        KIO::Job *job = KIO::listDir(url, KIO::HideProgressInfo, true);
        connect(job, SIGNAL(entries(KIO::Job*, const KIO::UDSEntryList&)),
                this, SLOT(slotEntries(KIO::Job*, const KIO::UDSEntryList&)));
        connect(job, SIGNAL(result(KJob*)),
                this, SLOT(slotListResult(KJob*)));
        busy = true;

        if (!wait)
            return true;

        while (busy)
            qApp->processEvents();
        return result;
    }
}
예제 #3
0
void KRSearchMod::scanLocalDir(QUrl urlToScan)
{
    QString dir = vfs::ensureTrailingSlash(urlToScan).path();

    QT_DIR* d = QT_OPENDIR(dir.toLocal8Bit());
    if (!d) return ;

    QT_DIRENT* dirEnt;

    while ((dirEnt = QT_READDIR(d)) != NULL) {
        QString name = QString::fromLocal8Bit(dirEnt->d_name);

        // we don't scan the ".",".." enteries
        if (name == "." || name == "..") continue;

        QT_STATBUF stat_p;
        QT_LSTAT((dir + name).toLocal8Bit(), &stat_p);

        QUrl url = QUrl::fromLocalFile(dir + name);

        QString mime;
        if (query->searchInArchives() || !query->hasMimeType()) {
            QMimeDatabase db;
            QMimeType mt = db.mimeTypeForUrl(url);
            if (mt.isValid())
                mime = mt.name();
        }

        // creating a vfile object for matching with krquery
        vfile * vf = new vfile(name, (KIO::filesize_t)stat_p.st_size, KRpermHandler::mode2QString(stat_p.st_mode),
                               stat_p.st_mtime, S_ISLNK(stat_p.st_mode), false/*FIXME*/, stat_p.st_uid, stat_p.st_gid,
                               mime, "", stat_p.st_mode, -1, url);

        if (query->isRecursive()) {
            if (S_ISLNK(stat_p.st_mode) && query->followLinks())
                unScannedUrls.push(QUrl::fromLocalFile(QDir(dir + name).canonicalPath()));
            else if (S_ISDIR(stat_p.st_mode))
                unScannedUrls.push(url);
        }
        if (query->searchInArchives()) {
            if (KRarcHandler::arcSupported(mime)) {
                QUrl archiveURL = url;
                bool encrypted;
                QString realType = arcHandler.getType(encrypted, url.path(), mime);

                if (!encrypted) {
                    if (realType == "tbz" || realType == "tgz" || realType == "tarz" || realType == "tar" || realType == "tlz")
                        archiveURL.setScheme("tar");
                    else
                        archiveURL.setScheme("krarc");

                    unScannedUrls.push(archiveURL);
                }
            }
        }

        if (query->match(vf)) {
            // if we got here - we got a winner
            results.append(dir + name);
            emit found(name, dir, (KIO::filesize_t) stat_p.st_size, stat_p.st_mtime,
                       KRpermHandler::mode2QString(stat_p.st_mode), stat_p.st_uid, stat_p.st_gid, query->foundText());
        }
        delete vf;

        if (timer.elapsed() >= EVENT_PROCESS_DELAY) {
            qApp->processEvents();
            timer.start();
            if (stopSearch) return;
        }
    }
    // clean up
    QT_CLOSEDIR(d);
}