Skip to content

Commit 611d2a4

Browse files
committed
address CR
1 parent d66032c commit 611d2a4

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

convert.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"net"
66
"path/filepath"
7+
"runtime"
8+
"strings"
79

810
ma "github.com/multiformats/go-multiaddr"
911
)
@@ -192,7 +194,11 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
192194
}
193195
return network, "[" + ip + "]" + ":" + port, nil
194196
case "unix":
195-
return network, filepath.FromSlash(ip), nil
197+
if runtime.GOOS == "windows" {
198+
// convert /c:/... to c:\...
199+
ip = filepath.FromSlash(strings.TrimLeft(ip, "/"))
200+
}
201+
return network, ip, nil
196202
default:
197203
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
198204
}
@@ -264,5 +270,15 @@ func parseUnixNetAddr(a net.Addr) (ma.Multiaddr, error) {
264270
return nil, errIncorrectNetAddr
265271
}
266272

267-
return ma.NewComponent("unix", filepath.ToSlash(ac.Name))
273+
path := ac.Name
274+
if runtime.GOOS == "windows" {
275+
// Convert c:\foobar\... to c:/foobar/...
276+
path = filepath.ToSlash(path)
277+
}
278+
if len(path) == 0 || path[0] != '/' {
279+
// convert "" and "c:/..." to "/..."
280+
path = "/" + path
281+
}
282+
283+
return ma.NewComponent("unix", path)
268284
}

convert_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package manet
22

33
import (
44
"net"
5+
"runtime"
56
"testing"
67

78
ma "github.com/multiformats/go-multiaddr"
@@ -65,17 +66,21 @@ func TestFromIP4(t *testing.T) {
6566
}
6667

6768
func TestFromUnix(t *testing.T) {
69+
path := "/c:/foo/bar"
70+
if runtime.GOOS == "windows" {
71+
path = "c:\foo\bar"
72+
}
6873
testConvert(t, "/unix/c:/foo/bar", func() (ma.Multiaddr, error) {
69-
return FromNetAddr(&net.UnixAddr{Name: "/c:/foo/bar", Net: "unix"})
70-
})
71-
testConvert(t, "/unix/foo/bar", func() (ma.Multiaddr, error) {
72-
return FromNetAddr(&net.UnixAddr{Name: "/foo/bar", Net: "unix"})
74+
return FromNetAddr(&net.UnixAddr{Name: path, Net: "unix"})
7375
})
7476
}
7577

7678
func TestToUnix(t *testing.T) {
77-
testToNetAddr(t, "/unix/c:/foo/bar", "unix", "/c:/foo/bar")
78-
testToNetAddr(t, "/unix/foo/bar", "unix", "/foo/bar")
79+
path := "/c:/foo/bar"
80+
if runtime.GOOS == "windows" {
81+
path = "c:\foo\bar"
82+
}
83+
testToNetAddr(t, "/unix/c:/foo/bar", "unix", path)
7984
}
8085

8186
func TestFromIP6(t *testing.T) {

0 commit comments

Comments
 (0)