From 1a7ebe167dbb13e791b901a13c804e13c5e59365 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 29 Nov 2015 02:38:28 +0000 Subject: mlib: remove non-existent entries Files in a music library may be deleted or renamed, so our library should not persist old data. Unfortunately this double-stats all files, but using a hash for temporary storage could also bloat memory/disk usage and probably isn't worth it at the moment. --- lib/dtas/mlib.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'lib/dtas/mlib.rb') diff --git a/lib/dtas/mlib.rb b/lib/dtas/mlib.rb index 9a112c9..34c0c82 100644 --- a/lib/dtas/mlib.rb +++ b/lib/dtas/mlib.rb @@ -273,6 +273,12 @@ class DTAS::Mlib cd(path) do # TODO: use parent_id if given dir = dir_vivify(@pwd.split(%r{/+}n), st.ctime.to_i) + dir_id = dir[:id] + + @db[:nodes].where(parent_id: dir_id).each do |node| + File.exist?(node[:name]) or remove_entry(node) + end + Dir.foreach('.', encoding: Encoding::BINARY) do |x| case x when '.', '..', %r{\n}n @@ -280,7 +286,7 @@ class DTAS::Mlib # mpd could not support them, either. So lets not bother for now. next else - scan_any(x, dir[:id]) + scan_any(x, dir_id) end end end @@ -393,4 +399,21 @@ class DTAS::Mlib end puts end + + def remove_entry(node) + root_id = root_node[:id] + node_id = node[:id] + q = { parent_id: node_id } + nodes = @db[:nodes] + nodes.where(q).each do |nd| + next if nd[:id] == root_id + case nd[:tlen] + when DM_DIR, DM_IGN + remove_entry(nd) + end + end + nodes.where(q).delete + @db[:comments].where(node_id: node_id).delete + nodes.where(id: node_id).delete + end end -- cgit v1.2.3-24-ge0c7