Пример #1
0
// Returns 'false' if
// - one of the path components is empty, or
// - one of the path components is ".", or
// - one of the path components is ".."
// and 'true' otherwise.
uio_bool
validPathName(const char *path, size_t len) {
	const char *pathEnd;
	const char *start, *end;

	pathEnd = path + len;
	getFirstPathComponent(path, pathEnd, &start, &end);
	while (start < pathEnd) {
		if (end == start || (end - start == 1 && start[0] == '.') ||
				(end - start == 2 && start[0] == '.' && start[1] == '.'))
			return false;
		getNextPathComponent(pathEnd, &start, &end);
	}
	return true;
}
Пример #2
0
/*
 * Follow a path starting from a specified physical dir as long as possible.
 * When you can get no further, 'endGPDir' will be filled in with the dir
 * where you ended up, and 'pathRest' will point into the original path. to
 * the beginning of the part that was not matched.
 * It is allowed to have endGPDir point to gPDir and/or restPath
 * point to path when calling this function.
 * returns: 0 if the complete path was matched
 *          ENOENT if some component (the next one) didn't exists
 *          ENODIR if a component (the next one) exists but isn't a dir
 * See also uio_walkPhysicalPath. The difference is that this one works
 * directly on the GPDirs and is faster because of that.
 */
int
uio_walkGPPath(uio_GPDir *startGPDir, const char *path,
		size_t pathLen, uio_GPDir **endGPDir, const char **pathRest) {
	const char *pathEnd;
	const char *partStart, *partEnd;
	char *tempBuf;
	uio_GPDir *gPDir;
	uio_GPDirEntry *entry;
	int retVal;

	gPDir = startGPDir;
	tempBuf = uio_malloc(strlen(path) + 1);
			// XXX: Use a dynamically allocated array when moving to C99.
	pathEnd = path + pathLen;
	getFirstPathComponent(path, pathEnd, &partStart, &partEnd);
	while (1) {
		if (partStart == pathEnd) {
			retVal = 0;
			break;
		}
		memcpy(tempBuf, partStart, partEnd - partStart);
		tempBuf[partEnd - partStart] = '\0';

		entry = uio_GPDir_getGPDirEntry(gPDir, tempBuf);
		if (entry == NULL) {
			retVal = ENOENT;
			break;
		}
		if (!uio_GPDirEntry_isDir(entry)) {
			retVal = ENOTDIR;
			break;
		}
		gPDir = (uio_GPDir *) entry;
		getNextPathComponent(pathEnd, &partStart, &partEnd);
	}

	uio_free(tempBuf);
	*pathRest = partStart;
	*endGPDir = gPDir;
	return retVal;
}
Пример #3
0
boolean walkPath(char *filepath, SdFile& parentDir,
		 boolean (*callback)(SdFile& parentDir,
				     char *filePathComponent,
				     boolean isLastComponent,
				     void *object),
		 void *object = NULL) {
  /*
     
     When given a file path (and parent directory--normally root),
     this function traverses the directories in the path and at each
     level calls the supplied callback function while also providing
     the supplied object for context if required.

       e.g. given the path '/foo/bar/baz'
            the callback would be called at the equivalent of
	    '/foo', '/foo/bar' and '/foo/bar/baz'.

     The implementation swaps between two different directory/file
     handles as it traverses the directories and does not use recursion
     in an attempt to use memory efficiently.

     If a callback wishes to stop the directory traversal it should
     return false--in this case the function will stop the traversal,
     tidy up and return false.

     If a directory path doesn't exist at some point this function will
     also return false and not subsequently call the callback.

     If a directory path specified is complete, valid and the callback
     did not indicate the traversal should be interrupted then this
     function will return true.

   */


  SdFile subfile1;
  SdFile subfile2;

  char buffer[PATH_COMPONENT_BUFFER_LEN]; 

  unsigned int offset = 0;

  SdFile *p_parent;
  SdFile *p_child;

  SdFile *p_tmp_sdfile;  
  
  p_child = &subfile1;
  
  p_parent = &parentDir;

  while (true) {

    boolean moreComponents = getNextPathComponent(filepath, &offset, buffer);

    boolean shouldContinue = callback((*p_parent), buffer, !moreComponents, object);

    if (!shouldContinue) {
      // TODO: Don't repeat this code?
      // If it's one we've created then we
      // don't need the parent handle anymore.
      if (p_parent != &parentDir) {
        (*p_parent).close();
      }
      return false;
    }
    
    if (!moreComponents) {
      break;
    }
    
    boolean exists = (*p_child).open(*p_parent, buffer, O_RDONLY);

    // If it's one we've created then we
    // don't need the parent handle anymore.
    if (p_parent != &parentDir) {
      (*p_parent).close();
    }
    
    // Handle case when it doesn't exist and we can't continue...
    if (exists) {
      // We alternate between two file handles as we go down
      // the path.
      if (p_parent == &parentDir) {
        p_parent = &subfile2;
      }

      p_tmp_sdfile = p_parent;
      p_parent = p_child;
      p_child = p_tmp_sdfile;
    } else {
      return false;
    }
  }
  
  if (p_parent != &parentDir) {
    (*p_parent).close(); // TODO: Return/ handle different?
  }

  return true;
}
Пример #4
0
boolean walkPath(char *filepath, SdFile& parentDir,
                 boolean (*callback)(SdFile& parentDir, char *filePathComponent, boolean isLastComponent, void *object), void *object = NULL) {
  /*
   */


  SdFile subfile1;
  SdFile subfile2;

  char buffer[PATH_COMPONENT_BUFFER_LEN]; 

  unsigned int offset = 0;

  SdFile *p_parent;
  SdFile *p_child;

  SdFile *p_tmp_sdfile;  
  
  p_child = &subfile1;
  
  p_parent = &parentDir;

  while (true) {

    boolean moreComponents = getNextPathComponent(filepath, &offset, buffer);

    boolean shouldContinue = callback((*p_parent), buffer, !moreComponents, object);

    if (!shouldContinue) {
      // TODO: Don't repeat this code?
      // If it's one we've created then we
      // don't need the parent handle anymore.
      if (p_parent != &parentDir) {
        (*p_parent).close();
      }
      return false;
    }
    
    boolean exists = (*p_child).open(*p_parent, buffer, O_RDONLY);

    // If it's one we've created then we
    // don't need the parent handle anymore.
    if (p_parent != &parentDir) {
      (*p_parent).close();
    }
    
    // Handle case when it doesn't exist and we can't continue...
    if (exists) {
      // We alternate between two file handles as we go down
      // the path.
      if (p_parent == &parentDir) {
        p_parent = &subfile2;
      }

      p_tmp_sdfile = p_parent;
      p_parent = p_child;
      p_child = p_tmp_sdfile;
    } else {
      return false;
    }
    
    if (!moreComponents) {
      // TODO: Check if this check should be earlier.
      break;
    }
  }
  
  (*p_parent).close(); // TODO: Return/ handle different?

  return true;
}