Skip to content

Commit

Permalink
Add fallback to readlink to handle relative symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
ewianda committed Jan 28, 2025
1 parent ae0e702 commit 672f785
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion docs/tar.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 18 additions & 5 deletions lib/private/modify_mtree.awk
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,24 @@
symlink_map[path] = $1
# Resolve the symlink if it exists
resolved_path = ""
cmd = "readlink -f " path
cmd = "readlink -f \"" path "\""
cmd | getline resolved_path
close(cmd)
# If readlink -f fails use readlink for relative links
if (resolved_path == "") {
cmd = "readlink \"" path "\""
cmd | getline resolved_path
close(cmd)
}


if (resolved_path) {
if (resolved_path ~ bin_dir) {
if (resolved_path ~ bin_dir || resolved_path ~ /\.\.\//) {
# Strip down the resolved path to start from bin_dir
sub("^.*" bin_dir, bin_dir, resolved_path)
if (path != resolved_path) {
# Replace the content field with the new path
# If the resolved path is different from the original path,
# or if it's a relative path
if (path != resolved_path || resolved_path ~ /\.\.\//) {
symlink = resolved_path
}
}
Expand Down Expand Up @@ -106,7 +114,12 @@ END {
split(line, fields, SUBSEP)
field0 = fields[1]
resolved_path = fields[2]
linked_to = symlink_map[resolved_path]
if (resolved_path in symlink_map) {
linked_to = symlink_map[resolved_path]
}
else {
linked_to = resolved_path
}
# Adjust the line for symlink using the map we created
new_line = field0 " type=link link=" linked_to
print new_line
Expand Down
2 changes: 1 addition & 1 deletion lib/tar.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def mtree_mutate(
name: name of the target, output will be `[name].mtree`.
mtree: input mtree file, typically created by `mtree_spec`.
srcs: list of files to resolve symlinks for.
preserve_symlinks: whether to preserve symlinks in the tar.
preserve_symlinks: `EXPERIMENTAL!` We may remove or change it at any point without further notice. Flag to determine whether to preserve symlinks in the tar.
strip_prefix: prefix to remove from all paths in the tar. Files and directories not under this prefix are dropped.
package_dir: directory prefix to add to all paths in the tar.
mtime: new modification time for all entries.
Expand Down

0 comments on commit 672f785

Please sign in to comment.