forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes gopasspw#2402 RELEASE_NOTES=[BUGFIX] Fix symlink deduplication. Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
- Loading branch information
1 parent
e031b69
commit 987d6d3
Showing
3 changed files
with
115 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package fs | ||
|
||
import ( | ||
"io/fs" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWalkTooLong(t *testing.T) { | ||
// Walking a path with a symlink loop should fail. | ||
|
||
td := t.TempDir() | ||
storeDir := filepath.Join(td, "store") | ||
fn := filepath.Join(storeDir, "real", "file.txt") | ||
assert.NoError(t, os.MkdirAll(filepath.Dir(fn), 0o700)) | ||
assert.NoError(t, ioutil.WriteFile(fn, []byte("foobar"), 0o600)) | ||
|
||
ptr := filepath.Join(storeDir, "path", "via", "link") | ||
|
||
assert.NoError(t, os.MkdirAll(filepath.Dir(ptr), 0o700)) | ||
|
||
assert.NoError(t, os.Symlink(filepath.Join(storeDir, "path"), filepath.Join(storeDir, "path", "via", "loop"))) | ||
|
||
// test the walkFunc | ||
assert.Error(t, walkSymlinks(storeDir, func(path string, info fs.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if info.IsDir() && strings.HasPrefix(info.Name(), ".") { | ||
return fs.SkipDir | ||
} | ||
if info.IsDir() { | ||
return nil | ||
} | ||
rPath := strings.TrimPrefix(path, storeDir) | ||
if rPath == "" { | ||
return nil | ||
} | ||
|
||
return nil | ||
})) | ||
} | ||
|
||
func TestWalkSameFile(t *testing.T) { | ||
// Two files visible via different link chains should both end up in the result set. | ||
|
||
td := t.TempDir() | ||
storeDir := filepath.Join(td, "store") | ||
fn := filepath.Join(storeDir, "real", "file.txt") | ||
assert.NoError(t, os.MkdirAll(filepath.Dir(fn), 0o700)) | ||
assert.NoError(t, ioutil.WriteFile(fn, []byte("foobar"), 0o600)) | ||
|
||
ptr1 := filepath.Join(storeDir, "path", "via", "one", "link") | ||
ptr2 := filepath.Join(storeDir, "another", "path", "to", "this", "file") | ||
|
||
assert.NoError(t, os.MkdirAll(filepath.Dir(ptr1), 0o700)) | ||
assert.NoError(t, os.MkdirAll(filepath.Dir(ptr2), 0o700)) | ||
|
||
assert.NoError(t, os.Symlink(fn, ptr1)) | ||
assert.NoError(t, os.Symlink(fn, ptr2)) | ||
|
||
// test the walkFunc | ||
seen := map[string]bool{} | ||
want := map[string]bool{ | ||
"another/path/to/this/file": true, | ||
"path/via/one/link": true, | ||
"real/file.txt": true, | ||
} | ||
|
||
assert.NoError(t, walkSymlinks(storeDir, func(path string, info fs.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if info.IsDir() && strings.HasPrefix(info.Name(), ".") { | ||
return fs.SkipDir | ||
} | ||
if info.IsDir() { | ||
return nil | ||
} | ||
rPath := strings.TrimPrefix(path, storeDir) | ||
if rPath == "" { | ||
return nil | ||
} | ||
rPath = strings.TrimPrefix(rPath, "/") | ||
rPath = filepath.ToSlash(rPath) // support running this test on Windows | ||
seen[rPath] = true | ||
|
||
return nil | ||
})) | ||
|
||
assert.Equal(t, want, seen) | ||
} |