From 261c3a0720f59086266ad55f251784af0e8c5456 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:13:49 +0100 Subject: [PATCH] Sys.Info.links: how many names a file has From st_nlink, beside ino, in both Sys. For arno, which must tell a file made in its scratch dir from a hard link to one outside. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 85ea96b..b4c48f2 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -649,28 +649,3 @@ Met in rrem's `RremUcd`, whose tables `tools/Ucd.raz` writes: before the tool's first run, each table needs a placeholder of one element, which the code reading it must then not take for a real one. - -## A file's link count - -`Sys.Info` names a file (`dev`, `ino`) but doesn't say how many names -it has. A program that lets a writer loose in a directory of its own -can't tell a file made there from a hard link to one somewhere else: -both are regular files inside the directory, and writing to either -writes to the one file. - -The proposal, in `Info`, beside `ino`: - -``` -links*: U64; (* how many names the file has: 1 for a file no hard link shares *) -``` - -filled in `fromStat` from `st_nlink`, which is `st[2]` on -x86_64 Linux, as `st_ino` is `st[1]`. A field more in a record breaks -only a program that builds an `Info` by a literal of all its fields, -so it could come with a thaw. - -Met in arno: a conversation's scratch directory is the model's own, -and its `edit`, `move` and `remove` there go ahead without asking. The -Pascal arno refused that freedom for a file with a second link, which -shares its contents with a name outside, perhaps one of the human's; -the raz arno can't, until `Info` says how many links a file has. diff --git a/doc/raz-lib.md b/doc/raz-lib.md index a1d98f2..aafeef0 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -179,6 +179,7 @@ symlink*: BOOL; (* the path itself is a symbolic link *) ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) dev*: U64; (* the file system it is on; with ino, it names the file *) + links*: U64; (* how many names it has: 1 for a file no hard link shares *) end; Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *) Dir* = record ... end; (* an open directory; its fields are the target's *) @@ -357,7 +358,9 @@ files have none. `Info.dev` is the file system the file is on: two files are one only if both `dev` and `ino` agree, which is how a program following links finds that one has led it back into a -directory it is already in. `Info.mode` holds the nine +directory it is already in. `Info.links` is how many names the file +has: more than 1 if a hard link shares it, so that writing to it +writes to a file named elsewhere too; 0 on a target that doesn't say. `Info.mode` holds the nine bits of `rwxrwxrwx`, and `chmod` sets them, 1EDH (755) for a program, 1A4H (644) for data. diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 9618e90..cc2bc60 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -41,6 +41,7 @@ symlink*: BOOL; (* the path itself is a symbolic link *) ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) dev*: U64; (* the file system it is on; with ino, it names the file *) + links*: U64; (* how many names it has: 1 for a file no hard link shares *) end; (* What a directory says a name in it is, without asking the file: @@ -440,6 +441,7 @@ i.size := st[6]; (* st_size *) i.dev := U64(st[0]); (* st_dev *) i.ino := U64(st[1]); (* st_ino *) + i.links := U64(st[2]); (* st_nlink *) i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) i.mode := st[3] & 1FFH; i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) @@ -2243,7 +2245,7 @@ if (entryKind(ADR(d)) # isFile) and (entryKind(ADR(d)) # unknown) then exit(2); end; i, err := info("/tmp/raz-sys-dir/g"); j, err := info("/tmp/raz-sys-dir"); - if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) then exit(2); end; + if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) or (i.links # 1) then exit(2); end; name, ok := nextEntry(ADR(d)); if ok then exit(2); end; closeDir(ADR(d)); diff --git a/lib/Sys.raz b/lib/Sys.raz index e7b4680..ae8bbd4 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -60,6 +60,7 @@ symlink*: BOOL; (* the path itself is a symbolic link *) ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) dev*: U64; (* the file system it is on; with ino, it names the file *) + links*: U64; (* how many names it has: 1 for a file no hard link shares *) end; (* What a directory says a name in it is, without asking the file: @@ -507,6 +508,7 @@ i.size := st[6]; (* st_size *) i.dev := U64(st[0]); (* st_dev *) i.ino := U64(st[1]); (* st_ino *) + i.links := U64(st[2]); (* st_nlink *) i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *) i.mode := st[3] & 1FFH; i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *) @@ -2064,7 +2066,7 @@ if (entryKind(ADR(d)) # isFile) and (entryKind(ADR(d)) # unknown) then exit(2); end; i, err := info("/tmp/raz-sys-dir/g"); j, err := info("/tmp/raz-sys-dir"); - if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) then exit(2); end; + if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) or (i.links # 1) then exit(2); end; name, ok := nextEntry(ADR(d)); if ok then exit(2); end; closeDir(ADR(d));