From 7c1a243aad8345f1e848a95dbedd6c3af9fa977e Mon Sep 17 00:00:00 2001 From: hoelee Date: Tue, 10 Nov 2026 09:49:48 +0800 Subject: [PATCH] trash GC: measure the 72h hold from ARRIVAL, not from the original's mtime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Retiring an original is `os.replace`, which keeps the file's own mtime, so a 2014 movie replaced today carried a 2014 timestamp into the quarantine and `st_mtime > cutoff` judged it expired on arrival — measured 2026-11-10 on the owner's library: 86 of 118 quarantined entries were already "expired" the moment they landed, i.e. the promised 72-hour rollback window was 0 hours for every old file. New `trash_entry_ts()`: sidecar `ts` (written by move_to_trash, second precision) → `/` directory name taken as END of that day (a day late, never a second early) → mtime only as the last resort. `purge_trash()` now gates on it. The three safety invariants are unchanged: a sidecar whose `live` file is gone is the LAST copy and is never collected, the `.purge` stamp still throttles a directory to one sweep an hour, and deletion stays per-file (never rmtree). Tests (hevc-transcode/tests/test_engine_install_gates.py): fake-expiry helpers now backdate the sidecar `ts` too; two new pins — an old original replaced today must keep its full 72 h, and a missing sidecar may only ever make an entry look younger. --- scripts/library_run.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/scripts/library_run.py b/scripts/library_run.py index 7a2a219..d396b89 100644 --- a/scripts/library_run.py +++ b/scripts/library_run.py @@ -731,11 +731,41 @@ def restore_from_trash(trash_path, original): return True +def trash_entry_ts(path, st=None): + """这份原件**什么时候进的隔离区** —— 72 h 保留期必须从这里算,不是从文件的 mtime。 + + 为什么(2026-11-10 实测):退场用的是 `os.replace`,它保留源文件自己的 mtime。于是 + 「2014 年的老片今天被替换」在隔离区里带着 2014 年的时间戳,`st_mtime > cutoff` 一测 + 就判成「早就过期」—— 实测 118 份里 86 份是**一进隔离区就已经过期**,承诺的 72 小时 + 回滚窗对老片实际是 0 小时。反过来,刚下载几小时的新片才是真有 72 小时的那种。 + + 取值的顺序(越靠前越权威): + ① 旁注 `.json` 的 `ts`(`move_to_trash` 写下的,精确到秒); + ② 目录名 `//` —— 只到天,故取**当天结束**(宁可晚一天也不早一秒); + ③ 都没有(手工丢进去的)才退回 mtime。 + """ + try: + with open(path + ".json", encoding="utf-8") as fh: + ts = (json.load(fh) or {}).get("ts") + if ts: + return time.mktime(time.strptime(str(ts)[:19], "%Y-%m-%dT%H:%M:%S")) + except (OSError, ValueError, TypeError): + pass + day = os.path.basename(os.path.dirname(path)) + try: + return time.mktime(time.strptime(day, "%Y-%m-%d")) + 86400.0 + except ValueError: + pass + return (st.st_mtime if st is not None else os.path.getmtime(path)) + + def purge_trash(dirs, ttl_hours=TRASH_TTL_HOURS, dry=False): """回收隔离区里过期的原件 —— 但**绝不删最后一份**。 旁注里记着当初的 `live`;如果那个正式文件已经不见了,说明这正是「原件未替换」的形态 (新文件没落成,原件是唯一的副本),删掉就把可恢复的事故变成数据丢失。留着,并说清楚。 + + 过期判据走 `trash_entry_ts()`(入区时间),不走 mtime —— 见那个函数的注释。 """ stats = {"removed": 0, "bytes": 0, "kept": 0, "errors": 0, "dirs": 0} cutoff = time.time() - float(ttl_hours) * 3600.0 @@ -760,7 +790,7 @@ def purge_trash(dirs, ttl_hours=TRASH_TTL_HOURS, dry=False): st = os.stat(p) except OSError: continue - if st.st_mtime > cutoff: + if trash_entry_ts(p, st) > cutoff: continue live = "" try: