Skip to content

Commit

Permalink
fs/copy: continue copying xattrs in case one fails
Browse files Browse the repository at this point in the history
Signed-off-by: Niels de Vos <ndevos@redhat.com>
  • Loading branch information
nixpanic committed Nov 21, 2018
1 parent cc6b518 commit 194c23c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
9 changes: 2 additions & 7 deletions fs/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"sync"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

var bufferPool = &sync.Pool{
Expand Down Expand Up @@ -131,12 +130,8 @@ func copyDirectory(dst, src string, inodes map[uint64]string, o *copyDirOpts) er
return errors.Wrap(err, "failed to copy file info")
}

if err := copyXAttrs(target, source); err != nil {
if o.allowXAttrErrors {
logrus.Warnf("failed to copy xattrs from %s to %s: %v", source, target, err)
} else {
return errors.Wrap(err, "failed to copy xattrs")
}
if err := copyXAttrs(target, source, o.allowXAttrErrors); err != nil {
return errors.Wrap(err, "failed to copy xattrs")
}
}

Expand Down
24 changes: 20 additions & 4 deletions fs/copy_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"syscall"

"github.com/containerd/continuity/sysx"
"github.com/sirupsen/logrus"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -101,18 +102,33 @@ func copyFileContent(dst, src *os.File) error {
return nil
}

func copyXAttrs(dst, src string) error {
func copyXAttrs(dst, src string, allowErrors bool) error {
xattrKeys, err := sysx.LListxattr(src)
if err != nil {
return errors.Wrapf(err, "failed to list xattrs on %s", src)
e := errors.Wrapf(err, "failed to list xattrs on %s", src)
if allowErrors {
logrus.Warnf("%v", e)
return nil
}
return e
}
for _, xattr := range xattrKeys {
data, err := sysx.LGetxattr(src, xattr)
if err != nil {
return errors.Wrapf(err, "failed to get xattr %q on %s", xattr, src)
e := errors.Wrapf(err, "failed to get xattr %q on %s", xattr, src)
if allowErrors {
logrus.Warnf("%v", e)
return nil
}
return e
}
if err := sysx.LSetxattr(dst, xattr, data, 0); err != nil {
return errors.Wrapf(err, "failed to set xattr %q on %s", xattr, dst)
e := errors.Wrapf(err, "failed to set xattr %q on %s", xattr, dst)
if allowErrors {
logrus.Warnf("%v", e)
return nil
}
return e
}
}

Expand Down
24 changes: 20 additions & 4 deletions fs/copy_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"syscall"

"github.com/containerd/continuity/sysx"
"github.com/sirupsen/logrus"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -69,18 +70,33 @@ func copyFileContent(dst, src *os.File) error {
return err
}

func copyXAttrs(dst, src string) error {
func copyXAttrs(dst, src string, allowErrors bool) error {
xattrKeys, err := sysx.LListxattr(src)
if err != nil {
return errors.Wrapf(err, "failed to list xattrs on %s", src)
e := errors.Wrapf(err, "failed to list xattrs on %s", src)
if allowErrors {
logrus.Warnf("%v", e)
return nil
}
return e
}
for _, xattr := range xattrKeys {
data, err := sysx.LGetxattr(src, xattr)
if err != nil {
return errors.Wrapf(err, "failed to get xattr %q on %s", xattr, src)
e := errors.Wrapf(err, "failed to get xattr %q on %s", xattr, src)
if allowErrors {
logrus.Warnf("%v", e)
return nil
}
return e
}
if err := sysx.LSetxattr(dst, xattr, data, 0); err != nil {
return errors.Wrapf(err, "failed to set xattr %q on %s", xattr, dst)
e := errors.Wrapf(err, "failed to set xattr %q on %s", xattr, dst)
if allowErrors {
logrus.Warnf("%v", e)
return nil
}
return e
}
}

Expand Down
2 changes: 1 addition & 1 deletion fs/copy_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func copyFileContent(dst, src *os.File) error {
return err
}

func copyXAttrs(dst, src string) error {
func copyXAttrs(dst, src string, allowErrors bool) error {
return nil
}

Expand Down

0 comments on commit 194c23c

Please sign in to comment.