-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
purge_from_cache.sh
executable file
·131 lines (113 loc) · 3.92 KB
/
purge_from_cache.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
# MIT License
#
# Copyright (c) 2017 Tobias Ellinghaus
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# make sure parsed alias will be expanded
shopt -s expand_aliases
. "$(dirname "$0")/common.sh"
if pgrep -x "darktable" > /dev/null ; then
echo "error: darktable is running, please exit first"
exit 1
fi
# default values
configdir="$HOME/.config/darktable"
cache_base="${HOME}/.cache/darktable"
library="$configdir/library.db"
dryrun=1
LIBDB=""
# remember the command line to show it in the end when not purging
commandline="$0 $*"
# handle command line arguments
while [ "$#" -ge 1 ] ; do
option="$1"
case ${option} in
-h|--help)
echo "Delete thumbnails of images that are no longer in darktable's library"
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo " -c|--cachedir <path> path to the place where darktable's thumbnail caches are stored"
echo " (default: '${cache_base}')"
echo " -d|--configdir <path> path to the darktable config directory"
echo " (default: '${configdir}')"
echo " -l|--library <path> path to the library.db"
echo " (default: '${library}')"
echo " -p|--purge actually delete the files instead of just finding them"
exit 0
;;
-l|--library)
LIBDB="$2"
shift
;;
-c|--cachedir|--cache_base)
cache_base="$2"
shift
;;
-d|--configdir)
configdir="$2"
shift
;;
-p|--purge)
dryrun=0
;;
*)
echo "warning: ignoring unknown option $option"
;;
esac
shift
done
library="$configdir/library.db"
if [ "$LIBDB" != "" ]; then
library="$LIBDB"
fi
# set the command to run for each stale file
action="echo found stale mipmap in "
if [ ${dryrun} -eq 0 ]; then
action="rm --force"
fi
# get absolute canonical path to library. needed for cache dir
library=$($ReadLink "${library}")
if [ ! -f "${library}" ]; then
echo "error: library db '${library}' doesn't exist"
exit 1
fi
# the mipmap directory matching the selected library
cache_dir="${cache_base}/mipmaps-$(printf "%s" "${library}" | sha1sum | cut --delimiter=" " --fields=1).d"
if [ ! -d "${cache_dir}" ]; then
echo "error: cache directory '${cache_dir}' doesn't exist"
exit 1
fi
# get a list of all image ids from the library
id_list=$(mktemp -t darktable-tmp.XXXXXX)
sqlite3 "${library}" "select id from images order by id" > "${id_list}"
# iterate over cached mipmaps and check for each if the image is in the db
find "${cache_dir}" -type f | while read -r mipmap; do
# get the image id from the filename
id=$(echo "${mipmap}" | sed 's,.*/\([0-9]*\).*,\1,')
# ... and delete it if it's not in the library
grep "^${id}\$" "${id_list}" > /dev/null || ${action} "${mipmap}"
done
rm --force "${id_list}"
if [ $dryrun -eq 1 ]; then
echo
echo to really remove stale thumbnails from the cache call:
echo "${commandline} --purge"
fi