Skip to content

PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.

feat(core): count received files renamed for Windows

baiyuze066665ca4cbe
diff --git a/client-core/internal/pack/frames.go b/client-core/internal/pack/frames.go
index 9e4e87f..e4eaed2 100644
--- a/client-core/internal/pack/frames.go
+++ b/client-core/internal/pack/frames.go
@@ -26,6 +26,7 @@ import (
"strings"
"github.com/klauspost/compress/zstd"
+ "golang.org/x/text/unicode/norm"
"google.golang.org/protobuf/proto"
pb "github.com/baiyuze/pasteferry/proto/gen/pasteferry/v1"
@@ -399,6 +400,12 @@ func (n *Names) Restore(name, mapped string, isDir bool) {
}
}
+// Renamed 判断一个条目落盘时名字是不是改过(Windows 上替换了不允许的字符,或者与只差大小写的同名条目错开)。
+// Mac 上的分解形式统一成组合形式看不出区别,不算改名。
+func Renamed(name, local string) bool {
+ return path.Base(local) != norm.NFC.String(path.Base(strings.TrimSuffix(name, "/")))
+}
+
// Skip 表示这个条目在本机不该落盘:Windows 上的 .DS_Store 与 AppleDouble 文件。
func Skip(name string) bool { return windowsNames && skipOnWindows(path.Base(name)) }
diff --git a/client-core/internal/pack/frames_test.go b/client-core/internal/pack/frames_test.go
index eaba68b..e8c1ec0 100644
--- a/client-core/internal/pack/frames_test.go
+++ b/client-core/internal/pack/frames_test.go
@@ -135,3 +135,29 @@ func TestManifestDedupesTopNames(t *testing.T) {
t.Fatalf("同名的顶层项目应当改名:%v %v", tops, err)
}
}
+
+func TestRenamedOnlyWhenTheNameReallyChanged(t *testing.T) {
+ old := windowsNames
+ windowsNames = true
+ defer func() { windowsNames = old }()
+ n := NewNames()
+ cases := []struct {
+ name string
+ renamed bool
+ }{
+ {"资料/a:b?c.txt", true}, // Windows 不允许的字符换成全角
+ {"资料/普通文件.txt", false}, // 原样
+ {"资料/\u304b\u3099.txt", false}, // Mac 上的分解形式(か + 浊点)统一成组合形式,看不出区别,不算
+ {"资料/Readme.md", false},
+ {"资料/README.md", true}, // 与上一个只差大小写,Windows 上改成「README (2).md」
+ }
+ for _, c := range cases {
+ local, err := n.Rel(c.name, false)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := Renamed(c.name, local); got != c.renamed {
+ t.Errorf("%q → %q:改名 %v,应为 %v", c.name, local, got, c.renamed)
+ }
+ }
+}
diff --git a/client-core/internal/send/incoming.go b/client-core/internal/send/incoming.go
index f685e56..93f2841 100644
--- a/client-core/internal/send/incoming.go
+++ b/client-core/internal/send/incoming.go
@@ -704,6 +704,7 @@ func noLinkParents(base, dir string, checked map[string]bool) error {
// finalize 把收完的顶层项目从临时目录移到保存位置(重名就改名),回告对方。
func (e *Engine) finalize(in *incoming, t store.Transfer) error {
var tops []store.TransferEntry
+ var renamed int64
for from := int64(0); ; {
batch, err := e.opts.Store.TransferEntries(in.id, from, 1000)
if err != nil {
@@ -719,6 +720,9 @@ func (e *Engine) finalize(in *incoming, t store.Transfer) error {
if !strings.Contains(en.Path, "/") && en.LocalPath != "" {
tops = append(tops, en)
}
+ if en.LocalPath != "" && pack.Renamed(en.Path, en.LocalPath) {
+ renamed++
+ }
}
from = batch[len(batch)-1].Index + 1
}
@@ -743,7 +747,7 @@ func (e *Engine) finalize(in *incoming, t store.Transfer) error {
_ = os.Remove(filepath.Join(in.dest, partialDir)) // 空了才删得掉
t = e.withProgress(t)
- t.Items, t.Paths, t.DoneBytes = items, finals, t.TotalBytes
+ t.Items, t.Paths, t.DoneBytes, t.Renamed = items, finals, t.TotalBytes, renamed
e.finish(t, store.TransferDone, "")
_ = e.opts.Send(in.device, &pb.PeerMessage{Payload: &pb.PeerMessage_SendResult{SendResult: &pb.SendResult{
TransferId: t.ID, Attempt: in.attempt, Ok: true}}})
diff --git a/client-core/internal/send/progress.go b/client-core/internal/send/progress.go
index fa8d7f1..4705ec8 100644
--- a/client-core/internal/send/progress.go
+++ b/client-core/internal/send/progress.go
@@ -147,7 +147,7 @@ func (e *Engine) toProto(t store.Transfer) *pb.Transfer {
Id: t.ID, Direction: pb.TransferDirection(t.Direction), DeviceId: t.DeviceID, DeviceName: t.DeviceName,
Status: t.Status, Paths: t.Paths, TotalBytes: t.TotalBytes, DoneBytes: t.DoneBytes,
FileCount: t.FileCount, Error: t.Error, ShortfallBytes: t.Shortfall, Relayed: t.Relayed,
- Seen: t.Seen, CreatedAtUnix: t.CreatedAt.Unix(),
+ Seen: t.Seen, CreatedAtUnix: t.CreatedAt.Unix(), RenamedFiles: t.Renamed,
}
if !t.FinishedAt.IsZero() {
p.FinishedAtUnix = t.FinishedAt.Unix()
diff --git a/client-core/internal/store/transfer.go b/client-core/internal/store/transfer.go
index a5ff8cd..a16cb3e 100644
--- a/client-core/internal/store/transfer.go
+++ b/client-core/internal/store/transfer.go
@@ -60,6 +60,7 @@ type Transfer struct {
Relayed bool
AllowRelay bool // 发出的:用户已同意经官方服务中转
Attempt int // 发出的:已经发起过几次
+ Renamed int64 // 收到的:名字在本机改过的文件数
CreatedAt time.Time
UpdatedAt time.Time
FinishedAt time.Time
@@ -124,7 +125,8 @@ CREATE TABLE IF NOT EXISTS transfer_entries (
if err != nil {
return fmt.Errorf("建隔空传送表: %w", err)
}
- return nil
+ // 测试版建的表没有这一列
+ return s.addColumnIfMissing("transfers", "renamed", "INTEGER NOT NULL DEFAULT 0")
}
// PutTransfer 新建或整体更新一条传输记录。
@@ -150,18 +152,18 @@ func (s *Store) PutTransfer(t Transfer) error {
_, err = s.db.Exec(`
INSERT INTO transfers (id, direction, device_id, device_name, status, items, paths,
total_bytes, done_bytes, file_count, dest_dir, error, shortfall,
- seen, relayed, allow_relay, attempt, created_at, updated_at, finished_at)
-VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+ seen, relayed, allow_relay, attempt, created_at, updated_at, finished_at, renamed)
+VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
ON CONFLICT(id) DO UPDATE SET
device_name=excluded.device_name, status=excluded.status, items=excluded.items,
paths=excluded.paths, total_bytes=excluded.total_bytes, done_bytes=excluded.done_bytes,
file_count=excluded.file_count, dest_dir=excluded.dest_dir, error=excluded.error,
shortfall=excluded.shortfall, seen=excluded.seen, relayed=excluded.relayed,
allow_relay=excluded.allow_relay, attempt=excluded.attempt,
- updated_at=excluded.updated_at, finished_at=excluded.finished_at`,
+ updated_at=excluded.updated_at, finished_at=excluded.finished_at, renamed=excluded.renamed`,
t.ID, t.Direction, t.DeviceID, t.DeviceName, t.Status, string(items), string(paths),
t.TotalBytes, t.DoneBytes, t.FileCount, t.DestDir, t.Error, t.Shortfall,
- t.Seen, t.Relayed, t.AllowRelay, t.Attempt, t.CreatedAt.Unix(), t.UpdatedAt.Unix(), finished)
+ t.Seen, t.Relayed, t.AllowRelay, t.Attempt, t.CreatedAt.Unix(), t.UpdatedAt.Unix(), finished, t.Renamed)
if err != nil {
return fmt.Errorf("写入传输记录: %w", err)
}
@@ -177,7 +179,7 @@ func nonNilItems(items []Item) []Item {
const transferColumns = `id, direction, device_id, device_name, status, items, paths,
total_bytes, done_bytes, file_count, dest_dir, error, shortfall,
- seen, relayed, allow_relay, attempt, created_at, updated_at, finished_at`
+ seen, relayed, allow_relay, attempt, created_at, updated_at, finished_at, renamed`
func (s *Store) GetTransfer(id string) (Transfer, error) {
list, err := s.queryTransfers(`SELECT `+transferColumns+` FROM transfers WHERE id=?`, id)
@@ -229,7 +231,7 @@ func (s *Store) queryTransfers(q string, args ...any) ([]Transfer, error) {
var created, updated, finished int64
if err := rows.Scan(&t.ID, &t.Direction, &t.DeviceID, &t.DeviceName, &t.Status, &items, &paths,
&t.TotalBytes, &t.DoneBytes, &t.FileCount, &t.DestDir, &t.Error, &t.Shortfall,
- &t.Seen, &t.Relayed, &t.AllowRelay, &t.Attempt, &created, &updated, &finished); err != nil {
+ &t.Seen, &t.Relayed, &t.AllowRelay, &t.Attempt, &created, &updated, &finished, &t.Renamed); err != nil {
return nil, err
}
_ = json.Unmarshal([]byte(items), &t.Items)
diff --git a/design/send-files.md b/design/send-files.md
index 50c5955..0db6cb5 100644
--- a/design/send-files.md
+++ b/design/send-files.md
@@ -539,7 +539,11 @@ StreamEnd{}
- **临时目录**:`<保存位置>/.pasteferry-partial/<批次 ID>/`。和保存位置在同一块盘上,收完用重命名移过去,大文件也是瞬间完成,不会多占一份空间。Windows 上给这个目录设隐藏属性。
- **移到保存位置**:一批里的顶层项目各自移过去。
- 重名时改名:`报告.pdf` → `报告 (2).pdf`,`照片` → `照片 (2)`。和访达、资源管理器的习惯一致。
- - 文件名在本机不合法时(比如 Mac 的文件名里有 `:` 发到 Windows),沿用 `pack/names.go` 的替换规则。
+ - 文件名原样保留,只有接收方是 Windows、名字里有 Windows 不允许的字符时才改(2026-10-22 与用户确认):
+ - `: * ? " < > | \` 换成相近的全角字符(`a:b?c.txt` → `a:b?c.txt`),控制字符换成 `_`,结尾的点和空格后面补 `_`,设备保留名(`CON`、`NUL` 等)前面加 `_`;只差大小写的同名文件加 `(2)`。沿用 `pack/names.go`,和复制同步一致。
+ - 只改文件名,文件内容一个字节都不动。Mac、Linux 上收到时名字原样落盘。
+ - 收到的那一行写明「N 个文件名含 Windows 不支持的字符,已换成相近的全角字符」(`Transfer.renamed_files`)。
+ - Windows 上的 `.DS_Store`、`._` 开头的 AppleDouble 文件不落盘,也和复制同步一致。
- **空间检查**:接受之前看保存位置所在磁盘的剩余空间,要大于「总大小 + 200 MB」,否则回绝 `no_space` 并带上差额。续传时只算剩下的部分。
- **单文件上限**:保存位置是 FAT32 时,大于 4 GB 的文件放不下,回绝 `file_too_large`。
- **传输中保存位置没了**(外置盘拔了):停止,回 `SendResult{dest_unavailable}`,本机通知用户。
diff --git a/proto/gen/pasteferry/v1/daemon.pb.go b/proto/gen/pasteferry/v1/daemon.pb.go
index 27ca958..4074694 100644
--- a/proto/gen/pasteferry/v1/daemon.pb.go
+++ b/proto/gen/pasteferry/v1/daemon.pb.go
@@ -4341,7 +4341,10 @@ type Transfer struct {
CurrentFile string `protobuf:"bytes,19,opt,name=current_file,json=currentFile,proto3" json:"current_file,omitempty"` // 正在传的文件,相对路径;最多每 0.5 秒换一次
CurrentIndex int64 `protobuf:"varint,20,opt,name=current_index,json=currentIndex,proto3" json:"current_index,omitempty"` // 它是第几个文件,从 1 开始
// 收到的:顶层项目是否还在原处(用户移走或删除后为 false),与 paths 一一对应
- Present []bool `protobuf:"varint,21,rep,packed,name=present,proto3" json:"present,omitempty"`
+ Present []bool `protobuf:"varint,21,rep,packed,name=present,proto3" json:"present,omitempty"`
+ // 收到的:文件名里有本机文件系统不允许的字符、改过名的文件数。只有 Windows 上会有:
+ // : * ? " < > | \ 换成相近的全角字符,见 pack/names.go。文件内容不受影响
+ RenamedFiles int64 `protobuf:"varint,22,opt,name=renamed_files,json=renamedFiles,proto3" json:"renamed_files,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -4523,6 +4526,13 @@ func (x *Transfer) GetPresent() []bool {
return nil
}
+func (x *Transfer) GetRenamedFiles() int64 {
+ if x != nil {
+ return x.RenamedFiles
+ }
+ return 0
+}
+
type ListTransfersRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
Direction TransferDirection `protobuf:"varint,1,opt,name=direction,proto3,enum=pasteferry.v1.TransferDirection" json:"direction,omitempty"` // UNSPECIFIED 表示两类都要
@@ -5120,7 +5130,7 @@ const file_pasteferry_v1_daemon_proto_rawDesc = "" +
"\vallow_relay\x18\x03 \x01(\bR\n" +
"allowRelay\"\x1d\n" +
"\vTransferRef\x12\x0e\n" +
- "\x02id\x18\x01 \x01(\tR\x02id\"\xc4\x05\n" +
+ "\x02id\x18\x01 \x01(\tR\x02id\"\xe9\x05\n" +
"\bTransfer\x12\x0e\n" +
"\x02id\x18\x01 \x01(\tR\x02id\x12>\n" +
"\tdirection\x18\x02 \x01(\x0e2 .pasteferry.v1.TransferDirectionR\tdirection\x12\x1b\n" +
@@ -5147,7 +5157,8 @@ const file_pasteferry_v1_daemon_proto_rawDesc = "" +
"\rscanned_files\x18\x12 \x01(\x03R\fscannedFiles\x12!\n" +
"\fcurrent_file\x18\x13 \x01(\tR\vcurrentFile\x12#\n" +
"\rcurrent_index\x18\x14 \x01(\x03R\fcurrentIndex\x12\x18\n" +
- "\apresent\x18\x15 \x03(\bR\apresent\"\x8d\x01\n" +
+ "\apresent\x18\x15 \x03(\bR\apresent\x12#\n" +
+ "\rrenamed_files\x18\x16 \x01(\x03R\frenamedFiles\"\x8d\x01\n" +
"\x14ListTransfersRequest\x12>\n" +
"\tdirection\x18\x01 \x01(\x0e2 .pasteferry.v1.TransferDirectionR\tdirection\x12\x14\n" +
"\x05limit\x18\x02 \x01(\x05R\x05limit\x12\x1f\n" +
diff --git a/proto/pasteferry/v1/daemon.proto b/proto/pasteferry/v1/daemon.proto
index ca45890..116b86a 100644
--- a/proto/pasteferry/v1/daemon.proto
+++ b/proto/pasteferry/v1/daemon.proto
@@ -692,6 +692,9 @@ message Transfer {
int64 current_index = 20; // 它是第几个文件,从 1 开始
// 收到的:顶层项目是否还在原处(用户移走或删除后为 false),与 paths 一一对应
repeated bool present = 21;
+ // 收到的:文件名里有本机文件系统不允许的字符、改过名的文件数。只有 Windows 上会有:
+ // : * ? " < > | \ 换成相近的全角字符,见 pack/names.go。文件内容不受影响
+ int64 renamed_files = 22;
}
message ListTransfersRequest {
diff --git a/ui/lib/gen/pasteferry/v1/daemon.pb.dart b/ui/lib/gen/pasteferry/v1/daemon.pb.dart
index cd3b06c..cb47d03 100644
--- a/ui/lib/gen/pasteferry/v1/daemon.pb.dart
+++ b/ui/lib/gen/pasteferry/v1/daemon.pb.dart
@@ -5246,6 +5246,7 @@ class Transfer extends $pb.GeneratedMessage {
$core.String? currentFile,
$fixnum.Int64? currentIndex,
$core.Iterable<$core.bool>? present,
+ $fixnum.Int64? renamedFiles,
}) {
final result = Transfer._();
if (id != null) result.id = id;
@@ -5269,6 +5270,7 @@ class Transfer extends $pb.GeneratedMessage {
if (currentFile != null) result.currentFile = currentFile;
if (currentIndex != null) result.currentIndex = currentIndex;
if (present != null) result.present.addAll(present);
+ if (renamedFiles != null) result.renamedFiles = renamedFiles;
return result;
}
@@ -5308,6 +5310,7 @@ class Transfer extends $pb.GeneratedMessage {
..aOS(19, _omitFieldNames ? '' : 'currentFile')
..aInt64(20, _omitFieldNames ? '' : 'currentIndex')
..p<$core.bool>(21, _omitFieldNames ? '' : 'present', $pb.PbFieldType.KB)
+ ..aInt64(22, _omitFieldNames ? '' : 'renamedFiles')
..hasRequiredFields = false;
@$core.Deprecated('See https://github.com/google/protobuf.dart/issues/998.')
@@ -5507,6 +5510,17 @@ class Transfer extends $pb.GeneratedMessage {
/// 收到的:顶层项目是否还在原处(用户移走或删除后为 false),与 paths 一一对应
@$pb.TagNumber(21)
$pb.PbList<$core.bool> get present => $_getList(20);
+
+ /// 收到的:文件名里有本机文件系统不允许的字符、改过名的文件数。只有 Windows 上会有:
+ /// : * ? " < > | \ 换成相近的全角字符,见 pack/names.go。文件内容不受影响
+ @$pb.TagNumber(22)
+ $fixnum.Int64 get renamedFiles => $_getI64(21);
+ @$pb.TagNumber(22)
+ set renamedFiles($fixnum.Int64 value) => $_setInt64(21, value);
+ @$pb.TagNumber(22)
+ $core.bool hasRenamedFiles() => $_has(21);
+ @$pb.TagNumber(22)
+ void clearRenamedFiles() => $_clearField(22);
}
class ListTransfersRequest extends $pb.GeneratedMessage {
diff --git a/ui/lib/gen/pasteferry/v1/daemon.pbjson.dart b/ui/lib/gen/pasteferry/v1/daemon.pbjson.dart
index dd58f4f..a173f17 100644
--- a/ui/lib/gen/pasteferry/v1/daemon.pbjson.dart
+++ b/ui/lib/gen/pasteferry/v1/daemon.pbjson.dart
@@ -1480,6 +1480,7 @@ const Transfer$json = {
{'1': 'current_file', '3': 19, '4': 1, '5': 9, '10': 'currentFile'},
{'1': 'current_index', '3': 20, '4': 1, '5': 3, '10': 'currentIndex'},
{'1': 'present', '3': 21, '4': 3, '5': 8, '10': 'present'},
+ {'1': 'renamed_files', '3': 22, '4': 1, '5': 3, '10': 'renamedFiles'},
],
};
@@ -1497,7 +1498,8 @@ final $typed_data.Uint8List transferDescriptor = $convert.base64Decode(
'RlZF9hdF91bml4GBAgASgDUg1jcmVhdGVkQXRVbml4EigKEGZpbmlzaGVkX2F0X3VuaXgYESAB'
'KANSDmZpbmlzaGVkQXRVbml4EiMKDXNjYW5uZWRfZmlsZXMYEiABKANSDHNjYW5uZWRGaWxlcx'
'IhCgxjdXJyZW50X2ZpbGUYEyABKAlSC2N1cnJlbnRGaWxlEiMKDWN1cnJlbnRfaW5kZXgYFCAB'
- 'KANSDGN1cnJlbnRJbmRleBIYCgdwcmVzZW50GBUgAygIUgdwcmVzZW50');
+ 'KANSDGN1cnJlbnRJbmRleBIYCgdwcmVzZW50GBUgAygIUgdwcmVzZW50EiMKDXJlbmFtZWRfZm'
+ 'lsZXMYFiABKANSDHJlbmFtZWRGaWxlcw==');
@$core.Deprecated('Use listTransfersRequestDescriptor instead')
const ListTransfersRequest$json = {