Fixed path convertion/mapping for in/out and for command args.
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 28 Aug 2017 19:44:26 +0000 (21:44 +0200)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Tue, 29 Aug 2017 06:56:46 +0000 (08:56 +0200)
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
lib/apiv1/exec.go
lib/folder/folder-interface.go
lib/folder/folder-pathmap.go
lib/syncthing/folder-st.go

index 6300dba..fd0f8bb 100644 (file)
@@ -118,8 +118,8 @@ func (s *APIService) execCmd(c *gin.Context) {
                common.APIError(c, "Unknown id")
                return
        }
-       folder := *f
-       prj := folder.GetConfig()
+       fld := *f
+       prj := fld.GetConfig()
 
        // Build command line
        cmd := []string{}
@@ -135,7 +135,7 @@ func (s *APIService) execCmd(c *gin.Context) {
                }
        }
 
-       cmd = append(cmd, "cd", folder.GetFullPath(args.RPath))
+       cmd = append(cmd, "cd", fld.GetFullPath(args.RPath))
        // FIXME - add 'exec' prevents to use syntax:
        //       xds-exec -l debug -c xds-config.env -- "cd build && cmake .."
        //  but exec is mandatory to allow to pass correctly signals
@@ -145,7 +145,15 @@ func (s *APIService) execCmd(c *gin.Context) {
 
        // Process command arguments
        cmdArgs := make([]string, len(args.Args)+1)
-       copy(cmdArgs, args.Args)
+
+       // Copy and Translate path from client to server
+       for _, aa := range args.Args {
+               if strings.Contains(aa, prj.ClientPath) {
+                       cmdArgs = append(cmdArgs, fld.ConvPathCli2Svr(aa))
+               } else {
+                       cmdArgs = append(cmdArgs, aa)
+               }
+       }
 
        // Allocate pts if tty if used
        if args.TTY {
@@ -193,9 +201,14 @@ func (s *APIService) execCmd(c *gin.Context) {
 
                // Set correct path
                data := e.UserData
-               rootPath := (*data)["RootPath"].(string)
-               clientPath := (*data)["ClientPath"].(string)
-               stdin = strings.Replace(stdin, clientPath, rootPath+"/"+clientPath, -1)
+               prjID := (*data)["ID"].(string)
+               f := s.mfolders.Get(prjID)
+               if f == nil {
+                       s.log.Errorf("InputCB: Cannot get folder ID %s", prjID)
+               } else {
+                       // Translate paths from client to server
+                       stdin = (*f).ConvPathCli2Svr(stdin)
+               }
 
                return stdin, nil
        }
@@ -212,12 +225,16 @@ func (s *APIService) execCmd(c *gin.Context) {
                // Retrieve project ID and RootPath
                data := e.UserData
                prjID := (*data)["ID"].(string)
-               prjRootPath := (*data)["RootPath"].(string)
                gdbServerTTY := (*data)["gdbServerTTY"].(string)
 
-               // Cleanup any references to internal rootpath in stdout & stderr
-               stdout = strings.Replace(stdout, prjRootPath, "", -1)
-               stderr = strings.Replace(stderr, prjRootPath, "", -1)
+               f := s.mfolders.Get(prjID)
+               if f == nil {
+                       s.log.Errorf("OutputCB: Cannot get folder ID %s", prjID)
+               } else {
+                       // Translate paths from server to client
+                       stdout = (*f).ConvPathSvr2Cli(stdout)
+                       stderr = (*f).ConvPathSvr2Cli(stderr)
+               }
 
                s.log.Debugf("%s emitted - WS sid[4:] %s - id:%s - prjID:%s", ExecOutEvent, e.Sid[4:], e.CmdID, prjID)
                if stdout != "" {
@@ -330,8 +347,6 @@ func (s *APIService) execCmd(c *gin.Context) {
        // User data (used within callbacks)
        data := make(map[string]interface{})
        data["ID"] = prj.ID
-       data["RootPath"] = prj.RootPath
-       data["ClientPath"] = prj.ClientPath
        data["ExitImmediate"] = args.ExitImmediate
        if args.TTY && args.TTYGdbserverFix {
                data["gdbServerTTY"] = "workaround"
@@ -341,7 +356,7 @@ func (s *APIService) execCmd(c *gin.Context) {
        execWS.UserData = &data
 
        // Start command execution
-       s.log.Debugf("Execute [Cmd ID %s]: %v %v", execWS.CmdID, execWS.Cmd, execWS.Args)
+       s.log.Infof("Execute [Cmd ID %s]: %v %v", execWS.CmdID, execWS.Cmd, execWS.Args)
 
        err = execWS.Start()
        if err != nil {
index c04cbd7..4beccb8 100644 (file)
@@ -27,6 +27,8 @@ type IFOLDER interface {
        Add(cfg FolderConfig) (*FolderConfig, error)              // Add a new folder
        GetConfig() FolderConfig                                  // Get folder public configuration
        GetFullPath(dir string) string                            // Get folder full path
+       ConvPathCli2Svr(s string) string                          // Convert path from Client to Server
+       ConvPathSvr2Cli(s string) string                          // Convert path from Server to Client
        Remove() error                                            // Remove a folder
        RegisterEventChange(cb *EventCB, data *EventCBData) error // Request events registration (sent through WS)
        UnRegisterEventChange() error                             // Un-register events
index f73f271..1020026 100644 (file)
@@ -5,6 +5,7 @@ import (
        "io/ioutil"
        "os"
        "path/filepath"
+       "strings"
 
        common "github.com/iotbzh/xds-common/golib"
        "github.com/iotbzh/xds-server/lib/xdsconfig"
@@ -80,7 +81,7 @@ func (f *PathMap) GetConfig() FolderConfig {
        return f.config
 }
 
-// GetFullPath returns the full path
+// GetFullPath returns the full path of a directory (from server POV)
 func (f *PathMap) GetFullPath(dir string) string {
        if &dir == nil {
                return f.config.DataPathMap.ServerPath
@@ -88,6 +89,28 @@ func (f *PathMap) GetFullPath(dir string) string {
        return filepath.Join(f.config.DataPathMap.ServerPath, dir)
 }
 
+// ConvPathCli2Svr Convert path from Client to Server
+func (f *PathMap) ConvPathCli2Svr(s string) string {
+       if f.config.ClientPath != "" && f.config.DataPathMap.ServerPath != "" {
+               return strings.Replace(s,
+                       f.config.ClientPath,
+                       f.config.DataPathMap.ServerPath,
+                       -1)
+       }
+       return s
+}
+
+// ConvPathSvr2Cli Convert path from Server to Client
+func (f *PathMap) ConvPathSvr2Cli(s string) string {
+       if f.config.ClientPath != "" && f.config.DataPathMap.ServerPath != "" {
+               return strings.Replace(s,
+                       f.config.DataPathMap.ServerPath,
+                       f.config.ClientPath,
+                       -1)
+       }
+       return s
+}
+
 // Remove a folder
 func (f *PathMap) Remove() error {
        // nothing to do
index da27062..a372b6f 100644 (file)
@@ -3,6 +3,7 @@ package st
 import (
        "fmt"
        "path/filepath"
+       "strings"
 
        "github.com/iotbzh/xds-server/lib/folder"
        "github.com/iotbzh/xds-server/lib/xdsconfig"
@@ -93,7 +94,7 @@ func (f *STFolder) GetConfig() folder.FolderConfig {
        return f.fConfig
 }
 
-// GetFullPath returns the full path
+// GetFullPath returns the full path of a directory (from server POV)
 func (f *STFolder) GetFullPath(dir string) string {
        if &dir == nil {
                dir = ""
@@ -104,6 +105,28 @@ func (f *STFolder) GetFullPath(dir string) string {
        return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
 }
 
+// ConvPathCli2Svr Convert path from Client to Server
+func (f *STFolder) ConvPathCli2Svr(s string) string {
+       if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
+               return strings.Replace(s,
+                       f.fConfig.ClientPath,
+                       f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
+                       -1)
+       }
+       return s
+}
+
+// ConvPathSvr2Cli Convert path from Server to Client
+func (f *STFolder) ConvPathSvr2Cli(s string) string {
+       if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
+               return strings.Replace(s,
+                       f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
+                       f.fConfig.ClientPath,
+                       -1)
+       }
+       return s
+}
+
 // Remove a folder
 func (f *STFolder) Remove() error {
        return f.st.FolderDelete(f.stfConfig.ID)