Ejemplo n.º 1
0
std::vector<Index::lookupDirectory> Index::initDirectoryPatterns()
{
	std::vector<lookupDirectory> result;

	result.push_back(lookupDirectory(true, "$XDG_DATA_DIRS/applications/"));
	result.push_back(lookupDirectory(true, "$XDG_DATA_HOME/applications/"));
	result.push_back(lookupDirectory(false, "$XDG_CONFIG_DIRS/"));
	result.push_back(lookupDirectory(false, "$XDG_CONFIG_HOME/"));

	return result;
}
Ejemplo n.º 2
0
void Index::findDirectories()
{
	directories_ = directoryPatterns_;

	bool again;

	do
	{
		std::vector<lookupDirectory> directories;

		again = false;

		for (std::vector<lookupDirectory>::size_type i = 0; i < directories_.size(); ++i)
		{
			std::vector<std::string> unfolded = unfoldVariable(directories_.at(i).path);

			if (unfolded.size() > 0 && unfolded.at(0) != directories_.at(i).path)
			{
				again = true;
			}

			for (std::vector<std::string>::size_type j = 0; j < unfolded.size(); ++j)
			{
				directories.push_back(lookupDirectory(directories_.at(i).withSubdirectories, unfolded.at(j)));
			}
		}

		directories_.swap(directories);
		directories.clear();
	} while (again);
}
Ejemplo n.º 3
0
static void lookupDirectory(const QString& path, const QString &relPart,
			    const QRegExp &regexp,
			    QStringList& list,
			    QStringList& relList,
			    bool recursive, bool unique)
{
  QString pattern = regexp.pattern();
  if (recursive || pattern.contains('?') || pattern.contains('*'))
  {
    if (path.isEmpty()) //for sanity
      return;
    // We look for a set of files.
    DIR *dp = opendir( QFile::encodeName(path));
    if (!dp)
      return;

#ifdef Q_WS_WIN
    assert(path.at(path.length() - 1) == '/' || path.at(path.length() - 1) == '\\');
#else
    assert(path.at(path.length() - 1) == '/');
#endif

    struct dirent *ep;
    KDE_struct_stat buff;

    QString _dot(".");
    QString _dotdot("..");

    while( ( ep = readdir( dp ) ) != 0L )
    {
      QString fn( QFile::decodeName(ep->d_name));
      if (fn == _dot || fn == _dotdot || fn.at(fn.length() - 1).latin1() == '~')
	continue;

      if (!recursive && !regexp.exactMatch(fn))
	continue; // No match

      QString pathfn = path + fn;
      if ( KDE_stat( QFile::encodeName(pathfn), &buff ) != 0 ) {
	kdDebug() << "Error stat'ing " << pathfn << " : " << perror << endl;
	continue; // Couldn't stat (e.g. no read permissions)
      }
      if ( recursive ) {
	if ( S_ISDIR( buff.st_mode )) {
	  lookupDirectory(pathfn + '/', relPart + fn + '/', regexp, list, relList, recursive, unique);
	}
        if (!regexp.exactMatch(fn))
	  continue; // No match
      }
      if ( S_ISREG( buff.st_mode))
      {
        if (!unique || !relList.contains(relPart + fn))
        {
	    list.append( pathfn );
	    relList.append( relPart + fn );
        }
      }
    }
    closedir( dp );
  }
  else
  {
     // We look for a single file.
     QString fn = pattern;
     QString pathfn = path + fn;
     KDE_struct_stat buff;
     if ( KDE_stat( QFile::encodeName(pathfn), &buff ) != 0 )
        return; // File not found
     if ( S_ISREG( buff.st_mode))
     {
       if (!unique || !relList.contains(relPart + fn))
       {
         list.append( pathfn );
         relList.append( relPart + fn );
       }
     }
  }
}
Ejemplo n.º 4
0
static void lookupPrefix(const QString& prefix, const QString& relpath,
                         const QString& relPart,
			 const QRegExp &regexp,
			 QStringList& list,
			 QStringList& relList,
			 bool recursive, bool unique)
{
    if (relpath.isEmpty()) {
       lookupDirectory(prefix, relPart, regexp, list,
		       relList, recursive, unique);
       return;
    }
    QString path;
    QString rest;

    if (relpath.length())
    {
       int slash = relpath.find('/');
       if (slash < 0)
	   rest = relpath.left(relpath.length() - 1);
       else {
	   path = relpath.left(slash);
	   rest = relpath.mid(slash + 1);
       }
    }

    if (prefix.isEmpty()) //for sanity
      return;
#ifdef Q_WS_WIN
    assert(prefix.at(prefix.length() - 1) == '/' || prefix.at(prefix.length() - 1) == '\\');
#else
    assert(prefix.at(prefix.length() - 1) == '/');
#endif
    KDE_struct_stat buff;

    if (path.contains('*') || path.contains('?')) {

	QRegExp pathExp(path, true, true);
	DIR *dp = opendir( QFile::encodeName(prefix) );
	if (!dp) {
	    return;
	}

	struct dirent *ep;

        QString _dot(".");
        QString _dotdot("..");

	while( ( ep = readdir( dp ) ) != 0L )
	    {
		QString fn( QFile::decodeName(ep->d_name));
		if (fn == _dot || fn == _dotdot || fn.at(fn.length() - 1) == '~')
		    continue;

		if ( !pathExp.exactMatch(fn) )
		    continue; // No match
		QString rfn = relPart+fn;
		fn = prefix + fn;
		if ( KDE_stat( QFile::encodeName(fn), &buff ) != 0 ) {
		    kdDebug() << "Error statting " << fn << " : " << perror << endl;
		    continue; // Couldn't stat (e.g. no permissions)
		}
		if ( S_ISDIR( buff.st_mode ))
		    lookupPrefix(fn + '/', rest, rfn + '/', regexp, list, relList, recursive, unique);
	    }

	closedir( dp );
    } else {
        // Don't stat, if the dir doesn't exist we will find out
        // when we try to open it.
        lookupPrefix(prefix + path + '/', rest,
                     relPart + path + '/', regexp, list,
                     relList, recursive, unique);
    }
}
Ejemplo n.º 5
0
/*
 * Open a file with the given name and mode.
 * Return > 0 on success, < 0 on failure (e.g. does not exist).
 */
