Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Resolve symlink issue.
Browse files Browse the repository at this point in the history
If there is certain symlink in the container image like this:

/A --> /X/Y/Z

AND if the "/X/Y/Z" doesn't exists on the host OS, then the current
invocation to os.Stat will give errors saying that the "Could not
obtain size for /X/Y/Z". This is because that it will follow the
link to check the /X/Y/Z on the host. That is improper.

The proper behaviour should be:
From the perspective of the symlink inside the container image, the
valid target file it pointing to is always some "existing file" in
the container image.
From the perspective of the host OS, it should just respect the
"closure" of the file system inside the container image.

To resolve it, instead of the os.Stat, we can use os.Lstat which
will not follow the link to check the target file.

Signed-off-by: zhongjie <[email protected]>
  • Loading branch information
intelzhongjie committed Mar 15, 2021
1 parent e191f5b commit 66baffe
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/util/fs_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type DirectoryEntry struct {
}

func GetSize(path string) int64 {
stat, err := os.Stat(path)
stat, err := os.Lstat(path)
if err != nil {
logrus.Errorf("Could not obtain size for %s: %s", path, err)
return -1
Expand All @@ -56,7 +56,7 @@ func GetSize(path string) int64 {

//GetFileContents returns the contents of a file at the specified path
func GetFileContents(path string) (*string, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
if _, err := os.Lstat(path); os.IsNotExist(err) {
return nil, err
}

Expand Down Expand Up @@ -145,11 +145,11 @@ func CheckSameSymlink(f1name, f2name string) (bool, error) {

func CheckSameFile(f1name, f2name string) (bool, error) {
// Check first if files differ in size and immediately return
f1stat, err := os.Stat(f1name)
f1stat, err := os.Lstat(f1name)
if err != nil {
return false, err
}
f2stat, err := os.Stat(f2name)
f2stat, err := os.Lstat(f2name)
if err != nil {
return false, err
}
Expand Down

0 comments on commit 66baffe

Please sign in to comment.