Skip to content

Commit

Permalink
Merge branch 'master' into feat/select-api-support
Browse files Browse the repository at this point in the history
  • Loading branch information
denizsurmeli committed Aug 4, 2023
2 parents 0efbd5d + 393e8dd commit d6692eb
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 5 deletions.
9 changes: 8 additions & 1 deletion command/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,14 @@ func (c Copy) Run(ctx context.Context) error {
}

for object := range objch {
if object.Type.IsDir() || errorpkg.IsCancelation(object.Err) {
if errorpkg.IsCancelation(object.Err) || object.Type.IsDir() {
continue
}

if !object.Type.IsRegular() {
err := fmt.Errorf("object '%v' is not a regular file", object)
merrorObjects = multierror.Append(merrorObjects, err)
printError(c.fullCommand, c.op, err)
continue
}

Expand Down
8 changes: 4 additions & 4 deletions command/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func expandSource(
followSymlinks bool,
srcurl *url.URL,
) (<-chan *storage.Object, error) {
var isDir bool
var objType storage.ObjectType
// if the source is local, we send a Stat call to know if we have
// directory or file to walk. For remote storage, we don't want to send
// Stat since it doesn't have any folder semantics.
Expand All @@ -28,17 +28,17 @@ func expandSource(
if err != nil {
return nil, err
}
isDir = obj.Type.IsDir()
objType = obj.Type
}

// call storage.List for only walking operations.
if srcurl.IsWildcard() || srcurl.AllVersions || isDir {
if srcurl.IsWildcard() || srcurl.AllVersions || objType.IsDir() {
return client.List(ctx, srcurl, followSymlinks), nil
}

ch := make(chan *storage.Object, 1)
if storage.ShouldProcessURL(srcurl, followSymlinks) {
ch <- &storage.Object{URL: srcurl}
ch <- &storage.Object{URL: srcurl, Type: objType}
}
close(ch)
return ch, nil
Expand Down
42 changes: 42 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"
"net"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -4196,3 +4197,44 @@ func TestCountingWriter(t *testing.T) {
expected := fs.Expected(t, fs.WithFile(filename, content, fs.WithMode(0644)))
assert.Assert(t, fs.Equal(cmd.Dir, expected))
}

// It should skip special files
func TestUploadingSocketFile(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip()
}

t.Parallel()

s3client, s5cmd := setup(t)
bucket := s3BucketFromTestName(t)
createBucket(t, s3client, bucket)

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

sockaddr := workdir.Join("/s5cmd.sock")
ln, err := net.Listen("unix", sockaddr)
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
ln.Close()
os.Remove(sockaddr)
})

cmd := s5cmd("cp", sockaddr, "s3://"+bucket+"/")
result := icmd.RunCmd(cmd, withWorkingDir(workdir))

// assert error message
assertLines(t, result.Stderr(), map[int]compareFunc{
0: contains(`is not a regular file`),
})

// assert logs are empty (no copy)
assertLines(t, result.Stdout(), nil)

// assert exit code
result.Assert(t, icmd.Expected{ExitCode: 1})
}
43 changes: 43 additions & 0 deletions e2e/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package e2e

import (
"fmt"
"net"
"os"
"path/filepath"
"runtime"
"testing"
Expand Down Expand Up @@ -1920,3 +1922,44 @@ func TestSyncS3BucketToS3BucketThatDoesNotExist(t *testing.T) {
0: contains(`status code: 404`),
})
}

// If source path contains a special file it should not be synced
func TestSyncSocketDestinationEmpty(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip()
}

t.Parallel()

s3client, s5cmd := setup(t)
bucket := s3BucketFromTestName(t)
createBucket(t, s3client, bucket)

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

sockaddr := workdir.Join("/s5cmd.sock")
ln, err := net.Listen("unix", sockaddr)
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
ln.Close()
os.Remove(sockaddr)
})

cmd := s5cmd("sync", ".", "s3://"+bucket+"/")
result := icmd.RunCmd(cmd, withWorkingDir(workdir))

// assert error message
assertLines(t, result.Stderr(), map[int]compareFunc{
0: contains(`is not a regular file`),
})

// assert logs are empty (no sync)
assertLines(t, result.Stdout(), nil)

// assert exit code
result.Assert(t, icmd.Expected{ExitCode: 1})
}
5 changes: 5 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ func (o ObjectType) IsSymlink() bool {
return o.mode&os.ModeSymlink != 0
}

// IsRegular checks if the object is a regular file.
func (o ObjectType) IsRegular() bool {
return o.mode.IsRegular()
}

// ShouldProcessURL returns true if follow symlinks is enabled.
// If follow symlinks is disabled we should not process the url.
// (this check is needed only for local files)
Expand Down

0 comments on commit d6692eb

Please sign in to comment.