Truncate cache file on duplicate

This commit is contained in:
Chris Down 2018-02-20 11:03:35 +00:00
parent 1896ceaccc
commit 4e969bd67d

View file

@ -104,6 +104,8 @@ fi
mkdir -p -m0700 "$cache_dir" mkdir -p -m0700 "$cache_dir"
declare -A last_data declare -A last_data
declare -A last_filename
declare -A last_cache_file_output
command -v clipnotify >/dev/null 2>&1 && has_clipnotify=1 command -v clipnotify >/dev/null 2>&1 && has_clipnotify=1
@ -139,6 +141,7 @@ while true; do
fi fi
for selection in "${cm_selections[@]}"; do for selection in "${cm_selections[@]}"; do
cache_file=${cache_file_prefix}_$selection
data=$(_xsel -o --"$selection"; printf x) data=$(_xsel -o --"$selection"; printf x)
debug "Data before stripping: $data" debug "Data before stripping: $data"
@ -160,6 +163,7 @@ while true; do
continue continue
fi fi
# If we were in the middle of doing a selection when the previous poll # If we were in the middle of doing a selection when the previous poll
# ran, then we may have got a partial clip. # ran, then we may have got a partial clip.
possible_partial=${last_data[$selection]} possible_partial=${last_data[$selection]}
@ -167,29 +171,34 @@ while true; do
[[ $possible_partial && $data == *"$possible_partial" ]]; then [[ $possible_partial && $data == *"$possible_partial" ]]; then
debug "$possible_partial is a possible partial of $data" debug "$possible_partial is a possible partial of $data"
debug "Removing ${last_filename[$selection]}" debug "Removing ${last_filename[$selection]}"
previous_size=$(wc -c <<< "${last_cache_file_output[$selection]}")
truncate -s -"$previous_size" "$cache_file"
rm -- "${last_filename[$selection]}" rm -- "${last_filename[$selection]}"
fi fi
last_data[$selection]=$data last_data[$selection]=$data
last_filename[$selection]=$filename
first_line=$(get_first_line "$data") first_line=$(get_first_line "$data")
debug "New clipboard entry on $selection selection: \"$first_line\"" debug "New clipboard entry on $selection selection: \"$first_line\""
cache_file=${cache_file_prefix}_$selection
# Without checking ${last_data[any]}, we often double write since both # Without checking ${last_data[any]}, we often double write since both
# selections get the same content # selections get the same content
cache_file_output="$(date +%s) $first_line"
if [[ ${last_data[any]} != "$data" ]]; then if [[ ${last_data[any]} != "$data" ]]; then
filename="$cache_dir/$(cksum <<< "$first_line")" filename="$cache_dir/$(cksum <<< "$first_line")"
debug "Writing $data to $filename" debug "Writing $data to $filename"
printf '%s' "$data" > "$filename" printf '%s' "$data" > "$filename"
debug "Writing $first_line to $cache_file" debug "Writing $cache_file_output to $cache_file"
printf '%d %s\n' "$(date +%s)" "$first_line" >> "$cache_file" printf '%s\n' "$cache_file_output" >> "$cache_file"
fi fi
last_data[any]=$data last_data[any]=$data
last_cache_file_output[$selection]=$cache_file_output
if (( CM_OWN_CLIPBOARD )) && [[ $selection != primary ]] && if (( CM_OWN_CLIPBOARD )) && [[ $selection != primary ]] &&
element_in clipboard "${cm_selections[@]}"; then element_in clipboard "${cm_selections[@]}"; then