Skip to content

Commit

Permalink
Merge pull request #1144 from giuseppe/chown-skip-hard-links
Browse files Browse the repository at this point in the history
drivers, chown: support chown of hard links
  • Loading branch information
rhatdan authored Feb 21, 2022
2 parents fb97d67 + f59aa7a commit 765d5b3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
5 changes: 4 additions & 1 deletion drivers/chown.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ func chownByMapsMain() {
if len(toHost.UIDs()) == 0 && len(toHost.GIDs()) == 0 {
toHost = nil
}

chowner := newLChowner()

chown := func(path string, info os.FileInfo, _ error) error {
if path == "." {
return nil
}
return platformLChown(path, info, toHost, toContainer)
return chowner.LChown(path, info, toHost, toContainer)
}
if err := pwalk.Walk(".", chown); err != nil {
fmt.Fprintf(os.Stderr, "error during chown: %v", err)
Expand Down
36 changes: 35 additions & 1 deletion drivers/chown_unix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package graphdriver
Expand All @@ -6,17 +7,50 @@ import (
"errors"
"fmt"
"os"
"sync"
"syscall"

"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/pkg/system"
)

func platformLChown(path string, info os.FileInfo, toHost, toContainer *idtools.IDMappings) error {
type inode struct {
Dev uint64
Ino uint64
}

type platformChowner struct {
mutex sync.Mutex
inodes map[inode]bool
}

func newLChowner() *platformChowner {
return &platformChowner{
inodes: make(map[inode]bool),
}
}

func (c *platformChowner) LChown(path string, info os.FileInfo, toHost, toContainer *idtools.IDMappings) error {
st, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return nil
}

i := inode{
Dev: uint64(st.Dev),
Ino: uint64(st.Ino),
}
c.mutex.Lock()
_, found := c.inodes[i]
if !found {
c.inodes[i] = true
}
c.mutex.Unlock()

if found {
return nil
}

// Map an on-disk UID/GID pair from host to container
// using the first map, then back to the host using the
// second map. Skip that first step if they're 0, to
Expand Down
10 changes: 9 additions & 1 deletion drivers/chown_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package graphdriver
Expand All @@ -9,6 +10,13 @@ import (
"github.com/containers/storage/pkg/idtools"
)

func platformLChown(path string, info os.FileInfo, toHost, toContainer *idtools.IDMappings) error {
type platformChowner struct {
}

func newLChowner() *platformChowner {
return &platformChowner{}
}

func (c *platformChowner) LChown(path string, info os.FileInfo, toHost, toContainer *idtools.IDMappings) error {
return &os.PathError{"lchown", path, syscall.EWINDOWS}
}

0 comments on commit 765d5b3

Please sign in to comment.