[lxc-devel] [lxd/master] tree-wide: use containerGetParentAndSnapshotName()

brauner on Github lxc-bot at linuxcontainers.org
Tue Apr 25 14:41:44 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170425/f6939eb3/attachment.bin>
-------------- next part --------------
From 2fe8d23fac8f905828be40196960e93ad8026d37 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 25 Apr 2017 16:24:17 +0200
Subject: [PATCH] tree-wide: use containerGetParentAndSnapshotName()

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 lxd/container_lxc.go       |  2 +-
 lxd/container_snapshot.go  |  3 +-
 lxd/db_update.go           |  6 ++--
 lxd/storage_btrfs.go       | 22 +++++-------
 lxd/storage_dir.go         | 18 +++++-----
 lxd/storage_lvm.go         | 21 +++++------
 lxd/storage_pools_utils.go |  4 +--
 lxd/storage_zfs.go         | 88 +++++++++++++++++++---------------------------
 8 files changed, 69 insertions(+), 95 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index a897c7a..b222af3 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -4000,7 +4000,7 @@ func (c *containerLXC) Export(w io.Writer, properties map[string]string) error {
 		// Get the container's architecture
 		var arch string
 		if c.IsSnapshot() {
-			parentName := strings.SplitN(c.name, shared.SnapshotDelimiter, 2)[0]
+			parentName, _, _ := containerGetParentAndSnapshotName(c.name)
 			parent, err := containerLoadByName(c.daemon, parentName)
 			if err != nil {
 				tw.Close()
diff --git a/lxd/container_snapshot.go b/lxd/container_snapshot.go
index 3e022ac..a4d2ace 100644
--- a/lxd/container_snapshot.go
+++ b/lxd/container_snapshot.go
@@ -5,7 +5,6 @@ import (
 	"fmt"
 	"net/http"
 	"strconv"
-	"strings"
 
 	"github.com/gorilla/mux"
 
@@ -36,7 +35,7 @@ func containerSnapshotsGet(d *Daemon, r *http.Request) Response {
 	resultMap := []*api.ContainerSnapshot{}
 
 	for _, snap := range snaps {
-		snapName := strings.SplitN(snap.Name(), shared.SnapshotDelimiter, 2)[1]
+		_, snapName, _ := containerGetParentAndSnapshotName(snap.Name())
 		if recursion == 0 {
 			url := fmt.Sprintf("/%s/containers/%s/snapshots/%s", version.APIVersion, cname, snapName)
 			resultString = append(resultString, url)
diff --git a/lxd/db_update.go b/lxd/db_update.go
index bc641c6..ee1dc71 100644
--- a/lxd/db_update.go
+++ b/lxd/db_update.go
@@ -561,9 +561,9 @@ func dbUpdateFromV11(currentVersion int, version int, d *Daemon) error {
 	errors := 0
 
 	for _, cName := range cNames {
-		snappieces := strings.SplitN(cName, shared.SnapshotDelimiter, 2)
-		oldPath := shared.VarPath("containers", snappieces[0], "snapshots", snappieces[1])
-		newPath := shared.VarPath("snapshots", snappieces[0], snappieces[1])
+		snapParentName, snapOnlyName, _ := containerGetParentAndSnapshotName(cName)
+		oldPath := shared.VarPath("containers", snapParentName, "snapshots", snapOnlyName)
+		newPath := shared.VarPath("snapshots", snapParentName, snapOnlyName)
 		if shared.PathExists(oldPath) && !shared.PathExists(newPath) {
 			logger.Info(
 				"Moving snapshot",
diff --git a/lxd/storage_btrfs.go b/lxd/storage_btrfs.go
index 56424e3..df66a02 100644
--- a/lxd/storage_btrfs.go
+++ b/lxd/storage_btrfs.go
@@ -796,10 +796,10 @@ func (s *storageBtrfs) copySnapshot(target container, source container) error {
 	sourceContainerSubvolumeName := getSnapshotMountPoint(s.pool.Name, sourceName)
 	targetContainerSubvolumeName := getSnapshotMountPoint(s.pool.Name, targetName)
 
-	fields := strings.SplitN(target.Name(), shared.SnapshotDelimiter, 2)
-	containersPath := getSnapshotMountPoint(s.pool.Name, fields[0])
-	snapshotMntPointSymlinkTarget := shared.VarPath("storage-pools", s.pool.Name, "snapshots", fields[0])
-	snapshotMntPointSymlink := shared.VarPath("snapshots", fields[0])
+	targetParentName, _, _ := containerGetParentAndSnapshotName(target.Name())
+	containersPath := getSnapshotMountPoint(s.pool.Name, targetParentName)
+	snapshotMntPointSymlinkTarget := shared.VarPath("storage-pools", s.pool.Name, "snapshots", targetParentName)
+	snapshotMntPointSymlink := shared.VarPath("snapshots", targetParentName)
 	err := createSnapshotMountpoint(containersPath, snapshotMntPointSymlinkTarget, snapshotMntPointSymlink)
 	if err != nil {
 		return err
@@ -871,11 +871,10 @@ func (s *storageBtrfs) ContainerCopy(target container, source container, contain
 			return err
 		}
 
-		fields := strings.SplitN(snap.Name(), shared.SnapshotDelimiter, 2)
-		newSnapName := fmt.Sprintf("%s/%s", target.Name(), fields[1])
+		_, snapOnlyName, _ := containerGetParentAndSnapshotName(snap.Name())
+		newSnapName := fmt.Sprintf("%s/%s", target.Name(), snapOnlyName)
 		targetSnapshot, err := containerLoadByName(s.d, newSnapName)
 		if err != nil {
-			logger.Errorf("1111: %s", newSnapName)
 			return err
 		}
 
@@ -1131,8 +1130,7 @@ func (s *storageBtrfs) ContainerSnapshotDelete(snapshotContainer container) erro
 	os.Remove(sourceSnapshotMntPoint)
 	os.Remove(snapshotSubvolumeName)
 
-	sourceFields := strings.SplitN(snapshotContainer.Name(), shared.SnapshotDelimiter, 2)
-	sourceName := sourceFields[0]
+	sourceName, _, _ := containerGetParentAndSnapshotName(snapshotContainer.Name())
 	snapshotSubvolumePath := s.getSnapshotSubvolumePath(s.pool.Name, sourceName)
 	os.Remove(snapshotSubvolumePath)
 	if !shared.PathExists(snapshotSubvolumePath) {
@@ -1237,8 +1235,7 @@ func (s *storageBtrfs) ContainerSnapshotCreateEmpty(snapshotContainer container)
 	}
 
 	// Create the snapshot subvole path on the storage pool.
-	sourceFields := strings.SplitN(snapshotContainer.Name(), shared.SnapshotDelimiter, 2)
-	sourceName := sourceFields[0]
+	sourceName, _, _ := containerGetParentAndSnapshotName(snapshotContainer.Name())
 	snapshotSubvolumePath := s.getSnapshotSubvolumePath(s.pool.Name, sourceName)
 	snapshotSubvolumeName := getSnapshotMountPoint(s.pool.Name, snapshotContainer.Name())
 	if !shared.PathExists(snapshotSubvolumePath) {
@@ -1757,8 +1754,7 @@ func (s *btrfsMigrationSourceDriver) SendWhileRunning(conn *websocket.Conn, op *
 	// Deal with sending a snapshot to create a container on another LXD
 	// instance.
 	if s.container.IsSnapshot() {
-		sourceFields := strings.SplitN(containerName, shared.SnapshotDelimiter, 2)
-		sourceName = sourceFields[0]
+		sourceName, _, _ := containerGetParentAndSnapshotName(containerName)
 		snapshotsPath := getSnapshotMountPoint(containerPool, sourceName)
 		tmpContainerMntPoint, err := ioutil.TempDir(snapshotsPath, sourceName)
 		if err != nil {
diff --git a/lxd/storage_dir.go b/lxd/storage_dir.go
index 7e4677c..95dd459 100644
--- a/lxd/storage_dir.go
+++ b/lxd/storage_dir.go
@@ -396,10 +396,10 @@ func (s *storageDir) copySnapshot(target container, source container) error {
 	sourceContainerMntPoint := getSnapshotMountPoint(s.pool.Name, sourceName)
 	targetContainerMntPoint := getSnapshotMountPoint(s.pool.Name, targetName)
 
-	fields := strings.SplitN(target.Name(), shared.SnapshotDelimiter, 2)
-	containersPath := getSnapshotMountPoint(s.pool.Name, fields[0])
-	snapshotMntPointSymlinkTarget := shared.VarPath("storage-pools", s.pool.Name, "snapshots", fields[0])
-	snapshotMntPointSymlink := shared.VarPath("snapshots", fields[0])
+	targetParentName, _, _ := containerGetParentAndSnapshotName(target.Name())
+	containersPath := getSnapshotMountPoint(s.pool.Name, targetParentName)
+	snapshotMntPointSymlinkTarget := shared.VarPath("storage-pools", s.pool.Name, "snapshots", targetParentName)
+	snapshotMntPointSymlink := shared.VarPath("snapshots", targetParentName)
 	err := createSnapshotMountpoint(containersPath, snapshotMntPointSymlinkTarget, snapshotMntPointSymlink)
 	if err != nil {
 		return err
@@ -457,8 +457,8 @@ func (s *storageDir) ContainerCopy(target container, source container, container
 			return err
 		}
 
-		fields := strings.SplitN(snap.Name(), shared.SnapshotDelimiter, 2)
-		newSnapName := fmt.Sprintf("%s/%s", target.Name(), fields[1])
+		_, snapOnlyName, _ := containerGetParentAndSnapshotName(snap.Name())
+		newSnapName := fmt.Sprintf("%s/%s", target.Name(), snapOnlyName)
 		targetSnapshot, err := containerLoadByName(s.d, newSnapName)
 		if err != nil {
 			return err
@@ -655,8 +655,7 @@ func (s *storageDir) ContainerSnapshotCreateEmpty(snapshotContainer container) e
 	// Check if the symlink
 	// ${LXD_DIR}/snapshots/<source_container_name> -> ${POOL_PATH}/snapshots/<source_container_name>
 	// exists and if not create it.
-	fields := strings.SplitN(targetContainerName, shared.SnapshotDelimiter, 2)
-	sourceContainerName := fields[0]
+	sourceContainerName, _, _ := containerGetParentAndSnapshotName(targetContainerName)
 	sourceContainerSymlink := shared.VarPath("snapshots", sourceContainerName)
 	sourceContainerSymlinkTarget := getSnapshotMountPoint(s.pool.Name, sourceContainerName)
 	if !shared.PathExists(sourceContainerSymlink) {
@@ -694,8 +693,7 @@ func (s *storageDir) ContainerSnapshotDelete(snapshotContainer container) error
 	// Check if we can remove the snapshot symlink:
 	// ${LXD_DIR}/snapshots/<container_name> -> ${POOL}/snapshots/<container_name>
 	// by checking if the directory is empty.
-	fields := strings.SplitN(snapshotContainerName, shared.SnapshotDelimiter, 2)
-	sourceContainerName := fields[0]
+	sourceContainerName, _, _ := containerGetParentAndSnapshotName(snapshotContainerName)
 	snapshotContainerPath := getSnapshotMountPoint(s.pool.Name, sourceContainerName)
 	empty, _ := shared.PathIsEmpty(snapshotContainerPath)
 	if empty == true {
diff --git a/lxd/storage_lvm.go b/lxd/storage_lvm.go
index abdfed3..127eff4 100644
--- a/lxd/storage_lvm.go
+++ b/lxd/storage_lvm.go
@@ -1060,8 +1060,7 @@ func (s *storageLvm) ContainerCreate(container container) error {
 
 	if container.IsSnapshot() {
 		containerMntPoint := getSnapshotMountPoint(s.pool.Name, containerName)
-		fields := strings.SplitN(containerName, shared.SnapshotDelimiter, 2)
-		sourceName := fields[0]
+		sourceName, _, _ := containerGetParentAndSnapshotName(containerName)
 		snapshotMntPointSymlinkTarget := shared.VarPath("storage-pools", s.pool.Name, "snapshots", sourceName)
 		snapshotMntPointSymlink := shared.VarPath("snapshots", sourceName)
 		err := os.MkdirAll(containerMntPoint, 0755)
@@ -1274,8 +1273,7 @@ func (s *storageLvm) ContainerDelete(container container) error {
 	}
 
 	if container.IsSnapshot() {
-		fields := strings.SplitN(containerName, shared.SnapshotDelimiter, 2)
-		sourceName := fields[0]
+		sourceName, _, _ := containerGetParentAndSnapshotName(containerName)
 		snapshotMntPointSymlinkTarget := shared.VarPath("storage-pools", s.pool.Name, "snapshots", sourceName)
 		snapshotMntPointSymlink := shared.VarPath("snapshots", sourceName)
 		err = deleteSnapshotMountpoint(containerMntPoint, snapshotMntPointSymlinkTarget, snapshotMntPointSymlink)
@@ -1400,10 +1398,10 @@ func (s *storageLvm) copyContainerThinpool(target container, source container, r
 }
 
 func (s *storageLvm) copySnapshot(target container, source container) error {
-	fields := strings.SplitN(target.Name(), shared.SnapshotDelimiter, 2)
-	containersPath := getSnapshotMountPoint(s.pool.Name, fields[0])
-	snapshotMntPointSymlinkTarget := shared.VarPath("storage-pools", s.pool.Name, "snapshots", fields[0])
-	snapshotMntPointSymlink := shared.VarPath("snapshots", fields[0])
+	targetParentName, _, _ := containerGetParentAndSnapshotName(target.Name())
+	containersPath := getSnapshotMountPoint(s.pool.Name, targetParentName)
+	snapshotMntPointSymlinkTarget := shared.VarPath("storage-pools", s.pool.Name, "snapshots", targetParentName)
+	snapshotMntPointSymlink := shared.VarPath("snapshots", targetParentName)
 	err := createSnapshotMountpoint(containersPath, snapshotMntPointSymlinkTarget, snapshotMntPointSymlink)
 	if err != nil {
 		return err
@@ -1460,8 +1458,8 @@ func (s *storageLvm) ContainerCopy(target container, source container, container
 	}
 
 	for _, snap := range snapshots {
-		fields := strings.SplitN(snap.Name(), shared.SnapshotDelimiter, 2)
-		newSnapName := fmt.Sprintf("%s/%s", target.Name(), fields[1])
+		_, snapOnlyName, _ := containerGetParentAndSnapshotName(snap.Name())
+		newSnapName := fmt.Sprintf("%s/%s", target.Name(), snapOnlyName)
 
 		logger.Debugf("Copying LVM container storage for snapshot %s -> %s.", snap.Name(), newSnapName)
 
@@ -1780,8 +1778,7 @@ func (s *storageLvm) createSnapshotContainer(snapshotContainer container, source
 	targetIsSnapshot := snapshotContainer.IsSnapshot()
 	if targetIsSnapshot {
 		targetContainerMntPoint = getSnapshotMountPoint(s.pool.Name, targetContainerName)
-		sourceFields := strings.SplitN(sourceContainerName, shared.SnapshotDelimiter, 2)
-		sourceName := sourceFields[0]
+		sourceName, _, _ := containerGetParentAndSnapshotName(sourceContainerName)
 		snapshotMntPointSymlinkTarget := shared.VarPath("storage-pools", poolName, "snapshots", sourceName)
 		snapshotMntPointSymlink := shared.VarPath("snapshots", sourceName)
 		err = createSnapshotMountpoint(targetContainerMntPoint, snapshotMntPointSymlinkTarget, snapshotMntPointSymlink)
diff --git a/lxd/storage_pools_utils.go b/lxd/storage_pools_utils.go
index e43d102..bcac4d1 100644
--- a/lxd/storage_pools_utils.go
+++ b/lxd/storage_pools_utils.go
@@ -102,8 +102,8 @@ func storagePoolUsedByGet(db *sql.DB, poolID int64, poolName string) ([]string,
 		switch apiEndpoint {
 		case storagePoolVolumeAPIEndpointContainers:
 			if strings.Index(volumes[i].Name, shared.SnapshotDelimiter) > 0 {
-				fields := strings.SplitN(volumes[i].Name, shared.SnapshotDelimiter, 2)
-				poolUsedBy[i] = fmt.Sprintf("/%s/containers/%s/snapshots/%s", version.APIVersion, fields[0], fields[1])
+				parentName, snapOnlyName, _ := containerGetParentAndSnapshotName(volumes[i].Name)
+				poolUsedBy[i] = fmt.Sprintf("/%s/containers/%s/snapshots/%s", version.APIVersion, parentName, snapOnlyName)
 			} else {
 				poolUsedBy[i] = fmt.Sprintf("/%s/containers/%s", version.APIVersion, volumes[i].Name)
 			}
diff --git a/lxd/storage_zfs.go b/lxd/storage_zfs.go
index b6552a0..26fb7fc 100644
--- a/lxd/storage_zfs.go
+++ b/lxd/storage_zfs.go
@@ -736,13 +736,12 @@ func (s *storageZfs) copyWithoutSnapshotsSparse(target container, source contain
 
 	sourceZfsDataset := ""
 	sourceZfsDatasetSnapshot := ""
-	sourceFields := strings.SplitN(sourceContainerName, shared.SnapshotDelimiter, 2)
-	sourceName := sourceFields[0]
+	sourceName, sourceSnapOnlyName, isSnapshotName := containerGetParentAndSnapshotName(sourceContainerName)
 
 	targetZfsDataset := fmt.Sprintf("containers/%s", targetContainerName)
 
-	if len(sourceFields) == 2 {
-		sourceZfsDatasetSnapshot = sourceFields[1]
+	if isSnapshotName {
+		sourceZfsDatasetSnapshot = sourceSnapOnlyName
 	}
 
 	revert := true
@@ -842,10 +841,10 @@ func (s *storageZfs) copyWithoutSnapshotFull(target container, source container)
 	targetSnapshotDataset := targetDataset
 
 	if sourceIsSnapshot {
-		sourceFields := strings.SplitN(source.Name(), shared.SnapshotDelimiter, 2)
-		snapshotSuffix = fmt.Sprintf("snapshot-%s", sourceFields[1])
-		sourceDataset = fmt.Sprintf("%s/containers/%s@%s", poolName, sourceFields[0], snapshotSuffix)
-		targetSnapshotDataset = fmt.Sprintf("%s/containers/%s at snapshot-%s", poolName, targetName, sourceFields[1])
+		sourceParentName, sourceSnapOnlyName, _ := containerGetParentAndSnapshotName(source.Name())
+		snapshotSuffix = fmt.Sprintf("snapshot-%s", sourceSnapOnlyName)
+		sourceDataset = fmt.Sprintf("%s/containers/%s@%s", poolName, sourceParentName, snapshotSuffix)
+		targetSnapshotDataset = fmt.Sprintf("%s/containers/%s at snapshot-%s", poolName, targetName, sourceSnapOnlyName)
 	} else {
 		snapshotSuffix = uuid.NewRandom().String()
 		sourceDataset = fmt.Sprintf("%s/containers/%s@%s", poolName, sourceName, snapshotSuffix)
@@ -916,27 +915,27 @@ func (s *storageZfs) copyWithoutSnapshotFull(target container, source container)
 
 func (s *storageZfs) copyWithSnapshots(target container, source container, parentSnapshot string) error {
 	sourceName := source.Name()
-	fields := strings.SplitN(target.Name(), shared.SnapshotDelimiter, 2)
-	containersPath := getSnapshotMountPoint(s.pool.Name, fields[0])
-	snapshotMntPointSymlinkTarget := shared.VarPath("storage-pools", s.pool.Name, "snapshots", fields[0])
-	snapshotMntPointSymlink := shared.VarPath("snapshots", fields[0])
+	targetParentName, _, _ := containerGetParentAndSnapshotName(target.Name())
+	containersPath := getSnapshotMountPoint(s.pool.Name, targetParentName)
+	snapshotMntPointSymlinkTarget := shared.VarPath("storage-pools", s.pool.Name, "snapshots", targetParentName)
+	snapshotMntPointSymlink := shared.VarPath("snapshots", targetParentName)
 	err := createSnapshotMountpoint(containersPath, snapshotMntPointSymlinkTarget, snapshotMntPointSymlink)
 	if err != nil {
 		return err
 	}
 
 	poolName := s.getOnDiskPoolName()
-	sourceFields := strings.SplitN(sourceName, shared.SnapshotDelimiter, 2)
-	currentSnapshotDataset := fmt.Sprintf("%s/containers/%s at snapshot-%s", poolName, sourceFields[0], sourceFields[1])
+	sourceParentName, sourceSnapOnlyName, _ := containerGetParentAndSnapshotName(sourceName)
+	currentSnapshotDataset := fmt.Sprintf("%s/containers/%s at snapshot-%s", poolName, sourceParentName, sourceSnapOnlyName)
 	args := []string{"send", currentSnapshotDataset}
 	if parentSnapshot != "" {
-		parentFields := strings.SplitN(parentSnapshot, shared.SnapshotDelimiter, 2)
-		parentSnapshotDataset := fmt.Sprintf("%s/containers/%s at snapshot-%s", poolName, parentFields[0], parentFields[1])
+		parentName, parentSnaponlyName, _ := containerGetParentAndSnapshotName(parentSnapshot)
+		parentSnapshotDataset := fmt.Sprintf("%s/containers/%s at snapshot-%s", poolName, parentName, parentSnaponlyName)
 		args = append(args, "-i", parentSnapshotDataset)
 	}
 
 	zfsSendCmd := exec.Command("zfs", args...)
-	targetSnapshotDataset := fmt.Sprintf("%s/containers/%s at snapshot-%s", poolName, fields[0], fields[1])
+	targetSnapshotDataset := fmt.Sprintf("%s/containers/%s at snapshot-%s", poolName, sourceParentName, sourceSnapOnlyName)
 	zfsRecvCmd := exec.Command("zfs", "receive", targetSnapshotDataset)
 
 	zfsRecvCmd.Stdin, _ = zfsSendCmd.StdoutPipe()
@@ -1009,8 +1008,8 @@ func (s *storageZfs) ContainerCopy(target container, source container, container
 				return err
 			}
 
-			fields := strings.SplitN(snap.Name(), shared.SnapshotDelimiter, 2)
-			newSnapName := fmt.Sprintf("%s/%s", target.Name(), fields[1])
+			_, snapOnlyName, _ := containerGetParentAndSnapshotName(snap.Name())
+			newSnapName := fmt.Sprintf("%s/%s", target.Name(), snapOnlyName)
 			targetSnapshot, err := containerLoadByName(s.d, newSnapName)
 			if err != nil {
 				return err
@@ -1155,9 +1154,8 @@ func (s *storageZfs) ContainerRestore(target container, source container) error
 	}
 
 	// Restore the snapshot
-	fields := strings.SplitN(source.Name(), shared.SnapshotDelimiter, 2)
-	cName := fields[0]
-	snapName := fmt.Sprintf("snapshot-%s", fields[1])
+	cName, snapOnlyName, _ := containerGetParentAndSnapshotName(source.Name())
+	snapName := fmt.Sprintf("snapshot-%s", snapOnlyName)
 
 	err = s.zfsPoolVolumeSnapshotRestore(fmt.Sprintf("containers/%s", cName), snapName)
 	if err != nil {
@@ -1239,9 +1237,8 @@ func (s *storageZfs) ContainerSnapshotCreate(snapshotContainer container, source
 
 	sourceContainerName := sourceContainer.Name()
 
-	fields := strings.SplitN(snapshotContainerName, shared.SnapshotDelimiter, 2)
-	cName := fields[0]
-	snapName := fmt.Sprintf("snapshot-%s", fields[1])
+	cName, snapshotSnapOnlyName, _ := containerGetParentAndSnapshotName(snapshotContainerName)
+	snapName := fmt.Sprintf("snapshot-%s", snapshotSnapOnlyName)
 
 	sourceZfsDataset := fmt.Sprintf("containers/%s", cName)
 	err := s.zfsPoolVolumeSnapshotCreate(sourceZfsDataset, snapName)
@@ -1282,9 +1279,8 @@ func (s *storageZfs) ContainerSnapshotCreate(snapshotContainer container, source
 func (s *storageZfs) ContainerSnapshotDelete(snapshotContainer container) error {
 	logger.Debugf("Deleting ZFS storage volume for snapshot \"%s\" on storage pool \"%s\".", s.volume.Name, s.pool.Name)
 
-	fields := strings.SplitN(snapshotContainer.Name(), shared.SnapshotDelimiter, 2)
-	sourceContainerName := fields[0]
-	snapName := fmt.Sprintf("snapshot-%s", fields[1])
+	sourceContainerName, sourceContainerSnapOnlyName, _ := containerGetParentAndSnapshotName(snapshotContainer.Name())
+	snapName := fmt.Sprintf("snapshot-%s", sourceContainerSnapOnlyName)
 
 	if s.zfsFilesystemEntityExists(fmt.Sprintf("containers/%s@%s", sourceContainerName, snapName), true) {
 		removable, err := s.zfsPoolVolumeSnapshotRemovable(fmt.Sprintf("containers/%s", sourceContainerName), snapName)
@@ -1335,7 +1331,7 @@ func (s *storageZfs) ContainerSnapshotDelete(snapshotContainer container) error
 	}
 
 	// Legacy
-	snapPath := shared.VarPath(fmt.Sprintf("snapshots/%s/%s.zfs", sourceContainerName, fields[1]))
+	snapPath := shared.VarPath(fmt.Sprintf("snapshots/%s/%s.zfs", sourceContainerName, sourceContainerSnapOnlyName))
 	if shared.PathExists(snapPath) {
 		err := os.Remove(snapPath)
 		if err != nil {
@@ -1361,12 +1357,11 @@ func (s *storageZfs) ContainerSnapshotRename(snapshotContainer container, newNam
 
 	oldName := snapshotContainer.Name()
 
-	oldFields := strings.SplitN(snapshotContainer.Name(), shared.SnapshotDelimiter, 2)
-	oldcName := oldFields[0]
-	oldZfsDatasetName := fmt.Sprintf("snapshot-%s", oldFields[1])
+	oldcName, oldSnapOnlyName, _ := containerGetParentAndSnapshotName(snapshotContainer.Name())
+	oldZfsDatasetName := fmt.Sprintf("snapshot-%s", oldSnapOnlyName)
 
-	newFields := strings.SplitN(newName, shared.SnapshotDelimiter, 2)
-	newZfsDatasetName := fmt.Sprintf("snapshot-%s", newFields[1])
+	_, newSnapOnlyName, _ := containerGetParentAndSnapshotName(newName)
+	newZfsDatasetName := fmt.Sprintf("snapshot-%s", newSnapOnlyName)
 
 	if oldZfsDatasetName != newZfsDatasetName {
 		err := s.zfsPoolVolumeSnapshotRename(fmt.Sprintf("containers/%s", oldcName), oldZfsDatasetName, newZfsDatasetName)
@@ -1382,7 +1377,7 @@ func (s *storageZfs) ContainerSnapshotRename(snapshotContainer container, newNam
 		s.ContainerSnapshotRename(snapshotContainer, oldName)
 	}()
 
-	oldStyleSnapshotMntPoint := shared.VarPath(fmt.Sprintf("snapshots/%s/%s.zfs", oldcName, oldFields[1]))
+	oldStyleSnapshotMntPoint := shared.VarPath(fmt.Sprintf("snapshots/%s/%s.zfs", oldcName, oldSnapOnlyName))
 	if shared.PathExists(oldStyleSnapshotMntPoint) {
 		err := os.Remove(oldStyleSnapshotMntPoint)
 		if err != nil {
@@ -1424,13 +1419,7 @@ func (s *storageZfs) ContainerSnapshotRename(snapshotContainer container, newNam
 func (s *storageZfs) ContainerSnapshotStart(container container) (bool, error) {
 	logger.Debugf("Initializing ZFS storage volume for snapshot \"%s\" on storage pool \"%s\".", s.volume.Name, s.pool.Name)
 
-	fields := strings.SplitN(container.Name(), shared.SnapshotDelimiter, 2)
-	if len(fields) < 2 {
-		return false, fmt.Errorf("Invalid snapshot name: %s", container.Name())
-	}
-
-	cName := fields[0]
-	sName := fields[1]
+	cName, sName, _ := containerGetParentAndSnapshotName(container.Name())
 	sourceFs := fmt.Sprintf("containers/%s", cName)
 	sourceSnap := fmt.Sprintf("snapshot-%s", sName)
 	destFs := fmt.Sprintf("snapshots/%s/%s", cName, sName)
@@ -1448,12 +1437,7 @@ func (s *storageZfs) ContainerSnapshotStart(container container) (bool, error) {
 func (s *storageZfs) ContainerSnapshotStop(container container) (bool, error) {
 	logger.Debugf("Stopping ZFS storage volume for snapshot \"%s\" on storage pool \"%s\".", s.volume.Name, s.pool.Name)
 
-	fields := strings.SplitN(container.Name(), shared.SnapshotDelimiter, 2)
-	if len(fields) < 2 {
-		return false, fmt.Errorf("Invalid snapshot name: %s", container.Name())
-	}
-	cName := fields[0]
-	sName := fields[1]
+	cName, sName, _ := containerGetParentAndSnapshotName(container.Name())
 	destFs := fmt.Sprintf("snapshots/%s/%s", cName, sName)
 
 	err := s.zfsPoolVolumeDestroy(destFs)
@@ -2381,9 +2365,9 @@ func (s *zfsMigrationSourceDriver) Snapshots() []container {
 }
 
 func (s *zfsMigrationSourceDriver) send(conn *websocket.Conn, zfsName string, zfsParent string, readWrapper func(io.ReadCloser) io.ReadCloser) error {
-	fields := strings.SplitN(s.container.Name(), shared.SnapshotDelimiter, 2)
+	sourceParentName, _, _ := containerGetParentAndSnapshotName(s.container.Name())
 	poolName := s.zfs.getOnDiskPoolName()
-	args := []string{"send", fmt.Sprintf("%s/containers/%s@%s", poolName, fields[0], zfsName)}
+	args := []string{"send", fmt.Sprintf("%s/containers/%s@%s", poolName, sourceParentName, zfsName)}
 	if zfsParent != "" {
 		args = append(args, "-i", fmt.Sprintf("%s/containers/%s@%s", poolName, s.container.Name(), zfsParent))
 	}
@@ -2426,8 +2410,8 @@ func (s *zfsMigrationSourceDriver) send(conn *websocket.Conn, zfsName string, zf
 
 func (s *zfsMigrationSourceDriver) SendWhileRunning(conn *websocket.Conn, op *operation, bwlimit string) error {
 	if s.container.IsSnapshot() {
-		fields := strings.SplitN(s.container.Name(), shared.SnapshotDelimiter, 2)
-		snapshotName := fmt.Sprintf("snapshot-%s", fields[1])
+		_, snapOnlyName, _ := containerGetParentAndSnapshotName(s.container.Name())
+		snapshotName := fmt.Sprintf("snapshot-%s", snapOnlyName)
 		wrapper := StorageProgressReader(op, "fs_progress", s.container.Name())
 		return s.send(conn, snapshotName, "", wrapper)
 	}


More information about the lxc-devel mailing list