-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparator
333 lines (310 loc) · 14.6 KB
/
comparator
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
#To be able to efficiently use all the files, we have to take 1 pattern line from
#metadata file A and compare it with all the lines in the metadata file B. If there
#is a match, then the file is conserved and therefore, not written into the
#difference.txt file containing the modifications. If there are mo matches,
#The missing file is written into the .txt
#Consider : A=databaseA B=databaseB S=databaseS R=databaseRAM
#Usage : compare databaseA databaseS databaseB
compare()
{
if [ $# -lt 3 ] ; then
echo "Not enough arguments, 4 files are required"
echo "Usage : compare databaseA DB databaseB"
echo "DB is the database of the last sync"
fi
if [ ! -f "$1" ] || [ ! -f "$2" ] || [ ! -f "$3" ]; then
echo "Not a file"
fi
#Path to temp databaseRAM (equivalent of databaseS)
pathToDataBaseRAM="/dev/shm/syncTmpDataBaseS"
pathToA=`sed '1q;d' $2`
pathToB=`sed '2q;d' $2`
echo "DB A :"
cat $1
echo "DB B :"
cat $3
#Remove the 2 first lines (which contains the path to the files in A and B)
tail -n +3 $2 > "$pathToDataBaseRAM"
#Iterate in R
while IFS= read -r line2; do
echo " "
echo "DB RAM : ""$line2"
#if [[ "$(echo "$line2" | awk '{print $2}')" -eq "directory" ]]; then
# line2=$(echo "$line2" | awk '{print $2}')
#fi
#get line number of match btw A and R (actual from while)
nline1=$(awk -v var="$line2" '$0 == var {print NR}' $1)
nline1=$((0 + nline1)) #add a 0 in case of no return
nline3=$(awk -v var="$line2" '$0 == var {print NR}' $3)
nline3=$((0 + nline3)) #add a 0 in case of no return
echo nline1 = $nline1 nline3 = $nline3
if [[ $nline1 -ne 0 ]] && [[ $nline3 -ne 0 ]]; then #no sync to do
echo Already sync
sed -i -e "${nline1}d" $1 #deleting the corresponding line in databaseA
sed -i -e "${nline3}d" $3 #deleting the corresponding line in databaseB
elif [[ $nline1 -eq 0 ]] && [[ $nline3 -eq 0 ]]; then #both are equal to 0 : conflict
echo "File has changed in A and B"
#searching if the file has been modified or deleted :
#get the file name + path
fileline2=`echo $line2 | head -n1 | awk '{print $1}'`
#search if the file name + path is in B
nline3=`awk -v var="$fileline2" '$1 == var{print NR}' $3`
nline3=$((0 + nline3)) #add a 0 in case of no return
#searching if the file has been modified or deleted :
#search if the file name + path is in A
nline1=`awk -v var="$fileline2" '$1 == var{print NR}' $1`
nline1=$((0 + nline3)) #add a 0 in case of no return
echo nline1 $nline1 nline3 $nline3
if [[ $nline1 -eq 0 ]] && [[ $nline3 -eq 0 ]]; then #both deleted
#Remove file in #!/bin/sh
sed -i "/$line2/d" $2 #delete the line in S
elif [[ $nline1 -eq 0 ]]; then #file deleted in A and modified in B
echo "file deleted in A and modified in B"
echo "Copy file B to A"
#Copy file B to A + upate S
cp -rp "$pathToB""$fileline2" "$pathToA""$fileline2" #> /dev/null #don't display error from cp
#(usualy file not found) ???
sed -i "/$line2/d" $2 #delete the line in S
sed "${nline3}q;d" $3 >> "$2" #save in S the new file with new metadata
elif [[ $nline3 -eq 0 ]]; then #file deleted in B and modified in A
echo "file deleted in B and modified in A"
echo "Copy file A to B"
#Copy file B to A + upate S
cp -rp "$pathToA""$fileline2" "$pathToB""$fileline2" #> /dev/null #don't display error from cp
#(usualy file not found) ???
sed -i "/$line2/d" $2 #delete the line in S
sed "${nline1}q;d" $1 >> "$2" #save in S the new file with new metadata
else
echo "Conflict"
#name, nameline, line, databaseA, databaseS, databaseB, pathToA, pathToB
modDateA=$(cat $1 | awk -v var=$nline1 '{if (NR==var) print}' | awk '{print $7}' | sed "s/-//g")
modDateB=$(cat $3 | awk -v var=$nline3 '{if (NR==var) print}' | awk '{print $7}' | sed "s/-//g")
echo modDateA: $modDateA
echo modDateB: $modDateB
if (( $(echo "$modDateA > $modDateB" | bc -l) )); then
mv "$pathToA""$fileline2" "$pathToA""$fileline2""_conflict"
#Rename the file with the most recent mod date to file_conflict in the metadata
conflictLine="${1/"$fileline2"/"$fileline2"_conflict}"
#Update the DB with both metadata, the file_conflict and the file.
echo "$conflictLine" >> $pathToDataBaseRAM
awk '{if (NR=='$nline3') print $0}' $3 >> $pathToDataBaseRAM
elif (( $(echo "$modDateA < $modDateB" | bc -l) )); then
#Rename the file with the most recent mod date in its arb
mv "$pathToB""$fileline2" "$pathToB""$fileline2"_conflict
#Rename the file with the most recent mod date to file_conflict in the metadata
conflictLine="${3/"$fileline2"/"$fileline2"_conflict}"
#Update the DB with both metadata, the file_conflict and the file.
echo "$conflictLine" >> $pathToDataBaseRAM
awk '{if (NR=='$nline1') print $0}' $1 >> $pathToDataBaseRAM
else
modTimeA=$(cat $1 | awk -v var=$nline1 '{if (NR==var) print}' | awk '{print $8}' | sed "s/://g")
echo modTimeA: $modTimeA
#Find the string at the line $nameline inside B
modTimeB=$(cat $3 | awk -v var=$nline3 '{if (NR==var) print}' | awk '{print $8}' | sed "s/://g")
echo modTimeB: $modTimeB
if (( $(echo "$modTimeA > $modTimeB" | bc -l) )); then
#Rename the file with the most recent mod date in its arb
mv "$pathToA""$fileline2" "$pathToA""$fileline2""_conflict"
#Rename the file with the most recent mod date to file_conflict in the metadata
#Change the filename in metadata
sed -i "s/"$fileline2"/"$fileline2"_conflict/g" $1
sed "${nline1}q;d" $1 >> "$2"
awk '{if (NR=='$nline3') print $0}' $3 >> $pathToDataBaseRAM
else
#Rename the file with the most recent mod date in its arb
mv "$pathToB""$fileline2" "$pathToB""$fileline2""_conflict"
#Rename the file with the most recent mod date to file_conflict in the metadata
sed -i "s/"$fileline2"/"$fileline2"_conflict/g" $3
sed "${nline1}q;d" $3 >> "$2"
awk '{if (NR=='$nline1') print $0}' $1 >> $pathToDataBaseRAM
fi
fi
fi
elif [[ $nline3 -eq 0 ]]; then #file has changed in B (A == R)
echo "File has changed in B"
#searching if the file has been modified or deleted :
#get the file name + path
fileline2=`echo $line2 | head -n1 | awk '{print $1}'`
#search if the file name + path is in B
nline3=`awk -v var="$fileline2" '$1 == var{print NR}' $3`
nline3=$((0 + nline3)) #add a 0 in case of no return
if [[ $nline3 -ne 0 ]]; then #file + path found ==> B has been modified
echo "The file still exist but has changed"
#Copy file B to A + upate S
cp -rp "$pathToB""$fileline2" "$pathToA""$fileline2" #> /dev/null #don't display error from cp
#(usualy file not found) ???
sed -i "/$line2/d" $2 #delete the line in S
sed "${nline3}q;d" $3 >> "$2" #save in S the new file with new metadata
else
echo "The file has been deleted"
#Delete the file in A + update S
rm -r "$pathToA""$fileline2" #> /dev/null #don't display error from rm
#(usualy file not found)
sed -i "/$line2/d" $2 #delete the line in S
fi
fileline2="$fileline2"" "
sed -i "/$fileline2/d" $1 #delete the line in DB A
sed -i "/$fileline2/d" $3 #delete the line in DB B
elif [[ $nline1 -eq 0 ]]; then #file has changed in A (B == R)
echo "File has changed in A"
#searching if the file has been modified or deleted :
#get the file name + path
fileline2=`echo $line2 | head -n1 | awk '{print $1}'`
#search if the file name + path is in A
nline1=`awk -v var="$fileline2" '$1 == var{print NR}' $1`
nline1=$((0 + nline1)) #add a 0 in case of no return
if [[ $nline1 -ne 0 ]]; then #file + path found ==> A has been modified
echo "The file still exist but has changed"
#TODO : copy file A to B + upate S
cp -rp "$pathToA""$fileline2" "$pathToB""$fileline2" #> /dev/null #don't display error from cp
#(usualy file not found) ???
sed -i "/$line2/d" $2 #delete the line in S
sed "${nline1}q;d" $1 >> "$2" #save in S the new file with new metadata
else
echo "The file has been deleted"
#Delete the file in B + update S
rm -r "$pathToB""$fileline2" #> /dev/null #don't display error from rm
#(usualy file not found)
sed -i "/$line2/d" $2 #delete the line in S
fi
fileline2="$fileline2"" "
sed -i "/$fileline2/d" $1 #delete the line in DB A
sed -i "/$fileline2/d" $3 #delete the line in DB B
fi
#cat $2
done < "$pathToDataBaseRAM"
echo ""
#rm "$pathToDataBaseRAM"
#read each line of A, get the filename, check if it exists in B.
#If the file isn't in B, copy it to the DB and to B
#If the file is in B, shows a conflict.
while IFS= read -r line1; do #Read A
echo "databaseA" : "$line1"
name=$(echo "$line1" | head -n1 | awk '{print $1}')
nameline=$(awk -v var="$name" '$1 == var{print NR}' $3)
nameline=$((0+ nameline))
if [ $nameline -eq 0 ]; then #New file in abr1 not present in abr2
echo New file "$name" detected
echo "copying from A to B"
cp -rp "$pathToA""$name" "$pathToB""$name"
echo "$line1" >> "$2" #Save the line from A into S
#sed -i "/$line1/d" "$1" #delete the line in A (OPTIONAL)
else #New file is present in abr2
echo "File also in B"
nlineS=$(awk -v var="$line1" 'var == $1{print NR}' $3)
nlineS=$((0 + nlineS))
if [ $nlineS -ne 0 ]; then #new file exists in both abr1 and 2, same metadata.
echo "$line1" >> $2
#sed -i "/$line1/d" $1
sed -i "/$nameline/d" $3
else
echo "Conflict"
#name, nameline, line, databaseA, databaseS, databaseB, pathToA, pathToB
modDateA=$(cat $1 | awk -v var=$nameline '{if (NR==var) print}' | awk '{print $7}' | sed "s/-//g")
modDateB=$(cat $3 | awk -v var=$nameline '{if (NR==var) print}' | awk '{print $7}' | sed "s/-//g")
echo modDateA: $modDateA
echo modDateB: $modDateB
if (( $(echo "$modDateA > $modDateB" | bc -l) )); then
mv "$pathToA""$name" "$pathToA""$name""_conflict"
#Rename the file with the most recent mod date to file_conflict in the metadata
sed -i "s/"$name"/"$name"_conflict/g" $1
sed "${nameline}q;d" $1 >> "$2"
awk '{if (NR=='$nameline') print $0}' $3 >> $pathToDataBaseRAM
elif (( $(echo "$modDateA > $modDateB" | bc -l) )); then
#Rename the file with the most recent mod date in its arb
mv "$pathToB""$name" "$pathToB""$name""_conflict"
sed -i "s/"$name"/"$name"_conflict/g" $3
sed "${nameline}q;d" $3 >> "$2"
awk '{if (NR=='$nameline') print $0}' $1 >> $pathToDataBaseRAM
else
modTimeA=$(cat $1 | awk -v var=$nameline '{if (NR==var) print}' | awk '{print $8}' | sed "s/://g")
echo modTimeA: $modTimeA
#Find the string at the line $nameline inside B
modTimeB=$(cat $3 | awk -v var=$nameline '{if (NR==var) print}' | awk '{print $8}' | sed "s/://g")
echo modTimeB: $modTimeB
if (( $(echo "$modTimeA > $modTimeB" | bc -l) )); then
#Rename the file with the most recent mod date in its arb
mv "$pathToA""$name" "$pathToA""$name""_conflict"
#Rename the file with the most recent mod date to file_conflict in the metadata
sed -i "s/"$name"/"$name"_conflict/g" $1
sed "${nameline}q;d" $1 >> "$2"
awk '{if (NR=='$nameline') print $0}' $3 >> $pathToDataBaseRAM
else
#Rename the file with the most recent mod date in its arb
mv "$pathToB""$name" "$pathToB""$name"_conflict
#Rename the file with the most recent mod date to file_conflict in the metadata
conflictLine="${3/"$name"/"$name"_conflict}"
sed -i "s/"$name"/"$name"_conflict/g" $3
sed "${nameline}q;d" $3 >> "$2"
awk '{if (NR=='$nameline') print $0}' $1 >> $pathToDataBaseRAM
fi
fi
fi
fi
echo ""
done < "$1"
while IFS= read -r line3; do #Read B
echo "databaseB" : "$line3"
name=$(echo "$line3" | head -n1 | awk '{print $1}')
nameline=$(awk -v var="$name" '$1 == var{print NR}' $1)
nameline=$((0+ nameline))
if [ $nameline -eq 0 ]; then #New file in abr1 not present in abr2
echo New file "$name" detected
echo "copying from B to A"
cp -rp "$pathToB""$name" "$pathToA""$name"
echo $line3 >> "$2"#Save the line from B into S
#sed -i "/{$line3}/d" "$3" #delete the line in B (OPTIONAL)
else #New file is present in abr2
echo "File also in A"
nlineS=$(awk -v var="$line1" 'var == $1{print NR}' $3)
nlineS=$((0 + nlineS))
if [ $nlineS -ne 0 ]; then #new file exists in both abr1 and 2, same metadata.
echo "$line3" >> $2
sed -i "/$line3/d" "$1"
#sed -i "/$line/d" "$3"
else
echo "Conflict"
#name, nameline, line, databaseA, databaseS, databaseB, pathToA, pathToB
modDateA=$(cat $1 | awk -v var=$nameline '{if (NR==var) print}' | awk '{print $7}' | sed "s/-//g")
modDateB=$(cat $3 | awk -v var=$nameline '{if (NR==var) print}' | awk '{print $7}' | sed "s/-//g")
echo modDateA: $modDateA
echo modDateB: $modDateB
if (( $(echo "$modDateA > $modDateB" | bc -l) )); then
mv "$pathToA""$name" "$pathToA""$name""_conflict"
sed -i "s/"$name"/"$name"_conflict/g" $1
sed "${nameline}q;d" $1 >> "$2"
awk '{if (NR=='$nameline') print $0}' $3 >> $pathToDataBaseRAM
elif (( $(echo "$modDateA > $modDateB" | bc -l) )); then
#Rename the file with the most recent mod date in its arb
mv "$pathToB""$name" "$pathToB""$name"_conflict
sed -i "s/"$name"/"$name"_conflict/g" $3
sed "${nameline}q;d" $3 >> "$2"
awk '{if (NR=='$nameline') print $0}' $1 >> $pathToDataBaseRAM
else
modTimeA=$(cat $1 | awk -v var=$nameline '{if (NR==var) print}' | awk '{print $8}' | sed "s/://g")
echo modTimeA: $modTimeA
#Find the string at the line $nameline inside B
modTimeB=$(cat $3 | awk -v var=$nameline '{if (NR==var) print}' | awk '{print $8}' | sed "s/://g")
echo modTimeB: $modTimeB
if (( $(echo "$modTimeA > $modTimeB" | bc -l) )); then
#Rename the file with the most recent mod date in its arb
mv "$pathToA""$name" "$pathToA""$name""_conflict"
#Rename the file with the most recent mod date to file_conflict in the metadata
sed -i "s/"$name"/"$name"_conflict/g" $1
sed "${nameline}q;d" $1 >> "$2"
awk -v var="$nameline" '{if (NR==var) print $0}' $3 >> $pathToDataBaseRAM
else
#Rename the file with the most recent mod date in its arb
mv "$pathToB""$name" "$pathToB""$name""_conflict"
#Rename the file with the most recent mod date to file_conflict in the metadata
sed -i "s/"$name"/"$name"_conflict/g" $3
sed "${nameline}q;d" $3 >> "$2"
awk -v var="$nameline" '{if (NR==var) print $0}' $1 >> $pathToDataBaseRAM
fi
fi
fi
fi
echo ""
done < "$3"
}