1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Fix golint issues on the windows graph driver.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-09-29 17:43:10 -04:00
parent 724b410c55
commit 1fffc7a89d

View file

@ -26,23 +26,30 @@ import (
"github.com/microsoft/hcsshim" "github.com/microsoft/hcsshim"
) )
// init registers the windows graph drivers to the register.
func init() { func init() {
graphdriver.Register("windowsfilter", InitFilter) graphdriver.Register("windowsfilter", InitFilter)
graphdriver.Register("windowsdiff", InitDiff) graphdriver.Register("windowsdiff", InitDiff)
} }
const ( const (
// diffDriver is an hcsshim driver type
diffDriver = iota diffDriver = iota
// filterDriver is an hcsshim driver type
filterDriver filterDriver
) )
// Driver represents a windows graph driver.
type Driver struct { type Driver struct {
info hcsshim.DriverInfo // info stores the shim driver information
sync.Mutex // Protects concurrent modification to active info hcsshim.DriverInfo
active map[string]int // Mutex protects concurrent modification to active
sync.Mutex
// active stores references to the activated layers
active map[string]int
} }
// New returns a new Windows storage filter driver. // InitFilter returns a new Windows storage filter driver.
func InitFilter(home string, options []string) (graphdriver.Driver, error) { func InitFilter(home string, options []string) (graphdriver.Driver, error) {
logrus.Debugf("WindowsGraphDriver InitFilter at %s", home) logrus.Debugf("WindowsGraphDriver InitFilter at %s", home)
d := &Driver{ d := &Driver{
@ -55,7 +62,7 @@ func InitFilter(home string, options []string) (graphdriver.Driver, error) {
return d, nil return d, nil
} }
// New returns a new Windows differencing disk driver. // InitDiff returns a new Windows differencing disk driver.
func InitDiff(home string, options []string) (graphdriver.Driver, error) { func InitDiff(home string, options []string) (graphdriver.Driver, error) {
logrus.Debugf("WindowsGraphDriver InitDiff at %s", home) logrus.Debugf("WindowsGraphDriver InitDiff at %s", home)
d := &Driver{ d := &Driver{
@ -68,6 +75,7 @@ func InitDiff(home string, options []string) (graphdriver.Driver, error) {
return d, nil return d, nil
} }
// String returns the string representation of a driver.
func (d *Driver) String() string { func (d *Driver) String() string {
switch d.info.Flavour { switch d.info.Flavour {
case diffDriver: case diffDriver:
@ -79,28 +87,29 @@ func (d *Driver) String() string {
} }
} }
// Status returns the status of the driver.
func (d *Driver) Status() [][2]string { func (d *Driver) Status() [][2]string {
return [][2]string{ return [][2]string{
{"Windows", ""}, {"Windows", ""},
} }
} }
// Exists returns true if the given id is registered with // Exists returns true if the given id is registered with this driver.
// this driver
func (d *Driver) Exists(id string) bool { func (d *Driver) Exists(id string) bool {
rId, err := d.resolveId(id) rID, err := d.resolveID(id)
if err != nil { if err != nil {
return false return false
} }
result, err := hcsshim.LayerExists(d.info, rId) result, err := hcsshim.LayerExists(d.info, rID)
if err != nil { if err != nil {
return false return false
} }
return result return result
} }
// Create creates a new layer with the given id.
func (d *Driver) Create(id, parent string) error { func (d *Driver) Create(id, parent string) error {
rPId, err := d.resolveId(parent) rPId, err := d.resolveID(parent)
if err != nil { if err != nil {
return err return err
} }
@ -154,21 +163,22 @@ func (d *Driver) Create(id, parent string) error {
return nil return nil
} }
// dir returns the absolute path to the layer.
func (d *Driver) dir(id string) string { func (d *Driver) dir(id string) string {
return filepath.Join(d.info.HomeDir, filepath.Base(id)) return filepath.Join(d.info.HomeDir, filepath.Base(id))
} }
// Remove unmounts and removes the dir information // Remove unmounts and removes the dir information.
func (d *Driver) Remove(id string) error { func (d *Driver) Remove(id string) error {
rId, err := d.resolveId(id) rID, err := d.resolveID(id)
if err != nil { if err != nil {
return err return err
} }
return hcsshim.DestroyLayer(d.info, rId) return hcsshim.DestroyLayer(d.info, rID)
} }
// Get returns the rootfs path for the id. This will mount the dir at it's given path // Get returns the rootfs path for the id. This will mount the dir at it's given path.
func (d *Driver) Get(id, mountLabel string) (string, error) { func (d *Driver) Get(id, mountLabel string) (string, error) {
logrus.Debugf("WindowsGraphDriver Get() id %s mountLabel %s", id, mountLabel) logrus.Debugf("WindowsGraphDriver Get() id %s mountLabel %s", id, mountLabel)
var dir string var dir string
@ -176,38 +186,38 @@ func (d *Driver) Get(id, mountLabel string) (string, error) {
d.Lock() d.Lock()
defer d.Unlock() defer d.Unlock()
rId, err := d.resolveId(id) rID, err := d.resolveID(id)
if err != nil { if err != nil {
return "", err return "", err
} }
// Getting the layer paths must be done outside of the lock. // Getting the layer paths must be done outside of the lock.
layerChain, err := d.getLayerChain(rId) layerChain, err := d.getLayerChain(rID)
if err != nil { if err != nil {
return "", err return "", err
} }
if d.active[rId] == 0 { if d.active[rID] == 0 {
if err := hcsshim.ActivateLayer(d.info, rId); err != nil { if err := hcsshim.ActivateLayer(d.info, rID); err != nil {
return "", err return "", err
} }
if err := hcsshim.PrepareLayer(d.info, rId, layerChain); err != nil { if err := hcsshim.PrepareLayer(d.info, rID, layerChain); err != nil {
if err2 := hcsshim.DeactivateLayer(d.info, rId); err2 != nil { if err2 := hcsshim.DeactivateLayer(d.info, rID); err2 != nil {
logrus.Warnf("Failed to Deactivate %s: %s", id, err) logrus.Warnf("Failed to Deactivate %s: %s", id, err)
} }
return "", err return "", err
} }
} }
mountPath, err := hcsshim.GetLayerMountPath(d.info, rId) mountPath, err := hcsshim.GetLayerMountPath(d.info, rID)
if err != nil { if err != nil {
if err2 := hcsshim.DeactivateLayer(d.info, rId); err2 != nil { if err2 := hcsshim.DeactivateLayer(d.info, rID); err2 != nil {
logrus.Warnf("Failed to Deactivate %s: %s", id, err) logrus.Warnf("Failed to Deactivate %s: %s", id, err)
} }
return "", err return "", err
} }
d.active[rId]++ d.active[rID]++
// If the layer has a mount path, use that. Otherwise, use the // If the layer has a mount path, use that. Otherwise, use the
// folder path. // folder path.
@ -220,10 +230,11 @@ func (d *Driver) Get(id, mountLabel string) (string, error) {
return dir, nil return dir, nil
} }
// Put adds a new layer to the driver.
func (d *Driver) Put(id string) error { func (d *Driver) Put(id string) error {
logrus.Debugf("WindowsGraphDriver Put() id %s", id) logrus.Debugf("WindowsGraphDriver Put() id %s", id)
rId, err := d.resolveId(id) rID, err := d.resolveID(id)
if err != nil { if err != nil {
return err return err
} }
@ -231,21 +242,22 @@ func (d *Driver) Put(id string) error {
d.Lock() d.Lock()
defer d.Unlock() defer d.Unlock()
if d.active[rId] > 1 { if d.active[rID] > 1 {
d.active[rId]-- d.active[rID]--
} else if d.active[rId] == 1 { } else if d.active[rID] == 1 {
if err := hcsshim.UnprepareLayer(d.info, rId); err != nil { if err := hcsshim.UnprepareLayer(d.info, rID); err != nil {
return err return err
} }
if err := hcsshim.DeactivateLayer(d.info, rId); err != nil { if err := hcsshim.DeactivateLayer(d.info, rID); err != nil {
return err return err
} }
delete(d.active, rId) delete(d.active, rID)
} }
return nil return nil
} }
// Cleanup ensures the information the driver stores is properly removed.
func (d *Driver) Cleanup() error { func (d *Driver) Cleanup() error {
return nil return nil
} }
@ -253,13 +265,13 @@ func (d *Driver) Cleanup() error {
// Diff produces an archive of the changes between the specified // Diff produces an archive of the changes between the specified
// layer and its parent layer which may be "". // layer and its parent layer which may be "".
func (d *Driver) Diff(id, parent string) (arch archive.Archive, err error) { func (d *Driver) Diff(id, parent string) (arch archive.Archive, err error) {
rId, err := d.resolveId(id) rID, err := d.resolveID(id)
if err != nil { if err != nil {
return return
} }
// Getting the layer paths must be done outside of the lock. // Getting the layer paths must be done outside of the lock.
layerChain, err := d.getLayerChain(rId) layerChain, err := d.getLayerChain(rID)
if err != nil { if err != nil {
return return
} }
@ -268,24 +280,24 @@ func (d *Driver) Diff(id, parent string) (arch archive.Archive, err error) {
// To support export, a layer must be activated but not prepared. // To support export, a layer must be activated but not prepared.
if d.info.Flavour == filterDriver { if d.info.Flavour == filterDriver {
if d.active[rId] == 0 { if d.active[rID] == 0 {
if err = hcsshim.ActivateLayer(d.info, rId); err != nil { if err = hcsshim.ActivateLayer(d.info, rID); err != nil {
d.Unlock() d.Unlock()
return return
} }
defer func() { defer func() {
if err := hcsshim.DeactivateLayer(d.info, rId); err != nil { if err := hcsshim.DeactivateLayer(d.info, rID); err != nil {
logrus.Warnf("Failed to Deactivate %s: %s", rId, err) logrus.Warnf("Failed to Deactivate %s: %s", rID, err)
} }
}() }()
} else { } else {
if err = hcsshim.UnprepareLayer(d.info, rId); err != nil { if err = hcsshim.UnprepareLayer(d.info, rID); err != nil {
d.Unlock() d.Unlock()
return return
} }
defer func() { defer func() {
if err := hcsshim.PrepareLayer(d.info, rId, layerChain); err != nil { if err := hcsshim.PrepareLayer(d.info, rID, layerChain); err != nil {
logrus.Warnf("Failed to re-PrepareLayer %s: %s", rId, err) logrus.Warnf("Failed to re-PrepareLayer %s: %s", rID, err)
} }
}() }()
} }
@ -293,7 +305,7 @@ func (d *Driver) Diff(id, parent string) (arch archive.Archive, err error) {
d.Unlock() d.Unlock()
return d.exportLayer(rId, layerChain) return d.exportLayer(rID, layerChain)
} }
// Changes produces a list of changes between the specified layer // Changes produces a list of changes between the specified layer
@ -306,7 +318,7 @@ func (d *Driver) Changes(id, parent string) ([]archive.Change, error) {
// layer with the specified id and parent, returning the size of the // layer with the specified id and parent, returning the size of the
// new layer in bytes. // new layer in bytes.
func (d *Driver) ApplyDiff(id, parent string, diff archive.Reader) (size int64, err error) { func (d *Driver) ApplyDiff(id, parent string, diff archive.Reader) (size int64, err error) {
rPId, err := d.resolveId(parent) rPId, err := d.resolveID(parent)
if err != nil { if err != nil {
return return
} }
@ -350,7 +362,7 @@ func (d *Driver) ApplyDiff(id, parent string, diff archive.Reader) (size int64,
// and its parent and returns the size in bytes of the changes // and its parent and returns the size in bytes of the changes
// relative to its base filesystem directory. // relative to its base filesystem directory.
func (d *Driver) DiffSize(id, parent string) (size int64, err error) { func (d *Driver) DiffSize(id, parent string) (size int64, err error) {
rPId, err := d.resolveId(parent) rPId, err := d.resolveID(parent)
if err != nil { if err != nil {
return return
} }
@ -369,6 +381,7 @@ func (d *Driver) DiffSize(id, parent string) (size int64, err error) {
return archive.ChangesSize(layerFs, changes), nil return archive.ChangesSize(layerFs, changes), nil
} }
// RestoreCustomImages adds any auto-detected OS specific images to the tag and graph store.
func (d *Driver) RestoreCustomImages(tagger graphdriver.Tagger, recorder graphdriver.Recorder) (imageIDs []string, err error) { func (d *Driver) RestoreCustomImages(tagger graphdriver.Tagger, recorder graphdriver.Recorder) (imageIDs []string, err error) {
strData, err := hcsshim.GetSharedBaseImages() strData, err := hcsshim.GetSharedBaseImages()
if err != nil { if err != nil {
@ -422,7 +435,7 @@ func (d *Driver) RestoreCustomImages(tagger graphdriver.Tagger, recorder graphdr
} }
// Create the alternate ID file. // Create the alternate ID file.
if err := d.setId(img.ID, folderName); err != nil { if err := d.setID(img.ID, folderName); err != nil {
return nil, err return nil, err
} }
@ -433,12 +446,14 @@ func (d *Driver) RestoreCustomImages(tagger graphdriver.Tagger, recorder graphdr
return imageIDs, nil return imageIDs, nil
} }
// GetMetadata returns custom driver information.
func (d *Driver) GetMetadata(id string) (map[string]string, error) { func (d *Driver) GetMetadata(id string) (map[string]string, error) {
m := make(map[string]string) m := make(map[string]string)
m["dir"] = d.dir(id) m["dir"] = d.dir(id)
return m, nil return m, nil
} }
// exportLayer generates an archive from a layer based on the given ID.
func (d *Driver) exportLayer(id string, parentLayerPaths []string) (arch archive.Archive, err error) { func (d *Driver) exportLayer(id string, parentLayerPaths []string) (arch archive.Archive, err error) {
layerFolder := d.dir(id) layerFolder := d.dir(id)
@ -476,6 +491,7 @@ func (d *Driver) exportLayer(id string, parentLayerPaths []string) (arch archive
} }
// importLayer adds a new layer to the tag and graph store based on the given data.
func (d *Driver) importLayer(id string, layerData archive.Reader, parentLayerPaths []string) (size int64, err error) { func (d *Driver) importLayer(id string, layerData archive.Reader, parentLayerPaths []string) (size int64, err error) {
layerFolder := d.dir(id) layerFolder := d.dir(id)
@ -505,8 +521,9 @@ func (d *Driver) importLayer(id string, layerData archive.Reader, parentLayerPat
return return
} }
func (d *Driver) resolveId(id string) (string, error) { // resolveID computes the layerID information based on the given id.
content, err := ioutil.ReadFile(filepath.Join(d.dir(id), "layerId")) func (d *Driver) resolveID(id string) (string, error) {
content, err := ioutil.ReadFile(filepath.Join(d.dir(id), "layerID"))
if os.IsNotExist(err) { if os.IsNotExist(err) {
return id, nil return id, nil
} else if err != nil { } else if err != nil {
@ -515,14 +532,16 @@ func (d *Driver) resolveId(id string) (string, error) {
return string(content), nil return string(content), nil
} }
func (d *Driver) setId(id, altId string) error { // setID stores the layerId in disk.
err := ioutil.WriteFile(filepath.Join(d.dir(id), "layerId"), []byte(altId), 0600) func (d *Driver) setID(id, altID string) error {
err := ioutil.WriteFile(filepath.Join(d.dir(id), "layerId"), []byte(altID), 0600)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
// getLayerChain returns the layer chain information.
func (d *Driver) getLayerChain(id string) ([]string, error) { func (d *Driver) getLayerChain(id string) ([]string, error) {
jPath := filepath.Join(d.dir(id), "layerchain.json") jPath := filepath.Join(d.dir(id), "layerchain.json")
content, err := ioutil.ReadFile(jPath) content, err := ioutil.ReadFile(jPath)
@ -541,6 +560,7 @@ func (d *Driver) getLayerChain(id string) ([]string, error) {
return layerChain, nil return layerChain, nil
} }
// setLayerChain stores the layer chain information in disk.
func (d *Driver) setLayerChain(id string, chain []string) error { func (d *Driver) setLayerChain(id string, chain []string) error {
content, err := json.Marshal(&chain) content, err := json.Marshal(&chain)
if err != nil { if err != nil {