int GOSFS_Open(struct Mount_Point *mountPoint, const char *path, int mode,
        struct File **pFile) {
    GOSFSinstance *instance = (GOSFSinstance *) mountPoint->fsData;
    GOSFSdirectory *dir = 0;
    GOSFSfileNode *dirFileNode = 0, *fileNode = 0;
    GOSFSptr *cached;
    struct File *file;
    char *filePath = path;
    int rc;

    cached = Malloc(sizeof(GOSFSptr));
    if (cached == 0)
        goto memfail;

    dirFileNode = lookupDirectory(instance, &dir, &filePath);
    if (dirFileNode == 0 && dir == 0) {
        /* Complete failure. */
        rc = EACCESS;
        goto fail;
    }

    /* Now lookup desired file in the directory. */
    fileNode = lookupFileInDirectory(dir, filePath, strlen(filePath));
    if (fileNode == 0) {
        /* File does not exist. */
        if (mode & O_CREATE) {
            /* Create the file in this directory. */
            fileNode = createFileInDirectory(instance, dirFileNode, dir,
                    filePath);
            if (fileNode == 0) {
                rc = EACCESS;
                goto fail;
            }
        } else {
            rc = EACCESS;
            goto fail;
        }
    }

    /* Fill in GOSFSptr fields */
    if (dirFileNode)
        /* Non-root directory */
        cached->blockNum = dirFileNode->blocks[0];
    else
        /* Root directory */
        cached->blockNum = instance->superblock->rootDirPointer;

    /* Calculate offset via ptr arithmetic */
    cached->offset = fileNode - dir->files;

    /* Copy over file node */
    memcpy(&cached->node, fileNode, sizeof(GOSFSfileNode));

    /* Allocate file for VFS */
    file = Allocate_File(&s_gosfsFileOps, 0, fileNode->size, cached, mode,
            mountPoint);
    if (file == 0)
        goto memfail;

    /* Success! */
    *pFile = file;
    if (dirFileNode)
        Free(dirFileNode);
    Free(dir);
    return 0;

    memfail: rc = ENOMEM;
    goto fail;

    fail: if (dirFileNode)
        Free(dirFileNode);
    if (dir)
        Free(dir);
    if (cached)
        Free(cached);
    return rc;
}