Skip to content

feat: walking through dereferenced symlink directory #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,20 @@ func FilesFromDisk(ctx context.Context, options *FromDiskOptions, filenames map[
var linkTarget string
if isSymlink(info) {
if options != nil && options.FollowSymlinks {
originalFilename := filename
filename, info, err = followSymlink(filename)
if err != nil {
return err
}
if info.IsDir() {
symlinkDirFiles, err := FilesFromDisk(ctx, options, map[string]string{filename: nameInArchive})
if err != nil {
return fmt.Errorf("getting files from symlink directory %s dereferenced to %s: %w", originalFilename, linkTarget, err)
}

files = append(files, symlinkDirFiles...)
return nil
}
} else {
// preserve symlinks
linkTarget, err = os.Readlink(filename)
Expand All @@ -128,6 +138,7 @@ func FilesFromDisk(ctx context.Context, options *FromDiskOptions, filenames map[
}

files = append(files, file)

return nil
})
if walkErr != nil {
Expand Down
52 changes: 52 additions & 0 deletions archives_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package archives

import (
"context"
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
"testing"
)
Expand Down Expand Up @@ -266,6 +268,13 @@ func TestNameOnDiskToNameInArchive(t *testing.T) {
}
}

func fixSeparators(path string) string {
if runtime.GOOS == "windows" {
return strings.ReplaceAll(path, "/", "\\")
}
return path
}

func TestFollowSymlink(t *testing.T) {
// Create temp directory for tests
tmpDir := t.TempDir()
Expand Down Expand Up @@ -510,3 +519,46 @@ func TestFollowSymlink(t *testing.T) {
}
})
}

func TestFilesFromDisk_SymlinkOutsideFileNamesMap(t *testing.T) {
tmpDir := t.TempDir()
otherTmpDir := t.TempDir()

testDirName := "test_dir"
testDir := filepath.Join(otherTmpDir, testDirName)
if err := os.Mkdir(testDir, 0755); err != nil {
t.Fatal(err)
}

testFileName := "test.txt"
testFile := filepath.Join(testDir, testFileName)
if err := os.WriteFile(testFile, []byte("test content"), 0644); err != nil {
t.Fatal(err)
}

symlinkDirName := "symlink_dir"
symlinkDir := filepath.Join(tmpDir, symlinkDirName)
if err := os.Symlink(testDir, symlinkDir); err != nil {
t.Fatal(err)
}

files, err := FilesFromDisk(context.Background(), &FromDiskOptions{
FollowSymlinks: true,
}, map[string]string{symlinkDir: ""})
if err != nil {
t.Fatal(err)
}

sort.Slice(files, func(i, j int) bool {
return files[i].NameInArchive < files[j].NameInArchive
})

if files[0].NameInArchive != symlinkDirName {
t.Fatalf("expected file name '%s', got '%s'", symlinkDirName, files[0].NameInArchive)
}

testFilePath := fmt.Sprintf("%s/%s", symlinkDirName, testFileName)
if files[1].NameInArchive != testFilePath {
t.Fatalf("expected file name '%s', got '%s'", testFilePath, files[1].NameInArchive)
}
}
Loading