Skip to content
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

Follow symbolic links #135

Merged
merged 9 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This is a major release with many breaking changes.
- `-ds`, `-dw`, `-us` and `-uw` global flags are no longer available. Multipart
concurrency and part size flags are now part of the `cp/mv` command. New
replacement flags are `--concurrency | -c` and `--part-size | -p`. ([#110](https://github.com/peak/s5cmd/pull/110))
- s5cmd `cp` command follows symbolic links by default (only when uploading to s3 from local filesystem). Use `--no-follow-symlinks` flag to disable this feature.

#### Features

Expand Down
5 changes: 5 additions & 0 deletions command/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ var copyCommandFlags = []cli.Flag{
Name: "parents",
Usage: "create same directory structure of source, starting from the first wildcard",
},
&cli.BoolFlag{
Name: "no-follow-symlinks",
Usage: "do not follow symbolic links (s5cmd follows symbolic links by default)",
igungor marked this conversation as resolved.
Show resolved Hide resolved
},
&cli.StringFlag{
Name: "storage-class",
Usage: "set storage class for target ('STANDARD','REDUCED_REDUNDANCY','GLACIER','STANDARD_IA')",
Expand Down Expand Up @@ -124,6 +128,7 @@ var CopyCommand = &cli.Command{
partSize: c.Int64("part-size") * megabytes,
}

storage.FollowSymlinks = !c.Bool("no-follow-symlinks")
return copyCommand.Run(c.Context)
},
}
Expand Down
4 changes: 3 additions & 1 deletion command/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func expandSource(
}

ch := make(chan *storage.Object, 1)
ch <- &storage.Object{URL: srcurl}
if storage.ShouldProcessUrl(srcurl) {
ch <- &storage.Object{URL: srcurl}
}
close(ch)
return ch, nil
}
Expand Down
160 changes: 160 additions & 0 deletions command/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package command

import (
"context"
"os"
"reflect"
"sort"
"testing"

"github.com/stretchr/testify/assert"
"gotest.tools/v3/fs"

"github.com/stretchr/testify/mock"

"github.com/peak/s5cmd/storage"
Expand Down Expand Up @@ -175,6 +179,162 @@ func TestExpandSources(t *testing.T) {
}
}

func TestExpandSource_Follow_Link_To_Single_File(t *testing.T) {
folderLayout := []fs.PathOp{
fs.WithDir(
"a",
fs.WithFile("f1.txt", ""),
),
fs.WithDir(
"b",
),
}

workdir := fs.NewDir(t, "expandsourcetest", folderLayout...)
defer workdir.Remove()

os.Symlink(workdir.Join("a/f1.txt"), workdir.Join("b/my_link"))
igungor marked this conversation as resolved.
Show resolved Hide resolved

ctx := context.Background()
workdirUrl, _ := url.New(workdir.Join("b/my_link"))

//follow symbolic links
storage.FollowSymlinks = true
ch, _ := expandSource(ctx, storage.NewFilesystem(), workdirUrl)
var expected []string
for obj := range ch {
expected = append(expected, obj.URL.Absolute())
}
assert.Equal(t, []string{workdir.Join("b/my_link")}, expected)
}

func TestExpandSource_Do_Not_Follow_Link_To_Single_File(t *testing.T) {
folderLayout := []fs.PathOp{
fs.WithDir(
"a",
fs.WithFile("f1.txt", ""),
),
fs.WithDir(
"b",
),
}

workdir := fs.NewDir(t, "expandsourcetest", folderLayout...)
defer workdir.Remove()

os.Symlink(workdir.Join("a/f1.txt"), workdir.Join("b/my_link"))

ctx := context.Background()
workdirUrl, _ := url.New(workdir.Join("b/my_link"))

//do not follow symbolic links
storage.FollowSymlinks = false
ch, _ := expandSource(ctx, storage.NewFilesystem(), workdirUrl)
var expected []string
for obj := range ch {
expected = append(expected, obj.URL.Absolute())
}
assert.Empty(t, expected)
}

