-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh
executable file
·145 lines (136 loc) · 4.44 KB
/
refresh
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/sh
set -eu
echo__() {
printf "%s\n" "$1"
}
linux_is_removable() {
dev="$(echo__ "$1" | /usr/bin/sed -E 's/[0-9]+(p[0-9]+)?$//')"
if [ "$dev" = "mmcblk" ]; then
echo 1
return
fi
removable_flag=/sys/block/"$dev"/removable
if [ -r "$removable_flag" ]; then
/usr/bin/cat -- "$removable_flag"
else
echo 0
fi
}
linux_mount() {
dev_name="${1}"
point_dir="${2}"
point="${2}/${3}"
mount="${2}/mount_${3}"
umount="${2}/umount_${3}"
echo "#!/bin/sh" > "$mount"
{
echo "set -eu"
echo ""
echo "echo__() {"
echo " printf \"%s\\n\" \"\$1\""
echo "}"
echo ""
echo "on_mount() {"
echo " echo \"#!/bin/sh\" > \"$umount\""
echo " {"
echo " echo \"set -eu\""
echo " echo \"/usr/bin/pumount -- \\\"$point\\\" || { /bin/sleep 3; exit 1; }\""
echo " echo \"/bin/rm -f -- \\\"$point\\\"\""
echo " echo \"/bin/rm -f -- \\\"$umount\\\"\""
echo " } >> \"$umount\""
echo " /bin/chmod a+x -- \"$umount\""
echo " echo__ \"/dev/$dev_name -> $point\""
echo "}"
echo ""
echo "if ! [ -e \"$point\" ]; then"
echo " if /usr/bin/pmount -A -c utf8 -- /dev/\"$dev_name\" 2>/dev/null; then"
echo " /bin/mkdir -p -- \"$point_dir\" \\"
echo " && /bin/ln -s -- /media/\"$dev_name\" \"$point\" \\"
echo " && on_mount"
echo " fi"
echo "else"
echo " echo__ \"Cannot mount /dev/$dev_name: $point already exist.\""
echo "fi"
echo "/bin/sleep 1"
} >> "$mount"
/bin/chmod a+x -- "$mount"
}
bsd_mount() {
dev="${1}"
point="${2}/${3}"
mount="${2}/mount_${3}"
umount="${2}/umount_${3}"
echo "#!/bin/sh" > "$mount"
{
echo "set -eu"
echo ""
echo "echo__() {"
echo " printf \"%s\\n\" \"\$1\""
echo "}"
echo ""
echo "on_mount() {"
echo " echo \"#!/bin/sh\" > \"$umount\""
echo " {"
echo " echo \"set -eu\""
echo " echo \"/sbin/umount -- \\\"$point\\\" || { read -rt 3 stub; exit 1; }\""
echo " echo \"/bin/rmdir -- \\\"$point\\\" || true\""
echo " echo \"/bin/rm -f -- \\\"$umount\\\"\""
echo " } >> \"$umount\""
echo " /bin/chmod a+x \"$umount\""
echo " echo__ \"$dev -> $point\""
echo "}"
echo ""
echo "if { /usr/local/bin/ntfsinfo -m \"$dev\" > /dev/null 2>&1; }; then"
echo " if /bin/mkdir -p -- \"$point\"; then"
echo " if /sbin/mount_fusefs -- auto \"$point\" ntfs-3g \"$dev\" \"$point\"; then"
echo " on_mount"
echo " else"
echo " /bin/rmdir -- \"$point\" || true"
echo " fi"
echo " fi"
echo "elif /usr/bin/file -Ls -- \"$dev\" | /usr/bin/grep -q FAT; then"
echo " if /bin/mkdir -p -- \"$point\"; then"
echo " if /sbin/mount_msdosfs -L \"\$LANG\" -D CP866 -o rw -- \"$dev\" \"$point\"; then"
echo " on_mount"
echo " else"
echo " /bin/rmdir -- \"$point\" || true"
echo " fi"
echo " fi"
echo "fi"
echo "read -rt 1 stub"
} >> "$mount"
/bin/chmod a+x "$mount"
}
if [ -x /bin/uname ]; then
system="$(/bin/uname)"
elif [ -x /usr/bin/uname ]; then
system="$(/usr/bin/uname)"
else
echo "Cannot execute 'uname'."
exit 1
fi
point_dir="$(dirname "$(realpath "$0")")"
case "$system" in
Linux)
/bin/rm -f -- "$point_dir"/mount_*
dev_list=$(/usr/bin/find /dev -maxdepth 1 -regextype egrep \
-regex '/dev/(([sh]d[a-z]+[0-9]*)|(mmcblk[0-9]+p[0-9]+))' -print | /usr/bin/sort -r)
for dev in $dev_list; do
dev_name="$(/usr/bin/basename -- "$dev")"
is_removable="$(linux_is_removable "$dev_name")"
if [ "$is_removable" = 1 ]; then
linux_mount "$dev_name" "$point_dir" "$dev_name"
fi
done
;;
FreeBSD)
/bin/rm -f -- "$point_dir"/mount_*
dev_list=$(/usr/bin/find -E /dev -depth 1 -regex '/dev/da[0-9]+([sp][0-9]+)?' -print | /usr/bin/sort -r)
for dev in $dev_list; do
dev_name="$(/usr/bin/basename -- "$dev")"
bsd_mount "$dev" "$point_dir" "$dev_name"
done
;;
*)
echo "Unknown system '$system'."
esac