shithub: lu9-p9

Download patch

ref: 8b4185716fbbb9b83eb2b2041037f389c3de9b7f
parent: a9a2e3aab41d483985e568343ffdd78d900cc6ab
author: kvik <kvik@a-b.xyz>
date: Sun Apr 18 17:47:17 EDT 2021

fs: replace p9.fd2path with file:path() method

--- a/fs.c
+++ b/fs.c
@@ -289,28 +289,28 @@
 }
 
 static int
-p9_remove(lua_State *L)
+p9_path(lua_State *L)
 {
-	const char *file;
+	int fd;
+	char *buf;
 	
-	file = luaL_checkstring(L, 1);
-	if(remove(file) == -1)
-		return error(L, "remove: %r");
-	lua_pushboolean(L, 1);
+	fd = filefd(L, 1);
+	buf = getbuffer(L, Iosize);
+	if(fd2path(fd, buf, Iosize) != 0)
+		return error(L, "fd2path: %r");
+	lua_pushstring(L, buf);
 	return 1;
 }
 
 static int
-p9_fd2path(lua_State *L)
+p9_remove(lua_State *L)
 {
-	lua_Integer fd;
-	char *buf;
+	const char *file;
 	
-	fd = luaL_checkinteger(L, 1);
-	buf = getbuffer(L, Iosize);
-	if(fd2path(fd, buf, Iosize) != 0)
-		return error(L, "fd2path: %r");
-	lua_pushstring(L, buf);
+	file = luaL_checkstring(L, 1);
+	if(remove(file) == -1)
+		return error(L, "remove: %r");
+	lua_pushboolean(L, 1);
 	return 1;
 }
 
--- a/p9.c
+++ b/p9.c
@@ -157,7 +157,6 @@
 	{"pipe", p9_pipe},
 	
 	{"remove", p9_remove},
-	{"fd2path", p9_fd2path},
 	
 	{"stat", p9_stat},
 	{"walk", p9_walk},
@@ -192,6 +191,7 @@
 		{"write", p9_write},
 		{"seek", p9_seek},
 		{"iounit", p9_iounit},
+		{"path", p9_path},
 		{nil, nil},
 	};
 	luaL_newmetatable(L, "p9-File");
--- a/test.lua
+++ b/test.lua
@@ -68,13 +68,20 @@
 assert(p9.file(fd):seek(0))
 p9.file(fd):close()
 
--- fd2path
+-- file:path()
 do
-	local fd = p9.create("/tmp/fd2path")
-	assert(p9.fd2path(fd.fd) == "/tmp/fd2path")
-	fd:close()
+	local f = p9.create("/tmp/fd2path")
+	assert(f:path() == "/tmp/fd2path")
+	f:close()
 end
 
+-- file:iounit()
+do
+	local f = assert(p9.open("/srv/slashmnt"))
+	assert(f:iounit() ~= 0)
+	f:close()
+end
+
 -- pipe
 do
 	local p₀, p₁ = assert(p9.pipe())
@@ -81,13 +88,6 @@
 	p₀:write("ABCD")
 	assert(p₁:read() == "ABCD")
 	p₀:close(); p₁:close()
-end
-
--- iounit
-do
-	local f = assert(p9.open("/srv/slashmnt"))
-	assert(f:iounit() != 0)
-	f:close()
 end
 
 -- Filesystem