Skip to content

Commit

Permalink
Handle symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
ewianda committed Jan 20, 2025
1 parent 0b8b916 commit e87fad1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 233 deletions.
44 changes: 44 additions & 0 deletions lib/private/modify_mtree.awk
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
# Edits mtree files. See the modify_mtree macro in /lib/tar.bzl.
function create_map(filename) {
for (i in map) delete map[i]
while (getline < filename > 0) {
if ($0 ~ /type=file/ && $0 ~ /content=/) {
match($0, /content=[^ ]+/)
content_field = substr($0, RSTART, RLENGTH)
split(content_field, parts, "=")
path = parts[2]
map[path] = $1
}
}
close(filename)
}
BEGIN {
# Here we're setting the filename, but you could pass it as an argument to awk
#filename = "bazel-bin/lib/tests/tar/mtree17.spec"

if (mtree_spec != "") {
create_map(mtree_spec)
}
}
{
if (strip_prefix != "") {
if ($1 == strip_prefix) {
Expand Down Expand Up @@ -45,5 +66,28 @@
if (package_dir != "") {
sub(/^/, package_dir "/")
}
if ($0 ~ /type=file/ && $0 ~ /content=/) {
match($0, /content=[^ ]+/)
content_field = substr($0, RSTART, RLENGTH)
split(content_field, parts, "=")
path = parts[2]

# Resolve the symlink if it exists
resolved_path = ""
cmd = "readlink -f " path
cmd | getline resolved_path
close(cmd)

if (resolved_path) {
if (resolved_path ~ bin_dir) {
# 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
$0 = $1 " type=link link=" map[resolved_path]
}
}
}
}
print;
}
53 changes: 31 additions & 22 deletions lib/private/tar.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -451,37 +451,51 @@ def _mtree_mutate_impl(ctx):
src[DefaultInfo].default_runfiles.files
for src in ctx.attr.srcs
]
args = ctx.actions.args()
bsdtar = ctx.toolchains[TAR_TOOLCHAIN_TYPE]
mtree_generator = ctx.executable.mtree_generator.path

out_mtree = ctx.outputs.out
args.add("--input", ctx.file.mtree)
args.add("--output", out_mtree)
args.add("--bin_dir", ctx.bin_dir.path)
env = {
"MTREE": ctx.file.mtree.path,
"OUT": out_mtree.path,
"BIN_DIR": ctx.bin_dir.path,
}

if ctx.attr.owner:
args.add("--owner", ctx.attr.owner)
env["OWNER"] = ctx.attr.owner
if ctx.attr.ownername:
args.add("--ownername", ctx.attr.ownername)
env["OWNERNAME"] = ctx.attr.ownername
if ctx.attr.strip_prefix:
args.add("--strip_prefix", ctx.attr.strip_prefix)
env["STRIP_PREFIX"] = ctx.attr.strip_prefix
if ctx.attr.package_dir:
args.add("--package_dir", ctx.attr.package_dir)
env["PACKAGE_DIR"] = ctx.attr.package_dir
if ctx.attr.mtime:
args.add("--mtime", ctx.attr.mtime)
env["MTIME"] = ctx.attr.mtime

#executable = bsdtar.tarinfo.binary,
inputs = ctx.files.srcs[:]
inputs.append(ctx.file.mtree)
ctx.actions.run(
executable = mtree_generator,
arguments = [args],
inputs.append(ctx.file.awk_script)

ctx.actions.run_shell(
command = """
env > /home/ewianda/projects/env
awk \
-v strip_prefix="$STRIP_PREFIX" \
-v package_dir="$PACKAGE_DIR" \
-v mtime="$MTIME" \
-v owner="$OWNER" \
-v ownername="$OWNERNAME" \
-v bin_dir="$BIN_DIR" \
-v mtree_spec="$MTREE" \
-f {awk_script} {mtree} > {out}
""".format(
awk_script = ctx.file.awk_script.path,
mtree = ctx.file.mtree.path,
out = out_mtree.path,
),
env = env,
inputs = depset(
direct = inputs,
transitive = srcs_runfiles + [
ctx.attr.mtree_generator.default_runfiles.files,
],
transitive = srcs_runfiles,
),
outputs = [out_mtree],
)
Expand All @@ -500,11 +514,6 @@ mtree_mutate = rule(
"owner": attr.string(),
"ownername": attr.string(),
"out": attr.output(),
"mtree_generator": attr.label(
default = Label("//tools/mtree:mtree"),
executable = True,
cfg = "exec",
),
},
toolchains = [
TAR_TOOLCHAIN_TYPE,
Expand Down
14 changes: 0 additions & 14 deletions tools/mtree/BUILD.bazel

This file was deleted.

197 changes: 0 additions & 197 deletions tools/mtree/main.go

This file was deleted.

0 comments on commit e87fad1

Please sign in to comment.