feat(proto/server)!: 升级至协议 v2(统一错误模型);全面替换 Request/Response 并移除 ret

- Breaking change: 协议不向后兼容,旧客户端需同步升级
- Proto: 新增 FsOp/ErrorCause/FsError/RpcStatus;为所有 FS 接口定义 <Op>Request/<Op>Response;删除历史 ret 字段
- Server: 所有 RPC 返回统一的 RpcStatus;成功 ok=true,失败填充 FsError(operation/paths/sys_msg 等)
  - Open/Read/Write 对齐新字段(fi/data/written);Readdir/Opendir/Releasedir 等返回类型调整
  - Rename 传回 from/to;OpendirRequest 不再输入 fi,服务端生成并回传
- Docs: 新增 docs/protocol_v2.md;README 标注 v2 破坏性升级与用法
- Build: 主要面向 Windows(winapi)。Linux 环境类型检查可能失败

后续:完善 errno 抽取与 context 填充;可选引入流式 read/write 以优化大文件传输。
This commit is contained in:
2025-09-23 21:25:06 +08:00
parent 4fbaf2410a
commit d2b98b4020
4 changed files with 632 additions and 736 deletions

View File

@ -1,4 +1,13 @@
# wls_vfs
windows linux文件共享使用fuse + grpc + protobuffer
语言rust
Windows/Linux 文件共享服务端gRPC + Protobuf)。
本仓库已升级至协议 v2统一错误模型
- 所有 RPC 使用显式的 `Request`/`Response` 类型;
- 响应统一携带 `RpcStatus { ok, error }`
- 删除历史 `ret` 字段;失败信息通过 `FsError` 完整承载(`code`/`sys_msg`/`operation`/`paths`/`context` 等)。
协议详情见:`proto/lws.proto``docs/protocol_v2.md`
注意:本项目主要在 Windows 上构建运行(依赖 `winapi`)。在非 Windows 平台构建仅用于类型检查,可能失败。

14
docs/protocol_v2.md Normal file
View File

@ -0,0 +1,14 @@
# LWS 协议 v2统一错误模型
- RpcStatus { ok, error }
- FsError { code, message, sys_msg, grpc_code, grpc_message, operation(FsOp), paths[], context{...}, causes[], server, timestamp_ms, retriable }
成功ok = true失败ok = false 且 error.code 为正数 POSIX errno。
常用 context keys: flags, offset, size, uid, gid, mode, fh, mask, xattr_name, xattr_size。
FsOp 与 RPC 映射:与客户端 docs 相同(参见 proto/lws.proto 中的 service 定义)。
服务端返回建议:
- 失败时设置 error.code 与 sys_msg并补充 operation/paths/context。
- 传输层错误由客户端据 gRPC `Status` 统一映射。

View File