func TestExpandSource_Follow_Link_To_Directory(t *testing.T) {
folderLayout := []fs.PathOp{
fs.WithDir(
"a",
fs.WithFile("f1.txt", ""),
fs.WithFile("f2.txt", ""),
fs.WithDir("b",
fs.WithFile("f3.txt", "")),
),
fs.WithDir(
"c",
),
}

workdir := fs.NewDir(t, "expandsourcetest", folderLayout...)
defer workdir.Remove()

os.Symlink(workdir.Join("a"), workdir.Join("c/my_link"))

ctx := context.Background()
workdirUrl, _ := url.New(workdir.Join("c/my_link"))

//follow symbolic links
storage.FollowSymlinks = true
ch, _ := expandSource(ctx, storage.NewFilesystem(), workdirUrl)
var expected []string
for obj := range ch {
expected = append(expected, obj.URL.Absolute())
}
sort.Strings(expected)
assert.Equal(t, []string{
workdir.Join("c/my_link/b/f3.txt"),
workdir.Join("c/my_link/f1.txt"),
workdir.Join("c/my_link/f2.txt"),
}, expected)
}

func TestExpandSource_Do_Not_Follow_Link_To_Directory(t *testing.T) {
folderLayout := []fs.PathOp{
fs.WithDir(
"a",
fs.WithFile("f1.txt", ""),
fs.WithFile("f2.txt", ""),
fs.WithDir("b",
fs.WithFile("f3.txt", "")),
),
fs.WithDir(
"c",
),
}

workdir := fs.NewDir(t, "expandsourcetest", folderLayout...)
defer workdir.Remove()

os.Symlink(workdir.Join("a"), workdir.Join("c/my_link"))

ctx := context.Background()
workdirUrl, _ := url.New(workdir.Join("c/my_link"))

//do not follow symbolic links
storage.FollowSymlinks = false
ch, _ := expandSource(ctx, storage.NewFilesystem(), workdirUrl)
var expected []string
for obj := range ch {
expected = append(expected, obj.URL.Absolute())
}
assert.Empty(t, expected)
}

func TestExpandSource_Do_Not_Follow_Symlinks(t *testing.T) {
ctx := context.Background()
fileContent := "CAFEBABE"
folderLayout := []fs.PathOp{
fs.WithDir(
"a",
fs.WithFile("f1.txt", fileContent),
),
fs.WithDir("b"),
fs.WithDir("c"),
}

workdir := fs.NewDir(t, t.Name(), folderLayout...)
defer workdir.Remove()

workdirUrl, _ := url.New(workdir.Path())
os.Symlink(workdir.Join("a/f1.txt"), workdir.Join("b/link1"))
os.Symlink(workdir.Join("b/link1"), workdir.Join("c/link2"))

//do not follow symbolic links
storage.FollowSymlinks = false
ch, _ := expandSource(ctx, storage.NewFilesystem(), workdirUrl)
var expected []string
for obj := range ch {
expected = append(expected, obj.URL.Absolute())
}
assert.Equal(t, []string{workdir.Join("a/f1.txt")}, expected)
}

