-
Notifications
You must be signed in to change notification settings - Fork 0
/
kokatu
executable file
·148 lines (116 loc) · 3.73 KB
/
kokatu
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
#!/bin/sh
#############################################
#======= C O N F I G U R A T I O N S =======#
#############################################
kokatu_db="/tmp/kokatu.db"
kokatu_start_path="/"
compression=""
rg_options="-z"
# same default optios to the ones used in updatedb [/etc/updatedb.conf]
prunefs="9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset cramfs debugfs devpts devtmpfs ecryptfs exofs ftpfs fuse fuse.encfs fuse.sshfs fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs shfs smbfs sockfs sshfs sysfs tmpfs ubifs udf usbfs vboxsf"
prunenames=".git .hg .svn"
prunepaths="/afs /media /mnt /net /sfs /tmp /udev /var/cache /var/lib/pacman/local /var/lock /var/run /var/spool /var/tmp"
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
#############################################
#=== A U X I L I A R F U N C T I O N S ===#
#############################################
__mesage() {
color=$1 && shift
tag=$1 && shift
mesg=$1 && shift
printf "${color}${tag}${NC} ${mesg}\n" "$@" >&2
}
_error() {
tag=$1 && shift
__mesage "${RED}" "$tag" "$@"
}
_exit_error() {
_error "ERROR:" "$@"
exit 1
}
_info() {
tag=$1 && shift
__mesage "${GREEN}" "$tag" "$@"
}
_check_command() {
command -v "$1" >/dev/null || _exit_error "'$1' is not installed"
}
_debug_info() {
echo "DB: $kokatu_db"
echo "Start Path: $kokatu_start_path"
echo "Compression: $compression"
}
#############################################
#============= C O M M A N D S =============#
#############################################
usage() {
_info "USAGE:" "$(basename "$0") [-d <database-path>] [-v] {[-p <start-path>] [-u] | [-c] [-i] <pattern>}"
exit 2
}
set_database() {
[ ! -d "$(dirname $1)" ] && _exit_error "Directory $(dirname $1) doesn't exist"
kokatu_db="$1"
}
set_start_path() {
[ ! -d "$(dirname $1)" ] && _exit_error "Directory $(dirname $1) doesn't exist"
kokatu_start_path="$1"
}
update_database() {
[ "$(id -u)" -ne 0 ] && _exit_error "The database update needs to be run as root"
_check_command fd
prune_options=""
for fs in $prunefs; do
value="$(rg "^$fs" /etc/mtab | awk '{printf " -E " $2}')"
[ -n "$value" ] && prune_options="$prune_options $value"
done
for names in $prunenames; do prune_options="$prune_options -E $names"; done
for paths in $prunepaths; do prune_options="$prune_options -E $paths"; done
if [ ! -z "$compression" ]; then
_check_command lz4
rm "$kokatu_db" 2>/dev/null
fd -uu $prune_options . $kokatu_start_path | lz4 -f -q - "$kokatu_db.lz4"
else
rm "$kokatu_db.lz4" 2>/dev/null
fd -uu $prune_options . $kokatu_start_path >"$kokatu_db"
fi
exit 0
}
find_string() {
_check_command rg
[ ! -z "$compression" ] && kokatu_db="$kokatu_db.lz4"
[ ! -f "$kokatu_db" ] && _exit_error "Database not found [$kokatu_db]"
rg -N $rg_options "$1" "$kokatu_db"
}
#############################################
#============ M A I N L O O P ============#
#############################################
while getopts 'dp:uciv?h' c; do
case $c in
d)
set_database "${OPTARG}"
;;
p)
set_start_path "${OPTARG}"
;;
u)
update_database
;;
c)
rg_options="$rg_options -c"
;;
i)
rg_options="$rg_options -i"
;;
v)
_debug_info
;;
h | ?)
usage
;;
esac
done
shift $((OPTIND - 1))
[ $# -eq 0 ] && _exit_error "No pattern was inputted"
find_string "$@"