[lxc-devel] [lxd/master] Bugfixes

stgraber on Github lxc-bot at linuxcontainers.org
Tue Jan 24 19:06:57 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170124/8aadfbac/attachment.bin>
-------------- next part --------------
From 426461fb2b2aff59aa0a02f3caa970a80398b52a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 24 Jan 2017 11:43:22 -0500
Subject: [PATCH 1/2] Better handle timestamps
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxc/image.go                          |  6 +++---
 lxc/info.go                           |  4 ++--
 lxc/list.go                           |  4 ++--
 shared/simplestreams/simplestreams.go |  4 ++--
 shared/util.go                        | 13 +++++++++++++
 5 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/lxc/image.go b/lxc/image.go
index b4ad827..bd8836f 100644
--- a/lxc/image.go
+++ b/lxc/image.go
@@ -347,16 +347,16 @@ func (c *imageCmd) run(config *lxd.Config, args []string) error {
 		fmt.Printf(i18n.G("Public: %s")+"\n", public)
 		fmt.Printf(i18n.G("Timestamps:") + "\n")
 		const layout = "2006/01/02 15:04 UTC"
-		if info.CreatedAt.UTC().Unix() != 0 {
+		if shared.TimeIsSet(info.CreatedAt) {
 			fmt.Printf("    "+i18n.G("Created: %s")+"\n", info.CreatedAt.UTC().Format(layout))
 		}
 		fmt.Printf("    "+i18n.G("Uploaded: %s")+"\n", info.UploadedAt.UTC().Format(layout))
-		if info.ExpiresAt.UTC().Unix() != 0 {
+		if shared.TimeIsSet(info.ExpiresAt) {
 			fmt.Printf("    "+i18n.G("Expires: %s")+"\n", info.ExpiresAt.UTC().Format(layout))
 		} else {
 			fmt.Printf("    " + i18n.G("Expires: never") + "\n")
 		}
-		if info.LastUsedAt.UTC().Unix() != 0 {
+		if shared.TimeIsSet(info.LastUsedAt) {
 			fmt.Printf("    "+i18n.G("Last used: %s")+"\n", info.LastUsedAt.UTC().Format(layout))
 		} else {
 			fmt.Printf("    " + i18n.G("Last used: never") + "\n")
diff --git a/lxc/info.go b/lxc/info.go
index 30a3f66..2300ce0 100644
--- a/lxc/info.go
+++ b/lxc/info.go
@@ -91,7 +91,7 @@ func (c *infoCmd) containerInfo(d *lxd.Client, name string, showLog bool) error
 		fmt.Printf(i18n.G("Remote: %s")+"\n", d.Remote.Addr)
 	}
 	fmt.Printf(i18n.G("Architecture: %s")+"\n", ct.Architecture)
-	if ct.CreatedAt.UTC().Unix() != 0 {
+	if shared.TimeIsSet(ct.CreatedAt) {
 		fmt.Printf(i18n.G("Created: %s")+"\n", ct.CreatedAt.UTC().Format(layout))
 	}
 
@@ -211,7 +211,7 @@ func (c *infoCmd) containerInfo(d *lxd.Client, name string, showLog bool) error
 		fields := strings.Split(snap.Name, shared.SnapshotDelimiter)
 		fmt.Printf("  %s", fields[len(fields)-1])
 
-		if snap.CreationDate.UTC().Unix() != 0 {
+		if shared.TimeIsSet(snap.CreationDate) {
 			fmt.Printf(" ("+i18n.G("taken at %s")+")", snap.CreationDate.UTC().Format(layout))
 		}
 
diff --git a/lxc/list.go b/lxc/list.go
index 1fd9317..e492007 100644
--- a/lxc/list.go
+++ b/lxc/list.go
@@ -604,7 +604,7 @@ func (c *listCmd) ProfilesColumnData(cInfo api.Container, cState *api.ContainerS
 func (c *listCmd) CreatedColumnData(cInfo api.Container, cState *api.ContainerState, cSnaps []api.ContainerSnapshot) string {
 	layout := "2006/01/02 15:04 UTC"
 
-	if cInfo.CreatedAt.UTC().Unix() != 0 {
+	if shared.TimeIsSet(cInfo.CreatedAt) {
 		return cInfo.CreatedAt.UTC().Format(layout)
 	}
 
@@ -614,7 +614,7 @@ func (c *listCmd) CreatedColumnData(cInfo api.Container, cState *api.ContainerSt
 func (c *listCmd) LastUsedColumnData(cInfo api.Container, cState *api.ContainerState, cSnaps []api.ContainerSnapshot) string {
 	layout := "2006/01/02 15:04 UTC"
 
-	if !cInfo.LastUsedAt.IsZero() && cInfo.LastUsedAt.UTC().Unix() != 0 {
+	if !cInfo.LastUsedAt.IsZero() && shared.TimeIsSet(cInfo.LastUsedAt) {
 		return cInfo.LastUsedAt.UTC().Format(layout)
 	}
 
diff --git a/shared/simplestreams/simplestreams.go b/shared/simplestreams/simplestreams.go
index d71fcd3..490cc4f 100644
--- a/shared/simplestreams/simplestreams.go
+++ b/shared/simplestreams/simplestreams.go
@@ -32,11 +32,11 @@ func (a ssSortImage) Swap(i, j int) {
 func (a ssSortImage) Less(i, j int) bool {
 	if a[i].Properties["os"] == a[j].Properties["os"] {
 		if a[i].Properties["release"] == a[j].Properties["release"] {
-			if a[i].CreatedAt.UTC().Unix() == 0 {
+			if !shared.TimeIsSet(a[i].CreatedAt) {
 				return true
 			}
 
-			if a[j].CreatedAt.UTC().Unix() == 0 {
+			if !shared.TimeIsSet(a[j].CreatedAt) {
 				return false
 			}
 
diff --git a/shared/util.go b/shared/util.go
index 4e939c4..21c2b29 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -19,6 +19,7 @@ import (
 	"regexp"
 	"strconv"
 	"strings"
+	"time"
 	"unicode"
 )
 
@@ -773,3 +774,15 @@ func RunCommand(name string, arg ...string) error {
 
 	return nil
 }
+
+func TimeIsSet(ts time.Time) bool {
+	if ts.Unix() <= 0 {
+		return false
+	}
+
+	if ts.UTC().Unix() <= 0 {
+		return false
+	}
+
+	return true
+}

From 7ef8beb81b5f7ec3214489179fca3d634ee7fd21 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 24 Jan 2017 14:02:11 -0500
Subject: [PATCH 2/2] Improve error handling and reporting during export
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Related to issue #2801

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/container_lxc.go | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 4ba8031..6a9c7e0 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -3833,21 +3833,30 @@ func (c *containerLXC) Export(w io.Writer, properties map[string]string) error {
 
 	// Include all the rootfs files
 	fnam = c.RootfsPath()
-	filepath.Walk(fnam, writeToTar)
+	err = filepath.Walk(fnam, writeToTar)
+	if err != nil {
+		shared.LogError("Failed exporting container", ctxMap)
+		return err
+	}
 
 	// Include all the templates
 	fnam = c.TemplatesPath()
 	if shared.PathExists(fnam) {
-		filepath.Walk(fnam, writeToTar)
+		err = filepath.Walk(fnam, writeToTar)
+		if err != nil {
+			shared.LogError("Failed exporting container", ctxMap)
+			return err
+		}
 	}
 
 	err = tw.Close()
 	if err != nil {
 		shared.LogError("Failed exporting container", ctxMap)
+		return err
 	}
 
 	shared.LogInfo("Exported container", ctxMap)
-	return err
+	return nil
 }
 
 func collectCRIULogFile(c container, imagesDir string, function string, method string) error {
@@ -4744,13 +4753,15 @@ func (c *containerLXC) tarStoreFile(linkmap map[uint64]string, offset int, tw *t
 	if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
 		link, err = os.Readlink(path)
 		if err != nil {
-			return err
+			return fmt.Errorf("failed to resolve symlink: %s", err)
 		}
 	}
+
 	hdr, err := tar.FileInfoHeader(fi, link)
 	if err != nil {
-		return err
+		return fmt.Errorf("failed to create tar info header: %s", err)
 	}
+
 	hdr.Name = path[offset:]
 	if fi.IsDir() || fi.Mode()&os.ModeSymlink == os.ModeSymlink {
 		hdr.Size = 0
@@ -4760,7 +4771,7 @@ func (c *containerLXC) tarStoreFile(linkmap map[uint64]string, offset int, tw *t
 
 	hdr.Uid, hdr.Gid, major, minor, ino, nlink, err = shared.GetFileStat(path)
 	if err != nil {
-		return fmt.Errorf("error getting file info: %s", err)
+		return fmt.Errorf("failed to get file stat: %s", err)
 	}
 
 	// Unshift the id under /rootfs/ for unpriv containers
@@ -4770,6 +4781,7 @@ func (c *containerLXC) tarStoreFile(linkmap map[uint64]string, offset int, tw *t
 			return nil
 		}
 	}
+
 	if major != -1 {
 		hdr.Devmajor = int64(major)
 		hdr.Devminor = int64(minor)
@@ -4789,23 +4801,25 @@ func (c *containerLXC) tarStoreFile(linkmap map[uint64]string, offset int, tw *t
 	// Handle xattrs.
 	hdr.Xattrs, err = shared.GetAllXattr(path)
 	if err != nil {
-		return err
+		return fmt.Errorf("failed to read xattr: %s", err)
 	}
 
 	if err := tw.WriteHeader(hdr); err != nil {
-		return fmt.Errorf("error writing header: %s", err)
+		return fmt.Errorf("failed to write tar header: %s", err)
 	}
 
 	if hdr.Typeflag == tar.TypeReg {
 		f, err := os.Open(path)
 		if err != nil {
-			return fmt.Errorf("tarStoreFile: error opening file: %s", err)
+			return fmt.Errorf("failed to open the file: %s", err)
 		}
 		defer f.Close()
+
 		if _, err := io.Copy(tw, f); err != nil {
-			return fmt.Errorf("error copying file %s", err)
+			return fmt.Errorf("failed to copy file content: %s", err)
 		}
 	}
+
 	return nil
 }
 


More information about the lxc-devel mailing list