~nabijaczleweli/voreutils

b21cee30fb68d817921e6eeb8de137eff10d8580 — наб 4 months ago 5c3e788
Add sync

Unlike GNU coreutils, we don't error out if -d and no files
5 files changed, 116 insertions(+), 2 deletions(-)

M README.md
A cmd/sync.cpp
M cmd/tac.cpp
M include/vore-file
A man/sync.1
M README.md => README.md +1 -1
@@ 27,7 27,7 @@ GNU coreutils provide the following 105 binaries, according to `dpkg -L coreutil
  [ ] /bin/rmdir
  [ ] /bin/sleep
  [ ] /bin/stty
  [ ] /bin/sync
  [x] /bin/sync
  [ ] /bin/touch
  [x] /bin/true
  [ ] /bin/uname

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


#include <unistd.h>
#include <cstring>
#include <vore-file>
#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);
	return 1;
}

static __attribute__((unused)) int syncfs(...) {
	errno = ENOSYS;
	return -1;
}


enum class file_mode : std::uint8_t { normal, just_data, whole_fs };


int main(int argc, char * const * argv) {
	auto mode = file_mode::normal;
	for(auto && [arg, _] : vore::opt::get{argc,
	                                      argv,
	                                      "df",
	                                      {{"data", no_argument, nullptr, 'd'},  //
	                                       {"file-system", no_argument, nullptr, 'f'}}})
		switch(arg) {
			case 'd':
				if(mode == file_mode::whole_fs) {
					std::fprintf(stderr, "-f excludes -d!\n");
					return usage(argv[0]);
				}
				mode = file_mode::just_data;
				break;
			case 'f':
				if(mode == file_mode::just_data) {
					std::fprintf(stderr, "-d excludes -f!\n");
					return usage(argv[0]);
				}
				mode = file_mode::whole_fs;
				break;
			default:
				return usage(argv[0]);
		}

	if(*(argv + optind)) {
		for(auto file : vore::opt::args{argv + optind}) {
			vore::file::fd<false> fd{file, O_RDONLY | O_NONBLOCK | O_CLOEXEC};
			if(fd == -1) {
				std::fprintf(stderr, "%s: couldn't open %s: %s\n", argv[0], file, std::strerror(errno));
				continue;
			}

			int err;
			switch(mode) {
				case file_mode::normal:
					err = fsync(fd);
					break;
				case file_mode::just_data:
					err = fdatasync(fd);
					break;
				case file_mode::whole_fs:
					err = syncfs(*fd);
					break;
			}
			if (err == -1)
				std::fprintf(stderr, "%s: couldn't sync %s: %s\n", argv[0], file, std::strerror(errno));
		}
	} else
		sync();
}

M cmd/tac.cpp => cmd/tac.cpp +1 -1
@@ 76,7 76,7 @@ int main(int argc, char * const * argv) {

	std::vector<char> filbuf;
	std::vector<std::pair<std::string_view, std::string_view>> shards;
	for(auto file : vore::opt::args(*(argv + optind) ? (argv + optind) : default_files)) {
	for(auto file : vore::opt::args{*(argv + optind) ? (argv + optind) : default_files}) {
		vore::file::mapping mapped;
		{
			vore::file::fd<true> fd{file, O_RDONLY | O_CLOEXEC};

M include/vore-file => include/vore-file +1 -0
@@ 47,6 47,7 @@ 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/sync.1 => man/sync.1 +35 -0
@@ 0,0 1,35 @@
.\" SPDX-License-Identifier: 0BSD
.\"
.Dd
.Dt SYNC 1
.Os
.
.Sh NAME
.Nm sync
.Nd flush kernel caches
.Sh SYNOPSIS
.Nm
.Op Fl d Ns | Ns Fl f
.Oo Ar file Oc Ns …
.
.Sh DESCRIPTION
Instruct the kernel to commit cached writes to
.Ar file Ns s
to their backing storage.
If none specified, flush all caches.
.
.Sh OPTIONS
.Bl -tag -compact -width "-f, --file-system"
.It Fl d , -data
Don't flush metadata.
.It Fl f , -file-system
Flush entire filesystem containing each
.Ar file .
This is a Linux extension, and will fail elsewhere.
.El
.
.Sh SEE ALSO
.Xr fdatasync 2 ,
.Xr fsync 2 ,
.Xr sync 2 ,
.Xr syncfs 2