func keys(urls map[string][]*storage.Object) []string {
var urlKeys []string
for key := range urls {
Expand Down
125 changes: 125 additions & 0 deletions e2e/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package e2e

import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -2175,3 +2176,127 @@ func TestCopyMultipleLocalNestedFilesToS3WithParents(t *testing.T) {
assert.Assert(t, ensureS3Object(s3client, bucket, "prefix/a/file1.txt", "file1"))
assert.Assert(t, ensureS3Object(s3client, bucket, "prefix/b/c/file2.txt", "file2"))
}

// cp --no-follow-symlinks my_link s3://bucket/prefix/
func TestCopyLinkToASingleFileWithFollowSymlinkDisabled(t *testing.T) {
t.Parallel()

s3client, s5cmd, cleanup := setup(t)
defer cleanup()

const bucket = "bucket"
createBucket(t, s3client, bucket)

fileContent := "CAFEBABE"
folderLayout := []fs.PathOp{
fs.WithDir(
"a",
fs.WithFile("f1.txt", fileContent),
),
fs.WithDir("b"),
}

workdir := fs.NewDir(t, t.Name(), folderLayout...)
defer workdir.Remove()

os.Symlink(workdir.Join("a/f1.txt"), workdir.Join("b/my_link"))

dst := fmt.Sprintf("s3://%v/prefix/", bucket)

cmd := s5cmd("cp", "--no-follow-symlinks", "b/my_link", dst)
result := icmd.RunCmd(cmd, withWorkingDir(workdir))

result.Assert(t, icmd.Success)

assertLines(t, result.Stdout(), map[int]compareFunc{
0: equals(""),
}, sortInput(true))
}

// cp --parents * s3://bucket/prefix/
func TestCopyWithFollowSymlink(t *testing.T) {
t.Parallel()

s3client, s5cmd, cleanup := setup(t)
defer cleanup()

const bucket = "bucket"
createBucket(t, s3client, bucket)

fileContent := "CAFEBABE"
folderLayout := []fs.PathOp{
fs.WithDir(
"a",
fs.WithFile("f1.txt", fileContent),
),
fs.WithDir("b"),
fs.WithDir("c"),
}

workdir := fs.NewDir(t, t.Name(), folderLayout...)
defer workdir.Remove()

os.Symlink(workdir.Join("a/f1.txt"), workdir.Join("b/link1"))
os.Symlink(workdir.Join("b/link1"), workdir.Join("c/link2"))

dst := fmt.Sprintf("s3://%v/prefix/", bucket)

cmd := s5cmd("cp", "--parents", "*", dst)
result := icmd.RunCmd(cmd, withWorkingDir(workdir))

result.Assert(t, icmd.Success)

assertLines(t, result.Stdout(), map[int]compareFunc{
0: equals(""),
1: equals("cp a/f1.txt %va/f1.txt", dst),
2: equals("cp b/link1 %vb/link1", dst),
3: equals("cp c/link2 %vc/link2", dst),
}, sortInput(true))

// assert s3 objects
assert.Assert(t, ensureS3Object(s3client, bucket, "prefix/a/f1.txt", fileContent))
assert.Assert(t, ensureS3Object(s3client, bucket, "prefix/b/link1", fileContent))
assert.Assert(t, ensureS3Object(s3client, bucket, "prefix/c/link2", fileContent))
}

// cp --parents --no-follow-symlinks * s3://bucket/prefix/
func TestCopyWithNoFollowSymlink(t *testing.T) {
t.Parallel()

s3client, s5cmd, cleanup := setup(t)
defer cleanup()

const bucket = "bucket"
createBucket(t, s3client, bucket)

fileContent := "CAFEBABE"
folderLayout := []fs.PathOp{
fs.WithDir(
"a",
fs.WithFile("f1.txt", fileContent),
),
fs.WithDir("b"),
fs.WithDir("c"),
}

workdir := fs.NewDir(t, t.Name(), folderLayout...)
defer workdir.Remove()

os.Symlink(workdir.Join("a/f1.txt"), workdir.Join("b/link1"))
os.Symlink(workdir.Join("b/link1"), workdir.Join("c/link2"))

dst := fmt.Sprintf("s3://%v/prefix/", bucket)

cmd := s5cmd("cp", "--parents", "--no-follow-symlinks", "*", dst)
result := icmd.RunCmd(cmd, withWorkingDir(workdir))

result.Assert(t, icmd.Success)

assertLines(t, result.Stdout(), map[int]compareFunc{
0: equals(""),
1: equals("cp a/f1.txt %va/f1.txt", dst),
}, sortInput(true))

// assert s3 objects
assert.Assert(t, ensureS3Object(s3client, bucket, "prefix/a/f1.txt", fileContent))
}
13 changes: 11 additions & 2 deletions storage/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func (f *Filesystem) expandGlob(ctx context.Context, src *url.URL) <-chan *Objec
}

func walkDir(ctx context.Context, storage Storage, src *url.URL, fn func(o *Object)) {
//skip if symlink is pointing to a dir and --no-follow-symlink
if !ShouldProcessUrl(src) {
return
}
err := godirwalk.Walk(src.Absolute(), &godirwalk.Options{
Callback: func(pathname string, dirent *godirwalk.Dirent) error {
// we're interested in files
Expand All @@ -119,15 +123,20 @@ func walkDir(ctx context.Context, storage Storage, src *url.URL, fn func(o *Obje
fileurl.SetRelative(src.Absolute())

obj, err := storage.Stat(ctx, fileurl)

//skip if symlink is pointing to a file and --no-follow-symlink
if !ShouldProcessUrl(fileurl) {
return nil
}

if err != nil {
return err
}
fn(obj)
return nil
},
// TODO(ig): enable following symlink once we have the necessary cli
// flags
FollowSymbolicLinks: false,
FollowSymbolicLinks: FollowSymlinks,
})
if err != nil {
obj := &Object{Err: err}
Expand Down
Loading