~nabijaczleweli/voreutils

8f2234246abc6a7ede18496533030b14082f5d68 — наб 3 months ago 4e62ce5
Add unlink
6 files changed, 87 insertions(+), 5 deletions(-)

M README.md
M cmd/sync.cpp
A cmd/unlink.cpp
M include/vore-file
A man/unlink.1
A tests/unlink
M README.md => README.md +1 -1
@@ 101,7 101,7 @@ GNU coreutils provide the following 105 binaries, according to `dpkg -L coreutil
  [ ] /usr/bin/tty
  [ ] /usr/bin/unexpand
  [ ] /usr/bin/uniq
  [ ] /usr/bin/unlink
  [x] /usr/bin/unlink
  [ ] /usr/bin/users
  [ ] /usr/bin/wc
  [ ] /usr/bin/who

M cmd/sync.cpp => cmd/sync.cpp +1 -3
@@ 7,8 7,6 @@
#include <vore-getopt>
#include <vore-optarg>

using namespace std::literals;


static int usage(const char * argv0) {
	std::fprintf(stderr, "usage: %s [-d|-f] [file]...\n", argv0);


@@ 67,7 65,7 @@ int main(int argc, char * const * argv) {
					err = fdatasync(fd);
					break;
				case file_mode::whole_fs:
					err = syncfs(*fd);
					err = syncfs((int)fd);
					break;
			}
			if (err == -1)

A cmd/unlink.cpp => cmd/unlink.cpp +32 -0
@@ 0,0 1,32 @@
// SPDX-License-Identifier: 0BSD


#include <cstdio>
#include <cstring>
#include <errno.h>
#include <string_view>
#include <unistd.h>

using namespace std::literals;


// -- behaviour from FreeBSD
int main(int, const char * const * argv) {
	const char * tounlink = nullptr;
	std::size_t idx       = 0;

	if(argv[0] && argv[1])
		tounlink = argv[idx = 1];
	if(tounlink && tounlink == "--"sv && argv[2])
		tounlink = argv[idx = 2];

	if(!tounlink || argv[idx + 1]) {
		std::fprintf(stderr, "usage: %s [--] file\n", argv[0]);
		return 1;
	}

	if(unlink(tounlink) == -1) {
		std::fprintf(stderr, "%s: %s: %s\n", argv[0], tounlink, std::strerror(errno));
		return 1;
	}
}

M include/vore-file => include/vore-file +0 -1
@@ 47,7 47,6 @@ namespace vore::file {
				close(this->desc);
		}

		constexpr int operator*() const noexcept { return this->desc; }
		constexpr operator int() const noexcept { return this->desc; }

	private:

A man/unlink.1 => man/unlink.1 +22 -0
@@ 0,0 1,22 @@
.\" SPDX-License-Identifier: 0BSD
.\"
.Dd
.Dt UNLINK 1
.Os
.
.Sh NAME
.Nm unlink
.Nd remove file link
.Sh SYNOPSIS
.Nm
.Ar file
.Nm
.Sy --
.Ar file
.
.Sh DESCRIPTION
Removes
.Ar file .
.
.Sh SEE ALSO
.Xr unlink 2

A tests/unlink => tests/unlink +31 -0
@@ 0,0 1,31 @@
#!/bin/sh
# SPDX-License-Identifier: 0BSD

tmpdir="$(mktemp -dt "unlink.XXXXXXXXXX")/"
unlink="${CMDDIR}unlink"

altname="${tmpdir%/}"
altname="${altname##*/}"

cd "$tmpdir" || exit 1

for f in 'file' './file' '--'; do
	for fp in "$f" "${tmpdir}$f"; do
		for pref in '' '--'; do
			"$unlink" $pref "$fp" 2>err && echo "unlink: didn't fail on $fp ($pref)" >&3
			[ -s "err" ] || echo "unlink: no error for $fp ($pref)" >&3

			mkdir -- "$fp"
			"$unlink" $pref "$fp" 2>err && echo "unlink: didn't fail on dir $fp ($pref)" >&3
			[ -s "err" ] || echo "unlink: no error for dir $fp ($pref)" >&3
			rmdir -- "$fp"

			echo > "$fp"
			"$unlink" $pref "$fp"
			[ -f "$fp" ] && echo "unlink: didn't remove $fp ($pref)" >&3
		done
	done
done

cd /
rm -rf "$tmpdir" >&3