-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFile
145 lines (114 loc) · 3.51 KB
/
File
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/bash (source)
# Some utility functions for common tests
function Make_Directory() { mkdir -p "$@" ;}
function Exists() { [ -e "$1" ] ;}
function Is_Alias() { alias "$1" &> /dev/null ;}
function Is_Real_Directory() { [ -d "$1" -a ! -L "$1" ] ;}
function Is_Real_Empty_Directory() { Is_Real_Directory "$1" && Is_Empty_Directory "$1" ;}
function Is_Real_Nonempty_Directory() { Is_Real_Directory "$1" && Is_Nonempty_Directory "$1" ;}
function Is_Directory() { [ -d "$1" ] ;}
function Is_Executable() { [ -x "$1" ] ;}
function Is_File() { [ -f "$1" ] ;}
function Is_Process() { pidof "$1" &> /dev/null ;}
function Is_Readable() { [ -e "$1" -a -r "$1" ] ;}
function Is_Writable() { [ \( -e "$1" -a -w "$1" \) -o \( ! -e "$1" -a -w "$(dirname "$1")" \) ] ;}
function Is_Link() { [ -L "$1" -a -e "$1" ] ;}
function Is_Broken_Link() { [ -L "$1" -a ! -e "$1" ] ;}
function Executable_Exists_In_Path() { [ -n "$(type -p "${1}" 2>/dev/null)" ] ;}
# These functions provide basic tests to see if a file qualifies as a GoboLinux
# package or recipe. These test could (should) be extended to use 'file' to find
# out mime type and/or list the contents of the archive and look for specific
# files like 'Recipe' or 'Resources/Dependencies'
function Is_GoboLinux_Package() {
Is_File "$1" && \
test -z "$(echo "$(basename "$1")" | \
sed -r 's/^[0-9A-Za-z_.]+--[0-9A-Za-z_.]+(-r[0-9]*)?--(.+).tar.bz2/\2/' | \
sed -r '/i686/d;/ppc/d;/PPC/d')"
}
function Is_GoboLinux_Recipe() {
Is_File "$1" && \
-z "$(echo "$(basename "$1")" | \
sed -r 's/^[0-9A-Za-z_.]+--[0-9A-Za-z_.]+(-r[0-9]*)?--[rR]ecipe.tar.bz2/d' )"
}
function Is_Empty_Directory() {
local anyfiles
anyfiles=$(ls -A "$1" 2> /dev/null)
! test "$anyfiles"
}
function Is_Nonempty_Directory() {
local anyfiles
anyfiles=$(ls -A "$1" 2> /dev/null)
[ -d "$1" -a -n "$anyfiles" ]
}
function Assert_Dir() {
local dir=$1
if ! Is_Directory "$dir"
then
Quiet mkdir -p "$dir" || \
mkdir -p "$dir"
if [ "$?" != "0" ]
then
Die "Unable to create $dir"
fi
chmod a+rx "$dir"
fi
}
# Returns the given filename if it does not exist,
# or a variation if it does.
# (not perfect due to multiuser/multitasking)
function Unique_Name() {
if [ ! -e "$1" ]
then
echo "$1"
return
fi
local i=1
while [ -e "$1-$i" ]
do i=$[i+1]
done
echo "$1-$i"
}
# Returns a new temporary file
function Temporary_File() {
mktemp "$goboTemp/$scriptName.temp.XXXXXXXX"
}
# Returns a new temporary directory
function Temporary_Dir() {
mktemp -d "$goboTemp/$scriptName.temp.XXXXXXXX"
}
# Returns line $1 of file $2
function Read_Line() {
head -n "$1" "$2" | tail -n 1
}
# Count lines of file $1 avoiding the "is-last-line-empty" problem
function Count_Lines() {
cat -n "$1" | tail -1 | cut -b 1-7
}
function Is_Extension() {
Parameters "$@" extension filename
[ "`basename "$filename" "$extension"`" != "`basename "$filename"`" ]
}
#detsch, 23/08/2004
function Is_URL() {
[ "`echo "$1" | cut -b-5`" = "http:" -o \
"`echo "$1" | cut -b-6`" = "https:" -o \
"`echo "$1" | cut -b-4`" = "ftp:" ]
}
#calica 14/09/2004 -
function Line_Match() {
Exists "$1" || return $?
cat "$1" | grep -E -e "^.*$2.*$" 2>&1 >/dev/null
[ "$?" = "0" ]
}
function Get_Size() {
Parameters "$@" file
ls -l "$file" | awk '{print $5}'
}
function Get_MD5() {
Parameters "$@" file
md5sum "$file" | awk '{print $1}'
}
function Get_SHA() {
Parameters "$@" file
sha256sum "$file" | awk '{print $1}'
}