This repository has been archived by the owner on Nov 20, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_bookkeeper.sh
178 lines (163 loc) · 4.32 KB
/
repo_bookkeeper.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
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
#!/bin/bash
####################################################
# GRIM REPO ########################################
####################################################
# repo_bookkeeper.sh
#
# Wrapper for database and checksum generation.
# This should be the only program talking to checksum_wrapper
# and sql_wrapper.
####################################################
source $GR_PATH/sql_wrapper.sh
source $GR_PATH/checksum_wrapper.sh
source $GR_PATH/repo_log.sh
####
# file_exists
#
# A simple function for checking whether a file or
# dir exists in the databse. Echoes (and returns)
# 1 if it exists and 0 if not.
#
# $1 - file: the file to check
function file_existed {
file=$1;
checksum=`get_checksum "$file"`;
#echo "cheksum is: $checksum" 1>&2;
if [[ "$checksum" != "" ]]; then
return 1;
else
return 0;
fi;
}
####
# has_changed_locally
#
# A simple function for checking whether a file or
# dir has changed locally since last sync. Echoes (and returns)
# 1 if it has and 0 if not.
#
# $1 - file: the file to check
function has_changed_locally {
file=$1;
#check new checksum vs. old checksum
if [[ `calc_checksum "$file"` != `get_checksum "$file"` ]]; then
return 1;
else
return 0
fi;
}
####
# has_changed_remotely
#
# A simple function for checking whether a file or
# dir has changed on server since last sync. Echoes (and returns)
# 1 if it has and 0 if not.
#
# $1 - localfile: the file to check (incl. filename)
# $2 - serverinfo: the server
# $3 - remotefile: the path (incl. filename) to remote file
function has_file_changed_remotely {
localfile=$1;
serverinfo=$2;
remotefile=$3;
#check new checksum vs. old checksum
if [[ `calc_remote_file_checksum "$serverinfo" "$remotefile"` != `get_checksum "$localfile"` ]]; then
#has changed:
return 1;
else
return 0
fi;
}
###
# has_dir_changed_remotely
#
# runs through entries from server_conflict
# that involves it, and check if they have
# changed since last time. Used when a dir
# has been locally removed
#
# $1 - $server_conflicts
# $2 - $serverinfo
# $3 - $serverroot
# $4 - $dir (dir relative to serverroot)
# $5 - $localroot
function has_dir_changed_remotely {
local server_conflicts=$1;
local serverinfo=$2;
local serverroot=$3;
local dir=$4;
local localroot=$5;
#echo "SERVERROOTdir: $dir on full list: $server_conflicts" 1>&2
#for each file from serverconflicts that is
#inside dir and is a file (does not end with)
local relevant_conflicts=`echo "$server_conflicts" | grep "^$dir" | grep -v -P '/$'`;
#loop through them
while [ `echo $relevant_conflicts | wc -w` != 0 ]; do
#take first line from list of conflicts:
local conflict=`echo "$relevant_conflicts" | head -n 1`
#then remove that line from client_conflicts
relevant_conflicts=`echo "$relevant_conflicts" | tail --lines=+2`;
log 0 "next conflict=$conflict"
#for each, do a checksum check with remote file vs. checksums
has_file_changed_remotely "$localroot$conflict" "$serverinfo" "$serverroot$conflict";
local changed=$?;
#if even a single file has changed, we consider dir changed
if [[ $changed == "1" ]]; then
return 1;
fi;
done;
#if no file has changed, we reach this point, and return 0o
return 0;
}
####
# recalculate_all
#
# Recalculates all the md5 sums in the repository,
# clears the database, and inserts the new values
#
# $1 - path: the base directory to calculate from
function recalculate_all {
path=$1;
#clear the database
clear_database;
#recursively, calculate and insert all md5-sums
calculate_dir "$path";
}
####
# calculate_dir
#
# Calculates the md5 sums of the entire dir recursivesy
# and enters data into the database
#
# $1 - path to dir inklusive dirname
function calculate_dir {
path=$1;
#go through reculive list of files and dirs
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for file in `find $path`; do
#for each entry
IFS=$SAFEIFS;
if [ -d $file ]; then
#if dir, make sure we have trailing slash
file=`echo "$file" | sed 's#\([^/]\)$#\1/#'`;
fi;
checksum=`calc_checksum "$file"`;
set_checksum "$file" "$checksum";
IFS=$(echo -en "\n\b")
done;
IFS=$SAVEIFS;
}
####
# calculate_file
#
# Calculates the md5 sums of the file
# and enters data into the database
#
# $1 - path to file inklusive filename
function calculate_file {
file=$1;
#calculate checksum
checksum=`calc_checksum "$file"`;
set_checksum "$file" "$checksum";
}