Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -o / --output option to rgbfix to write separate output files #1666

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jmillikin
Copy link

This allows rgbfix to be used from build systems that discourage or forbid in-place modification of input files in build steps.

  • rgbfix foo.gb and cat foo.gb | rgbfix continue to work as-is.
  • New behavior: rgbfix foo.gb -o out.gb opens foo.gb read-only and writes output to out.gb
  • Existing stdin/stdout support also composes with -o, such that cat foo.gb | rgbfix -o out.gb and rgbfix foo.gb -o - | cat > out.gb work.

@Rangi42 Rangi42 requested a review from ISSOtm March 1, 2025 08:32
@Rangi42 Rangi42 added this to the 0.9.2 milestone Mar 1, 2025
@Rangi42 Rangi42 added enhancement Typically new features; lesser priority than bugs rgbfix This affects RGBFIX labels Mar 1, 2025
@Rangi42
Copy link
Contributor

Rangi42 commented Mar 1, 2025

Hey jmillikin, thank you for this contribution! I'd like to know what @ISSOtm thinks of this feature.

On the one hand, it can currently be done with rgbfix - > out.gb < in.gb, but an explicit rgbfix -o out.gb in.gb flag might be more obvious to users.

If we do want to merge it, the man/rgbfix.1 docs and the contrib/*_compl files would also need updating.

@jmillikin jmillikin force-pushed the rgbfix-output-filename branch from 58450ef to 3255762 Compare March 1, 2025 14:10
@jmillikin
Copy link
Author

Updated man/rgbfix.1 and contrib/*_compl/ for the -o option.

The build system I'm using (Bazel) is pretty strongly file-oriented, and doesn't make it easy to use stdin/stdout for communicating data to/from subprocesses. If invoking a shell manually there's issues of path quoting, so rgbfix would need to be invoked like this:

args = ["/bin/sh", "-c", "$1 - > $2 < $3", "sh", rgbfix.path, out_gb.path, in_gb.path],

... and frankly a desire to avoid that is what inspired the creation of this patch request.

@ISSOtm
Copy link
Member

ISSOtm commented Mar 1, 2025

The problem with >final.gb is that this is handled by the shell, and thus is created empty on failure.

The problem with -o is that the output file can be only partially (over)written.

The proper fix is to write to e.g. final.gb.tmp, and then mv final.gb.tmp final.gb to atomically overwrite the result file.

Note that the “input file” problem is also solved by the above fix; and that RGBLINK and RGBFIX should be considered a single step (they are only separate programs so that RGBFIX can be reused externally... and there have been feature requests for making RGBLINK self-sufficient).

@jmillikin
Copy link
Author

jmillikin commented Mar 1, 2025

Merging the execution of two separate binaries (rgblink and rgbfix) into a single build step would require me to write a wrapper script of some sort, which is a bit of a hassle.

rgbfix seems useful on its own, since it allows patchelf-style modifications to a ROM image without having to put all of the possible modifications into rgblink.

The problem with -o is that the output file can be only partially (over)written.

The proper fix is to write to e.g. final.gb.tmp, and then mv final.gb.tmp final.gb to atomically overwrite the result file.

In this patch the output file is opened with O_CREAT, so if it already exists then rgbfix will error out instead of trying to overwrite it. And if it errors out (calls exit(1)) then the output file will be ignored and discarded by the build system, so partial writes aren't a concern.

@Rangi42
Copy link
Contributor

Rangi42 commented Mar 2, 2025

there have been feature requests for making RGBLINK self-sufficient

This could be difficult, at least with the current v0.x CLIs, because rgblink and rgbfix each have a lot of flags already, and one program that does it all would get complicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Typically new features; lesser priority than bugs rgbfix This affects RGBFIX
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants