Bug 228448 - [PATCH] `unlink -foo` , the original use of `unlink`, no longer works.
Summary: [PATCH] `unlink -foo` , the original use of `unlink`, no longer works.
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords: patch, regression
Depends on:
Blocks:
 
Reported: 2018-05-23 23:50 UTC by Brennan Vincent
Modified: 2018-10-09 05:05 UTC (History)
2 users (show)

See Also:


Attachments
patch fixing the issue (1.27 KB, patch)
2018-05-23 23:50 UTC, Brennan Vincent
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Brennan Vincent 2018-05-23 23:50:57 UTC
Created attachment 193640 [details]
patch fixing the issue

`unlink -foo` fails, instead of removing a file named `-foo`.

The intended behavior of unlink(1) is to remove the file, as can be seen here, when it was added back in 1999: https://svnweb.freebsd.org/base/head/bin/rm/rm.c?r1=53819&r2=54895

Also, this matches POSIX: https://www.unix.com/man-page/posix/1P/unlink/

Patch attached to re-enable the old behavior and also update the documentation.
Comment 1 Ed Maste freebsd_committer freebsd_triage 2018-09-06 20:36:55 UTC
Thanks for the submission and I'd like to see this get fixed for 12.0.

It appears this was broken by r97533, but I don't really understand why that change was made. I guess I can see value in supporting "unlink -- file" but don't see a reason to call getopt. Perhaps we ought to just

if (argv == 2)
  unlink(argv[1])
else if (argv == 3 && argv[1] == "--")
  unlink(argv[2])

The man page in your patch needs one more minor update: s/Either of these/Any of these/.
Comment 2 commit-hook freebsd_committer freebsd_triage 2018-09-12 19:41:31 UTC
A commit references this bug:

Author: emaste
Date: Wed Sep 12 19:41:17 UTC 2018
New revision: 338620
URL: https://svnweb.freebsd.org/changeset/base/338620

Log:
  Fix unlink(1) for files starting with -

  Restore the original behavior of unlink(1), passing the provided filename
  directly to unlink(2), handling the first argument being "--" correctly.

  This fixes "unlink -foo", broken in r97533.

  PR:		228448
  Submitted by:	Brennan Vincent <brennan@umanwizard.com> (original version)
  Submitted by:	Yuri Pankov
  Reported by:	Brennan Vincent <brennan@umanwizard.com>
  Reviewed by:	emaste, kevans, vangyzen, 0mp
  Approved by:	re (delphij)
  Differential Revision: https://reviews.freebsd.org/D17132

Changes:
  head/bin/rm/Makefile
  head/bin/rm/rm.1
  head/bin/rm/rm.c
  head/bin/rm/tests/
  head/bin/rm/tests/Makefile
  head/bin/rm/tests/Makefile.depend
  head/bin/rm/tests/rm_test.sh
  head/etc/mtree/BSD.tests.dist