@ -16,43 +16,62 @@ syntax = "proto3";
package lws_vfs;
service LwsVfs {
// Sends a greeting
rpc SayHello(HelloRequest) returns (HelloReply) {}
rpc GetConfig(get_config) returns (get_config) {}
rpc fgetattr(getattr) returns (getattr) {}
rpc fsetxattr(setxattr) returns (setxattr) {}
rpc faccess(access) returns (access) {}
rpc freaddir(readdir) returns (readdir) {}
rpc fread(read) returns (read) {}
rpc fopen(open) returns (open) {}
rpc fwrite(write) returns (write) {}
rpc fgetxattr(getxattr) returns (getxattr) {}
rpc ftruncate(truncate) returns (truncate) {}
rpc futimens(utimens) returns (utimens) {}
rpc fchown(chown) returns (chown) {}
rpc frelease(release) returns (release) {}
rpc fmkdir(mkdir) returns (mkdir) {}
rpc frmdir(rmdir) returns (rmdir) {}
rpc fflush(flush) returns (flush) {}
rpc fopendir(opendir) returns (opendir) {}
rpc freleasedir(releasedir) returns (releasedir) {}
rpc fcreate(create) returns (create) {}
rpc funlink(unlink) returns (unlink) {}
rpc frename(rename) returns (rename) {}
// Filesystem operation types for diagnostics and observability.
enum FsOp {
FSOP_UNKNOWN = 0;
FSOP_GETATTR = 1;
FSOP_SETXATTR = 2;
FSOP_ACCESS = 3;
FSOP_READDIR = 4;
FSOP_READ = 5;
FSOP_OPEN = 6;
FSOP_WRITE = 7;
FSOP_GETXATTR = 8;
FSOP_TRUNCATE = 9;
FSOP_UTIMENS = 10;
FSOP_CHOWN = 11;
FSOP_RELEASE = 12;
FSOP_MKDIR = 13;
FSOP_RMDIR = 14;
FSOP_FLUSH = 15;
FSOP_OPENDIR = 16;
FSOP_RELEASEDIR = 17;
FSOP_CREATE = 18;
FSOP_UNLINK = 19;
FSOP_RENAME = 20;
FSOP_HELLO = 21;
FSOP_GET_CONFIG = 22;
}
// The request message containing the user's name.
message HelloRequest { string name = 1; }
// The response message containing the greetings
message HelloReply { string message = 1; }
message get_config {
string config = 1;
// Optional chained cause for nested errors.
message ErrorCause {
int32 code = 1; // POSIX errno if available
string message = 2; // human-readable message
string type = 3; // error type/category from server implementation
}
// Rich error payload carried by all responses.
message FsError {
int32 code = 1; // POSIX errno (>0)
string message = 2; // application error message
string sys_msg = 3; // strerror(code) or OS message
uint32 grpc_code = 4; // gRPC status code (if applicable)
string grpc_message = 5; // gRPC status message
FsOp operation = 6; // which fs op failed
repeated string paths = 7; // involved paths (e.g. [from, to])
map<string, string> context = 8; // flags/size/offset/uid/gid ...
repeated ErrorCause causes = 9; // nested causes
string server = 10; // server id/hostname
uint64 timestamp_ms = 11; // server time when error occurred
bool retriable = 12; // whether retry may succeed
}
message RpcStatus {
bool ok = 1; // true if the call succeeded
FsError error = 2; // populated when ok == false
}
// Common structs
message file_info {
uint32 flags = 1;
uint32 fh_old = 2;
@ -77,34 +96,9 @@ message fstat {
uint64 fst_blocks = 13;
}
// fuse api function message define:
message getattr {
string path = 1;
fstat stat = 2;
file_info fi = 3;
int32 ret = 15;
}
message setxattr {
string path = 1;
string name = 2;
bytes value = 3;
int64 size = 4;
uint32 flags = 5;
int32 ret = 15;
}
message getxattr {
string path = 1;
string name = 2;
bytes value = 3;
int64 size = 4;
int32 ret = 15;
}
message access {
string path = 1;
uint32 mask = 2;
int32 ret = 15;
message timespec {
int32 tv_sec = 1;
int64 tv_nsec = 2;
}
message direntry {
@ -112,112 +106,100 @@ message direntry {
uint32 kind = 2;
}
message readdir {
string path = 1;
repeated direntry dirs = 2;
uint32 offset = 3;
file_info fi = 4;
int32 ret = 15;
// Service and RPCs
service LwsVfs {
rpc SayHello(HelloRequest) returns (HelloReply) {}
rpc GetConfig(GetConfigRequest) returns (GetConfigResponse) {}
rpc fgetattr(GetattrRequest) returns (GetattrResponse) {}
rpc fsetxattr(SetxattrRequest) returns (EmptyResponse) {}
rpc faccess(AccessRequest) returns (EmptyResponse) {}
rpc freaddir(ReaddirRequest) returns (ReaddirResponse) {}
rpc fread(ReadRequest) returns (ReadResponse) {}
rpc fopen(OpenRequest) returns (OpenResponse) {}
rpc fwrite(WriteRequest) returns (WriteResponse) {}
rpc fgetxattr(GetxattrRequest) returns (GetxattrResponse) {}
rpc ftruncate(TruncateRequest) returns (EmptyResponse) {}
rpc futimens(UtimensRequest) returns (EmptyResponse) {}
rpc fchown(ChownRequest) returns (EmptyResponse) {}
rpc frelease(ReleaseRequest) returns (EmptyResponse) {}
rpc fmkdir(MkdirRequest) returns (EmptyResponse) {}
rpc frmdir(RmdirRequest) returns (EmptyResponse) {}
rpc fflush(FlushRequest) returns (EmptyResponse) {}
rpc fopendir(OpendirRequest) returns (OpendirResponse) {}
rpc freleasedir(ReleasedirRequest) returns (EmptyResponse) {}
rpc fcreate(CreateRequest) returns (CreateResponse) {}
rpc funlink(UnlinkRequest) returns (EmptyResponse) {}
rpc frename(RenameRequest) returns (EmptyResponse) {}
}
message open {
string path = 1;
file_info fi = 2;
int32 ret = 15;
}
// Hello
message HelloRequest { string name = 1; }
message HelloReply { string message = 1; RpcStatus status = 2; }
message read {
string path = 1;
bytes buff = 2;
int64 size = 3;
uint64 offset = 4;
file_info fi = 5;
int32 ret = 15;
}
// Config
message GetConfigRequest {}
message GetConfigResponse { string config = 1; RpcStatus status = 2; }
message write {
string path = 1;
bytes buff = 2;
uint64 size = 3;
uint64 offset = 4;
file_info fi = 5;
int32 ret = 15;
}
// getattr
message GetattrRequest { string path = 1; file_info fi = 2; }
message GetattrResponse { fstat stat = 1; RpcStatus status = 2; }
message truncate {
string path = 1;
int64 size = 3;
int32 ret = 15;
}
// xattr
message SetxattrRequest { string path = 1; string name = 2; bytes value = 3; int64 size = 4; uint32 flags = 5; }
message GetxattrRequest { string path = 1; string name = 2; int64 size = 3; }
message GetxattrResponse { bytes value = 1; RpcStatus status = 2; }
message timespec {
int32 tv_sec = 1;
int64 tv_nsec = 2;
}
message utimens {
string path = 1;
repeated timespec ts = 2; // const struct timespec ts[2]
int32 ret = 15;
}
// access
message AccessRequest { string path = 1; uint32 mask = 2; }
message chown {
string path = 1;
int32 uid = 2;
int32 gid = 3;
int32 ret = 15;
}
// readdir
message ReaddirRequest { string path = 1; uint32 offset = 2; file_info fi = 3; }
message ReaddirResponse { repeated direntry dirs = 1; RpcStatus status = 2; }
message release {
string path = 1;
file_info fi = 2;
uint32 flush = 3;
int32 ret = 15;
}
// open
message OpenRequest { string path = 1; file_info fi = 2; }
message OpenResponse { file_info fi = 1; RpcStatus status = 2; }
message mkdir {
string path = 1;
file_info fi = 2;
uint32 mode = 3;
int32 ret = 15;
}
// read
message ReadRequest { string path = 1; uint64 offset = 2; int64 size = 3; file_info fi = 4; }
message ReadResponse { bytes data = 1; RpcStatus status = 2; }
message rmdir {
string path = 1;
int32 ret = 15;
}
// write
message WriteRequest { string path = 1; uint64 offset = 2; bytes data = 3; file_info fi = 4; }
message WriteResponse { uint64 written = 1; RpcStatus status = 2; }
message flush {
string path = 1;
file_info fi = 2;
int32 ret = 15;
}
// truncate
message TruncateRequest { string path = 1; int64 size = 2; }
message opendir {
string path = 1;
file_info fi = 2;
int32 ret = 15;
}
// utimens
message UtimensRequest { string path = 1; repeated timespec ts = 2; }
message releasedir {
string path = 1;
file_info fi = 2;
int32 ret = 15;
}
// chown
message ChownRequest { string path = 1; int32 uid = 2; int32 gid = 3; }
message create {
string path = 1;
uint32 mode = 2;
file_info fi = 3;
int32 ret = 15;
}
// release/flush
message ReleaseRequest { string path = 1; file_info fi = 2; uint32 flush = 3; }
message FlushRequest { string path = 1; file_info fi = 2; }
message unlink {
string path = 1;
int32 ret = 15;
}
// mkdir/rmdir
message MkdirRequest { string path = 1; uint32 mode = 2; }
message RmdirRequest { string path = 1; }
message rename {
string path = 1;
string new = 2;
int32 ret = 15;
}
// opendir/releasedir
message OpendirRequest { string path = 1; }
message OpendirResponse { file_info fi = 1; RpcStatus status = 2; }
message ReleasedirRequest { string path = 1; file_info fi = 2; }
// create
message CreateRequest { string path = 1; uint32 mode = 2; }
message CreateResponse { file_info fi = 1; RpcStatus status = 2; }
// unlink
message UnlinkRequest { string path = 1; }
// rename
message RenameRequest { string path = 1; string new = 2; }
// Empty response with status only
message EmptyResponse { RpcStatus status = 1; }

1051
src/lib.rs

File diff suppressed because it is too large Load Diff