From 387641b4352d9f9da0d036db68a79e1fadbd4b5d Mon Sep 17 00:00:00 2001
From: Sebastien Douheret <sebastien.douheret@iot.bzh>
Date: Thu, 18 May 2017 23:18:04 +0200
Subject: Normalize relativePath projects to support Windows path.

---
 lib/common/filepath.go | 16 ++++++++++++++++
 lib/model/folder.go    |  6 ++++++
 2 files changed, 22 insertions(+)

(limited to 'lib')

diff --git a/lib/common/filepath.go b/lib/common/filepath.go
index 4c8c0da..8101c5a 100644
--- a/lib/common/filepath.go
+++ b/lib/common/filepath.go
@@ -7,6 +7,7 @@ import (
 	"path"
 	"path/filepath"
 	"regexp"
+	"strings"
 )
 
 // Exists returns whether the given file or directory exists or not
@@ -55,3 +56,18 @@ func ResolveEnvVar(s string) (string, error) {
 
 	return path.Clean(res), nil
 }
+
+// PathNormalize
+func PathNormalize(p string) string {
+	sep := string(filepath.Separator)
+	if sep != "/" {
+		return p
+	}
+	// Replace drive like C: by C/
+	res := p
+	if p[1:2] == ":" {
+		res = p[0:1] + sep + p[2:]
+	}
+	res = strings.Replace(res, "\\", "/", -1)
+	return filepath.Clean(res)
+}
diff --git a/lib/model/folder.go b/lib/model/folder.go
index be1bc33..e461f9c 100644
--- a/lib/model/folder.go
+++ b/lib/model/folder.go
@@ -3,6 +3,7 @@ package model
 import (
 	"fmt"
 
+	"github.com/iotbzh/xds-server/lib/common"
 	"github.com/iotbzh/xds-server/lib/syncthing"
 	"github.com/iotbzh/xds-server/lib/xdsconfig"
 )
@@ -69,8 +70,13 @@ func (c *Folder) UpdateFolder(newFolder xdsconfig.FolderConfig) (xdsconfig.Folde
 		return xdsconfig.FolderConfig{}, err
 	}
 
+	// Normalize path (needed for Windows path including bashlashes)
+	newFolder.RelativePath = common.PathNormalize(newFolder.RelativePath)
+
+	// Update config folder
 	c.Conf.Folders = c.Conf.Folders.Update(xdsconfig.FoldersConfig{newFolder})
 
+	// Update Syncthing folder
 	err := c.SThg.FolderChange(newFolder)
 
 	newFolder.BuilderSThgID = c.Conf.Builder.SyncThingID // FIXME - should be removed after local ST config rework
-- 
cgit