···237237238238 [ -f "$CID_MAP" ] || exit 0
239239240240+ # Rebuild cid-map in one pass, collecting stale entries to clean up.
241241+ # Avoids sed regex issues with special chars like [ ] in paths.
242242+ # Small race with pin-watcher's concurrent appends — any line the
243243+ # watcher writes during this loop may be lost, but will be re-added
244244+ # on the next inotify event or watcher restart.
245245+ tmp=$(${pkgs.coreutils}/bin/mktemp)
240246 removed=0
241247 while IFS=' ' read -r cid path; do
242248 [ -n "$cid" ] || continue
243243- if [ ! -e "$path" ]; then
249249+ if [ -e "$path" ]; then
250250+ echo "$cid $path" >> "$tmp"
251251+ else
244252 rel="''${path#$DOWNLOAD_ROOT/}"
245253 $IPFS files rm "$FLIX_INDEX/$rel" >/dev/null 2>&1 || true
246254 $IPFS pin rm "$cid" >/dev/null 2>&1 || true
247247- # drop this exact line from cid-map. small race with the watcher
248248- # (same tool it uses in remove_file), acceptable since reconcile
249249- # is idempotent across runs.
250250- ${pkgs.gnused}/bin/sed -i "\|^$cid $path$|d" "$CID_MAP"
251255 removed=$((removed + 1))
252256 fi
253257 done < "$CID_MAP"
254258255259 if [ "$removed" -gt 0 ]; then
260260+ ${pkgs.coreutils}/bin/mv "$tmp" "$CID_MAP"
256261 echo "reconciled: removed $removed stale entries"
257262 touch "$DIRTY_FLAG"
263263+ else
264264+ ${pkgs.coreutils}/bin/rm -f "$tmp"
258265 fi
259266 '';
260267in