-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncgen2
executable file
·231 lines (211 loc) · 6.72 KB
/
syncgen2
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
#/bin/sh
####################################################################
# Maintained by Jaakko Lappalainen. [email protected] - 2012
############### rods password ######################################
RODS_PASS='rods_*1234'
############## rods authentication program path ####################
RODS_AUTH_EXEC='/home/rodsadmin/iRODS/clients/icommands/bin/iinit'
################## locking vars ####################################
_HOME='/home/rodsadmin'
FAKE_TREES_HOME=$_HOME'/fake_trees'
RODS_HOME='/fluidMec/home/rods'
OUT_FILES_HOME=$_HOME'/out_files'
PROCESSING_FILES=$_HOME'/processing_files'
METAPOS_MTIME=2
METAPOS_SIZE=1
METAPOS_FILE=3
IFS='
'
log(){
echo "["`date +"%x %T"`"] "$1 >> $LOG
}
log_sep(){
echo "--------------------------------------------------------------" >> $LOG
}
do_exit(){
rm -f "$PIDFILE"
rm -f "$INPUT_FILE"
}
do_exit_error(){
log "EXITING SCRIPT..."
log_sep
do_exit
exit 255
}
addmetadata(){
name=$1
attname=$2
attvalue=$3
# echo "name: "$name" attname: "$attname" attvalue: "$attvalue
if [ -z "$name" ]; then
if [ -z "$attvalue" ]; then
if [ -z "$attname" ]; then
#echo "name: "$name" attname: "$attname" attvalue: "$attvalue
if [ "$attname" == "Modification_time" ]; then
# if [ $? '<' 1 ]; then
meta_q=`imeta ls -d "$name" "$attname" | awk ' /value:/ { print $2" "$3 }' | head -n1`
# fi
fi
if [ "$attname" == "Size" ]; then
# if [ $? '<' 1 ]; then
meta_q=`imeta ls -d "$name" "$attname" | awk ' /value:/ { print $2" "$3 }' | head -n1`
# fi
fi
q=$meta_q
if [ "$q" != "" ]; then
if [ "$q" != "$attvalue" ]; then
if [ "$attname" == "Modification_time" ]; then
echo -e 'add -d '"$name"' '\"$attname\"' '\"$attvalue\" >> $METAFILE
else
echo 'mod -d '"$name" "$attname" "$q"' v:'"$attvalue" >> $METAFILE
fi
fi
else
if [ "$attname" == "Modification_time" ]; then
echo -e 'add -d '"$name"' '\"$attname\"' '\"$attvalue\" >> $METAFILE
else
echo 'add -d '"$name"' '"$attname"' '"$attvalue" >> $METAFILE
fi
fi
fi
fi
fi
#rm -f $METAFILE
}
sync2(){
log "Syncing local-rods copy..."
imkdir -p "$RODS_HOME"/"$DIR"
ireg -f -R torroja -C "$FAKE_TREES_HOME"/"$DIR" "$RODS_HOME"/"$DIR" >> $LOG
log "Checking for default metadata changes in '"$DIR"'..."
#rm -f $METAFILE
while read line;do
name=`echo "$line" | cut -d '|' -f "$METAPOS_FILE"`
mtime=`echo "$line" | cut -d '|' -f "$METAPOS_MTIME"`
size=`echo "$line" | cut -d '|' -f "$METAPOS_SIZE"`
addmetadata "$RODS_HOME"/"$DIR"/"$name" "Modification_time" $mtime
addmetadata "$name" "Size" $size
done < $INPUT_FILE
if [ -f $METAFILE ]; then
log "Adding metadata..."
cat -s $METAFILE | imeta > /dev/null
#rm -f $METAFILE
else
log "No metadata changed."
fi
}
calculate_changes3(){
log "Calculating changes..."
mkdir -p "$FAKE_TREES_HOME"/"$DIR"
ls -1 "$FAKE_TREES_HOME"/"$DIR" > $CURRENTFILE
cat -s $INPUT_FILE |cut -d '|' -f $METAPOS_FILE | sort -u > $NEWFILE
comm -2 -3 --nocheck-order $NEWFILE $CURRENTFILE > $CREATEFILE
comm -2 -3 --nocheck-order $CURRENTFILE $NEWFILE > $DELETEFILE
rm -f $CURRENTFILE $NEWFILE
if [ -f "$CREATEFILE" ]; then
n_to_create=`wc -l $CREATEFILE | awk ' { print $1 }'`
if [ $n_to_create -gt 0 ]; then
UPDATE=1
log "Files to create: "$n_to_create
log "Creating fake files..."
while read line;do
file=`echo $line | cut -d '|' -f $METAPOS_FILE`
touch "$FAKE_TREES_HOME"/"$DIR"/"$file"
done < $CREATEFILE
fi
fi
if [ -f "$DELETEFILE" ]; then
n_to_delete=`wc -l $DELETEFILE | awk ' { print $1 } '`
if [ $n_to_delete '>' 0 ]; then
log "Files to delete: "$n_to_delete
log "Deleting rods files..."
while read line;do
file=`echo $line | cut -d '|' -f $METAPOS_FILE`
if [ "$file" '!=' "" ]; then
#if [ -f "$FAKE_TREES_HOME"/"$DIR"/"$file" ]; then
irm -Uf "$RODS_HOME"/"$DIR"/"$file"
#fi
fi
done < $DELETEFILE
log "Deleting files..."
while read line;do
file=`echo $line |cut -d '|' -f $METAPOS_FILE`
if [ "$file" '!=' "" ]; then
#if [ -f "$FAKE_TREES_HOME"/"$DIR"/"$file" ]; then
rm -f "$FAKE_TREES_HOME"/"$DIR"/"$file"
#fi
fi
done < $DELETEFILE
fi
fi
}
remove_empty(){
if [ $UPDATE '>' 0 ]; then
log "Checking empty dirs and collections..."
cd "$FAKE_TREES_HOME"
for dir in `find $DIR -type d`;do
n=`ls "$FAKE_TREES_HOME"/"$dir" |wc -w`
if [ $n '<' 1 ]; then
rm -r "$dir"
irm -rf "$RODS_HOME"/"$dir"
fi
done
fi
}
if [ $# '!=' 1 ]; then
echo "usage: syncgen2 <out-file-path>"
exit 255
fi
INPUT_FILE=$1
if [ -f $INPUT_FILE ]; then
echo "" > /dev/null
else
echo "Error: "$INPUT_FILE" no such file."
exit 255
fi
NAME=`basename "$INPUT_FILE"`
mv "$INPUT_FILE" "$PROCESSING_FILES"/"$NAME"
INPUT_FILE="$PROCESSING_FILES"/"$NAME"
DIR2=`head -n1 $INPUT_FILE | cut -d '|' -f $METAPOS_FILE`
DIR=`dirname $DIR2`
DIRNAME=`echo "$DIR" | sed -e "s/\//-/g"`
cat -s "$INPUT_FILE" | sed -e 's|'$DIR'\/||g' > "$INPUT_FILE".temp
mv "$INPUT_FILE".temp "$INPUT_FILE"
LOG="$_HOME"/log/"$DIRNAME".log
UPDATE=0
SCRIPTNAME=`basename $0`
CURRENTFILE="$_HOME"/.temp."$DIRNAME".current
NEWFILE="$_HOME"/.temp."$DIRNAME".new
DELETEFILE="$_HOME"/.temp."$DIRNAME".delete
CREATEFILE="$_HOME"/.temp."$DIRNAME".create
METAFILE="$_HOME"/.temp."$DIRNAME".meta
trap "rm -f $PIDFILE; rm -f $CURRENTFILE; rm -f $NEWFILE; rm -f $DELETEFILE; rm -f $CREATEFILE; rm -f $METAFILE; exit" INT TERM EXIT
rm -f $CURRENTFILE
rm -f $NEWFILE
rm -f $DELETEFILE
rm -f $CREATEFILE
rm -f $METAFILE
mkdir -p "$_HOME"/.pids
PIDFILE="$_HOME"/.pids/"$SCRIPTNAME"."$DIRNAME".pid
############### locking ###################
if [ -f "$PIDFILE" ]; then
#verify if the process is actually still running under this pid
OLDPID=`cat "$PIDFILE"`
RESULT=`ps -ef | grep "$OLDPID" | grep "$SCRIPTNAME"`
if [ -n "$RESULT" ]; then
log "Script already running! Exiting"
exit 255
fi
fi
PID=`ps -ef | grep "$SCRIPTNAME" | head -n1 | awk ' {print $2;} '`
echo "$PID" > "$PIDFILE"
log_sep
log "Starting script for '"$DIR"'..."
############ critical section ############
calculate_changes3
sync2
remove_empty
##########################################
log "Script ended."
log_sep
trap - INT TERM EXIT
do_exit