File: run_SafeDelete.sh

package info (click to toggle)
openh264 2.6.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,120 kB
  • sloc: cpp: 74,004; asm: 34,842; ansic: 23,866; sh: 2,540; python: 937; objc: 612; cs: 471; makefile: 354; java: 319; xml: 204; javascript: 17
file content (249 lines) | stat: -rwxr-xr-x 7,057 bytes parent folder | download | duplicates (6)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/bin/bash
#***********************************************************************************************************
# Encoder binary comparison test model
#           -- Compared with benchmark version using SHA-1 string
#           -- Test bit stream under folder  openh264/res
#           -- SHA-1 string of benchmark version for  all cases  of all bit streams
#               under folder  openh264/test/encoder_binary_comparion/SHA1Table
#           -- For more detail,please refer to file AboutTest.
#
#brief:
#           -- Usage: ./run_SafeDelete.sh  $DeleteItermPath
#                eg:    1  ./run_SafeDelete.sh  tempdata.info        --->delete only one file
#                eg:    2  ./run_SafeDelete.sh  ../TempDataFolder    --->delete entire folder
#                          ./run_SafeDelete.sh  /opt/TempData/ABC    --->delete entire folder ABC
#
# date:    10/06/2014 Created
#***********************************************************************************************************
#usage: runGetFileName    $FilePath
runGetFileName()
{
    #parameter check!
    if [ ! $# -eq 1  ]
    then
        echo "usage: runGetFileName  \$FilePath"
        return 1
     fi

     local PathInfo=$1
     local FileName=""

    if [[  $PathInfo  =~ ^"/"  ]]
     then
        FileName=` echo ${PathInfo} | awk 'BEGIN {FS="/"}; {print $NF}'`
        echo "${FileName}"
        return 0
     elif [[  $PathInfo  =~ ^".."  ]]
     then
        FileName=` echo ${PathInfo} | awk 'BEGIN {FS="/"}; {print $NF}'`
        echo "${FileName}"
        return 0
     else
        FileName=${PathInfo}
        echo "${FileName}"
        return 0
     fi

}
#******************************************************************************************************
#usage:    runGetFileFullPath  $FilePathInfo
#eg:    current path is /opt/VideoTest/openh264/ABC
#         runGetFileFullPath  abc.txt                  --->/opt/VideoTest/openh264/ABC
#         runGetFileFullPath  ../123.txt               --->/opt/VideoTest/openh264
#         runGetFileFullPath  /opt/VieoTest/456.txt    --->/opt/VieoTest
runGetFileFullPath()
{
    #parameter check!
    if [ ! $# -eq 1  ]
    then
        echo "usage:  runGetFileFullPath  \$FilePathInfo "
        return 1
     fi

     local PathInfo=$1
     local FullPath=""
     local CurrentDir=`pwd`

     if [[  $PathInfo  =~ ^"/"  ]]
     then
        FullPath=`echo ${PathInfo} |awk 'BEGIN {FS="/"} {for (i=1;i<NF;i++) printf("%s/",$i)}'`
        cd ${FullPath}
        FullPath=`pwd`
        cd ${CurrentDir}
        echo "${FullPath}"
        return 0
     elif [[  $PathInfo  =~ ^".."  ]]
     then
        FullPath=`echo ${PathInfo} |awk 'BEGIN {FS="/"} {for (i=1;i<NF;i++) printf("%s/",$i)}'`
        cd ${FullPath}
        FullPath=`pwd`
        cd ${CurrentDir}
        echo "${FullPath}"
        return 0
     else
        FullPath=${CurrentDir}
        echo "${FullPath}"
        return 0
     fi
}
#******************************************************************************************************
#usage:    runGetFolderFullPath  $FolderPathInfo
#eg:    current path is /opt/VideoTest/openh264/ABC
#         runGetFolderFullPat   SubFolder             --->/opt/VideoTest/openh264/ABC/ SubFolder
#         runGetFolderFullPat  ../EFG              --->/opt/VideoTest/openh264/EFG
#         runGetFolderFullPat  /opt/VieoTest/MyFolder    --->/opt/VieoTest/MyFolder
runGetFolderFullPath()
{
    #parameter check!
    if [ ! $# -eq 1  ]
    then
        echo "usage:  runGetFolderFullPath  \$FolderPathInfo  "
        return 1
     fi

     local PathInfo=$1
     local FullPath=""
     local CurrentDir=`pwd`

     if [[  $PathInfo  =~ ^"/"  ]]
     then
        FullPath=${PathInfo}
        cd ${FullPath}
        FullPath=`pwd`
        cd ${CurrentDir}
        echo "${FullPath}"
        return 0
     elif [[  $PathInfo  =~ ^".."  ]]
     then
        cd ${PathInfo}
        FullPath=`pwd`
        cd ${CurrentDir}
        echo "${FullPath}"
        return 0
     else
        FullPath="${CurrentDir}/${PathInfo}"
        cd ${FullPath}
        FullPath=`pwd`
        cd ${CurrentDir}
        echo "${FullPath}"
        return 0
     fi

}
#******************************************************************************************************
#usage: runUserNameCheck    $whoami
runUserNameCheck()
{
    #parameter check!
    if [ ! $# -eq 1  ]
    then
        echo "usage: runUserNameCheck  \$whoami"
        return 1
     fi

    local UserName=$1

    if [  ${UserName} = "root"  ]
    then
        echo ""
        echo "*********************************************"
        echo "delete files under root is not allowed"
        echo "detected by run_SafeDelete.sh"
        return 1
    else
        echo ""
        return 0
    fi
}
#******************************************************************************************************
#usage:    runFolderLocationCheck  $FullPath
runFolderLocationCheck()
{
    #parameter check!
    if [ ! $# -eq 1  ]
    then
        echo "usage:  runFolderLocationCheck  \$FullPath"
        return 1
     fi

    local Location=$1
    local FileDirDepth=`echo ${Location} | awk 'BEGIN {FS="/"} {print NF}'`

    #for other non-project folder data protection
    #eg /opt/VideoTest/DeletedItem depth=4
    if [  $FileDirDepth -lt 5 ]
    then
        echo ""
        echo "*********************************************"
        echo "FileDepth is  $FileDirDepth, and it is less thab the minimum depth(5)"
        echo "unsafe delete! try to delete non-project related files: $FileDir"
        echo "detected by run_SafeDelete.sh"
        return 1
    fi

    return 0
}
#******************************************************************************************************
#usage runSafeDelete $Pathinfo
runSafeDelete()
{
    #parameter check!
    if [ ! $# -eq 1  ]
    then
        echo "usage runSafeDelete \$FileFullPath"
        return 1
     fi

    local PathInfo=$1
    local UserName=`whoami`
    local FullPath=""
    local DeleteIterm=""
    local FileName=""

    #user validity check
    runUserNameCheck  ${UserName}
    if [ ! $? -eq 0 ]
    then
        return 1
    fi

    #get full path
    if [  -d $PathInfo  ]
    then
        FullPath=`runGetFolderFullPath  ${PathInfo} `
    elif [ -f $PathInfo ]
    then
        FullPath=`runGetFileFullPath  ${PathInfo} `
    else
        echo "delete item does not exist"
        echo "detected by run_SafeDelete.sh"
        return 1
    fi

    #location  validity check
    runFolderLocationCheck ${FullPath}
    if [ ! $? -eq 0 ]
    then
        return 1
    fi

    #delete file/folder
    if [  -d $PathInfo  ]
    then
        DeleteIterm=${FullPath}
        echo "deleted folder is:  $DeleteIterm"
        rm -rf ${DeleteIterm}
    elif [ -f $PathInfo ]
    then
        FileName=`runGetFileName ${PathInfo}`
        DeleteIterm="${FullPath}/${FileName}"
        echo "deleted file is:  $DeleteIterm"
        rm  ${DeleteIterm}
    fi
    echo ""

}
PathInfo=$1
runSafeDelete $PathInfo