-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1896 from buildpacks/bugfix/jjbustamante/issue-1286
Hardlinks are dereferenced in generated archives
- Loading branch information
Showing
13 changed files
with
247 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//go:build linux || darwin | ||
|
||
package archive | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
// hasHardlinks check if the given files has a hard-link associated with it | ||
func hasHardlinks(fi os.FileInfo, path string) (bool, error) { | ||
return fi.Sys().(*syscall.Stat_t).Nlink > 1, nil | ||
} | ||
|
||
// getInodeFromStat returns the inode (index node) value associated with the given file | ||
func getInodeFromStat(stat interface{}, path string) (inode uint64, err error) { | ||
s, ok := stat.(*syscall.Stat_t) | ||
if ok { | ||
inode = s.Ino | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//go:build windows | ||
|
||
package archive | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
// hasHardlinks returns true if the given file has hard-links associated with it | ||
func hasHardlinks(fi os.FileInfo, path string) (bool, error) { | ||
var numberOfLinks uint32 | ||
switch v := fi.Sys().(type) { | ||
case *syscall.ByHandleFileInformation: | ||
numberOfLinks = v.NumberOfLinks | ||
default: | ||
// We need an instance of a ByHandleFileInformation to read NumberOfLinks | ||
info, err := open(path) | ||
if err != nil { | ||
return false, err | ||
} | ||
numberOfLinks = info.NumberOfLinks | ||
} | ||
return numberOfLinks > 1, nil | ||
} | ||
|
||
// getInodeFromStat returns an equivalent representation of unix inode on windows based on FileIndexHigh and FileIndexLow values | ||
func getInodeFromStat(stat interface{}, path string) (inode uint64, err error) { | ||
s, ok := stat.(*syscall.ByHandleFileInformation) | ||
if ok { | ||
inode = (uint64(s.FileIndexHigh) << 32) | uint64(s.FileIndexLow) | ||
} else { | ||
s, err = open(path) | ||
if err == nil { | ||
inode = (uint64(s.FileIndexHigh) << 32) | uint64(s.FileIndexLow) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// open returns a ByHandleFileInformation object representation of the given file | ||
func open(path string) (*syscall.ByHandleFileInformation, error) { | ||
fPath, err := syscall.UTF16PtrFromString(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
handle, err := syscall.CreateFile( | ||
fPath, | ||
windows.FILE_READ_ATTRIBUTES, | ||
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE, | ||
nil, | ||
syscall.OPEN_EXISTING, | ||
syscall.FILE_FLAG_BACKUP_SEMANTICS, | ||
0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer syscall.CloseHandle(handle) | ||
|
||
var info syscall.ByHandleFileInformation | ||
if err = syscall.GetFileInformationByHandle(handle, &info); err != nil { | ||
return nil, err | ||
} | ||
return &info, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build-contents |
Empty file.
10 changes: 10 additions & 0 deletions
10
pkg/buildpack/testdata/buildpack-with-hardlink/buildpack.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
api = "0.3" | ||
|
||
[buildpack] | ||
id = "bp.one" | ||
version = "1.2.3" | ||
homepage = "http://one.buildpack" | ||
|
||
[[stacks]] | ||
id = "some.stack.id" | ||
mixins = ["mixinX", "build:mixinY", "run:mixinZ"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters