Commit 19745580 by 岑成威

fix: 更新log

1 parent 2f107757
import { TEMP_DB_FILE, DB_FILE } from './constants.js' import { TEMP_DB_FILE, DB_FILE, ENABLE_LEGACY_FLAT_DB_MERGE } from './constants.js'
import { normalizePath } from './pathCache.js' import { normalizePath } from './pathCache.js'
import { import {
loadPathCache, loadPathCache,
...@@ -24,6 +24,11 @@ import { ...@@ -24,6 +24,11 @@ import {
import { isRestrictedInternalPath, isOwnAppStoragePath } from './pathAccess.js' import { isRestrictedInternalPath, isOwnAppStoragePath } from './pathAccess.js'
import { resolveUniappSaveDir } from './docMediaMerge.js' import { resolveUniappSaveDir } from './docMediaMerge.js'
import { verifyAndRepairRecordMediaPaths } from './recordMediaVerify.js' import { verifyAndRepairRecordMediaPaths } from './recordMediaVerify.js'
import {
planAlternateScan,
markAlternateRootHandled,
resetAlternateHandledForForce
} from './mergeState.js'
import { logStep, logStepFail, waitForPlus } from './log.js' import { logStep, logStepFail, waitForPlus } from './log.js'
const STAGE = { const STAGE = {
...@@ -49,16 +54,17 @@ async function safeRemoveDocRelative(relativePath, label) { ...@@ -49,16 +54,17 @@ async function safeRemoveDocRelative(relativePath, label) {
return ok return ok
} }
/** @returns {'restricted'|'copy_failed'|'empty'|'merged'|'merge_failed'|'error'} */
async function mergeFromDbFile(dbPath, label, mergePkg) { async function mergeFromDbFile(dbPath, label, mergePkg) {
const tag = label || dbPath const tag = label || dbPath
if (!dbPath) { if (!dbPath) {
logStepFail(STAGE.DB, '跳过:数据库路径为空', { 标签: tag }) logStepFail(STAGE.DB, '跳过:数据库路径为空', { 标签: tag })
return return 'error'
} }
if (isRestrictedInternalPath(dbPath, mergePkg)) { if (isRestrictedInternalPath(dbPath, mergePkg)) {
logStepFail(STAGE.DB, '跳过:非本应用内部路径', { 路径: dbPath, 标签: tag }) logStepFail(STAGE.DB, '跳过:非本应用内部路径', { 路径: dbPath, 标签: tag })
return return 'restricted'
} }
const hint = isOwnAppStoragePath(dbPath, mergePkg) ? '本应用' : '其它' const hint = isOwnAppStoragePath(dbPath, mergePkg) ? '本应用' : '其它'
...@@ -78,7 +84,7 @@ async function mergeFromDbFile(dbPath, label, mergePkg) { ...@@ -78,7 +84,7 @@ async function mergeFromDbFile(dbPath, label, mergePkg) {
来源: dbPath, 来源: dbPath,
错误: e?.message || String(e) 错误: e?.message || String(e)
}) })
return return 'copy_failed'
} }
try { try {
...@@ -95,7 +101,7 @@ async function mergeFromDbFile(dbPath, label, mergePkg) { ...@@ -95,7 +101,7 @@ async function mergeFromDbFile(dbPath, label, mergePkg) {
logStep(STAGE.DB, '临时库无数据,结束本来源合并', { 标签: tag, 来源: dbPath }) logStep(STAGE.DB, '临时库无数据,结束本来源合并', { 标签: tag, 来源: dbPath })
await closeTempDb() await closeTempDb()
await safeRemoveDocRelative(TEMP_DB_FILE, '无数据清理临时库') await safeRemoveDocRelative(TEMP_DB_FILE, '无数据清理临时库')
return return 'empty'
} }
logStep(STAGE.DB, '开始向主库合并记录', { 待合并条数: count, 来源: dbPath }) logStep(STAGE.DB, '开始向主库合并记录', { 待合并条数: count, 来源: dbPath })
...@@ -121,9 +127,10 @@ async function mergeFromDbFile(dbPath, label, mergePkg) { ...@@ -121,9 +127,10 @@ async function mergeFromDbFile(dbPath, label, mergePkg) {
错误: e?.message || String(e) 错误: e?.message || String(e)
}) })
} }
} else { return 'merged'
logStepFail(STAGE.DB, '写入主库未完全成功,保留来源库', { 来源: dbPath, 标签: tag })
} }
logStepFail(STAGE.DB, '写入主库未完全成功,保留来源库', { 来源: dbPath, 标签: tag })
return 'merge_failed'
} catch (e) { } catch (e) {
logStepFail(STAGE.DB, '处理来源库异常', { logStepFail(STAGE.DB, '处理来源库异常', {
来源: dbPath, 来源: dbPath,
...@@ -140,15 +147,16 @@ async function mergeFromDbFile(dbPath, label, mergePkg) { ...@@ -140,15 +147,16 @@ async function mergeFromDbFile(dbPath, label, mergePkg) {
} catch (_) { } catch (_) {
/* ignore */ /* ignore */
} }
return 'error'
} }
} }
async function processPath(packageRoot, mergePkg, index, total) { async function processPath(packageRoot, mergePkg, currentRoot, index, total) {
logStep(STAGE.PATH, `扫描包根 (${index}/${total})`, { 包根: packageRoot }) logStep(STAGE.PATH, `扫描包根 (${index}/${total})`, { 包根: packageRoot })
if (isRestrictedInternalPath(packageRoot, mergePkg)) { if (isRestrictedInternalPath(packageRoot, mergePkg)) {
logStepFail(STAGE.PATH, '跳过:非本应用内部包根', { 包根: packageRoot }) logStepFail(STAGE.PATH, '跳过:非本应用内部包根', { 包根: packageRoot })
return return 'restricted'
} }
if (isOwnAppStoragePath(packageRoot, mergePkg)) { if (isOwnAppStoragePath(packageRoot, mergePkg)) {
...@@ -156,17 +164,31 @@ async function processPath(packageRoot, mergePkg, index, total) { ...@@ -156,17 +164,31 @@ async function processPath(packageRoot, mergePkg, index, total) {
} }
const dbPath = await findDbInPackageRoot(packageRoot) const dbPath = await findDbInPackageRoot(packageRoot)
let outcome = 'no_db'
if (dbPath) { if (dbPath) {
if (!isRestrictedInternalPath(dbPath, mergePkg)) { if (!isRestrictedInternalPath(dbPath, mergePkg)) {
await mergeFromDbFile(dbPath, packageRoot, mergePkg) outcome = await mergeFromDbFile(dbPath, packageRoot, mergePkg)
} else { } else {
logStepFail(STAGE.PATH, '发现库但路径受限,跳过合并', { 库路径: dbPath }) logStepFail(STAGE.PATH, '发现库但路径受限,跳过合并', { 库路径: dbPath })
outcome = 'restricted'
} }
} else { } else {
logStep(STAGE.PATH, '该包根下未找到 zyzs.db', { 包根: packageRoot }) logStep(STAGE.PATH, '该包根下未找到 zyzs.db', { 包根: packageRoot })
} }
logStep(STAGE.PATH, `包根扫描结束 (${index}/${total})`, { 包根: packageRoot }) const canMarkHandled = ['no_db', 'empty', 'merged', 'restricted'].includes(outcome)
if (canMarkHandled) {
markAlternateRootHandled(currentRoot, packageRoot)
logStep(STAGE.PATH, '已记录 alternate 包根为已处理', {
包根: packageRoot,
结果: outcome
})
} else {
logStep(STAGE.PATH, '未标记已处理(便于下次重试)', { 包根: packageRoot, 结果: outcome })
}
logStep(STAGE.PATH, `包根扫描结束 (${index}/${total})`, { 包根: packageRoot, 结果: outcome })
return outcome
} }
/** 主库在 apps/xxx/doc 时,包根下扁平 zyzs.db 视为遗留库 */ /** 主库在 apps/xxx/doc 时,包根下扁平 zyzs.db 视为遗留库 */
...@@ -228,7 +250,7 @@ class StoragePathMerge { ...@@ -228,7 +250,7 @@ class StoragePathMerge {
logStep(STAGE.RUN, '强制重新归并(手动触发)') logStep(STAGE.RUN, '强制重新归并(手动触发)')
} }
const task = StoragePathMerge.executeRun() const task = StoragePathMerge.executeRun({ force })
StoragePathMerge._running = task StoragePathMerge._running = task
try { try {
...@@ -239,7 +261,7 @@ class StoragePathMerge { ...@@ -239,7 +261,7 @@ class StoragePathMerge {
} }
} }
static async executeRun() { static async executeRun({ force = false } = {}) {
logStep(STAGE.RUN, '======== 路径合并开始 ========') logStep(STAGE.RUN, '======== 路径合并开始 ========')
logStep(STAGE.RUN, '目标:外置 emulated ↔ 内部 /data/data、/data/user') logStep(STAGE.RUN, '目标:外置 emulated ↔ 内部 /data/data、/data/user')
...@@ -285,40 +307,51 @@ class StoragePathMerge { ...@@ -285,40 +307,51 @@ class StoragePathMerge {
条目: cache.map(c => ({ 路径: c.path, 当前: c.isCurrent })) 条目: cache.map(c => ({ 路径: c.path, 当前: c.isCurrent }))
}) })
const paths = getNonCurrentPaths(cache) const nonCurrentItems = getNonCurrentPaths(cache)
const scanList = paths.map(p => p.path) const allAlternateRoots = nonCurrentItems.map(p => p.path)
const appId = getAppId()
const alternateSaveDirs = scanList.map(root => resolveUniappSaveDir(root, appId)) if (force) {
resetAlternateHandledForForce(currentRoot)
}
logStep(STAGE.RUN, '待扫描非当前包根', { const scanPlan = planAlternateScan(currentRoot, nonCurrentItems, force)
数量: paths.length, logStep(STAGE.RUN, 'alternate 包根扫描计划', {
路径列表: scanList, 说明: '均为当前 app 在不同沙箱下的包根(外置 /data/data /data/user),非其它应用',
备用录音目录: alternateSaveDirs 全部非当前包根: allAlternateRoots,
计划: scanPlan
}) })
const total = paths.length const appId = getAppId()
const alternateSaveDirs = allAlternateRoots.map(root => resolveUniappSaveDir(root, appId))
if (scanPlan.skip) {
logStep(STAGE.RUN, '跳过 alternate 库扫描', { 原因: scanPlan.reason })
} else {
const scanList = scanPlan.paths
const total = scanList.length
let idx = 0 let idx = 0
for (const item of paths) { for (const packageRoot of scanList) {
idx++ idx++
try { try {
await processPath(item.path, mergePkg, idx, total) await processPath(packageRoot, mergePkg, currentRoot, idx, total)
} catch (e) { } catch (e) {
logStepFail(STAGE.PATH, `包根扫描异常 (${idx}/${total})`, { logStepFail(STAGE.PATH, `包根扫描异常 (${idx}/${total})`, {
包根: item.path, 包根: packageRoot,
错误: e?.message || String(e) 错误: e?.message || String(e)
}) })
} }
} }
if (total === 0) {
logStep(STAGE.RUN, '无非当前包根需扫描,跳过 alternate 库合并')
} }
if (ENABLE_LEGACY_FLAT_DB_MERGE) {
try { try {
await processLegacyFlatDbAtCurrentRoot(currentRoot, mergePkg) await processLegacyFlatDbAtCurrentRoot(currentRoot, mergePkg)
} catch (e) { } catch (e) {
logStepFail(STAGE.PATH, '遗留扁平库检查异常', { 错误: e?.message || String(e) }) logStepFail(STAGE.PATH, '遗留扁平库检查异常', { 错误: e?.message || String(e) })
} }
} else {
logStep(STAGE.PATH, '已关闭:当前包根遗留扁平库检测(ENABLE_LEGACY_FLAT_DB_MERGE=false)')
}
try { try {
logStep(STAGE.MEDIA, '======== 主库媒体路径校验开始 ========') logStep(STAGE.MEDIA, '======== 主库媒体路径校验开始 ========')
......
export const PATH_CACHE_KEY = 'zyzs_storage_path_cache' export const PATH_CACHE_KEY = 'zyzs_storage_path_cache'
/** 记录「当前包根 + 已处理过的 alternate 包根」,避免未切换时每次启动重复扫库 */
export const MERGE_STATE_KEY = 'zyzs_storage_merge_state'
export const DB_FILE = 'zyzs.db' export const DB_FILE = 'zyzs.db'
export const TEMP_DB_FILE = '_zyzs_temp_.db' export const TEMP_DB_FILE = '_zyzs_temp_.db'
export const MAIN_DB_NAME = 'zyzs' export const MAIN_DB_NAME = 'zyzs'
...@@ -13,3 +15,6 @@ export const TEMP_DB_PATH = `_doc/${TEMP_DB_FILE}` ...@@ -13,3 +15,6 @@ export const TEMP_DB_PATH = `_doc/${TEMP_DB_FILE}`
/** 与 PathUtils.SAVE_DIR_NAME 一致 */ /** 与 PathUtils.SAVE_DIR_NAME 一致 */
export const UNIAPP_SAVE_DIR = 'uniapp_save' export const UNIAPP_SAVE_DIR = 'uniapp_save'
/** 是否检测当前包根下扁平 zyzs.db(包根/zyzs.db,非 apps/.../doc) */
export const ENABLE_LEGACY_FLAT_DB_MERGE = false
import { MERGE_STATE_KEY } from './constants.js'
import { normalizePath } from './pathCache.js'
function emptyState() {
return { anchoredRoot: '', handledAlternates: [] }
}
export function loadMergeState() {
try {
const raw = uni.getStorageSync(MERGE_STATE_KEY)
if (!raw) return emptyState()
const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw
if (!parsed || typeof parsed !== 'object') return emptyState()
return {
anchoredRoot: normalizePath(parsed.anchoredRoot || ''),
handledAlternates: (parsed.handledAlternates || [])
.map(normalizePath)
.filter(Boolean)
}
} catch (_) {
return emptyState()
}
}
function saveMergeState(state) {
uni.setStorageSync(MERGE_STATE_KEY, JSON.stringify(state))
}
/** 强制归并时清空「已处理 alternate」,便于重新扫描 */
export function resetAlternateHandledForForce(currentRoot) {
const root = normalizePath(currentRoot)
saveMergeState({ anchoredRoot: root, handledAlternates: [] })
}
/**
* 包根切换 → 清空已处理列表;未切换且全部 alternate 已处理 → 可跳过扫描
* @param {string} currentRoot 当前运行包根
* @param {{ path: string }[]} nonCurrentItems getNonCurrentPaths 结果
* @param {boolean} force 手动强制归并
*/
export function planAlternateScan(currentRoot, nonCurrentItems, force) {
const root = normalizePath(currentRoot)
const all = (nonCurrentItems || []).map(item => normalizePath(item.path)).filter(Boolean)
if (force) {
return {
skip: false,
paths: all,
reason: '手动强制归并,重新扫描全部 alternate 包根'
}
}
if (!root || all.length === 0) {
return { skip: true, paths: [], reason: '无 alternate 包根需扫描' }
}
let state = loadMergeState()
const anchored = normalizePath(state.anchoredRoot)
if (anchored !== root) {
state = { anchoredRoot: root, handledAlternates: [] }
saveMergeState(state)
return {
skip: false,
paths: all,
reason: '包根已切换,重新扫描 alternate',
previousRoot: anchored || '(无)'
}
}
const handledSet = new Set(state.handledAlternates)
const pending = all.filter(p => !handledSet.has(p))
if (pending.length === 0) {
return {
skip: true,
paths: [],
reason: '当前包根未切换,本应用 alternate 包根均已处理过',
anchoredRoot: root,
handledAlternates: [...handledSet]
}
}
return {
skip: false,
paths: pending,
reason: '存在尚未处理的 alternate 包根',
pending,
handledAlternates: [...handledSet]
}
}
/** 探测/合并结束且无硬错误时,标记该 alternate 包根已处理(仅针对当前 anchoredRoot) */
export function markAlternateRootHandled(currentRoot, alternateRoot) {
const root = normalizePath(currentRoot)
const alt = normalizePath(alternateRoot)
if (!root || !alt || root === alt) return
let state = loadMergeState()
if (normalizePath(state.anchoredRoot) !== root) {
state = { anchoredRoot: root, handledAlternates: [] }
}
const set = new Set(state.handledAlternates)
set.add(alt)
state.handledAlternates = [...set]
saveMergeState(state)
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!