raz / commit 261c3a07

commit
261c3a0720f59086266ad55f251784af0e8c5456
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
aa7f5dff
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.

patch · log

4 files changed, +10 -28

PROPOSALS.md modified +0 -25

@@ -649,28 +649,3 @@
649649 Met in rrem's `RremUcd`, whose tables `tools/Ucd.raz` writes: before
650650 the tool's first run, each table needs a placeholder of one element,
651651 which the code reading it must then not take for a real one.
652-
653-## A file's link count
654-
655-`Sys.Info` names a file (`dev`, `ino`) but doesn't say how many names
656-it has. A program that lets a writer loose in a directory of its own
657-can't tell a file made there from a hard link to one somewhere else:
658-both are regular files inside the directory, and writing to either
659-writes to the one file.
660-
661-The proposal, in `Info`, beside `ino`:
662-
663-```
664-links*: U64; (* how many names the file has: 1 for a file no hard link shares *)
665-```
666-
667-filled in `fromStat` from `st_nlink`, which is `st[2]` on
668-x86_64 Linux, as `st_ino` is `st[1]`. A field more in a record breaks
669-only a program that builds an `Info` by a literal of all its fields,
670-so it could come with a thaw.
671-
672-Met in arno: a conversation's scratch directory is the model's own,
673-and its `edit`, `move` and `remove` there go ahead without asking. The
674-Pascal arno refused that freedom for a file with a second link, which
675-shares its contents with a name outside, perhaps one of the human's;
676-the raz arno can't, until `Info` says how many links a file has.

doc/raz-lib.md (history) modified +4 -1

@@ -179,6 +179,7 @@
179179 symlink*: BOOL; (* the path itself is a symbolic link *)
180180 ino*: U64; (* its inode, which a rename keeps and a new file doesn't *)
181181 dev*: U64; (* the file system it is on; with ino, it names the file *)
182+ links*: U64; (* how many names it has: 1 for a file no hard link shares *)
182183 end;
183184 Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *)
184185 Dir* = record ... end; (* an open directory; its fields are the target's *)
@@ -357,7 +358,9 @@
357358 files have none. `Info.dev` is the file system the file is on: two
358359 files are one only if both `dev` and `ino` agree, which is how a
359360 program following links finds that one has led it back into a
360-directory it is already in. `Info.mode` holds the nine
361+directory it is already in. `Info.links` is how many names the file
362+has: more than 1 if a hard link shares it, so that writing to it
363+writes to a file named elsewhere too; 0 on a target that doesn't say. `Info.mode` holds the nine
361364 bits of `rwxrwxrwx`, and `chmod` sets them, 1EDH (755) for a program,
362365 1A4H (644) for data.
363366

lib/Sys.linux-x86_64.raz (history) modified +3 -1

@@ -41,6 +41,7 @@
4141 symlink*: BOOL; (* the path itself is a symbolic link *)
4242 ino*: U64; (* its inode, which a rename keeps and a new file doesn't *)
4343 dev*: U64; (* the file system it is on; with ino, it names the file *)
44+ links*: U64; (* how many names it has: 1 for a file no hard link shares *)
4445 end;
4546
4647 (* What a directory says a name in it is, without asking the file:
@@ -440,6 +441,7 @@
440441 i.size := st[6]; (* st_size *)
441442 i.dev := U64(st[0]); (* st_dev *)
442443 i.ino := U64(st[1]); (* st_ino *)
444+ i.links := U64(st[2]); (* st_nlink *)
443445 i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *)
444446 i.mode := st[3] & 1FFH;
445447 i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *)
@@ -2243,7 +2245,7 @@
22432245 if (entryKind(ADR(d)) # isFile) and (entryKind(ADR(d)) # unknown) then exit(2); end;
22442246 i, err := info("/tmp/raz-sys-dir/g");
22452247 j, err := info("/tmp/raz-sys-dir");
2246- if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) then exit(2); end;
2248+ if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) or (i.links # 1) then exit(2); end;
22472249 name, ok := nextEntry(ADR(d));
22482250 if ok then exit(2); end;
22492251 closeDir(ADR(d));

lib/Sys.raz (history) modified +3 -1

@@ -60,6 +60,7 @@
6060 symlink*: BOOL; (* the path itself is a symbolic link *)
6161 ino*: U64; (* its inode, which a rename keeps and a new file doesn't *)
6262 dev*: U64; (* the file system it is on; with ino, it names the file *)
63+ links*: U64; (* how many names it has: 1 for a file no hard link shares *)
6364 end;
6465
6566 (* What a directory says a name in it is, without asking the file:
@@ -507,6 +508,7 @@
507508 i.size := st[6]; (* st_size *)
508509 i.dev := U64(st[0]); (* st_dev *)
509510 i.ino := U64(st[1]); (* st_ino *)
511+ i.links := U64(st[2]); (* st_nlink *)
510512 i.mtime := st[11] * 1000000000 + st[12]; (* st_mtim *)
511513 i.mode := st[3] & 1FFH;
512514 i.dir := st[3] & 0F000H = 4000H; (* S_IFDIR *)
@@ -2064,7 +2066,7 @@
20642066 if (entryKind(ADR(d)) # isFile) and (entryKind(ADR(d)) # unknown) then exit(2); end;
20652067 i, err := info("/tmp/raz-sys-dir/g");
20662068 j, err := info("/tmp/raz-sys-dir");
2067- if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) then exit(2); end;
2069+ if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) or (i.links # 1) then exit(2); end;
20682070 name, ok := nextEntry(ADR(d));
20692071 if ok then exit(2); end;
20702072 closeDir(ADR(d));