shithub: mc

ref: 8e1bb5d4fc204ce149ceb545aef7b783d73641bc
dir: /lib/fileutil/loopcheck+posixy.myr/

View raw version
use std
use sys

/* plan 9 can't have directory loops, by construction, so this is nops */
pkg fileutil =
	type loopcheck = std.htab((int64, int64), void)#

	const mkloopcheck	: (cwd : byte[:] -> loopcheck)
	const freeloopcheck	: (l : loopcheck -> void)
	const looped	: (l : loopcheck, p : byte[:] -> bool)
;;

const mkloopcheck = {cwd
	var ht : std.htab((int64, int64), void)#
	var l

	ht = std.mkht()
	l = (ht : loopcheck)
	looped(l, cwd)
	-> l
}

const freeloopcheck = {l
	std.htfree((l : std.htab((int64, int64), void)#))
}

const looped = {l, p
	var ht, has

	ht = (l : std.htab((int64, int64), void)#)
	match fid(p)
	| `std.Err e:
		-> false
	| `std.Ok id:
		has = std.hthas(ht, id)
		std.htput(ht, id, void)
		-> has
	;;
}

const fid = {p
	var sb

	if sys.stat(p, &sb) != 0
		-> `std.Err void
	else
		-> `std.Ok ((sb.dev : int64), (sb.ino : int64))
	;;
}

impl std.hashable (int64, int64) =
	hash = {fid
		var dev, ino

		(dev, ino) = fid
		-> std.hash(dev) ^ std.hash(ino)
	}
;;

impl std.equatable (int64, int64) =
	eq = {a, b
		var adev, aino
		var bdev, bino

		(adev, aino) = a
		(bdev, bino) = b
		-> adev == bdev && aino == bino
	}
;;