• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#***********************************************************************************************************
3# Encoder binary comparison test model
4#           -- Compared with benchmark version using SHA-1 string
5#           -- Test bit stream under folder  openh264/res
6#           -- SHA-1 string of benchmark version for  all cases  of all bit streams
7#               under folder  openh264/test/encoder_binary_comparion/SHA1Table
8#           -- For more detail,please refer to file AboutTest.
9#
10#brief:
11#           -- Usage: ./run_SafeDelete.sh  $DeleteItermPath
12#                eg:    1  ./run_SafeDelete.sh  tempdata.info        --->delete only one file
13#                eg:    2  ./run_SafeDelete.sh  ../TempDataFolder    --->delete entire folder
14#                          ./run_SafeDelete.sh  /opt/TempData/ABC    --->delete entire folder ABC
15#
16# date:    10/06/2014 Created
17#***********************************************************************************************************
18#usage: runGetFileName    $FilePath
19runGetFileName()
20{
21    #parameter check!
22    if [ ! $# -eq 1  ]
23    then
24        echo "usage: runGetFileName  \$FilePath"
25        return 1
26     fi
27
28     local PathInfo=$1
29     local FileName=""
30
31    if [[  $PathInfo  =~ ^"/"  ]]
32     then
33        FileName=` echo ${PathInfo} | awk 'BEGIN {FS="/"}; {print $NF}'`
34        echo "${FileName}"
35        return 0
36     elif [[  $PathInfo  =~ ^".."  ]]
37     then
38        FileName=` echo ${PathInfo} | awk 'BEGIN {FS="/"}; {print $NF}'`
39        echo "${FileName}"
40        return 0
41     else
42        FileName=${PathInfo}
43        echo "${FileName}"
44        return 0
45     fi
46
47}
48#******************************************************************************************************
49#usage:    runGetFileFullPath  $FilePathInfo
50#eg:    current path is /opt/VideoTest/openh264/ABC
51#         runGetFileFullPath  abc.txt                  --->/opt/VideoTest/openh264/ABC
52#         runGetFileFullPath  ../123.txt               --->/opt/VideoTest/openh264
53#         runGetFileFullPath  /opt/VieoTest/456.txt    --->/opt/VieoTest
54runGetFileFullPath()
55{
56    #parameter check!
57    if [ ! $# -eq 1  ]
58    then
59        echo "usage:  runGetFileFullPath  \$FilePathInfo "
60        return 1
61     fi
62
63     local PathInfo=$1
64     local FullPath=""
65     local CurrentDir=`pwd`
66
67     if [[  $PathInfo  =~ ^"/"  ]]
68     then
69        FullPath=`echo ${PathInfo} |awk 'BEGIN {FS="/"} {for (i=1;i<NF;i++) printf("%s/",$i)}'`
70        cd ${FullPath}
71        FullPath=`pwd`
72        cd ${CurrentDir}
73        echo "${FullPath}"
74        return 0
75     elif [[  $PathInfo  =~ ^".."  ]]
76     then
77        FullPath=`echo ${PathInfo} |awk 'BEGIN {FS="/"} {for (i=1;i<NF;i++) printf("%s/",$i)}'`
78        cd ${FullPath}
79        FullPath=`pwd`
80        cd ${CurrentDir}
81        echo "${FullPath}"
82        return 0
83     else
84        FullPath=${CurrentDir}
85        echo "${FullPath}"
86        return 0
87     fi
88}
89#******************************************************************************************************
90#usage:    runGetFolderFullPath  $FolderPathInfo
91#eg:    current path is /opt/VideoTest/openh264/ABC
92#         runGetFolderFullPat   SubFolder             --->/opt/VideoTest/openh264/ABC/ SubFolder
93#         runGetFolderFullPat  ../EFG              --->/opt/VideoTest/openh264/EFG
94#         runGetFolderFullPat  /opt/VieoTest/MyFolder    --->/opt/VieoTest/MyFolder
95runGetFolderFullPath()
96{
97    #parameter check!
98    if [ ! $# -eq 1  ]
99    then
100        echo "usage:  runGetFolderFullPath  \$FolderPathInfo  "
101        return 1
102     fi
103
104     local PathInfo=$1
105     local FullPath=""
106     local CurrentDir=`pwd`
107
108     if [[  $PathInfo  =~ ^"/"  ]]
109     then
110        FullPath=${PathInfo}
111        cd ${FullPath}
112        FullPath=`pwd`
113        cd ${CurrentDir}
114        echo "${FullPath}"
115        return 0
116     elif [[  $PathInfo  =~ ^".."  ]]
117     then
118        cd ${PathInfo}
119        FullPath=`pwd`
120        cd ${CurrentDir}
121        echo "${FullPath}"
122        return 0
123     else
124        FullPath="${CurrentDir}/${PathInfo}"
125        cd ${FullPath}
126        FullPath=`pwd`
127        cd ${CurrentDir}
128        echo "${FullPath}"
129        return 0
130     fi
131
132}
133#******************************************************************************************************
134#usage: runUserNameCheck    $whoami
135runUserNameCheck()
136{
137    #parameter check!
138    if [ ! $# -eq 1  ]
139    then
140        echo "usage: runUserNameCheck  \$whoami"
141        return 1
142     fi
143
144    local UserName=$1
145
146    if [  ${UserName} = "root"  ]
147    then
148        echo ""
149        echo "*********************************************"
150        echo "delete files under root is not allowed"
151        echo "detected by run_SafeDelete.sh"
152        return 1
153    else
154        echo ""
155        return 0
156    fi
157}
158#******************************************************************************************************
159#usage:    runFolderLocationCheck  $FullPath
160runFolderLocationCheck()
161{
162    #parameter check!
163    if [ ! $# -eq 1  ]
164    then
165        echo "usage:  runFolderLocationCheck  \$FullPath"
166        return 1
167     fi
168
169    local Location=$1
170    local FileDirDepth=`echo ${Location} | awk 'BEGIN {FS="/"} {print NF}'`
171
172    #for other non-project folder data protection
173    #eg /opt/VideoTest/DeletedItem depth=4
174    if [  $FileDirDepth -lt 5 ]
175    then
176        echo ""
177        echo "*********************************************"
178        echo "FileDepth is  $FileDirDepth, and it is less thab the minimum depth(5)"
179        echo "unsafe delete! try to delete non-project related files: $FileDir"
180        echo "detected by run_SafeDelete.sh"
181        return 1
182    fi
183
184    return 0
185}
186#******************************************************************************************************
187#usage runSafeDelete $Pathinfo
188runSafeDelete()
189{
190    #parameter check!
191    if [ ! $# -eq 1  ]
192    then
193        echo "usage runSafeDelete \$FileFullPath"
194        return 1
195     fi
196
197    local PathInfo=$1
198    local UserName=`whoami`
199    local FullPath=""
200    local DeleteIterm=""
201    local FileName=""
202
203    #user validity check
204    runUserNameCheck  ${UserName}
205    if [ ! $? -eq 0 ]
206    then
207        return 1
208    fi
209
210    #get full path
211    if [  -d $PathInfo  ]
212    then
213        FullPath=`runGetFolderFullPath  ${PathInfo} `
214    elif [ -f $PathInfo ]
215    then
216        FullPath=`runGetFileFullPath  ${PathInfo} `
217    else
218        echo "delete item does not exist"
219        echo "detected by run_SafeDelete.sh"
220        return 1
221    fi
222
223    #location  validity check
224    runFolderLocationCheck ${FullPath}
225    if [ ! $? -eq 0 ]
226    then
227        return 1
228    fi
229
230    #delete file/folder
231    if [  -d $PathInfo  ]
232    then
233        DeleteIterm=${FullPath}
234        echo "deleted folder is:  $DeleteIterm"
235        rm -rf ${DeleteIterm}
236    elif [ -f $PathInfo ]
237    then
238        FileName=`runGetFileName ${PathInfo}`
239        DeleteIterm="${FullPath}/${FileName}"
240        echo "deleted file is:  $DeleteIterm"
241        rm  ${DeleteIterm}
242    fi
243    echo ""
244
245}
246PathInfo=$1
247runSafeDelete $PathInfo
248
249
250