Exemplo n.º 1
0
Arquivo: tree.go Projeto: herrsebi/acd
// MkdirAll creates a directory named path, along with any necessary parents,
// and returns the directory node and nil, or else returns an error. If path is
// already a directory, MkdirAll does nothing and returns the directory node
// and nil.
func (nt *Tree) MkdirAll(path string) (*Node, error) {
	var (
		err        error
		folderNode = nt.Node
		logLevel   = log.GetLevel()
		nextNode   *Node
		node       *Node
	)

	// Short-circuit if the node already exists!
	{
		log.SetLevel(log.DisableLogLevel)
		node, err = nt.FindNode(path)
		log.SetLevel(logLevel)
	}
	if err == nil {
		if node.IsDir() {
			return node, err
		}
		log.Errorf("%s: %s", constants.ErrFileExistsAndIsNotFolder, path)
		return nil, constants.ErrFileExistsAndIsNotFolder
	}

	// chop off the first /.
	if strings.HasPrefix(path, "/") {
		path = path[1:]
	}
	parts := strings.Split(path, "/")
	if len(parts) == 0 {
		log.Errorf("%s: %s", constants.ErrCannotCreateRootNode, path)
		return nil, constants.ErrCannotCreateRootNode
	}

	for i, part := range parts {
		{
			log.SetLevel(log.DisableLogLevel)
			nextNode, err = nt.FindNode(strings.Join(parts[:i+1], "/"))
			log.SetLevel(logLevel)
		}
		if err != nil && err != constants.ErrNodeNotFound {
			return nil, err
		}
		if err == constants.ErrNodeNotFound {
			nextNode, err = folderNode.CreateFolder(part)
			if err != nil {
				return nil, err
			}
		}

		if !nextNode.IsDir() {
			log.Errorf("%s: %s", constants.ErrCannotCreateANodeUnderAFile, strings.Join(parts[:i+1], "/"))
			return nil, constants.ErrCannotCreateANodeUnderAFile
		}

		folderNode = nextNode
	}

	return folderNode, nil
}
Exemplo n.º 2
0
func TestMain(m *testing.M) {
	defer func() {
		if r := recover(); r != nil {
			cleanUp()
		}
	}()

	cacheFile = newTempFile("acd-cache-")
	cacheFiles = append(cacheFiles, cacheFile)
	testFolderPath = fmt.Sprintf("%s/%d", testFolderBasePath, time.Now().UnixNano())

	// disable all logs
	log.SetLevel(log.DebugLevel)

	// run all the tests
	code := m.Run()

	// Cleanup after the run
	cleanUp()

	// exit with the return status
	os.Exit(code)
}