]> git.saurik.com Git - cydia.git/blob - Library/move.sh
Do not crash if Safari's Info.plist can't be read.
[cydia.git] / Library / move.sh
1 #!/bin/bash
2
3 shopt -s extglob nullglob
4
5 if [[ ${1:0:1} == - ]]; then
6 v=$1
7 shift 1
8 else
9 v=
10 fi
11
12 function df_() {
13 free=$(df -B1 "$1")
14 free=${free% *%*}
15 free=${free%%*( )}
16 free=${free##* }
17 echo "${free}"
18 }
19
20 function mv_() {
21 src=$1
22
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
30
31 tmp=$(mktemp -d /var/stash/_.XXXXXX)
32 dst=${tmp}/${src##*/}
33
34 chmod 755 "${tmp}"
35 chown root.admin "${tmp}"
36
37 mkdir -- "${dst}" || {
38 rmdir -- "${tmp}"
39 exit 1
40 }
41
42 echo -n "${src}" >"${tmp}.lnk"
43
44 if [[ -e ${src} ]]; then
45 chmod --reference="${src}" "${dst}"
46 chown --reference="${src}" "${dst}"
47
48 cp -aT $v "${src}" "${dst}" || {
49 rm -rf "${tmp}"
50 exit 1
51 }
52
53 mv $v "${src}" "${src}.moved"
54 ln -s "${dst}" "${src}"
55 rm -rf $v "${src}.moved"
56 else
57 chmod 775 "${dst}"
58 chown root.admin "${dst}"
59 ln -s "${dst}" "${src}"
60 fi
61 }
62
63 function shift_() {
64 dir=${1%/}
65
66 if [[ -d ${dir} && ! -h ${dir} ]]; then
67 used=$(/usr/libexec/cydia/du -bs "${dir}")
68 used=${used%%$'\t'*}
69 free=$(df_ /var)
70
71 if [[ $((used + 524288)) -lt ${free} ]]; then
72 mv_ "${dir}"
73 fi
74 elif [[ ! -e ${dir} ]]; then
75 rm -f "${dir}"
76 mv_ "${dir}"
77 fi
78 }
79
80 shift_ "$@"