Page MenuHomeFreeBSD

install: Simplify path construction.
ClosedPublic

Authored by des on Apr 10 2024, 7:20 PM.
Tags
None
Referenced Files
Unknown Object (File)
Nov 25 2024, 4:44 PM
Unknown Object (File)
Nov 23 2024, 12:48 PM
Unknown Object (File)
Nov 21 2024, 1:19 PM
Unknown Object (File)
Nov 14 2024, 11:34 PM
Unknown Object (File)
Sep 11 2024, 8:25 PM
Unknown Object (File)
Sep 11 2024, 8:25 PM
Unknown Object (File)
Aug 7 2024, 12:05 AM
Unknown Object (File)
Jul 28 2024, 8:31 PM
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 Skipped
Unit
Tests Skipped
Build Status
Buildable 57050
Build 53938: arc lint + arc unit

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.