migrate: own fs/file and path sources with host-authorization assertions
Move the official stdlib file and path modules to their unique source authority without changing public paths or host authorization. Carry the pre-migration product assertions, including the bounded read and cleanup semantics. Refs cflmy/marqdo#9, cflmy/marqdo-std-fs#1
This commit was merged in pull request #2.
This commit is contained in:
1 parent
3ef21c7de0
commit
63bceae587
10 files changed
+893
-6
No files matched your search
@@ -0,0 +1,237 @@
|
||||
---
|
||||
import:
|
||||
utf8: "../text/utf8.mq.md"
|
||||
---
|
||||
|
||||
# @exec file
|
||||
|
||||
## 读文本
|
||||
|
||||
`params(路径)`
|
||||
|
||||
以 UTF-8 读取一个有界的项目文件。文件在打开时整文件载入,上限 8 MiB;
|
||||
结果使用可恢复 Result 形状。
|
||||
|
||||
**流 = resource.open("file", 路径, ["read"])**
|
||||
|
||||
**内存 = memory.new(128, 128)**
|
||||
|
||||
**读取 = resource.read(流, 内存, 0, 8388608)**
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
`if not 读取.ok`
|
||||
|
||||
`return 读取`
|
||||
|
||||
`else`
|
||||
`if not 关闭.ok`
|
||||
|
||||
`return 关闭`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**文本 = utf8.解码(内存, 0, 读取.value.count)**
|
||||
|
||||
`if not 文本.ok`
|
||||
|
||||
`return 文本`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return {ok: true, value: 文本.value}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 写文本
|
||||
|
||||
`params(路径, 文本)`
|
||||
|
||||
把文本按 UTF-8 写入项目文件。路径必须已经通过 fs_write 授权;
|
||||
文件只在 resource.close 成功时创建或覆盖,写入缓冲上限 8 MiB。
|
||||
|
||||
**内存 = memory.new(128, 128)**
|
||||
|
||||
**长度 = utf8.编码(文本, 内存, 0)**
|
||||
|
||||
**流 = resource.open("file", 路径, ["write"])**
|
||||
|
||||
**写入 = resource.write(流, 内存, 0, 长度)**
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
`if not 写入.ok`
|
||||
|
||||
`return 写入`
|
||||
|
||||
`else`
|
||||
`if not 关闭.ok`
|
||||
|
||||
`return 关闭`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return {ok: true, value: {count: 写入.value.count}}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 复制文件
|
||||
|
||||
`params(源, 目标)`
|
||||
|
||||
按字节复制一个有界的项目文件。源读取和目标写入分别需要
|
||||
fs_read/fs_write 授权,结果值与 [写文本](#写文本) 相同。
|
||||
|
||||
**输入 = resource.open("file", 源, ["read"])**
|
||||
|
||||
**内存 = memory.new(128, 128)**
|
||||
|
||||
**读取 = resource.read(输入, 内存, 0, 8388608)**
|
||||
|
||||
**关闭输入 = resource.close(输入)**
|
||||
|
||||
`if not 读取.ok`
|
||||
|
||||
`return 读取`
|
||||
|
||||
`else`
|
||||
`if not 关闭输入.ok`
|
||||
|
||||
`return 关闭输入`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**输出 = resource.open("file", 目标, ["write"])**
|
||||
|
||||
**写入 = resource.write(输出, 内存, 0, 读取.value.count)**
|
||||
|
||||
**关闭输出 = resource.close(输出)**
|
||||
|
||||
`if not 写入.ok`
|
||||
|
||||
`return 写入`
|
||||
|
||||
`else`
|
||||
`if not 关闭输出.ok`
|
||||
|
||||
`return 关闭输出`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return {ok: true, value: {count: 写入.value.count}}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 存在
|
||||
|
||||
`params(路径)`
|
||||
|
||||
`return resource.request("fs.exists", [路径])`
|
||||
|
||||
## 元数据
|
||||
|
||||
`params(路径)`
|
||||
|
||||
`return resource.request("fs.metadata", [路径])`
|
||||
|
||||
## 是文件
|
||||
|
||||
`params(路径)`
|
||||
|
||||
**元数据 = resource.request("fs.metadata", [路径])**
|
||||
|
||||
`if not 元数据.ok`
|
||||
|
||||
`return 元数据`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return {ok: true, value: 元数据.value.kind == "file"}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
## 是目录
|
||||
|
||||
`params(路径)`
|
||||
|
||||
**元数据 = resource.request("fs.metadata", [路径])**
|
||||
|
||||
`if not 元数据.ok`
|
||||
|
||||
`return 元数据`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return {ok: true, value: 元数据.value.kind == "directory"}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
## 列出目录
|
||||
|
||||
`params(路径)`
|
||||
|
||||
`return resource.request("fs.list", [路径])`
|
||||
|
||||
## 创建目录
|
||||
|
||||
`params(路径)`
|
||||
|
||||
`return resource.request("fs.mkdir", [路径])`
|
||||
|
||||
## 删除
|
||||
|
||||
`params(路径)`
|
||||
|
||||
删除一个文件或空目录。非空目录返回 fs.not_empty,不递归删除。
|
||||
|
||||
`return resource.request("fs.remove", [路径])`
|
||||
|
||||
## 重命名
|
||||
|
||||
`params(源, 目标)`
|
||||
|
||||
目标已存在时返回 fs.exists,不覆盖。
|
||||
|
||||
`return resource.request("fs.rename", [源, 目标])`
|
||||
|
||||
## 追加文本
|
||||
|
||||
`params(路径, 文本)`
|
||||
|
||||
把文本追加到项目文件;文件不存在时创建。仍受 8 MiB 单次读取上限和
|
||||
fs_write 授权约束。
|
||||
|
||||
`return resource.request("fs.append", [路径, 文本])`
|
||||
|
||||
## 读取片段
|
||||
|
||||
`params(路径, 偏移, 长度)`
|
||||
|
||||
从偏移处读取最多 长度 字节,按 UTF-8 解码后返回文本。偏移/长度必须
|
||||
是非负整数,长度上限 8 MiB。
|
||||
|
||||
`return resource.request("fs.read_at", [路径, 偏移, 长度])`
|
||||
|
||||
## 遍历
|
||||
|
||||
`params(路径)`
|
||||
|
||||
递归列出目录下的普通文件,返回相对项目根的路径列表。跳过符号链接,
|
||||
深度上限 64,条目上限 4096。
|
||||
|
||||
`return resource.request("fs.walk", [路径])`
|
||||
@@ -0,0 +1,211 @@
|
||||
|
||||
# @exec path
|
||||
## 是否绝对
|
||||
|
||||
`params(路径)`
|
||||
|
||||
**点 = text.codepoints(路径)**
|
||||
|
||||
`if 长度(点) == 0`
|
||||
|
||||
`return false`
|
||||
|
||||
`else`
|
||||
`if 点[0] == 47`
|
||||
|
||||
`return true`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`return false`
|
||||
|
||||
## 分割
|
||||
|
||||
`params(文本, 分隔码点)`
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
**结果 = []**
|
||||
|
||||
**当前 = ""**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 长度(点)`
|
||||
|
||||
`if 点[下标] == 分隔码点`
|
||||
|
||||
**结果 = list.append(结果, 当前)**
|
||||
|
||||
**当前 = ""**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**当前 = 当前 + text.from_codepoint(点[下标])**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`return list.append(结果, 当前)`
|
||||
|
||||
## 连接
|
||||
|
||||
`params(片段列表)`
|
||||
|
||||
空片段被跳过,片段之间只插入一个 /。
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 长度(片段列表)`
|
||||
|
||||
**片段 = 文本(片段列表[下标])**
|
||||
|
||||
`if 片段 != ""`
|
||||
|
||||
`if 结果 == ""`
|
||||
|
||||
**结果 = 片段**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**结果 = 结果 + "/" + 片段**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 文件名
|
||||
|
||||
`params(路径)`
|
||||
|
||||
**片段 = 分割(路径, 47)**
|
||||
|
||||
`return 片段[长度(片段) - 1]`
|
||||
|
||||
## 目录名
|
||||
|
||||
`params(路径)`
|
||||
|
||||
**片段 = 分割(路径, 47)**
|
||||
|
||||
**结果 = []**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 长度(片段) - 1`
|
||||
|
||||
**结果 = list.append(结果, 片段[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`return 连接(结果)`
|
||||
|
||||
## 扩展名
|
||||
|
||||
`params(路径)`
|
||||
|
||||
**名字 = 文件名(路径)**
|
||||
|
||||
**点列表 = 分割(名字, 46)**
|
||||
|
||||
`if 长度(点列表) <= 1`
|
||||
|
||||
`return ""`
|
||||
|
||||
`end`
|
||||
`return 点列表[长度(点列表) - 1]`
|
||||
|
||||
## 去尾
|
||||
|
||||
`params(列表)`
|
||||
|
||||
**结果 = []**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 长度(列表) - 1`
|
||||
|
||||
**结果 = list.append(结果, 列表[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 规范
|
||||
|
||||
`params(路径)`
|
||||
|
||||
词法规范化:折叠 .、空片段和重复 /,用 .. 消去前一片段;
|
||||
绝对路径保留前导 /。不访问文件系统。
|
||||
|
||||
**片段 = 分割(路径, 47)**
|
||||
|
||||
**绝对 = 是否绝对(路径)**
|
||||
|
||||
**栈 = []**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 长度(片段)`
|
||||
|
||||
**段 = 片段[下标]**
|
||||
|
||||
`if 段 == "" or 段 == "."`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
`else`
|
||||
`if 段 == ".."`
|
||||
|
||||
`if 长度(栈) > 0 and 栈[长度(栈) - 1] != ".."`
|
||||
|
||||
**栈 = 去尾(栈)**
|
||||
|
||||
`else`
|
||||
`if not 绝对`
|
||||
|
||||
**栈 = list.append(栈, 段)**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略绝对根 = 0**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**栈 = list.append(栈, 段)**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`if 绝对`
|
||||
|
||||
`return "/" + 连接(栈)`
|
||||
|
||||
`end`
|
||||
`return 连接(栈)`
|
||||
Reference in new issue
Block a user