]> git.saurik.com Git - cydia.git/blame - Library/move.sh
Amortize linear probing with a binary search sort.
[cydia.git] / Library / move.sh
CommitLineData
c7c6384e
JF
1#!/bin/bash
2
3shopt -s extglob nullglob
4
d4b6208d
JF
5if [[ ${1:0:1} == - ]]; then
6 v=$1
7 shift 1
8else
9 v=
10fi
11
c7c6384e
JF
12function df_() {
13 free=$(df -B1 "$1")
14 free=${free% *%*}
15 free=${free%%*( )}
16 free=${free##* }
17 echo "${free}"
18}
19
20function mv_() {
21 src=$1
c7c6384e 22
bcc58607
JF
23 if [[ ! -e /var/stash ]]; then
24 mkdir -p /var/db/stash
25 /usr/libexec/cydia/setnsfpn /var/db/stash
26 ln -s -t /var /var/db/stash
27 elif [[ -d /var/stash ]]; then
28 /usr/libexec/cydia/setnsfpn /var/stash
29 fi
82e370b3 30
8994cfca
JF
31 tmp=$(mktemp -d /var/stash/_.XXXXXX)
32 dst=${tmp}/${src##*/}
33
b132a972
JF
34 chmod 755 "${tmp}"
35 chown root.admin "${tmp}"
36
8994cfca
JF
37 mkdir -- "${dst}" || {
38 rmdir -- "${tmp}"
39 exit 1
40 }
c7c6384e 41
0193c0d3
JF
42 echo -n "${src}" >"${tmp}.lnk"
43
c7c6384e
JF
44 if [[ -e ${src} ]]; then
45 chmod --reference="${src}" "${dst}"
46 chown --reference="${src}" "${dst}"
47
d4b6208d 48 cp -aT $v "${src}" "${dst}" || {
8994cfca 49 rm -rf "${tmp}"
c7c6384e
JF
50 exit 1
51 }
52
0193c0d3
JF
53 mv $v "${src}" "${src}.moved"
54 ln -s "${dst}" "${src}"
55 rm -rf $v "${src}.moved"
c7c6384e
JF
56 else
57 chmod 775 "${dst}"
58 chown root.admin "${dst}"
0193c0d3 59 ln -s "${dst}" "${src}"
c7c6384e 60 fi
c7c6384e
JF
61}
62
63function shift_() {
6d9712c4 64 dir=${1%/}
c7c6384e
JF
65
66 if [[ -d ${dir} && ! -h ${dir} ]]; then
6204f56a 67 used=$(/usr/libexec/cydia/du -bs "${dir}")
c7c6384e
JF
68 used=${used%%$'\t'*}
69 free=$(df_ /var)
70
71 if [[ $((used + 524288)) -lt ${free} ]]; then
9b76c840 72 mv_ "${dir}"
c7c6384e 73 fi
9b76c840 74 elif [[ ! -e ${dir} ]]; then
c7c6384e 75 rm -f "${dir}"
9b76c840 76 mv_ "${dir}"
c7c6384e
JF
77 fi
78}
79
80shift_ "$@"