[lxc-devel] [lxd/master] lxc/file: Intercept user cancelation

stgraber on Github lxc-bot at linuxcontainers.org
Mon Aug 12 01:42:45 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 526 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190811/0ef8532e/attachment.bin>
-------------- next part --------------
From dd4a78de7561b7722caa43817c47e367bd0ff828 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Sun, 11 Aug 2019 21:41:28 -0400
Subject: [PATCH] lxc/file: Intercept user cancelation
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

So we can indicate that the initial server side handling cannot be canceled.
As soon as the transfer does begin, it is cancelable and ctrl+c will work.

Closes #6062

Signed-off-by: St├ęphane Graber <stgraber at ubuntu.com>
---
 lxc/file.go | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/lxc/file.go b/lxc/file.go
index b0e896f1d3..bf6251d6b3 100644
--- a/lxc/file.go
+++ b/lxc/file.go
@@ -6,6 +6,7 @@ import (
 	"io"
 	"io/ioutil"
 	"os"
+	"os/signal"
 	"path"
 	"path/filepath"
 	"strconv"
@@ -35,6 +36,37 @@ type cmdFile struct {
 	flagRecursive bool
 }
 
+func fileGetWrapper(server lxd.ContainerServer, container string, path string) (buf io.ReadCloser, resp *lxd.ContainerFileResponse, err error) {
+	// Signal handling
+	chSignal := make(chan os.Signal)
+	signal.Notify(chSignal, os.Interrupt)
+
+	// Operation handling
+	chDone := make(chan bool)
+	go func() {
+		buf, resp, err = server.GetContainerFile(container, path)
+		close(chDone)
+	}()
+
+	count := 0
+	for {
+		var err error
+
+		select {
+		case <-chDone:
+			return buf, resp, err
+		case <-chSignal:
+			count++
+
+			if count == 3 {
+				return nil, nil, fmt.Errorf(i18n.G("User signaled us three times, exiting. The remote operation will keep running"))
+			}
+
+			fmt.Println(i18n.G("Early server side processing of file tranfer requests cannot be canceled (interrupt two more times to force)"))
+		}
+	}
+}
+
 func (c *cmdFile) Command() *cobra.Command {
 	cmd := &cobra.Command{}
 	cmd.Use = i18n.G("file")
@@ -245,7 +277,7 @@ func (c *cmdFilePull) Run(cmd *cobra.Command, args []string) error {
 			return fmt.Errorf(i18n.G("Invalid source %s"), resource.name)
 		}
 
-		buf, resp, err := resource.server.GetContainerFile(pathSpec[0], pathSpec[1])
+		buf, resp, err := fileGetWrapper(resource.server, pathSpec[0], pathSpec[1])
 		if err != nil {
 			return err
 		}


More information about the lxc-devel mailing list