zen-fs-config 0.6.2 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +58 -1
- package/dist/index.d.ts +58 -1
- package/dist/index.js +207 -48
- package/dist/index.mjs +207 -48
- package/dist/zen-fs-config.js +207 -48
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -952,6 +952,22 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
952
952
|
private readonly pollIntervalMs?;
|
|
953
953
|
/** Tombstone cache — avoids redundant reads within a single flush() cycle. */
|
|
954
954
|
private tombstoneCache;
|
|
955
|
+
/**
|
|
956
|
+
* 墓碑存在期间被排除在同步之外的路径。
|
|
957
|
+
*
|
|
958
|
+
* 远端副本可能仍残留被删路径(哪怕只剩 `.keep` / `.keep.mtime` 占位)。
|
|
959
|
+
* 若同步照常进行,这类残留会被当成"远端新增"再拉回本地,造成空壳复活。
|
|
960
|
+
* 这些路径改由 processTombstones 主动删除,直到所有副本确认不存在、
|
|
961
|
+
* 墓碑被 GC 之后才恢复同步。
|
|
962
|
+
*/
|
|
963
|
+
private readonly tombstoneExcludePrefixes;
|
|
964
|
+
private readonly syncFilter;
|
|
965
|
+
/**
|
|
966
|
+
* 本轮 processTombstones 中,各副本对墓碑路径"已确认不存在"的结果。
|
|
967
|
+
* 目录类路径用 exists() 复核;`.mtime` 侧车对部分后端不可见,只能用
|
|
968
|
+
* unlink() 是否成功来判定。
|
|
969
|
+
*/
|
|
970
|
+
private readonly tombstoneConfirmations;
|
|
955
971
|
/** Per-backend sync state (successful pairs + connect failures). */
|
|
956
972
|
private backendStates;
|
|
957
973
|
/** Last known backend descriptors, for the synchronous unified handle API. */
|
|
@@ -1000,6 +1016,13 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1000
1016
|
* Results are cached within a flush() cycle to avoid redundant reads.
|
|
1001
1017
|
*/
|
|
1002
1018
|
private readTombstones;
|
|
1019
|
+
/**
|
|
1020
|
+
* 把当前墓碑路径同步到同步排除表。
|
|
1021
|
+
*
|
|
1022
|
+
* 排除表与各同步对共享同一个数组实例(zen-fs-sync 在每次检测时实时读取
|
|
1023
|
+
* `filter.excludePrefixes`),所以这里原地更新即可生效。
|
|
1024
|
+
*/
|
|
1025
|
+
private refreshSyncExclusions;
|
|
1003
1026
|
/** Invalidate the tombstone cache — call after tombstones are modified. */
|
|
1004
1027
|
private invalidateTombstoneCache;
|
|
1005
1028
|
/**
|
|
@@ -1013,6 +1036,24 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1013
1036
|
* effectively free for them.
|
|
1014
1037
|
*/
|
|
1015
1038
|
private processTombstones;
|
|
1039
|
+
/**
|
|
1040
|
+
* 删除墓碑目标在某个后端上的全部残留。
|
|
1041
|
+
*
|
|
1042
|
+
* 目录不能直接 unlink(会抛 EISDIR)。旧实现把这个异常当成"已被并发删除"
|
|
1043
|
+
* 而继续推进确认,墓碑随后被 GC;但副本上的占位目录仍然存在,下一次同步
|
|
1044
|
+
* 就把它当作远端内容拉回本地,形成空壳复活。
|
|
1045
|
+
*
|
|
1046
|
+
* 这里按树清理:
|
|
1047
|
+
* 1. 强制删掉 `.keep` / `.keep.mtime` 占位(部分后端把 `.keep` 隐藏在索引里,
|
|
1048
|
+
* readdir 看不到,只能按路径直接删);
|
|
1049
|
+
* 2. 递归清理可见子项;
|
|
1050
|
+
* 3. rmdir(后端不支持目录时退回 unlink)。
|
|
1051
|
+
*
|
|
1052
|
+
* @returns 是否确认该路径已不存在;清理失败返回 false,墓碑必须保留。
|
|
1053
|
+
*/
|
|
1054
|
+
private purgePathOnBackend;
|
|
1055
|
+
/** 后端无关的存在性判断:优先 exists(),否则退化为 stat()。 */
|
|
1056
|
+
private pathExistsOnBackend;
|
|
1016
1057
|
/**
|
|
1017
1058
|
* Safe existence check — returns false on any error instead of throwing.
|
|
1018
1059
|
* Used by processTombstones to avoid unnecessary unlink() calls.
|
|
@@ -1071,9 +1112,25 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1071
1112
|
*/
|
|
1072
1113
|
startBackgroundSync(): void;
|
|
1073
1114
|
/**
|
|
1074
|
-
* After sync: mark each tombstone as confirmed by
|
|
1115
|
+
* After sync: mark each tombstone as confirmed by replicas where the target
|
|
1116
|
+
* path was actually verified absent.
|
|
1117
|
+
*
|
|
1118
|
+
* 旧实现把所有副本无条件写进 confirmedBy,只要有一轮同步跑过就认为
|
|
1119
|
+
* "所有副本都删干净了"。但副本可能因为网络/后端限制删除失败,或只剩
|
|
1120
|
+
* `.keep` / `.keep.mtime` 占位目录;墓碑一旦被 GC,下一轮同步就把残留
|
|
1121
|
+
* 当作远端内容拉回本地。现在只确认 processTombstones 本轮真正清干净的副本。
|
|
1075
1122
|
*/
|
|
1076
1123
|
private updateTombstoneConfirmations;
|
|
1124
|
+
/**
|
|
1125
|
+
* 墓碑路径是否已在所有副本上确认不存在。
|
|
1126
|
+
*
|
|
1127
|
+
* `.mtime` 侧车与 `.keep` 占位对部分后端不可见(`exists()` 恒为 false),
|
|
1128
|
+
* 因此只能相信本轮 `processTombstones` 的 unlink 结果;其余路径在 GC 前
|
|
1129
|
+
* 现场复核一次,避免"确认之后副本又把残留带了回来"。
|
|
1130
|
+
*
|
|
1131
|
+
* 任一位置仍存在就返回 false —— 墓碑必须保留,直到真正删干净。
|
|
1132
|
+
*/
|
|
1133
|
+
private allReplicasConfirmAbsent;
|
|
1077
1134
|
/**
|
|
1078
1135
|
* GC: remove tombstones where all backends in backends.json have confirmed.
|
|
1079
1136
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -952,6 +952,22 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
952
952
|
private readonly pollIntervalMs?;
|
|
953
953
|
/** Tombstone cache — avoids redundant reads within a single flush() cycle. */
|
|
954
954
|
private tombstoneCache;
|
|
955
|
+
/**
|
|
956
|
+
* 墓碑存在期间被排除在同步之外的路径。
|
|
957
|
+
*
|
|
958
|
+
* 远端副本可能仍残留被删路径(哪怕只剩 `.keep` / `.keep.mtime` 占位)。
|
|
959
|
+
* 若同步照常进行,这类残留会被当成"远端新增"再拉回本地,造成空壳复活。
|
|
960
|
+
* 这些路径改由 processTombstones 主动删除,直到所有副本确认不存在、
|
|
961
|
+
* 墓碑被 GC 之后才恢复同步。
|
|
962
|
+
*/
|
|
963
|
+
private readonly tombstoneExcludePrefixes;
|
|
964
|
+
private readonly syncFilter;
|
|
965
|
+
/**
|
|
966
|
+
* 本轮 processTombstones 中,各副本对墓碑路径"已确认不存在"的结果。
|
|
967
|
+
* 目录类路径用 exists() 复核;`.mtime` 侧车对部分后端不可见,只能用
|
|
968
|
+
* unlink() 是否成功来判定。
|
|
969
|
+
*/
|
|
970
|
+
private readonly tombstoneConfirmations;
|
|
955
971
|
/** Per-backend sync state (successful pairs + connect failures). */
|
|
956
972
|
private backendStates;
|
|
957
973
|
/** Last known backend descriptors, for the synchronous unified handle API. */
|
|
@@ -1000,6 +1016,13 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1000
1016
|
* Results are cached within a flush() cycle to avoid redundant reads.
|
|
1001
1017
|
*/
|
|
1002
1018
|
private readTombstones;
|
|
1019
|
+
/**
|
|
1020
|
+
* 把当前墓碑路径同步到同步排除表。
|
|
1021
|
+
*
|
|
1022
|
+
* 排除表与各同步对共享同一个数组实例(zen-fs-sync 在每次检测时实时读取
|
|
1023
|
+
* `filter.excludePrefixes`),所以这里原地更新即可生效。
|
|
1024
|
+
*/
|
|
1025
|
+
private refreshSyncExclusions;
|
|
1003
1026
|
/** Invalidate the tombstone cache — call after tombstones are modified. */
|
|
1004
1027
|
private invalidateTombstoneCache;
|
|
1005
1028
|
/**
|
|
@@ -1013,6 +1036,24 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1013
1036
|
* effectively free for them.
|
|
1014
1037
|
*/
|
|
1015
1038
|
private processTombstones;
|
|
1039
|
+
/**
|
|
1040
|
+
* 删除墓碑目标在某个后端上的全部残留。
|
|
1041
|
+
*
|
|
1042
|
+
* 目录不能直接 unlink(会抛 EISDIR)。旧实现把这个异常当成"已被并发删除"
|
|
1043
|
+
* 而继续推进确认,墓碑随后被 GC;但副本上的占位目录仍然存在,下一次同步
|
|
1044
|
+
* 就把它当作远端内容拉回本地,形成空壳复活。
|
|
1045
|
+
*
|
|
1046
|
+
* 这里按树清理:
|
|
1047
|
+
* 1. 强制删掉 `.keep` / `.keep.mtime` 占位(部分后端把 `.keep` 隐藏在索引里,
|
|
1048
|
+
* readdir 看不到,只能按路径直接删);
|
|
1049
|
+
* 2. 递归清理可见子项;
|
|
1050
|
+
* 3. rmdir(后端不支持目录时退回 unlink)。
|
|
1051
|
+
*
|
|
1052
|
+
* @returns 是否确认该路径已不存在;清理失败返回 false,墓碑必须保留。
|
|
1053
|
+
*/
|
|
1054
|
+
private purgePathOnBackend;
|
|
1055
|
+
/** 后端无关的存在性判断:优先 exists(),否则退化为 stat()。 */
|
|
1056
|
+
private pathExistsOnBackend;
|
|
1016
1057
|
/**
|
|
1017
1058
|
* Safe existence check — returns false on any error instead of throwing.
|
|
1018
1059
|
* Used by processTombstones to avoid unnecessary unlink() calls.
|
|
@@ -1071,9 +1112,25 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1071
1112
|
*/
|
|
1072
1113
|
startBackgroundSync(): void;
|
|
1073
1114
|
/**
|
|
1074
|
-
* After sync: mark each tombstone as confirmed by
|
|
1115
|
+
* After sync: mark each tombstone as confirmed by replicas where the target
|
|
1116
|
+
* path was actually verified absent.
|
|
1117
|
+
*
|
|
1118
|
+
* 旧实现把所有副本无条件写进 confirmedBy,只要有一轮同步跑过就认为
|
|
1119
|
+
* "所有副本都删干净了"。但副本可能因为网络/后端限制删除失败,或只剩
|
|
1120
|
+
* `.keep` / `.keep.mtime` 占位目录;墓碑一旦被 GC,下一轮同步就把残留
|
|
1121
|
+
* 当作远端内容拉回本地。现在只确认 processTombstones 本轮真正清干净的副本。
|
|
1075
1122
|
*/
|
|
1076
1123
|
private updateTombstoneConfirmations;
|
|
1124
|
+
/**
|
|
1125
|
+
* 墓碑路径是否已在所有副本上确认不存在。
|
|
1126
|
+
*
|
|
1127
|
+
* `.mtime` 侧车与 `.keep` 占位对部分后端不可见(`exists()` 恒为 false),
|
|
1128
|
+
* 因此只能相信本轮 `processTombstones` 的 unlink 结果;其余路径在 GC 前
|
|
1129
|
+
* 现场复核一次,避免"确认之后副本又把残留带了回来"。
|
|
1130
|
+
*
|
|
1131
|
+
* 任一位置仍存在就返回 false —— 墓碑必须保留,直到真正删干净。
|
|
1132
|
+
*/
|
|
1133
|
+
private allReplicasConfirmAbsent;
|
|
1077
1134
|
/**
|
|
1078
1135
|
* GC: remove tombstones where all backends in backends.json have confirmed.
|
|
1079
1136
|
*
|
package/dist/index.js
CHANGED
|
@@ -2130,6 +2130,12 @@ async function removeDirectoryTree(fs, dir) {
|
|
|
2130
2130
|
} catch {
|
|
2131
2131
|
}
|
|
2132
2132
|
}
|
|
2133
|
+
function isNotFoundError(err) {
|
|
2134
|
+
const code = err?.code;
|
|
2135
|
+
if (code === "ENOENT" || code === "ENOTDIR") return true;
|
|
2136
|
+
const message = String(err?.message ?? err);
|
|
2137
|
+
return /ENOENT|not found|does not exist|\b404\b/i.test(message);
|
|
2138
|
+
}
|
|
2133
2139
|
function normalizeGroupName(value) {
|
|
2134
2140
|
if (typeof value !== "string") {
|
|
2135
2141
|
throw new Error("Group name must be a string");
|
|
@@ -2235,6 +2241,22 @@ var ConfigRepo = class {
|
|
|
2235
2241
|
pollIntervalMs;
|
|
2236
2242
|
/** Tombstone cache — avoids redundant reads within a single flush() cycle. */
|
|
2237
2243
|
tombstoneCache = null;
|
|
2244
|
+
/**
|
|
2245
|
+
* 墓碑存在期间被排除在同步之外的路径。
|
|
2246
|
+
*
|
|
2247
|
+
* 远端副本可能仍残留被删路径(哪怕只剩 `.keep` / `.keep.mtime` 占位)。
|
|
2248
|
+
* 若同步照常进行,这类残留会被当成"远端新增"再拉回本地,造成空壳复活。
|
|
2249
|
+
* 这些路径改由 processTombstones 主动删除,直到所有副本确认不存在、
|
|
2250
|
+
* 墓碑被 GC 之后才恢复同步。
|
|
2251
|
+
*/
|
|
2252
|
+
tombstoneExcludePrefixes = [];
|
|
2253
|
+
syncFilter = { excludePrefixes: this.tombstoneExcludePrefixes };
|
|
2254
|
+
/**
|
|
2255
|
+
* 本轮 processTombstones 中,各副本对墓碑路径"已确认不存在"的结果。
|
|
2256
|
+
* 目录类路径用 exists() 复核;`.mtime` 侧车对部分后端不可见,只能用
|
|
2257
|
+
* unlink() 是否成功来判定。
|
|
2258
|
+
*/
|
|
2259
|
+
tombstoneConfirmations = /* @__PURE__ */ new Map();
|
|
2238
2260
|
/** Per-backend sync state (successful pairs + connect failures). */
|
|
2239
2261
|
backendStates = /* @__PURE__ */ new Map();
|
|
2240
2262
|
/** Last known backend descriptors, for the synchronous unified handle API. */
|
|
@@ -2514,8 +2536,29 @@ var ConfigRepo = class {
|
|
|
2514
2536
|
return this.tombstoneCache;
|
|
2515
2537
|
}
|
|
2516
2538
|
this.tombstoneCache = await readTombstonesFromFs(this.cachedFS, (message) => log3.warn(message));
|
|
2539
|
+
this.refreshSyncExclusions(this.tombstoneCache);
|
|
2517
2540
|
return this.tombstoneCache;
|
|
2518
2541
|
}
|
|
2542
|
+
/**
|
|
2543
|
+
* 把当前墓碑路径同步到同步排除表。
|
|
2544
|
+
*
|
|
2545
|
+
* 排除表与各同步对共享同一个数组实例(zen-fs-sync 在每次检测时实时读取
|
|
2546
|
+
* `filter.excludePrefixes`),所以这里原地更新即可生效。
|
|
2547
|
+
*/
|
|
2548
|
+
refreshSyncExclusions(tombstones) {
|
|
2549
|
+
this.tombstoneExcludePrefixes.length = 0;
|
|
2550
|
+
const add = (target) => {
|
|
2551
|
+
this.tombstoneExcludePrefixes.push(target, `${target}/`);
|
|
2552
|
+
};
|
|
2553
|
+
for (const tombstone of tombstones) {
|
|
2554
|
+
if (!tombstone.path) continue;
|
|
2555
|
+
add(tombstone.path);
|
|
2556
|
+
if (tombstone.path === "/.keep" || tombstone.path.endsWith("/.keep") || tombstone.path.endsWith("/.keep.mtime")) {
|
|
2557
|
+
const dir = tombstone.path.slice(0, tombstone.path.lastIndexOf("/")) || "/";
|
|
2558
|
+
if (dir !== "/") add(dir);
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2561
|
+
}
|
|
2519
2562
|
/** Invalidate the tombstone cache — call after tombstones are modified. */
|
|
2520
2563
|
invalidateTombstoneCache() {
|
|
2521
2564
|
this.tombstoneCache = null;
|
|
@@ -2532,70 +2575,151 @@ var ConfigRepo = class {
|
|
|
2532
2575
|
*/
|
|
2533
2576
|
async processTombstones() {
|
|
2534
2577
|
const tombstones = await this.readTombstones();
|
|
2578
|
+
this.tombstoneConfirmations.clear();
|
|
2535
2579
|
if (tombstones.length === 0) return;
|
|
2536
2580
|
let processed = 0;
|
|
2537
2581
|
let alreadyDeleted = 0;
|
|
2582
|
+
let unresolved = 0;
|
|
2538
2583
|
for (const tombstone of tombstones) {
|
|
2539
|
-
const
|
|
2584
|
+
const isKeepPlaceholder = tombstone.path === "/.keep" || tombstone.path.endsWith("/.keep");
|
|
2585
|
+
const forceDelete = isMtimeSidecar(tombstone.path) || isKeepPlaceholder;
|
|
2540
2586
|
const tVersionPath = forceDelete ? null : versionPathFor(tombstone.path);
|
|
2541
|
-
|
|
2542
|
-
const existedOnPrimary = forceDelete || await this.safeExists(this.cachedFS, tombstone.path);
|
|
2543
|
-
log3.log(`[TOMB-TRACE] primary safeExists(${tombstone.path}) \u2192 ${existedOnPrimary}`);
|
|
2544
|
-
if (existedOnPrimary) {
|
|
2545
|
-
log3.log(`[TOMB-TRACE] tombstone check: ${tombstone.path} on primary \u2192 EXISTS`);
|
|
2546
|
-
}
|
|
2547
|
-
if (existedOnPrimary) {
|
|
2587
|
+
if (forceDelete) {
|
|
2548
2588
|
try {
|
|
2549
2589
|
await this.cachedFS.unlink(tombstone.path);
|
|
2550
2590
|
processed++;
|
|
2551
2591
|
} catch {
|
|
2552
2592
|
}
|
|
2553
|
-
}
|
|
2554
|
-
|
|
2555
|
-
const
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
}
|
|
2562
|
-
}
|
|
2593
|
+
} else {
|
|
2594
|
+
const existedOnPrimary = await this.pathExistsOnBackend(this.cachedFS, tombstone.path);
|
|
2595
|
+
const gone = await this.purgePathOnBackend(this.cachedFS, tombstone.path);
|
|
2596
|
+
if (gone) {
|
|
2597
|
+
if (existedOnPrimary) processed++;
|
|
2598
|
+
else alreadyDeleted++;
|
|
2599
|
+
} else unresolved++;
|
|
2600
|
+
if (tVersionPath) await this.purgePathOnBackend(this.cachedFS, tVersionPath);
|
|
2563
2601
|
}
|
|
2564
2602
|
for (const [replicaId, replica] of this.replicaBackends) {
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
log3.log(`[TOMB-TRACE] replica ${replicaId} safeExists(${tombstone.path}) \u2192 ${existed}`);
|
|
2568
|
-
if (existed) {
|
|
2569
|
-
log3.log(`[TOMB-TRACE] tombstone check: ${tombstone.path} on ${replicaId} \u2192 EXISTS`);
|
|
2603
|
+
let gone;
|
|
2604
|
+
if (forceDelete) {
|
|
2570
2605
|
try {
|
|
2571
|
-
log3.log(`[TOMB-TRACE] calling unlink(${tombstone.path}) on ${replicaId}`);
|
|
2572
2606
|
await replica.instance.unlink(tombstone.path);
|
|
2573
|
-
|
|
2574
|
-
processed++;
|
|
2607
|
+
gone = true;
|
|
2575
2608
|
} catch (err) {
|
|
2576
|
-
|
|
2577
|
-
alreadyDeleted++;
|
|
2609
|
+
gone = isNotFoundError(err);
|
|
2578
2610
|
}
|
|
2611
|
+
if (gone) processed++;
|
|
2579
2612
|
} else {
|
|
2580
|
-
|
|
2581
|
-
|
|
2613
|
+
const existed = await this.pathExistsOnBackend(replica.instance, tombstone.path);
|
|
2614
|
+
gone = await this.purgePathOnBackend(replica.instance, tombstone.path);
|
|
2615
|
+
if (gone) {
|
|
2616
|
+
if (existed) processed++;
|
|
2617
|
+
else alreadyDeleted++;
|
|
2618
|
+
}
|
|
2619
|
+
if (tVersionPath) await this.purgePathOnBackend(replica.instance, tVersionPath);
|
|
2582
2620
|
}
|
|
2583
|
-
if (
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
log3.log(`[TOMB-TRACE] calling unlink(${tVersionPath}) on ${replicaId}`);
|
|
2589
|
-
await replica.instance.unlink(tVersionPath);
|
|
2590
|
-
} catch (err) {
|
|
2591
|
-
log3.log(`[TOMB-TRACE] unlink(${tVersionPath}) on ${replicaId} FAILED: ${err}`);
|
|
2592
|
-
}
|
|
2621
|
+
if (gone) {
|
|
2622
|
+
let confirmed = this.tombstoneConfirmations.get(tombstone.path);
|
|
2623
|
+
if (!confirmed) {
|
|
2624
|
+
confirmed = /* @__PURE__ */ new Set();
|
|
2625
|
+
this.tombstoneConfirmations.set(tombstone.path, confirmed);
|
|
2593
2626
|
}
|
|
2627
|
+
confirmed.add(replicaId);
|
|
2628
|
+
} else {
|
|
2629
|
+
unresolved++;
|
|
2630
|
+
log3.warn(
|
|
2631
|
+
`[ConfigRepo] tombstone kept: ${tombstone.path} still present on replica ${replicaId}`
|
|
2632
|
+
);
|
|
2633
|
+
}
|
|
2634
|
+
}
|
|
2635
|
+
}
|
|
2636
|
+
if (processed > 0 || alreadyDeleted > 0 || unresolved > 0) {
|
|
2637
|
+
log3.log(
|
|
2638
|
+
`[ConfigRepo] processTombstones: ${tombstones.length} tombstone(s), ${processed} deleted, ${alreadyDeleted} already gone, ${unresolved} unresolved`
|
|
2639
|
+
);
|
|
2640
|
+
}
|
|
2641
|
+
}
|
|
2642
|
+
/**
|
|
2643
|
+
* 删除墓碑目标在某个后端上的全部残留。
|
|
2644
|
+
*
|
|
2645
|
+
* 目录不能直接 unlink(会抛 EISDIR)。旧实现把这个异常当成"已被并发删除"
|
|
2646
|
+
* 而继续推进确认,墓碑随后被 GC;但副本上的占位目录仍然存在,下一次同步
|
|
2647
|
+
* 就把它当作远端内容拉回本地,形成空壳复活。
|
|
2648
|
+
*
|
|
2649
|
+
* 这里按树清理:
|
|
2650
|
+
* 1. 强制删掉 `.keep` / `.keep.mtime` 占位(部分后端把 `.keep` 隐藏在索引里,
|
|
2651
|
+
* readdir 看不到,只能按路径直接删);
|
|
2652
|
+
* 2. 递归清理可见子项;
|
|
2653
|
+
* 3. rmdir(后端不支持目录时退回 unlink)。
|
|
2654
|
+
*
|
|
2655
|
+
* @returns 是否确认该路径已不存在;清理失败返回 false,墓碑必须保留。
|
|
2656
|
+
*/
|
|
2657
|
+
async purgePathOnBackend(fs, targetPath) {
|
|
2658
|
+
let isDir2 = false;
|
|
2659
|
+
try {
|
|
2660
|
+
const stat = await fs.stat(targetPath);
|
|
2661
|
+
isDir2 = typeof stat?.isDirectory === "function" ? stat.isDirectory() : !!(stat && ((stat.mode ?? 0) & 61440) === 16384);
|
|
2662
|
+
} catch {
|
|
2663
|
+
if (!await this.pathExistsOnBackend(fs, targetPath)) return true;
|
|
2664
|
+
}
|
|
2665
|
+
if (!isDir2) {
|
|
2666
|
+
try {
|
|
2667
|
+
await fs.unlink(targetPath);
|
|
2668
|
+
} catch {
|
|
2669
|
+
}
|
|
2670
|
+
return !await this.pathExistsOnBackend(fs, targetPath);
|
|
2671
|
+
}
|
|
2672
|
+
for (const placeholder of [`${targetPath}/.keep`, `${targetPath}/.keep.mtime`]) {
|
|
2673
|
+
try {
|
|
2674
|
+
await fs.unlink(placeholder);
|
|
2675
|
+
} catch {
|
|
2676
|
+
}
|
|
2677
|
+
}
|
|
2678
|
+
let children = [];
|
|
2679
|
+
try {
|
|
2680
|
+
children = await fs.readdir(targetPath);
|
|
2681
|
+
} catch {
|
|
2682
|
+
}
|
|
2683
|
+
for (const child of children) {
|
|
2684
|
+
await this.purgePathOnBackend(fs, `${targetPath}/${child}`);
|
|
2685
|
+
}
|
|
2686
|
+
try {
|
|
2687
|
+
await fs.rmdir(targetPath);
|
|
2688
|
+
} catch {
|
|
2689
|
+
try {
|
|
2690
|
+
await fs.unlink(targetPath);
|
|
2691
|
+
} catch {
|
|
2692
|
+
}
|
|
2693
|
+
}
|
|
2694
|
+
if (await this.pathExistsOnBackend(fs, targetPath)) {
|
|
2695
|
+
for (const placeholder of [`${targetPath}/.keep`, `${targetPath}/.keep.mtime`]) {
|
|
2696
|
+
try {
|
|
2697
|
+
await fs.unlink(placeholder);
|
|
2698
|
+
} catch {
|
|
2594
2699
|
}
|
|
2595
2700
|
}
|
|
2701
|
+
try {
|
|
2702
|
+
await fs.rmdir(targetPath);
|
|
2703
|
+
} catch {
|
|
2704
|
+
}
|
|
2705
|
+
return !await this.pathExistsOnBackend(fs, targetPath);
|
|
2596
2706
|
}
|
|
2597
|
-
|
|
2598
|
-
|
|
2707
|
+
return true;
|
|
2708
|
+
}
|
|
2709
|
+
/** 后端无关的存在性判断:优先 exists(),否则退化为 stat()。 */
|
|
2710
|
+
async pathExistsOnBackend(fs, path) {
|
|
2711
|
+
if (typeof fs?.exists === "function") {
|
|
2712
|
+
try {
|
|
2713
|
+
return !!await fs.exists(path);
|
|
2714
|
+
} catch {
|
|
2715
|
+
return false;
|
|
2716
|
+
}
|
|
2717
|
+
}
|
|
2718
|
+
try {
|
|
2719
|
+
await fs.stat(path);
|
|
2720
|
+
return true;
|
|
2721
|
+
} catch {
|
|
2722
|
+
return false;
|
|
2599
2723
|
}
|
|
2600
2724
|
}
|
|
2601
2725
|
/**
|
|
@@ -2751,7 +2875,13 @@ var ConfigRepo = class {
|
|
|
2751
2875
|
});
|
|
2752
2876
|
}
|
|
2753
2877
|
/**
|
|
2754
|
-
* After sync: mark each tombstone as confirmed by
|
|
2878
|
+
* After sync: mark each tombstone as confirmed by replicas where the target
|
|
2879
|
+
* path was actually verified absent.
|
|
2880
|
+
*
|
|
2881
|
+
* 旧实现把所有副本无条件写进 confirmedBy,只要有一轮同步跑过就认为
|
|
2882
|
+
* "所有副本都删干净了"。但副本可能因为网络/后端限制删除失败,或只剩
|
|
2883
|
+
* `.keep` / `.keep.mtime` 占位目录;墓碑一旦被 GC,下一轮同步就把残留
|
|
2884
|
+
* 当作远端内容拉回本地。现在只确认 processTombstones 本轮真正清干净的副本。
|
|
2755
2885
|
*/
|
|
2756
2886
|
async updateTombstoneConfirmations() {
|
|
2757
2887
|
const tombstones = await this.readTombstones();
|
|
@@ -2759,11 +2889,12 @@ var ConfigRepo = class {
|
|
|
2759
2889
|
let modified = 0;
|
|
2760
2890
|
for (const tombstone of tombstones) {
|
|
2761
2891
|
let changed = false;
|
|
2892
|
+
const confirmedReplicas = this.tombstoneConfirmations.get(tombstone.path);
|
|
2762
2893
|
for (const replicaId of this.replicaBackends.keys()) {
|
|
2763
|
-
if (
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2894
|
+
if (tombstone.confirmedBy.includes(replicaId)) continue;
|
|
2895
|
+
if (!confirmedReplicas?.has(replicaId)) continue;
|
|
2896
|
+
tombstone.confirmedBy.push(replicaId);
|
|
2897
|
+
changed = true;
|
|
2767
2898
|
}
|
|
2768
2899
|
if (changed) {
|
|
2769
2900
|
try {
|
|
@@ -2782,6 +2913,29 @@ var ConfigRepo = class {
|
|
|
2782
2913
|
log3.log(`[ConfigRepo] updateTombstoneConfirmations: ${modified} tombstone(s) updated`);
|
|
2783
2914
|
if (modified > 0) this.invalidateTombstoneCache();
|
|
2784
2915
|
}
|
|
2916
|
+
/**
|
|
2917
|
+
* 墓碑路径是否已在所有副本上确认不存在。
|
|
2918
|
+
*
|
|
2919
|
+
* `.mtime` 侧车与 `.keep` 占位对部分后端不可见(`exists()` 恒为 false),
|
|
2920
|
+
* 因此只能相信本轮 `processTombstones` 的 unlink 结果;其余路径在 GC 前
|
|
2921
|
+
* 现场复核一次,避免"确认之后副本又把残留带了回来"。
|
|
2922
|
+
*
|
|
2923
|
+
* 任一位置仍存在就返回 false —— 墓碑必须保留,直到真正删干净。
|
|
2924
|
+
*/
|
|
2925
|
+
async allReplicasConfirmAbsent(path) {
|
|
2926
|
+
const confirmed = this.tombstoneConfirmations.get(path);
|
|
2927
|
+
const hiddenPlaceholder = isMtimeSidecar(path) || path === "/.keep" || path.endsWith("/.keep");
|
|
2928
|
+
if (hiddenPlaceholder) {
|
|
2929
|
+
for (const replicaId of this.replicaBackends.keys()) {
|
|
2930
|
+
if (!confirmed?.has(replicaId)) return false;
|
|
2931
|
+
}
|
|
2932
|
+
return true;
|
|
2933
|
+
}
|
|
2934
|
+
for (const [, replica] of this.replicaBackends) {
|
|
2935
|
+
if (await this.pathExistsOnBackend(replica.instance, path)) return false;
|
|
2936
|
+
}
|
|
2937
|
+
return true;
|
|
2938
|
+
}
|
|
2785
2939
|
/**
|
|
2786
2940
|
* GC: remove tombstones where all backends in backends.json have confirmed.
|
|
2787
2941
|
*
|
|
@@ -2795,7 +2949,7 @@ var ConfigRepo = class {
|
|
|
2795
2949
|
if (tombstones.length === 0) return;
|
|
2796
2950
|
const allBackendIds = [...this.replicaBackends.keys()];
|
|
2797
2951
|
for (const tombstone of tombstones) {
|
|
2798
|
-
const allConfirmed =
|
|
2952
|
+
const allConfirmed = await this.allReplicasConfirmAbsent(tombstone.path);
|
|
2799
2953
|
if (allConfirmed) {
|
|
2800
2954
|
for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
|
|
2801
2955
|
const tombstonePath = tombstonePathFor(tombstone.path, dir);
|
|
@@ -3025,6 +3179,9 @@ var ConfigRepo = class {
|
|
|
3025
3179
|
direction: import_zen_fs_sync.SyncDirection.BiDirectional,
|
|
3026
3180
|
conflictStrategy: "source-wins",
|
|
3027
3181
|
pollIntervalMs,
|
|
3182
|
+
// 墓碑未清理前排除被删路径:副本残留(含 `.keep` 占位)不会被当成
|
|
3183
|
+
// "远端新增"再拉回本地。
|
|
3184
|
+
filter: this.syncFilter,
|
|
3028
3185
|
preSyncHook: async () => {
|
|
3029
3186
|
try {
|
|
3030
3187
|
this.invalidateTombstoneCache();
|
|
@@ -3515,6 +3672,8 @@ var ConfigRepo = class {
|
|
|
3515
3672
|
{
|
|
3516
3673
|
direction: import_zen_fs_sync.SyncDirection.BiDirectional,
|
|
3517
3674
|
conflictStrategy: "source-wins",
|
|
3675
|
+
// 墓碑未清理前排除被删路径,避免副本残留复活到本地。
|
|
3676
|
+
filter: this.syncFilter,
|
|
3518
3677
|
preSyncHook: async () => {
|
|
3519
3678
|
try {
|
|
3520
3679
|
this.invalidateTombstoneCache();
|
package/dist/index.mjs
CHANGED
|
@@ -1575,6 +1575,12 @@ async function removeDirectoryTree(fs, dir) {
|
|
|
1575
1575
|
} catch {
|
|
1576
1576
|
}
|
|
1577
1577
|
}
|
|
1578
|
+
function isNotFoundError(err) {
|
|
1579
|
+
const code = err?.code;
|
|
1580
|
+
if (code === "ENOENT" || code === "ENOTDIR") return true;
|
|
1581
|
+
const message = String(err?.message ?? err);
|
|
1582
|
+
return /ENOENT|not found|does not exist|\b404\b/i.test(message);
|
|
1583
|
+
}
|
|
1578
1584
|
function normalizeGroupName(value) {
|
|
1579
1585
|
if (typeof value !== "string") {
|
|
1580
1586
|
throw new Error("Group name must be a string");
|
|
@@ -1680,6 +1686,22 @@ var ConfigRepo = class {
|
|
|
1680
1686
|
pollIntervalMs;
|
|
1681
1687
|
/** Tombstone cache — avoids redundant reads within a single flush() cycle. */
|
|
1682
1688
|
tombstoneCache = null;
|
|
1689
|
+
/**
|
|
1690
|
+
* 墓碑存在期间被排除在同步之外的路径。
|
|
1691
|
+
*
|
|
1692
|
+
* 远端副本可能仍残留被删路径(哪怕只剩 `.keep` / `.keep.mtime` 占位)。
|
|
1693
|
+
* 若同步照常进行,这类残留会被当成"远端新增"再拉回本地,造成空壳复活。
|
|
1694
|
+
* 这些路径改由 processTombstones 主动删除,直到所有副本确认不存在、
|
|
1695
|
+
* 墓碑被 GC 之后才恢复同步。
|
|
1696
|
+
*/
|
|
1697
|
+
tombstoneExcludePrefixes = [];
|
|
1698
|
+
syncFilter = { excludePrefixes: this.tombstoneExcludePrefixes };
|
|
1699
|
+
/**
|
|
1700
|
+
* 本轮 processTombstones 中,各副本对墓碑路径"已确认不存在"的结果。
|
|
1701
|
+
* 目录类路径用 exists() 复核;`.mtime` 侧车对部分后端不可见,只能用
|
|
1702
|
+
* unlink() 是否成功来判定。
|
|
1703
|
+
*/
|
|
1704
|
+
tombstoneConfirmations = /* @__PURE__ */ new Map();
|
|
1683
1705
|
/** Per-backend sync state (successful pairs + connect failures). */
|
|
1684
1706
|
backendStates = /* @__PURE__ */ new Map();
|
|
1685
1707
|
/** Last known backend descriptors, for the synchronous unified handle API. */
|
|
@@ -1959,8 +1981,29 @@ var ConfigRepo = class {
|
|
|
1959
1981
|
return this.tombstoneCache;
|
|
1960
1982
|
}
|
|
1961
1983
|
this.tombstoneCache = await readTombstonesFromFs(this.cachedFS, (message) => log3.warn(message));
|
|
1984
|
+
this.refreshSyncExclusions(this.tombstoneCache);
|
|
1962
1985
|
return this.tombstoneCache;
|
|
1963
1986
|
}
|
|
1987
|
+
/**
|
|
1988
|
+
* 把当前墓碑路径同步到同步排除表。
|
|
1989
|
+
*
|
|
1990
|
+
* 排除表与各同步对共享同一个数组实例(zen-fs-sync 在每次检测时实时读取
|
|
1991
|
+
* `filter.excludePrefixes`),所以这里原地更新即可生效。
|
|
1992
|
+
*/
|
|
1993
|
+
refreshSyncExclusions(tombstones) {
|
|
1994
|
+
this.tombstoneExcludePrefixes.length = 0;
|
|
1995
|
+
const add = (target) => {
|
|
1996
|
+
this.tombstoneExcludePrefixes.push(target, `${target}/`);
|
|
1997
|
+
};
|
|
1998
|
+
for (const tombstone of tombstones) {
|
|
1999
|
+
if (!tombstone.path) continue;
|
|
2000
|
+
add(tombstone.path);
|
|
2001
|
+
if (tombstone.path === "/.keep" || tombstone.path.endsWith("/.keep") || tombstone.path.endsWith("/.keep.mtime")) {
|
|
2002
|
+
const dir = tombstone.path.slice(0, tombstone.path.lastIndexOf("/")) || "/";
|
|
2003
|
+
if (dir !== "/") add(dir);
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
1964
2007
|
/** Invalidate the tombstone cache — call after tombstones are modified. */
|
|
1965
2008
|
invalidateTombstoneCache() {
|
|
1966
2009
|
this.tombstoneCache = null;
|
|
@@ -1977,70 +2020,151 @@ var ConfigRepo = class {
|
|
|
1977
2020
|
*/
|
|
1978
2021
|
async processTombstones() {
|
|
1979
2022
|
const tombstones = await this.readTombstones();
|
|
2023
|
+
this.tombstoneConfirmations.clear();
|
|
1980
2024
|
if (tombstones.length === 0) return;
|
|
1981
2025
|
let processed = 0;
|
|
1982
2026
|
let alreadyDeleted = 0;
|
|
2027
|
+
let unresolved = 0;
|
|
1983
2028
|
for (const tombstone of tombstones) {
|
|
1984
|
-
const
|
|
2029
|
+
const isKeepPlaceholder = tombstone.path === "/.keep" || tombstone.path.endsWith("/.keep");
|
|
2030
|
+
const forceDelete = isMtimeSidecar(tombstone.path) || isKeepPlaceholder;
|
|
1985
2031
|
const tVersionPath = forceDelete ? null : versionPathFor(tombstone.path);
|
|
1986
|
-
|
|
1987
|
-
const existedOnPrimary = forceDelete || await this.safeExists(this.cachedFS, tombstone.path);
|
|
1988
|
-
log3.log(`[TOMB-TRACE] primary safeExists(${tombstone.path}) \u2192 ${existedOnPrimary}`);
|
|
1989
|
-
if (existedOnPrimary) {
|
|
1990
|
-
log3.log(`[TOMB-TRACE] tombstone check: ${tombstone.path} on primary \u2192 EXISTS`);
|
|
1991
|
-
}
|
|
1992
|
-
if (existedOnPrimary) {
|
|
2032
|
+
if (forceDelete) {
|
|
1993
2033
|
try {
|
|
1994
2034
|
await this.cachedFS.unlink(tombstone.path);
|
|
1995
2035
|
processed++;
|
|
1996
2036
|
} catch {
|
|
1997
2037
|
}
|
|
1998
|
-
}
|
|
1999
|
-
|
|
2000
|
-
const
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
}
|
|
2007
|
-
}
|
|
2038
|
+
} else {
|
|
2039
|
+
const existedOnPrimary = await this.pathExistsOnBackend(this.cachedFS, tombstone.path);
|
|
2040
|
+
const gone = await this.purgePathOnBackend(this.cachedFS, tombstone.path);
|
|
2041
|
+
if (gone) {
|
|
2042
|
+
if (existedOnPrimary) processed++;
|
|
2043
|
+
else alreadyDeleted++;
|
|
2044
|
+
} else unresolved++;
|
|
2045
|
+
if (tVersionPath) await this.purgePathOnBackend(this.cachedFS, tVersionPath);
|
|
2008
2046
|
}
|
|
2009
2047
|
for (const [replicaId, replica] of this.replicaBackends) {
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
log3.log(`[TOMB-TRACE] replica ${replicaId} safeExists(${tombstone.path}) \u2192 ${existed}`);
|
|
2013
|
-
if (existed) {
|
|
2014
|
-
log3.log(`[TOMB-TRACE] tombstone check: ${tombstone.path} on ${replicaId} \u2192 EXISTS`);
|
|
2048
|
+
let gone;
|
|
2049
|
+
if (forceDelete) {
|
|
2015
2050
|
try {
|
|
2016
|
-
log3.log(`[TOMB-TRACE] calling unlink(${tombstone.path}) on ${replicaId}`);
|
|
2017
2051
|
await replica.instance.unlink(tombstone.path);
|
|
2018
|
-
|
|
2019
|
-
processed++;
|
|
2052
|
+
gone = true;
|
|
2020
2053
|
} catch (err) {
|
|
2021
|
-
|
|
2022
|
-
alreadyDeleted++;
|
|
2054
|
+
gone = isNotFoundError(err);
|
|
2023
2055
|
}
|
|
2056
|
+
if (gone) processed++;
|
|
2024
2057
|
} else {
|
|
2025
|
-
|
|
2026
|
-
|
|
2058
|
+
const existed = await this.pathExistsOnBackend(replica.instance, tombstone.path);
|
|
2059
|
+
gone = await this.purgePathOnBackend(replica.instance, tombstone.path);
|
|
2060
|
+
if (gone) {
|
|
2061
|
+
if (existed) processed++;
|
|
2062
|
+
else alreadyDeleted++;
|
|
2063
|
+
}
|
|
2064
|
+
if (tVersionPath) await this.purgePathOnBackend(replica.instance, tVersionPath);
|
|
2027
2065
|
}
|
|
2028
|
-
if (
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
log3.log(`[TOMB-TRACE] calling unlink(${tVersionPath}) on ${replicaId}`);
|
|
2034
|
-
await replica.instance.unlink(tVersionPath);
|
|
2035
|
-
} catch (err) {
|
|
2036
|
-
log3.log(`[TOMB-TRACE] unlink(${tVersionPath}) on ${replicaId} FAILED: ${err}`);
|
|
2037
|
-
}
|
|
2066
|
+
if (gone) {
|
|
2067
|
+
let confirmed = this.tombstoneConfirmations.get(tombstone.path);
|
|
2068
|
+
if (!confirmed) {
|
|
2069
|
+
confirmed = /* @__PURE__ */ new Set();
|
|
2070
|
+
this.tombstoneConfirmations.set(tombstone.path, confirmed);
|
|
2038
2071
|
}
|
|
2072
|
+
confirmed.add(replicaId);
|
|
2073
|
+
} else {
|
|
2074
|
+
unresolved++;
|
|
2075
|
+
log3.warn(
|
|
2076
|
+
`[ConfigRepo] tombstone kept: ${tombstone.path} still present on replica ${replicaId}`
|
|
2077
|
+
);
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
if (processed > 0 || alreadyDeleted > 0 || unresolved > 0) {
|
|
2082
|
+
log3.log(
|
|
2083
|
+
`[ConfigRepo] processTombstones: ${tombstones.length} tombstone(s), ${processed} deleted, ${alreadyDeleted} already gone, ${unresolved} unresolved`
|
|
2084
|
+
);
|
|
2085
|
+
}
|
|
2086
|
+
}
|
|
2087
|
+
/**
|
|
2088
|
+
* 删除墓碑目标在某个后端上的全部残留。
|
|
2089
|
+
*
|
|
2090
|
+
* 目录不能直接 unlink(会抛 EISDIR)。旧实现把这个异常当成"已被并发删除"
|
|
2091
|
+
* 而继续推进确认,墓碑随后被 GC;但副本上的占位目录仍然存在,下一次同步
|
|
2092
|
+
* 就把它当作远端内容拉回本地,形成空壳复活。
|
|
2093
|
+
*
|
|
2094
|
+
* 这里按树清理:
|
|
2095
|
+
* 1. 强制删掉 `.keep` / `.keep.mtime` 占位(部分后端把 `.keep` 隐藏在索引里,
|
|
2096
|
+
* readdir 看不到,只能按路径直接删);
|
|
2097
|
+
* 2. 递归清理可见子项;
|
|
2098
|
+
* 3. rmdir(后端不支持目录时退回 unlink)。
|
|
2099
|
+
*
|
|
2100
|
+
* @returns 是否确认该路径已不存在;清理失败返回 false,墓碑必须保留。
|
|
2101
|
+
*/
|
|
2102
|
+
async purgePathOnBackend(fs, targetPath) {
|
|
2103
|
+
let isDir2 = false;
|
|
2104
|
+
try {
|
|
2105
|
+
const stat = await fs.stat(targetPath);
|
|
2106
|
+
isDir2 = typeof stat?.isDirectory === "function" ? stat.isDirectory() : !!(stat && ((stat.mode ?? 0) & 61440) === 16384);
|
|
2107
|
+
} catch {
|
|
2108
|
+
if (!await this.pathExistsOnBackend(fs, targetPath)) return true;
|
|
2109
|
+
}
|
|
2110
|
+
if (!isDir2) {
|
|
2111
|
+
try {
|
|
2112
|
+
await fs.unlink(targetPath);
|
|
2113
|
+
} catch {
|
|
2114
|
+
}
|
|
2115
|
+
return !await this.pathExistsOnBackend(fs, targetPath);
|
|
2116
|
+
}
|
|
2117
|
+
for (const placeholder of [`${targetPath}/.keep`, `${targetPath}/.keep.mtime`]) {
|
|
2118
|
+
try {
|
|
2119
|
+
await fs.unlink(placeholder);
|
|
2120
|
+
} catch {
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
let children = [];
|
|
2124
|
+
try {
|
|
2125
|
+
children = await fs.readdir(targetPath);
|
|
2126
|
+
} catch {
|
|
2127
|
+
}
|
|
2128
|
+
for (const child of children) {
|
|
2129
|
+
await this.purgePathOnBackend(fs, `${targetPath}/${child}`);
|
|
2130
|
+
}
|
|
2131
|
+
try {
|
|
2132
|
+
await fs.rmdir(targetPath);
|
|
2133
|
+
} catch {
|
|
2134
|
+
try {
|
|
2135
|
+
await fs.unlink(targetPath);
|
|
2136
|
+
} catch {
|
|
2137
|
+
}
|
|
2138
|
+
}
|
|
2139
|
+
if (await this.pathExistsOnBackend(fs, targetPath)) {
|
|
2140
|
+
for (const placeholder of [`${targetPath}/.keep`, `${targetPath}/.keep.mtime`]) {
|
|
2141
|
+
try {
|
|
2142
|
+
await fs.unlink(placeholder);
|
|
2143
|
+
} catch {
|
|
2039
2144
|
}
|
|
2040
2145
|
}
|
|
2146
|
+
try {
|
|
2147
|
+
await fs.rmdir(targetPath);
|
|
2148
|
+
} catch {
|
|
2149
|
+
}
|
|
2150
|
+
return !await this.pathExistsOnBackend(fs, targetPath);
|
|
2041
2151
|
}
|
|
2042
|
-
|
|
2043
|
-
|
|
2152
|
+
return true;
|
|
2153
|
+
}
|
|
2154
|
+
/** 后端无关的存在性判断:优先 exists(),否则退化为 stat()。 */
|
|
2155
|
+
async pathExistsOnBackend(fs, path) {
|
|
2156
|
+
if (typeof fs?.exists === "function") {
|
|
2157
|
+
try {
|
|
2158
|
+
return !!await fs.exists(path);
|
|
2159
|
+
} catch {
|
|
2160
|
+
return false;
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
try {
|
|
2164
|
+
await fs.stat(path);
|
|
2165
|
+
return true;
|
|
2166
|
+
} catch {
|
|
2167
|
+
return false;
|
|
2044
2168
|
}
|
|
2045
2169
|
}
|
|
2046
2170
|
/**
|
|
@@ -2196,7 +2320,13 @@ var ConfigRepo = class {
|
|
|
2196
2320
|
});
|
|
2197
2321
|
}
|
|
2198
2322
|
/**
|
|
2199
|
-
* After sync: mark each tombstone as confirmed by
|
|
2323
|
+
* After sync: mark each tombstone as confirmed by replicas where the target
|
|
2324
|
+
* path was actually verified absent.
|
|
2325
|
+
*
|
|
2326
|
+
* 旧实现把所有副本无条件写进 confirmedBy,只要有一轮同步跑过就认为
|
|
2327
|
+
* "所有副本都删干净了"。但副本可能因为网络/后端限制删除失败,或只剩
|
|
2328
|
+
* `.keep` / `.keep.mtime` 占位目录;墓碑一旦被 GC,下一轮同步就把残留
|
|
2329
|
+
* 当作远端内容拉回本地。现在只确认 processTombstones 本轮真正清干净的副本。
|
|
2200
2330
|
*/
|
|
2201
2331
|
async updateTombstoneConfirmations() {
|
|
2202
2332
|
const tombstones = await this.readTombstones();
|
|
@@ -2204,11 +2334,12 @@ var ConfigRepo = class {
|
|
|
2204
2334
|
let modified = 0;
|
|
2205
2335
|
for (const tombstone of tombstones) {
|
|
2206
2336
|
let changed = false;
|
|
2337
|
+
const confirmedReplicas = this.tombstoneConfirmations.get(tombstone.path);
|
|
2207
2338
|
for (const replicaId of this.replicaBackends.keys()) {
|
|
2208
|
-
if (
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2339
|
+
if (tombstone.confirmedBy.includes(replicaId)) continue;
|
|
2340
|
+
if (!confirmedReplicas?.has(replicaId)) continue;
|
|
2341
|
+
tombstone.confirmedBy.push(replicaId);
|
|
2342
|
+
changed = true;
|
|
2212
2343
|
}
|
|
2213
2344
|
if (changed) {
|
|
2214
2345
|
try {
|
|
@@ -2227,6 +2358,29 @@ var ConfigRepo = class {
|
|
|
2227
2358
|
log3.log(`[ConfigRepo] updateTombstoneConfirmations: ${modified} tombstone(s) updated`);
|
|
2228
2359
|
if (modified > 0) this.invalidateTombstoneCache();
|
|
2229
2360
|
}
|
|
2361
|
+
/**
|
|
2362
|
+
* 墓碑路径是否已在所有副本上确认不存在。
|
|
2363
|
+
*
|
|
2364
|
+
* `.mtime` 侧车与 `.keep` 占位对部分后端不可见(`exists()` 恒为 false),
|
|
2365
|
+
* 因此只能相信本轮 `processTombstones` 的 unlink 结果;其余路径在 GC 前
|
|
2366
|
+
* 现场复核一次,避免"确认之后副本又把残留带了回来"。
|
|
2367
|
+
*
|
|
2368
|
+
* 任一位置仍存在就返回 false —— 墓碑必须保留,直到真正删干净。
|
|
2369
|
+
*/
|
|
2370
|
+
async allReplicasConfirmAbsent(path) {
|
|
2371
|
+
const confirmed = this.tombstoneConfirmations.get(path);
|
|
2372
|
+
const hiddenPlaceholder = isMtimeSidecar(path) || path === "/.keep" || path.endsWith("/.keep");
|
|
2373
|
+
if (hiddenPlaceholder) {
|
|
2374
|
+
for (const replicaId of this.replicaBackends.keys()) {
|
|
2375
|
+
if (!confirmed?.has(replicaId)) return false;
|
|
2376
|
+
}
|
|
2377
|
+
return true;
|
|
2378
|
+
}
|
|
2379
|
+
for (const [, replica] of this.replicaBackends) {
|
|
2380
|
+
if (await this.pathExistsOnBackend(replica.instance, path)) return false;
|
|
2381
|
+
}
|
|
2382
|
+
return true;
|
|
2383
|
+
}
|
|
2230
2384
|
/**
|
|
2231
2385
|
* GC: remove tombstones where all backends in backends.json have confirmed.
|
|
2232
2386
|
*
|
|
@@ -2240,7 +2394,7 @@ var ConfigRepo = class {
|
|
|
2240
2394
|
if (tombstones.length === 0) return;
|
|
2241
2395
|
const allBackendIds = [...this.replicaBackends.keys()];
|
|
2242
2396
|
for (const tombstone of tombstones) {
|
|
2243
|
-
const allConfirmed =
|
|
2397
|
+
const allConfirmed = await this.allReplicasConfirmAbsent(tombstone.path);
|
|
2244
2398
|
if (allConfirmed) {
|
|
2245
2399
|
for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
|
|
2246
2400
|
const tombstonePath = tombstonePathFor(tombstone.path, dir);
|
|
@@ -2470,6 +2624,9 @@ var ConfigRepo = class {
|
|
|
2470
2624
|
direction: SyncDirection.BiDirectional,
|
|
2471
2625
|
conflictStrategy: "source-wins",
|
|
2472
2626
|
pollIntervalMs,
|
|
2627
|
+
// 墓碑未清理前排除被删路径:副本残留(含 `.keep` 占位)不会被当成
|
|
2628
|
+
// "远端新增"再拉回本地。
|
|
2629
|
+
filter: this.syncFilter,
|
|
2473
2630
|
preSyncHook: async () => {
|
|
2474
2631
|
try {
|
|
2475
2632
|
this.invalidateTombstoneCache();
|
|
@@ -2960,6 +3117,8 @@ var ConfigRepo = class {
|
|
|
2960
3117
|
{
|
|
2961
3118
|
direction: SyncDirection.BiDirectional,
|
|
2962
3119
|
conflictStrategy: "source-wins",
|
|
3120
|
+
// 墓碑未清理前排除被删路径,避免副本残留复活到本地。
|
|
3121
|
+
filter: this.syncFilter,
|
|
2963
3122
|
preSyncHook: async () => {
|
|
2964
3123
|
try {
|
|
2965
3124
|
this.invalidateTombstoneCache();
|
package/dist/zen-fs-config.js
CHANGED
|
@@ -28144,6 +28144,12 @@ var ZenFSConfig = (() => {
|
|
|
28144
28144
|
} catch {
|
|
28145
28145
|
}
|
|
28146
28146
|
}
|
|
28147
|
+
function isNotFoundError(err2) {
|
|
28148
|
+
const code = err2?.code;
|
|
28149
|
+
if (code === "ENOENT" || code === "ENOTDIR") return true;
|
|
28150
|
+
const message = String(err2?.message ?? err2);
|
|
28151
|
+
return /ENOENT|not found|does not exist|\b404\b/i.test(message);
|
|
28152
|
+
}
|
|
28147
28153
|
function normalizeGroupName(value) {
|
|
28148
28154
|
if (typeof value !== "string") {
|
|
28149
28155
|
throw new Error("Group name must be a string");
|
|
@@ -28250,6 +28256,22 @@ var ZenFSConfig = (() => {
|
|
|
28250
28256
|
__publicField(this, "pollIntervalMs");
|
|
28251
28257
|
/** Tombstone cache — avoids redundant reads within a single flush() cycle. */
|
|
28252
28258
|
__publicField(this, "tombstoneCache", null);
|
|
28259
|
+
/**
|
|
28260
|
+
* 墓碑存在期间被排除在同步之外的路径。
|
|
28261
|
+
*
|
|
28262
|
+
* 远端副本可能仍残留被删路径(哪怕只剩 `.keep` / `.keep.mtime` 占位)。
|
|
28263
|
+
* 若同步照常进行,这类残留会被当成"远端新增"再拉回本地,造成空壳复活。
|
|
28264
|
+
* 这些路径改由 processTombstones 主动删除,直到所有副本确认不存在、
|
|
28265
|
+
* 墓碑被 GC 之后才恢复同步。
|
|
28266
|
+
*/
|
|
28267
|
+
__publicField(this, "tombstoneExcludePrefixes", []);
|
|
28268
|
+
__publicField(this, "syncFilter", { excludePrefixes: this.tombstoneExcludePrefixes });
|
|
28269
|
+
/**
|
|
28270
|
+
* 本轮 processTombstones 中,各副本对墓碑路径"已确认不存在"的结果。
|
|
28271
|
+
* 目录类路径用 exists() 复核;`.mtime` 侧车对部分后端不可见,只能用
|
|
28272
|
+
* unlink() 是否成功来判定。
|
|
28273
|
+
*/
|
|
28274
|
+
__publicField(this, "tombstoneConfirmations", /* @__PURE__ */ new Map());
|
|
28253
28275
|
/** Per-backend sync state (successful pairs + connect failures). */
|
|
28254
28276
|
__publicField(this, "backendStates", /* @__PURE__ */ new Map());
|
|
28255
28277
|
/** Last known backend descriptors, for the synchronous unified handle API. */
|
|
@@ -28528,8 +28550,29 @@ var ZenFSConfig = (() => {
|
|
|
28528
28550
|
return this.tombstoneCache;
|
|
28529
28551
|
}
|
|
28530
28552
|
this.tombstoneCache = await readTombstonesFromFs(this.cachedFS, (message) => log7.warn(message));
|
|
28553
|
+
this.refreshSyncExclusions(this.tombstoneCache);
|
|
28531
28554
|
return this.tombstoneCache;
|
|
28532
28555
|
}
|
|
28556
|
+
/**
|
|
28557
|
+
* 把当前墓碑路径同步到同步排除表。
|
|
28558
|
+
*
|
|
28559
|
+
* 排除表与各同步对共享同一个数组实例(zen-fs-sync 在每次检测时实时读取
|
|
28560
|
+
* `filter.excludePrefixes`),所以这里原地更新即可生效。
|
|
28561
|
+
*/
|
|
28562
|
+
refreshSyncExclusions(tombstones) {
|
|
28563
|
+
this.tombstoneExcludePrefixes.length = 0;
|
|
28564
|
+
const add = (target) => {
|
|
28565
|
+
this.tombstoneExcludePrefixes.push(target, `${target}/`);
|
|
28566
|
+
};
|
|
28567
|
+
for (const tombstone of tombstones) {
|
|
28568
|
+
if (!tombstone.path) continue;
|
|
28569
|
+
add(tombstone.path);
|
|
28570
|
+
if (tombstone.path === "/.keep" || tombstone.path.endsWith("/.keep") || tombstone.path.endsWith("/.keep.mtime")) {
|
|
28571
|
+
const dir = tombstone.path.slice(0, tombstone.path.lastIndexOf("/")) || "/";
|
|
28572
|
+
if (dir !== "/") add(dir);
|
|
28573
|
+
}
|
|
28574
|
+
}
|
|
28575
|
+
}
|
|
28533
28576
|
/** Invalidate the tombstone cache — call after tombstones are modified. */
|
|
28534
28577
|
invalidateTombstoneCache() {
|
|
28535
28578
|
this.tombstoneCache = null;
|
|
@@ -28546,70 +28589,151 @@ var ZenFSConfig = (() => {
|
|
|
28546
28589
|
*/
|
|
28547
28590
|
async processTombstones() {
|
|
28548
28591
|
const tombstones = await this.readTombstones();
|
|
28592
|
+
this.tombstoneConfirmations.clear();
|
|
28549
28593
|
if (tombstones.length === 0) return;
|
|
28550
28594
|
let processed = 0;
|
|
28551
28595
|
let alreadyDeleted = 0;
|
|
28596
|
+
let unresolved = 0;
|
|
28552
28597
|
for (const tombstone of tombstones) {
|
|
28553
|
-
const
|
|
28598
|
+
const isKeepPlaceholder = tombstone.path === "/.keep" || tombstone.path.endsWith("/.keep");
|
|
28599
|
+
const forceDelete = isMtimeSidecar(tombstone.path) || isKeepPlaceholder;
|
|
28554
28600
|
const tVersionPath = forceDelete ? null : versionPathFor(tombstone.path);
|
|
28555
|
-
|
|
28556
|
-
const existedOnPrimary = forceDelete || await this.safeExists(this.cachedFS, tombstone.path);
|
|
28557
|
-
log7.log(`[TOMB-TRACE] primary safeExists(${tombstone.path}) \u2192 ${existedOnPrimary}`);
|
|
28558
|
-
if (existedOnPrimary) {
|
|
28559
|
-
log7.log(`[TOMB-TRACE] tombstone check: ${tombstone.path} on primary \u2192 EXISTS`);
|
|
28560
|
-
}
|
|
28561
|
-
if (existedOnPrimary) {
|
|
28601
|
+
if (forceDelete) {
|
|
28562
28602
|
try {
|
|
28563
28603
|
await this.cachedFS.unlink(tombstone.path);
|
|
28564
28604
|
processed++;
|
|
28565
28605
|
} catch {
|
|
28566
28606
|
}
|
|
28567
|
-
}
|
|
28568
|
-
|
|
28569
|
-
const
|
|
28570
|
-
|
|
28571
|
-
|
|
28572
|
-
|
|
28573
|
-
|
|
28574
|
-
|
|
28575
|
-
}
|
|
28576
|
-
}
|
|
28607
|
+
} else {
|
|
28608
|
+
const existedOnPrimary = await this.pathExistsOnBackend(this.cachedFS, tombstone.path);
|
|
28609
|
+
const gone = await this.purgePathOnBackend(this.cachedFS, tombstone.path);
|
|
28610
|
+
if (gone) {
|
|
28611
|
+
if (existedOnPrimary) processed++;
|
|
28612
|
+
else alreadyDeleted++;
|
|
28613
|
+
} else unresolved++;
|
|
28614
|
+
if (tVersionPath) await this.purgePathOnBackend(this.cachedFS, tVersionPath);
|
|
28577
28615
|
}
|
|
28578
28616
|
for (const [replicaId, replica] of this.replicaBackends) {
|
|
28579
|
-
|
|
28580
|
-
|
|
28581
|
-
log7.log(`[TOMB-TRACE] replica ${replicaId} safeExists(${tombstone.path}) \u2192 ${existed}`);
|
|
28582
|
-
if (existed) {
|
|
28583
|
-
log7.log(`[TOMB-TRACE] tombstone check: ${tombstone.path} on ${replicaId} \u2192 EXISTS`);
|
|
28617
|
+
let gone;
|
|
28618
|
+
if (forceDelete) {
|
|
28584
28619
|
try {
|
|
28585
|
-
log7.log(`[TOMB-TRACE] calling unlink(${tombstone.path}) on ${replicaId}`);
|
|
28586
28620
|
await replica.instance.unlink(tombstone.path);
|
|
28587
|
-
|
|
28588
|
-
processed++;
|
|
28621
|
+
gone = true;
|
|
28589
28622
|
} catch (err2) {
|
|
28590
|
-
|
|
28591
|
-
alreadyDeleted++;
|
|
28623
|
+
gone = isNotFoundError(err2);
|
|
28592
28624
|
}
|
|
28625
|
+
if (gone) processed++;
|
|
28593
28626
|
} else {
|
|
28594
|
-
|
|
28595
|
-
|
|
28627
|
+
const existed = await this.pathExistsOnBackend(replica.instance, tombstone.path);
|
|
28628
|
+
gone = await this.purgePathOnBackend(replica.instance, tombstone.path);
|
|
28629
|
+
if (gone) {
|
|
28630
|
+
if (existed) processed++;
|
|
28631
|
+
else alreadyDeleted++;
|
|
28632
|
+
}
|
|
28633
|
+
if (tVersionPath) await this.purgePathOnBackend(replica.instance, tVersionPath);
|
|
28596
28634
|
}
|
|
28597
|
-
if (
|
|
28598
|
-
|
|
28599
|
-
|
|
28600
|
-
|
|
28601
|
-
|
|
28602
|
-
log7.log(`[TOMB-TRACE] calling unlink(${tVersionPath}) on ${replicaId}`);
|
|
28603
|
-
await replica.instance.unlink(tVersionPath);
|
|
28604
|
-
} catch (err2) {
|
|
28605
|
-
log7.log(`[TOMB-TRACE] unlink(${tVersionPath}) on ${replicaId} FAILED: ${err2}`);
|
|
28606
|
-
}
|
|
28635
|
+
if (gone) {
|
|
28636
|
+
let confirmed = this.tombstoneConfirmations.get(tombstone.path);
|
|
28637
|
+
if (!confirmed) {
|
|
28638
|
+
confirmed = /* @__PURE__ */ new Set();
|
|
28639
|
+
this.tombstoneConfirmations.set(tombstone.path, confirmed);
|
|
28607
28640
|
}
|
|
28641
|
+
confirmed.add(replicaId);
|
|
28642
|
+
} else {
|
|
28643
|
+
unresolved++;
|
|
28644
|
+
log7.warn(
|
|
28645
|
+
`[ConfigRepo] tombstone kept: ${tombstone.path} still present on replica ${replicaId}`
|
|
28646
|
+
);
|
|
28608
28647
|
}
|
|
28609
28648
|
}
|
|
28610
28649
|
}
|
|
28611
|
-
if (processed > 0 || alreadyDeleted > 0) {
|
|
28612
|
-
log7.log(
|
|
28650
|
+
if (processed > 0 || alreadyDeleted > 0 || unresolved > 0) {
|
|
28651
|
+
log7.log(
|
|
28652
|
+
`[ConfigRepo] processTombstones: ${tombstones.length} tombstone(s), ${processed} deleted, ${alreadyDeleted} already gone, ${unresolved} unresolved`
|
|
28653
|
+
);
|
|
28654
|
+
}
|
|
28655
|
+
}
|
|
28656
|
+
/**
|
|
28657
|
+
* 删除墓碑目标在某个后端上的全部残留。
|
|
28658
|
+
*
|
|
28659
|
+
* 目录不能直接 unlink(会抛 EISDIR)。旧实现把这个异常当成"已被并发删除"
|
|
28660
|
+
* 而继续推进确认,墓碑随后被 GC;但副本上的占位目录仍然存在,下一次同步
|
|
28661
|
+
* 就把它当作远端内容拉回本地,形成空壳复活。
|
|
28662
|
+
*
|
|
28663
|
+
* 这里按树清理:
|
|
28664
|
+
* 1. 强制删掉 `.keep` / `.keep.mtime` 占位(部分后端把 `.keep` 隐藏在索引里,
|
|
28665
|
+
* readdir 看不到,只能按路径直接删);
|
|
28666
|
+
* 2. 递归清理可见子项;
|
|
28667
|
+
* 3. rmdir(后端不支持目录时退回 unlink)。
|
|
28668
|
+
*
|
|
28669
|
+
* @returns 是否确认该路径已不存在;清理失败返回 false,墓碑必须保留。
|
|
28670
|
+
*/
|
|
28671
|
+
async purgePathOnBackend(fs, targetPath) {
|
|
28672
|
+
let isDir2 = false;
|
|
28673
|
+
try {
|
|
28674
|
+
const stat5 = await fs.stat(targetPath);
|
|
28675
|
+
isDir2 = typeof stat5?.isDirectory === "function" ? stat5.isDirectory() : !!(stat5 && ((stat5.mode ?? 0) & 61440) === 16384);
|
|
28676
|
+
} catch {
|
|
28677
|
+
if (!await this.pathExistsOnBackend(fs, targetPath)) return true;
|
|
28678
|
+
}
|
|
28679
|
+
if (!isDir2) {
|
|
28680
|
+
try {
|
|
28681
|
+
await fs.unlink(targetPath);
|
|
28682
|
+
} catch {
|
|
28683
|
+
}
|
|
28684
|
+
return !await this.pathExistsOnBackend(fs, targetPath);
|
|
28685
|
+
}
|
|
28686
|
+
for (const placeholder of [`${targetPath}/.keep`, `${targetPath}/.keep.mtime`]) {
|
|
28687
|
+
try {
|
|
28688
|
+
await fs.unlink(placeholder);
|
|
28689
|
+
} catch {
|
|
28690
|
+
}
|
|
28691
|
+
}
|
|
28692
|
+
let children = [];
|
|
28693
|
+
try {
|
|
28694
|
+
children = await fs.readdir(targetPath);
|
|
28695
|
+
} catch {
|
|
28696
|
+
}
|
|
28697
|
+
for (const child of children) {
|
|
28698
|
+
await this.purgePathOnBackend(fs, `${targetPath}/${child}`);
|
|
28699
|
+
}
|
|
28700
|
+
try {
|
|
28701
|
+
await fs.rmdir(targetPath);
|
|
28702
|
+
} catch {
|
|
28703
|
+
try {
|
|
28704
|
+
await fs.unlink(targetPath);
|
|
28705
|
+
} catch {
|
|
28706
|
+
}
|
|
28707
|
+
}
|
|
28708
|
+
if (await this.pathExistsOnBackend(fs, targetPath)) {
|
|
28709
|
+
for (const placeholder of [`${targetPath}/.keep`, `${targetPath}/.keep.mtime`]) {
|
|
28710
|
+
try {
|
|
28711
|
+
await fs.unlink(placeholder);
|
|
28712
|
+
} catch {
|
|
28713
|
+
}
|
|
28714
|
+
}
|
|
28715
|
+
try {
|
|
28716
|
+
await fs.rmdir(targetPath);
|
|
28717
|
+
} catch {
|
|
28718
|
+
}
|
|
28719
|
+
return !await this.pathExistsOnBackend(fs, targetPath);
|
|
28720
|
+
}
|
|
28721
|
+
return true;
|
|
28722
|
+
}
|
|
28723
|
+
/** 后端无关的存在性判断:优先 exists(),否则退化为 stat()。 */
|
|
28724
|
+
async pathExistsOnBackend(fs, path) {
|
|
28725
|
+
if (typeof fs?.exists === "function") {
|
|
28726
|
+
try {
|
|
28727
|
+
return !!await fs.exists(path);
|
|
28728
|
+
} catch {
|
|
28729
|
+
return false;
|
|
28730
|
+
}
|
|
28731
|
+
}
|
|
28732
|
+
try {
|
|
28733
|
+
await fs.stat(path);
|
|
28734
|
+
return true;
|
|
28735
|
+
} catch {
|
|
28736
|
+
return false;
|
|
28613
28737
|
}
|
|
28614
28738
|
}
|
|
28615
28739
|
/**
|
|
@@ -28765,7 +28889,13 @@ var ZenFSConfig = (() => {
|
|
|
28765
28889
|
});
|
|
28766
28890
|
}
|
|
28767
28891
|
/**
|
|
28768
|
-
* After sync: mark each tombstone as confirmed by
|
|
28892
|
+
* After sync: mark each tombstone as confirmed by replicas where the target
|
|
28893
|
+
* path was actually verified absent.
|
|
28894
|
+
*
|
|
28895
|
+
* 旧实现把所有副本无条件写进 confirmedBy,只要有一轮同步跑过就认为
|
|
28896
|
+
* "所有副本都删干净了"。但副本可能因为网络/后端限制删除失败,或只剩
|
|
28897
|
+
* `.keep` / `.keep.mtime` 占位目录;墓碑一旦被 GC,下一轮同步就把残留
|
|
28898
|
+
* 当作远端内容拉回本地。现在只确认 processTombstones 本轮真正清干净的副本。
|
|
28769
28899
|
*/
|
|
28770
28900
|
async updateTombstoneConfirmations() {
|
|
28771
28901
|
const tombstones = await this.readTombstones();
|
|
@@ -28773,11 +28903,12 @@ var ZenFSConfig = (() => {
|
|
|
28773
28903
|
let modified = 0;
|
|
28774
28904
|
for (const tombstone of tombstones) {
|
|
28775
28905
|
let changed = false;
|
|
28906
|
+
const confirmedReplicas = this.tombstoneConfirmations.get(tombstone.path);
|
|
28776
28907
|
for (const replicaId of this.replicaBackends.keys()) {
|
|
28777
|
-
if (
|
|
28778
|
-
|
|
28779
|
-
|
|
28780
|
-
|
|
28908
|
+
if (tombstone.confirmedBy.includes(replicaId)) continue;
|
|
28909
|
+
if (!confirmedReplicas?.has(replicaId)) continue;
|
|
28910
|
+
tombstone.confirmedBy.push(replicaId);
|
|
28911
|
+
changed = true;
|
|
28781
28912
|
}
|
|
28782
28913
|
if (changed) {
|
|
28783
28914
|
try {
|
|
@@ -28796,6 +28927,29 @@ var ZenFSConfig = (() => {
|
|
|
28796
28927
|
log7.log(`[ConfigRepo] updateTombstoneConfirmations: ${modified} tombstone(s) updated`);
|
|
28797
28928
|
if (modified > 0) this.invalidateTombstoneCache();
|
|
28798
28929
|
}
|
|
28930
|
+
/**
|
|
28931
|
+
* 墓碑路径是否已在所有副本上确认不存在。
|
|
28932
|
+
*
|
|
28933
|
+
* `.mtime` 侧车与 `.keep` 占位对部分后端不可见(`exists()` 恒为 false),
|
|
28934
|
+
* 因此只能相信本轮 `processTombstones` 的 unlink 结果;其余路径在 GC 前
|
|
28935
|
+
* 现场复核一次,避免"确认之后副本又把残留带了回来"。
|
|
28936
|
+
*
|
|
28937
|
+
* 任一位置仍存在就返回 false —— 墓碑必须保留,直到真正删干净。
|
|
28938
|
+
*/
|
|
28939
|
+
async allReplicasConfirmAbsent(path) {
|
|
28940
|
+
const confirmed = this.tombstoneConfirmations.get(path);
|
|
28941
|
+
const hiddenPlaceholder = isMtimeSidecar(path) || path === "/.keep" || path.endsWith("/.keep");
|
|
28942
|
+
if (hiddenPlaceholder) {
|
|
28943
|
+
for (const replicaId of this.replicaBackends.keys()) {
|
|
28944
|
+
if (!confirmed?.has(replicaId)) return false;
|
|
28945
|
+
}
|
|
28946
|
+
return true;
|
|
28947
|
+
}
|
|
28948
|
+
for (const [, replica] of this.replicaBackends) {
|
|
28949
|
+
if (await this.pathExistsOnBackend(replica.instance, path)) return false;
|
|
28950
|
+
}
|
|
28951
|
+
return true;
|
|
28952
|
+
}
|
|
28799
28953
|
/**
|
|
28800
28954
|
* GC: remove tombstones where all backends in backends.json have confirmed.
|
|
28801
28955
|
*
|
|
@@ -28809,7 +28963,7 @@ var ZenFSConfig = (() => {
|
|
|
28809
28963
|
if (tombstones.length === 0) return;
|
|
28810
28964
|
const allBackendIds = [...this.replicaBackends.keys()];
|
|
28811
28965
|
for (const tombstone of tombstones) {
|
|
28812
|
-
const allConfirmed =
|
|
28966
|
+
const allConfirmed = await this.allReplicasConfirmAbsent(tombstone.path);
|
|
28813
28967
|
if (allConfirmed) {
|
|
28814
28968
|
for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
|
|
28815
28969
|
const tombstonePath = tombstonePathFor(tombstone.path, dir);
|
|
@@ -29039,6 +29193,9 @@ var ZenFSConfig = (() => {
|
|
|
29039
29193
|
direction: SyncDirection.BiDirectional,
|
|
29040
29194
|
conflictStrategy: "source-wins",
|
|
29041
29195
|
pollIntervalMs,
|
|
29196
|
+
// 墓碑未清理前排除被删路径:副本残留(含 `.keep` 占位)不会被当成
|
|
29197
|
+
// "远端新增"再拉回本地。
|
|
29198
|
+
filter: this.syncFilter,
|
|
29042
29199
|
preSyncHook: async () => {
|
|
29043
29200
|
try {
|
|
29044
29201
|
this.invalidateTombstoneCache();
|
|
@@ -29529,6 +29686,8 @@ var ZenFSConfig = (() => {
|
|
|
29529
29686
|
{
|
|
29530
29687
|
direction: SyncDirection.BiDirectional,
|
|
29531
29688
|
conflictStrategy: "source-wins",
|
|
29689
|
+
// 墓碑未清理前排除被删路径,避免副本残留复活到本地。
|
|
29690
|
+
filter: this.syncFilter,
|
|
29532
29691
|
preSyncHook: async () => {
|
|
29533
29692
|
try {
|
|
29534
29693
|
this.invalidateTombstoneCache();
|