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

Commit 66baffe

Browse files
committed
Resolve symlink issue.
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 <zhongjie.shi@intel.com>
1 parent e191f5b commit 66baffe

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

pkg/util/fs_utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type DirectoryEntry struct {
3939
}
4040

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

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

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

146146
func CheckSameFile(f1name, f2name string) (bool, error) {
147147
// Check first if files differ in size and immediately return
148-
f1stat, err := os.Stat(f1name)
148+
f1stat, err := os.Lstat(f1name)
149149
if err != nil {
150150
return false, err
151151
}
152-
f2stat, err := os.Stat(f2name)
152+
f2stat, err := os.Lstat(f2name)
153153
if err != nil {
154154
return false, err
155155
}

0 commit comments

Comments
 (0)