From 069918e1fd9192f4395c286c50fa3a8f50002ada Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Tue, 26 Mar 2013 03:33:47 -0700 Subject: [PATCH 1/2] #174 Check if the image is already in the garbage before moving it. If it does, remove (really) the older one. --- graph.go | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/graph.go b/graph.go index 35f092703f..8043db0a88 100644 --- a/graph.go +++ b/graph.go @@ -6,6 +6,7 @@ import ( "os" "path" "path/filepath" + "strings" "time" ) @@ -103,12 +104,44 @@ func (graph *Graph) Garbage() (*Graph, error) { return NewGraph(path.Join(graph.Root, ":garbage:")) } +// Check if given error is "not empty" +// Note: this is the way golang do it internally with os.IsNotExists +func isNotEmpty(err error) bool { + switch pe := err.(type) { + case nil: + return false + case *os.PathError: + err = pe.Err + case *os.LinkError: + err = pe.Err + } + return strings.Contains(err.Error(), " not empty") +} + func (graph *Graph) Delete(id string) error { garbage, err := graph.Garbage() if err != nil { return err } - return os.Rename(graph.imageRoot(id), garbage.imageRoot(id)) + err = os.Rename(graph.imageRoot(id), garbage.imageRoot(id)) + if err != nil { + if isNotEmpty(err) { + Debugf("The image %s is already present in garbage. Removing it.", id) + if err = os.RemoveAll(garbage.imageRoot(id)); err != nil { + Debugf("Error while removing the image %s from garbage: %s\n", id, err) + return err + } + Debugf("Image %s removed from garbage", id) + if err = os.Rename(graph.imageRoot(id), garbage.imageRoot(id)); err != nil { + return err + } + Debugf("Image %s put in the garbage", id) + } else { + Debugf("Error putting the image %s to garbage: %s\n", id, err) + } + return err + } + return nil } func (graph *Graph) Undelete(id string) error { From baea0a98f84f7827485505425b23ee0ac7ce2a23 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 27 Mar 2013 12:09:58 -0700 Subject: [PATCH 2/2] Add unit test for #174 Delete image already deleted then repulled --- graph_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/graph_test.go b/graph_test.go index b9bdc5140f..61bac92d9e 100644 --- a/graph_test.go +++ b/graph_test.go @@ -158,6 +158,14 @@ func TestDelete(t *testing.T) { } assertNImages(graph, t, 1) + // Test delete twice (pull -> rm -> pull -> rm) + if err := graph.Register(archive, img1); err != nil { + t.Fatal(err) + } + if err := graph.Delete(img1.Id); err != nil { + t.Fatal(err) + } + assertNImages(graph, t, 1) } func assertNImages(graph *Graph, t *testing.T, n int) {