forked from NERSC/giveandtake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtake
executable file
·214 lines (200 loc) · 5.46 KB
/
take
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
#!/bin/bash -i
#echo "Give and Take is migrating due to Global Scratch Retirement. We'll be back soon"
#exit 1
# Sanitize environment. Shouldnt be needed with -i
destfoler=""
giver=""
temp=""
allflag=""
listflag=""
current_dir=""
user=""
giver_dir=""
var=""
owner=""
PATH=""
LD_LIBRARY_PATH=""
unset IFS
# Implementation Definitions
#SPOOLAREA=/global/scratch2/gt
SPOOLAREA=/project/projectdirs/gt
# Get Hostname
myhost=`/usr/common/usg/bin/nersc_host`
#
if [ "$myhost" == "genepool" ]; then
mylogger="/usr/bin/logger"
else
mylogger="/bin/logger"
fi
# Get user's Name
user=`/usr/bin/whoami`
current_dir=`/bin/pwd`
destfolder="$current_dir"
allflag="no"
listflag="no"
while getopts ":u:d:al" opt; do
case $opt in
u)
giver="$OPTARG"
;;
d)
destfolder="$OPTARG"
if [ ! -d $destfolder ]; then
echo "$destfolder not a directory"
exit 1
fi
;;
a)
allflag="yes"
;;
l)
listflag="yes"
;;
\?)
echo "Usage: "
echo "See available files from user: take -u <user>"
echo "Take one file from user: take -u <user> -d <destination> <files>"
echo "Take all files from user: take -u <user> -d <destination> -a"
echo "-d is an optional flag"
echo ""
exit 1
;;
:)
echo "Option -$OPTARG requires an argument."
exit 1
;;
esac
done
echo ""
if [ -z "$giver" ]
then
if [ "$listflag" != "yes" ]
then
echo "Usage: "
echo "See available files from user: take -u <user>"
echo "Take one file from user: take -u <user> -d <destination> <files>"
echo "Take all files from user: take -u <user> -d <destination> -a"
echo "-d is an optional flag"
echo ""
fi
# This might be slow. So it is optional
if [ "$listflag" == "yes" ]
then
echo "Checking for available files. This may take a minute."
filesavail="none"
for giver_dir in "$SPOOLAREA"/*/"$user"
do
giver=${giver_dir%/*}
giver=${giver##*/}
if [ "$(/bin/ls -A $giver_dir 2> /dev/null)" ]; then
echo "Files available to you from $giver:"
cd "$giver_dir"
/usr/bin/du -sh --time *
cd "$current_dir"
echo ""
filesavail="some"
fi
done
#echo "done check"
if [ "$filesavail" == "none" ]
then
echo "No files available to you."
fi
fi
else
shift $(( OPTIND-1 ))
# Check For Valid Giver
# This is checked implicitly below that giver directory exists
# temp=`/usr/bin/id "$giver" 2>&1`
# if [ $? -ne 0 ]; then
# echo "$giver does not exist"
# exit 1
# fi
if [ ! -d "$SPOOLAREA/$giver" ]; then
echo "$giver does not exist"
exit 1
fi
if [ "$allflag" == "yes" ]
then
if [ "$(/bin/ls -A $SPOOLAREA/$giver/$user/ 2> /dev/null)" ]; then
cd "$destfolder"
for var in "$SPOOLAREA/$giver/$user/"*
do
owner=`/usr/bin/stat -c %U $var`
if [ "$owner" != "$giver" -a "$giver" != "ftpup" ]
then
echo "Owner of $var, $owner, does not match $giver. Something nasty could be going on!"
exit 1
fi
# We remove the file once transfer is complete. We could in the future let purger handle this.
if [ -a ${var##*/} ]
then
echo "File ${var##*/} already exists at destination. Overwrite?"
select yn in "Yes" "No"; do
case $yn in
Yes ) /usr/bin/rsync --progress --remove-sent-files --no-p --no-g --chmod=ugo=rwX --log-format="Taking ${var##*/}" "$var" . ; echo "Took ${var##*/}" ; echo ""; break;;
No ) break;;
esac
done
else
/usr/bin/rsync --progress --remove-sent-files --no-p --no-g --chmod=ugo=rwX --log-format="Taking ${var##*/}" "$var" .
echo "Took ${var##*/}"
$mylogger "$user took ${var##*/}"
echo ""
fi
done
cd "$current_dir"
else
echo "No files available from $giver"
echo ""
fi
else
if [ -n "$1" ]
then
cd $destfolder
for var in "$@"
do
if [ -a "$SPOOLAREA/$giver/$user/$var" ]
then
owner=`/usr/bin/stat -c %U "$SPOOLAREA/$giver/$user/$var"`
if [ "$owner" != "$giver" -a "$giver" != "ftpup" ]
then
echo "Owner of $var, $owner, does not match $giver. Something nasty could be going on!"
exit 1
fi
if [ -a "$var" ]
then
echo "File $var already exists at destination. Overwrite?"
select yn in "Yes" "No"; do
case $yn in
Yes ) /usr/bin/rsync --progress --log-format="Taking $var" --no-p --no-g --chmod=ugo=rwX --remove-sent-files "$SPOOLAREA/$giver/$user/$var" . ; echo "Took $var" ; echo ""; break;;
No ) break;;
esac
done
else
/usr/bin/rsync --progress --remove-sent-files --log-format="Taking $var" --no-p --no-g --chmod=ugo=rwX "$SPOOLAREA/$giver/$user/$var" .
echo "Took $var"
$mylogger "$user took $var"
echo ""
fi
else
echo "No file $var available from $giver"
fi
done
cd $current_dir
else
giver_dir="$SPOOLAREA/$giver/$user"
if [ "$(/bin/ls -A $giver_dir 2> /dev/null)" ]; then
echo "Files available from $giver:"
cd "$giver_dir"
/usr/bin/du -sh --time *
cd $current_dir
echo ""
else
echo "No files available from $giver"
echo ""
fi
fi
fi
fi
exit 0