#!/usr/bin/env -S LC_ALL=C LANGUAGE=C awk -E # SPDX-License-Identifier: EUPL-1.2+ # SPDX-FileCopyrightText: 2025 Demi Marie Obenour BEGIN { RS = "\n"; FS = "\t"; file_count = 0; symlink_count = 0; rc_count = 0; is_rc = 0; exit_code = 0; done = 0; modes["120000"] = "symlink"; modes["040755"] = "directory"; modes["100644"] = "regular"; modes["100755"] = "regular"; } function fail(msg, status) { if (status ~ /^([1-9][0-9]?|1[0-9]{2}|2[0-4][1-9]|25[1-5])$/) { exit_code = status; } else { exit_code = 1; status = 1; } print ("FATAL: " msg) > "/dev/stderr"; exit status; } done { fail("Junk after DONE", 1); } /^DONE$/ { done = 1 next } # Make sure git produced valid output. !/^[0-7]{6}\t[ -~]+$/ { fail("git ls-files produced invalid output", 1); } # Extract data from built-in variables. { filename = $2; raw_mode = $1; # awk autocreates empty string entries if the key is invalid, # but the code exits in this case so that is okay. mode = modes[raw_mode]; } # Another check for a git bug. filename ~ /^\/|((^|\/)\.{0,2}($|\/))/ { fail("git ls-files output non-canonical or absolute path '" filename "'", 1); } filename ~ /[^[:alnum:]_.+@/-]/ { fail("filename '" filename "' has forbidden characters", 1); } /\.license$/ { if (raw_mode != "100644") { fail("License file '" filename "' is executable or not regular file", 1); } next; } mode == "directory" { next } filename ~ /^image\/etc\/s6-rc\// { if (mode != "regular") { fail("s6-rc-compile input '" filename "' isn't a regular file"); } rc_count += 1; rc_files[rc_count] = filename; next; } mode == "symlink" { symlink_count += 1; symlinks[symlink_count] = filename; next; } mode == "regular" { file_count += 1; files[file_count] = filename; next; } { fail("File '" filename "' is not regular file, directory, or symlink (mode " raw_mode ")"); } END { if (exit_code) { exit exit_code; } if (!done) { fail("Did not receive DONE line", 1); } printf ("# SPDX-License-Identifier: CC0-1.0\n" \ "# SPDX-FileCopyrightText: 2025 Demi Marie Obenour \n" \ "# Generated by scripts/genfile.sh. Any changes will be overwritten.\n" \ "FILES ::=") > out_file; for (array_index = 1; array_index <= file_count; array_index += 1) { printf " \\\n\t%s", files[array_index] > out_file; } printf ("\n\n" \ "# These are separate because they need to be included, but putting\n" \ "# them as make dependencies would confuse make.\n" \ "LINKS ::=") > out_file; for (array_index = 1; array_index <= symlink_count; array_index += 1) { printf " \\\n\t%s", symlinks[array_index] > out_file; } printf "\n\nS6_RC_FILES ::=" > out_file; for (array_index = 1; array_index <= rc_count; array_index += 1) { printf " \\\n\t%s", rc_files[array_index] > out_file; } printf "\n" > out_file; if (close(out_file)) { print ("Cannot close output file: " ERRNO "\n") > "/dev/stderr"; exit 1; } }