Fixed spaces handling in RPath - SPEC1116
[src/xds/xds-server.git] / lib / apiv1 / exec.go
index eb93af8..de40c70 100644 (file)
@@ -113,11 +113,13 @@ func (s *APIService) execCmd(c *gin.Context) {
                return
        }
 
-       prj := s.mfolder.GetFolderFromID(id)
-       if prj == nil {
+       f := s.mfolders.Get(id)
+       if f == nil {
                common.APIError(c, "Unknown id")
                return
        }
+       fld := *f
+       prj := fld.GetConfig()
 
        // Build command line
        cmd := []string{}
@@ -133,14 +135,25 @@ func (s *APIService) execCmd(c *gin.Context) {
                }
        }
 
-       // FIXME - SEB: exec prevents to use syntax:
-       //  xds-exec -l debug -c xds-config.env -- "cd build && cmake .."
-       cmd = append(cmd, "cd", prj.GetFullPath(args.RPath))
-       cmd = append(cmd, "&&", "exec", args.Cmd)
+       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
+       //  As workaround, exec is set for now on client side (eg. in xds-gdb)
+       //cmd = append(cmd, "&&", "exec", args.Cmd)
+       cmd = append(cmd, "&&", args.Cmd)
 
        // 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 {
@@ -163,7 +176,7 @@ func (s *APIService) execCmd(c *gin.Context) {
        execWS.Log = s.log
 
        // Append client project dir to environment
-       execWS.Env = append(args.Env, "CLIENT_PROJECT_DIR="+prj.RelativePath)
+       execWS.Env = append(args.Env, "CLIENT_PROJECT_DIR="+prj.ClientPath)
 
        // Set command execution timeout
        if args.CmdTimeout == 0 {
@@ -188,9 +201,14 @@ func (s *APIService) execCmd(c *gin.Context) {
 
                // Set correct path
                data := e.UserData
-               rootPath := (*data)["RootPath"].(string)
-               relaPath := (*data)["RelativePath"].(string)
-               stdin = strings.Replace(stdin, relaPath, rootPath+"/"+relaPath, -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
        }
@@ -207,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 != "" {
@@ -283,7 +305,7 @@ func (s *APIService) execCmd(c *gin.Context) {
                exitImm := (*data)["ExitImmediate"].(bool)
 
                // XXX - workaround to be sure that Syncthing detected all changes
-               if err := s.mfolder.ForceSync(prjID); err != nil {
+               if err := s.mfolders.ForceSync(prjID); err != nil {
                        s.log.Errorf("Error while syncing folder %s: %v", prjID, err)
                }
                if !exitImm {
@@ -291,8 +313,8 @@ func (s *APIService) execCmd(c *gin.Context) {
                        // FIXME pass as argument
                        tmo := 60
                        for t := tmo; t > 0; t-- {
-                               s.log.Debugf("Wait file insync for %s (%d/%d)", prjID, t, tmo)
-                               if sync, err := s.mfolder.IsFolderInSync(prjID); sync || err != nil {
+                               s.log.Debugf("Wait file in-sync for %s (%d/%d)", prjID, t, tmo)
+                               if sync, err := s.mfolders.IsFolderInSync(prjID); sync || err != nil {
                                        if err != nil {
                                                s.log.Errorf("ERROR IsFolderInSync (%s): %v", prjID, err)
                                        }
@@ -325,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["RelativePath"] = prj.RelativePath
        data["ExitImmediate"] = args.ExitImmediate
        if args.TTY && args.TTYGdbserverFix {
                data["gdbServerTTY"] = "workaround"
@@ -336,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 {