SchOS/cmd/reflink/main.c
Nexoniarz a1c965da1e include: one folder per subsystem, and includes that name it
Sixty-five headers sat side by side in include/, so `#include "irq.h"`
said nothing about where irq.h was or what it belonged to, and errno.h,
stat.h and fcntl.h at the top shadowed the system's for any host tool
built with -I./include. They are in folders now -- arch/ core/ fs/
dev/ gfx/ net/ uapi/ (and ldls/ for the Linux driver layer) -- and
every include names the folder, as Linux's do: "core/proc.h".

Done mechanically and checked the hard way: the kernel built from this
tree disassembles identically to the one built from the tree before
it. The full image boots, and userland is the same set of programs.
Paths in docs and comments follow; docs/guides/layout.md has a table
of what is where, and tools/fix-includes.py rewrites old-style
includes from the folders in the tree, for code written before this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S1giNvrHTrPMPw7V6x1kQ4
2026-10-23 15:05:12 +02:00

59 lines
1.5 KiB
C

// reflink -- copy a file by sharing its blocks instead of duplicating them.
//
// reflink <src> <dst>
//
// The copy is instant and free whatever the file's size: only metadata is
// written. Both files are ordinary and writable afterwards; the first write to
// either copies just the run it touches. `df` before and after is the check
// that it really shared rather than quietly copying.
//
// Uses FICLONE, which is Linux's ioctl for exactly this, so the same call works
// against any filesystem that supports cloning.
#include "core/types.h"
#include "uapi/user.h"
#include "uapi/fcntl.h"
#define FICLONE 0x40049409
static void
puts_(const char *s)
{
int n = 0;
while (s[n]) n++;
write(2, (void *)s, n);
}
int
main(int argc, char *argv[])
{
if (argc != 3) {
puts_("usage: reflink <src> <dst>\n");
exit();
}
int src = open(argv[1], O_RDONLY);
if (src < 0) {
puts_("reflink: cannot open "); puts_(argv[1]); puts_("\n");
exit();
}
// O_TRUNC matters: FICLONE replaces the destination's contents entirely,
// and creating it non-empty would leave blocks to release mid-clone.
int dst = open(argv[2], O_CREATE | O_WRONLY | O_TRUNC);
if (dst < 0) {
puts_("reflink: cannot create "); puts_(argv[2]); puts_("\n");
close(src);
exit();
}
if (ioctl(dst, FICLONE, (void *)(long)src) < 0) {
puts_("reflink: FICLONE failed\n");
close(src); close(dst);
exit();
}
close(src);
close(dst);
exit();
}