launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/container/directory.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package container
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"launchpad.net/juju-core/utils"
    11  )
    12  
    13  var (
    14  	ContainerDir        = "/var/lib/juju/containers"
    15  	RemovedContainerDir = "/var/lib/juju/removed-containers"
    16  )
    17  
    18  // NewDirectory creates a new directory for the container name in the
    19  // directory identified by `ContainerDir`.
    20  func NewDirectory(containerName string) (directory string, err error) {
    21  	directory = dirForName(containerName)
    22  	logger.Tracef("create directory: %s", directory)
    23  	if err = os.MkdirAll(directory, 0755); err != nil {
    24  		logger.Errorf("failed to create container directory: %v", err)
    25  		return "", err
    26  	}
    27  	return directory, nil
    28  }
    29  
    30  // RemoveDirectory moves the container directory from `ContainerDir`
    31  // to `RemovedContainerDir` and makes sure that the names don't clash.
    32  func RemoveDirectory(containerName string) error {
    33  	// Move the directory.
    34  	logger.Tracef("create old container dir: %s", RemovedContainerDir)
    35  	if err := os.MkdirAll(RemovedContainerDir, 0755); err != nil {
    36  		logger.Errorf("failed to create removed container directory: %v", err)
    37  		return err
    38  	}
    39  	removedDir, err := utils.UniqueDirectory(RemovedContainerDir, containerName)
    40  	if err != nil {
    41  		logger.Errorf("was not able to generate a unique directory: %v", err)
    42  		return err
    43  	}
    44  	if err := os.Rename(dirForName(containerName), removedDir); err != nil {
    45  		logger.Errorf("failed to rename container directory: %v", err)
    46  		return err
    47  	}
    48  	return nil
    49  
    50  }
    51  
    52  func dirForName(containerName string) string {
    53  	return filepath.Join(ContainerDir, containerName)
    54  }