Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 67f83fc

Browse files
committed
internal/fs: fix bug in isCaseSensitiveFilesystem
Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent d93ac98 commit 67f83fc

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

cmd/dep/gopath_scanner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (g *gopathScanner) overlay(rootM *dep.Manifest, rootL *dep.Lock) {
134134
}
135135

136136
func trimPathPrefix(p1, p2 string) string {
137-
if fs.HasFilepathPrefix(p1, p2) {
137+
if isPrefix, _ := fs.HasFilepathPrefix(p1, p2); isPrefix {
138138
return p1[len(p2):]
139139
}
140140
return p1

context.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ func (c *Ctx) DetectProjectGOPATH(p *Project) (string, error) {
210210
// detectGOPATH detects the GOPATH for a given path from ctx.GOPATHs.
211211
func (c *Ctx) detectGOPATH(path string) (string, error) {
212212
for _, gp := range c.GOPATHs {
213-
if fs.HasFilepathPrefix(path, gp) {
213+
isPrefix, err := fs.HasFilepathPrefix(path, gp)
214+
if err != nil {
215+
return "", errors.Wrap(err, "failed to detect GOPATH")
216+
}
217+
if isPrefix {
214218
return gp, nil
215219
}
216220
}
@@ -221,7 +225,11 @@ func (c *Ctx) detectGOPATH(path string) (string, error) {
221225
// `$GOPATH/src/` prefix. Returns an error for paths equal to, or without this prefix.
222226
func (c *Ctx) ImportForAbs(path string) (string, error) {
223227
srcprefix := filepath.Join(c.GOPATH, "src") + string(filepath.Separator)
224-
if fs.HasFilepathPrefix(path, srcprefix) {
228+
isPrefix, err := fs.HasFilepathPrefix(path, srcprefix)
229+
if err != nil {
230+
return "", errors.Wrap(err, "failed to find import path")
231+
}
232+
if isPrefix {
225233
if len(path) <= len(srcprefix) {
226234
return "", errors.New("dep does not currently support using GOPATH/src as the project root")
227235
}

internal/fs/fs.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
// same file. In that situation HasFilepathPrefix("/Foo/Bar", "/foo")
3131
// will return true. The implementation is *not* OS-specific, so a FAT32
3232
// filesystem mounted on Linux will be handled correctly.
33-
func HasFilepathPrefix(path, prefix string) bool {
33+
func HasFilepathPrefix(path, prefix string) (bool, error) {
3434
// this function is more convoluted then ideal due to need for special
3535
// handling of volume name/drive letter on Windows. vnPath and vnPrefix
3636
// are first compared, and then used to initialize initial values of p and
@@ -42,7 +42,7 @@ func HasFilepathPrefix(path, prefix string) bool {
4242
vnPath := strings.ToLower(filepath.VolumeName(path))
4343
vnPrefix := strings.ToLower(filepath.VolumeName(prefix))
4444
if vnPath != vnPrefix {
45-
return false
45+
return false, nil
4646
}
4747

4848
// Because filepath.Join("c:","dir") returns "c:dir", we have to manually
@@ -54,7 +54,7 @@ func HasFilepathPrefix(path, prefix string) bool {
5454
var dn string
5555

5656
if isDir, err := IsDir(path); err != nil {
57-
return false
57+
return false, errors.Wrap(err, "failed to check filepath prefix")
5858
} else if isDir {
5959
dn = path
6060
} else {
@@ -69,7 +69,7 @@ func HasFilepathPrefix(path, prefix string) bool {
6969
prefixes := strings.Split(prefix, string(os.PathSeparator))[1:]
7070

7171
if len(prefixes) > len(dirs) {
72-
return false
72+
return false, nil
7373
}
7474

7575
// d,p are initialized with "/" on *nix and volume name on Windows
@@ -84,7 +84,7 @@ func HasFilepathPrefix(path, prefix string) bool {
8484
// problematic filesystem is not the last one.
8585
caseSensitive, err := isCaseSensitiveFilesystem(filepath.Join(d, dirs[i]))
8686
if err != nil {
87-
return false
87+
return false, errors.Wrap(err, "failed to check filepath prefix")
8888
}
8989
if caseSensitive {
9090
d = filepath.Join(d, dirs[i])
@@ -95,11 +95,11 @@ func HasFilepathPrefix(path, prefix string) bool {
9595
}
9696

9797
if p != d {
98-
return false
98+
return false, nil
9999
}
100100
}
101101

102-
return true
102+
return true, nil
103103
}
104104

105105
// EquivalentPaths compares the paths passed to check if they are equivalent.
@@ -126,7 +126,11 @@ func EquivalentPaths(p1, p2 string) (bool, error) {
126126
p2, p2Filename = filepath.Split(p2)
127127
}
128128

129-
if !HasFilepathPrefix(p1, p2) || !HasFilepathPrefix(p2, p1) {
129+
if isPrefix1, err := HasFilepathPrefix(p1, p2); err != nil {
130+
return false, errors.Wrap(err, "failed to check for path equivalence")
131+
} else if isPrefix2, err := HasFilepathPrefix(p2, p1); err != nil {
132+
return false, errors.Wrap(err, "failed to check for path equivalence")
133+
} else if !isPrefix1 || !isPrefix2 {
130134
return false, nil
131135
}
132136

@@ -218,6 +222,11 @@ func isCaseSensitiveFilesystem(dir string) (bool, error) {
218222

219223
aInfo, err := os.Stat(alt)
220224
if err != nil {
225+
// If the file doesn't exists, assume we are on a case-sensitive filesystem.
226+
if os.IsNotExist(err) {
227+
return true, nil
228+
}
229+
221230
return false, errors.Wrap(err, "could not determine the case-sensitivity of the filesystem")
222231
}
223232

internal/fs/fs_test.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ func TestHasFilepathPrefix(t *testing.T) {
7575
t.Fatal(err)
7676
}
7777

78-
if got := HasFilepathPrefix(c.path, c.prefix); c.want != got {
78+
got, err := HasFilepathPrefix(c.path, c.prefix)
79+
if err != nil {
80+
t.Fatalf("unexpected error: %s", err)
81+
}
82+
if c.want != got {
7983
t.Fatalf("dir: %q, prefix: %q, expected: %v, got: %v", c.path, c.prefix, c.want, got)
8084
}
8185
}
@@ -122,13 +126,18 @@ func TestHasFilepathPrefix_Files(t *testing.T) {
122126
path string
123127
prefix string
124128
want bool
129+
err bool
125130
}{
126-
{existingFile, filepath.Join(dir2), true},
127-
{nonExistingFile, filepath.Join(dir2), false},
131+
{existingFile, filepath.Join(dir2), true, false},
132+
{nonExistingFile, filepath.Join(dir2), false, true},
128133
}
129134

130135
for _, c := range cases {
131-
if got := HasFilepathPrefix(c.path, c.prefix); c.want != got {
136+
got, err := HasFilepathPrefix(c.path, c.prefix)
137+
if err != nil && !c.err {
138+
t.Fatalf("unexpected error: %s", err)
139+
}
140+
if c.want != got {
132141
t.Fatalf("dir: %q, prefix: %q, expected: %v, got: %v", c.path, c.prefix, c.want, got)
133142
}
134143
}

0 commit comments

Comments
 (0)