Page MenuHomeFreeBSD

install: Simplify path construction.
ClosedPublic

Authored by des on Apr 10 2024, 7:20 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, May 18, 6:03 PM
Unknown Object (File)
Apr 25 2025, 4:46 AM
Unknown Object (File)
Apr 24 2025, 4:38 AM
Unknown Object (File)
Apr 22 2025, 9:56 PM
Unknown Object (File)
Apr 15 2025, 11:31 PM
Unknown Object (File)
Mar 26 2025, 6:34 AM
Unknown Object (File)
Feb 12 2025, 5:33 PM
Unknown Object (File)
Feb 8 2025, 12:56 AM
Subscribers

Details

Summary

There's no need to copy the path twice to split it into base and dir.
We simply call basename() first, then handle the two trivial cases in
which it isn't safe to call dirname().

While here, add an early check that the destination is not an empty
string. This would always fail eventually, so it may as well fail
right away. Also add a test case for this shortcut.

MFC after: 1 week
Sponsored by: Klara, Inc.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

markj added inline comments.
usr.bin/xinstall/xinstall.c
722

style(9) no longer requires local variables to be declared at the beginning of a function, and NetBSD has the declarations here, though the code's otherwise diverged somewhat.

744

basename(3) is documented as returning a pointer to "/" or "." in some degenerate cases, in which case this comparison doesn't make sense. Is it guaranteed that that won't happen here?

I also don't quite understand the comparison itself. It distinguishes between "//foo" and "/foo", is that intentional?

usr.bin/xinstall/xinstall.c
744
  1. You're right, if to_name_copy is empty then the comparison is invalid. I'll add a check that prevents calling makelink() with an empty to_name. In all other cases, basename() will return a pointer to somewhere within its input.
  1. Yes, because the latter is the only case in which dirname() would clobber the base name.

exit early if target is empty

des marked an inline comment as done.Apr 12 2024, 10:43 AM
des marked an inline comment as done.Apr 12 2024, 11:02 AM
des added inline comments.
usr.bin/xinstall/xinstall.c
722

Chalk it up to personal preference.

des marked an inline comment as done.Apr 12 2024, 11:02 AM
usr.bin/xinstall/xinstall.c
744

Yes, because the latter is the only case in which dirname() would clobber the base name.

Sorry, I don't follow. dirname("//foo") and dirname("/foo") give the same result.

des marked an inline comment as done.Apr 12 2024, 2:10 PM
des added inline comments.
usr.bin/xinstall/xinstall.c
744

dirname("/foo") will overwrite the f in foo with a NUL, clobbering base, but dirname("//foo") will overwrite the second /, leaving base unscathed.

markj added inline comments.
usr.bin/xinstall/xinstall.c
744

I see, ok. It's not easy to understand this block; I'd add a comment explaining what it does.

This revision is now accepted and ready to land.Apr 12 2024, 2:18 PM
des marked an inline comment as done.Apr 12 2024, 3:12 PM
des added inline comments.
usr.bin/xinstall/xinstall.c
744

It's a good think you asked for an explanatory comment, because it made me realize that I'd missed a fairly common case... I'll update the diff shortly.

fix cwd case, add comments and test case for empty destination

This revision now requires review to proceed.Apr 12 2024, 3:37 PM
des marked an inline comment as done.Apr 12 2024, 3:41 PM
This revision is now accepted and ready to land.Apr 12 2024, 5:29 PM
This revision was automatically updated to reflect the committed changes.