-
Notifications
You must be signed in to change notification settings - Fork 0
/
ota_img_data_provider.sh
149 lines (121 loc) · 2.54 KB
/
ota_img_data_provider.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/sh
# 注意:
# 本文件获取img_name000N的数据直接输出
# 所以只能用echo_err来输出打印信息,不能用echo,否则将导致镜像错误
echo_err()
{
echo $* 1>&2
}
# 获得字符串中的第几个word
get_word()
{
local str="$1"
local n=$2
local i=0
for word in $str
do
if [ $i = $n ]; then
echo $word
return 0
fi
let i=i+1
done
return 1
}
# 获得文件大小
size_file()
{
local file=$1
local result
result="`ls -l -L $file`"
if [ "$?" != "0" ]; then
echo_err "ls failed: $file"
return 1
fi
result=`get_word "$result" 4`
if [ "result" = "" ]; then
echo_err "ls failed 2: $file"
return 1
fi
echo $result
return 0
}
# 检查文件是否存在,以及是否为空
check_file()
{
local file=$1
local size
if [ ! -e "$file" ]; then
echo_err "error: $file not exist"
return 1
fi
size=`size_file $file`
if [ $? != 0 ]; then
echo_err "error: failed to get size: $file"
return 1
fi
if [ $size = 0 ]; then
echo_err "error: do not support empty file: $file"
return 1
fi
return 0
}
set_write_ok()
{
echo "ota ok: $img_name $img_size" > $img_name.ok
}
set_write_failed()
{
echo "ota ok: $1" > $img_name.failed
}
img_name=$1
img_size=$2
if [ "$img_name" = "" ]; then
echo_err "img_name is not set"
exit 1
fi
if [ "$img_size" = "" ]; then
echo_err "img_size is not set"
exit 1
fi
i=0
total_size=0
while true;
do
num=`printf "%04d" $i`
file=$img_name.$num
# 等待 /tmp/ota/img_name000i.ok
# 或者 /tmp/ota/img_name.quit 退出
while true;
do
if [ -e $file.ok ]; then
break
fi
if [ -e $img_name.quit ]; then
echo_err "get $img_name.quit, now quit"
set_write_failed "ota quit"
exit 1
fi
sleep 0.01
done
# 检查 /tmp/ota/img_name000i
check_file $file
if [ $? != 0 ]; then
echo_err "failed to check $file"
set_write_failed "check file $file"
exit 1
fi
size=`size_file $file`
# 输出 /tmp/ota/img_name000i
cat $file
# 创建 /tmp/ota/img_name000i.done
echo $size > $file.done
# 如果 文件大小完成,那么退出
let i=i+1
let total_size=total_size+size
if [ $total_size -ge $img_size ]; then
echo_err "$img_name read ok, now quit"
set_write_ok
exit 0
fi
done