File: ecflow_ui_transfer_file.sh.in

package info (click to toggle)
ecflow 5.13.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 51,304 kB
  • sloc: cpp: 259,208; python: 22,228; sh: 3,216; perl: 770; xml: 333; f90: 199; ansic: 141; makefile: 71
file content (410 lines) | stat: -rwxr-xr-x 11,670 bytes parent folder | download | duplicates (3)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/bin/bash

#============================================================================
# Copyright 2009- ECMWF.
# This software is licensed under the terms of the Apache Licence version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#============================================================================

set -e

META_MSG_LEN=60
#set -x #goes to stderr
 
function failed() {
    local msg=$1
    echo $msg >&2
    exit 1
}

function runSsh() {
    local cmd=$1 
    if [[ $proxyCommand != "" ]] ; then
        (set -x; ssh ${sshProxyJump} ${sshOptions} -o ProxyCommand="${proxyCommand}" ${remoteUH} "${cmd}" > ${targetFile})
    else
        (set -x; ssh ${sshProxyJump} ${sshOptions} ${remoteUH} "${cmd}" > ${targetFile})
    fi
}

function runScp() {
    if [[ $proxyCommand != "" ]] ; then
        (set -x; scp ${scpOptions} -o ProxyCommand="${proxyCommand}" ${remoteUH}:${sourceFile} ${targetFile})
    else
        (set -x; scp ${scpOptions} ${remoteUH}:${sourceFile} ${targetFile})
    fi
}

# The metadata is a fixed ${META_MSG_LEN} bytes added to the end of the result ${targetFile}
# So when we have the result:
#  - we need to extract the metadata and write it into ${metaFile}
#  - we need to remove the metadata from the end of the file
function proc_md_result() {

    usedLen=$(($META_MSG_LEN + 1))

    # get "metadata" from end of the file
    d=$(tail -c $usedLen $targetFile)

    echo "d=$d"
   
    # get file size
    size=$(wc -c <"$targetFile")

    # check pos mode with no delta. In this case a sole "1" is the file contents!
    if [[ $size -le 2 && ${byteMode} == "pos" ]] ; then
        if [[ $(echo $d | cut -c 1) == "1" ]] ; then
            echo "1" > $metaFile
            : > "$targetFile"
            return
        fi
    fi

    if [[ $size -lt $META_MSG_LEN ]] ; then
        failed "Invalid resulting file size=$size bytes! Expected at least $META_MSG_LEN bytes"
    fi

    # remove the metadata from the file. This is a generic
    # solution working even on MacOS (where there is no truncate)
    #echo $size
    size=$(($size - $usedLen))
    #echo $size
    head -c $size "$targetFile" > "${targetFile}.1"
    mv "${targetFile}.1" "$targetFile"

    # get metadata
    r=$(echo $d | cut -d ":" -f 1)
    t=$(echo $d | cut -d ":" -f 2)
    c=$(echo $d | cut -d ":" -f 3)

    # write metadata into file
    echo "$r:$t:$c:" > $metaFile
}

# transfer the whole file
function get_file_all() {
    echo "transfer the whole file"
    runScp
    #(set -x; scp ${scpOptions} ${remoteUH}:${sourceFile} ${targetFile})
}      

# transfer the file from a given byte offset
function get_file_pos() {
    cmd=$(cat <<-END
        tail -c +$((${byteValue}+1)) ${sourceFile}
END
)       
    echo "transfer file from byte offset=${byteValue}"
    runSsh "$cmd"
    #(set -x; ssh ${sshProxyJump} ${remoteUH} ${sshOptions} "${cmd}" > ${targetFile})
}

# transfer the last N bytes of the file
function get_file_last() {
    cmd=$(cat <<-END
        sourceSize=\$(wc -c < ${sourceFile})
        if [[ \${sourceSize} -gt 0 && \${sourceSize} -le ${byteValue} ]] ; then 
            cat ${sourceFile}
        else
            tail -c ${byteValue} ${sourceFile}
        fi
END
)
    #printf 'cmd: %s\n' "${cmd}"
    echo "transfer last ${byteValue} bytes"
    runSsh "$cmd"
}

# transfer the whole file with extra "metadata"
function get_file_all_md() {
    echo "transfer the whole file"

    # the metadata is a fixed META_MSG_LEN bytes added to the end of the result.
    # So when we have the result:
    #  - we need to extract the metadata and write it into ${metaFile}
    #  - we need to remove the metadata from the end of the file

    cmd=$(cat <<-END
        aModTime=\$(date -r "${sourceFile}" +%s)
        os=\$(uname | tr '[:upper:]' '[:lower:]')
        if [[ \$os == darwin || \$os == "freebsd" ]] ; then
            aCheckSum=\$(head -c 1024 ${sourceFile} 2>/dev/null | shasum | cut -d ' ' -f 1)
        else
            aCheckSum=\$(head -c 1024 ${sourceFile} 2>/dev/null | sha1sum | cut -d ' ' -f 1)
        fi
        m="0:\$aModTime:\$aCheckSum:"
        m=\$(printf %-${META_MSG_LEN}s \$m | tr ' ' X)
        cat "${sourceFile}"
        echo \$m
END
)
    #echo $cmd
    runSsh "$cmd"
  
    # extracts + removes metadata from the end of file
    proc_md_result
}

# transfer the file contents from a given byte offset. Based on the "metadata":
# - if the there is no change a single "1" trnasferred
# - if data after the offset seems to be appended this delta + metadata is transferred
# - otherwise the whole file + metadata is transferred back
function get_file_pos_md() {
    
    # the metadata is a fixed META_MSG_LEN bytes added to the end of the result.
    # So when we have the result:
    #  - we need to extract the metadata and write it into ${metaFile}
    #  - we need to remove the metadata from the end of the file

    cmd=$(cat <<-END
        aSize=\$(wc -c <"${sourceFile}")
        aModTime=\$(date -r "${sourceFile}" +%s)
        os=\$(uname | tr '[:upper:]' '[:lower:]')
        if [[ \$os == darwin || \$os == "freebsd" ]] ; then
            aCheckSum=\$(head -c 1024 ${sourceFile} 2>/dev/null | shasum | cut -d ' ' -f 1)
        else
            aCheckSum=\$(head -c 1024 ${sourceFile} 2>/dev/null | sha1sum | cut -d ' ' -f 1)
        fi

        all=1

        if [[ \$aSize -eq ${byteValue} ]] ; then
            if [[ ( \$aModTime -eq 0 || \$aModTime -eq ${modTime} ) && ( $checkSum == "x" || \$aCheckSum == "${checkSum}" ) ]] ; then
                echo "1"
                exit 0
            fi
        elif [[ \$aSize -gt ${byteValue} && \$aCheckSum == "${checkSum}" ]] ; then 
            all=0
        fi

        if [ \$all -eq 0 ] ; then
            tail -c +$((${byteValue}+1)) ${sourceFile}
            m="1:\$aModTime:\$aCheckSum:"
        else
            cat ${sourceFile}
            m="0:\$aModTime:\$aCheckSum:"
        fi
            
        m=\$(printf %-${META_MSG_LEN}s \$m | tr  ' ' X)
        echo \$m
END
)
    echo "transfer file from byte offset=${byteValue}"
    runSsh "$cmd"

    # extracts + removes metadata from end of file
    proc_md_result
}
 
# fetching directory listing
function get_dir() {
    dirPath=$(dirname ${sourceFile})
    sourceName=$(basename ${sourceFile})
    echo "dirPath="$dirPath
    echo "sourceName="$sourceName
    pattern=$(echo $sourceName | cut -d '.' -f 1)".*"
   
    # the task here is to get the file modification date, file size in bytes
    # and file name for each file matching the pattern in the remote directory.
    # ls -l cannot be used here because apart from the parsing problems the 
    # modification date is not always complete (e.g. for dates older than 6 months).
    # Using stat is not an option either since it can have different options on
    # different unix systems
    cmd=$(cat <<-END
    cd ${dirPath}
    for f in \$(ls $pattern) ; do 
        if [[ ! -d "\$f" ]] ; then
            echo \$(date -r "\$f" +%s) \$( wc -c "\$f" | awk '{print \$1}' ) \$f
        fi
    done
END
)
    echo
    runSsh "$cmd"
}


#echo " arguments=$*"
#echo "shell=${SHELL}"

mode=
sourceFile=
remoteUid=
remoteHost=
targetFile=
byteMode=
byteValue=
sshProxyJump=
proxyJump=
sshOptions="-C -o StrictHostKeyChecking=no -o PasswordAuthentication=no"
scpOptions="-C -o StrictHostKeyChecking=no -o PasswordAuthentication=no"
metaFile=
modTime=0
checkSum="x"
socksPort=

while getopts m:s:u:h:t:j:b:v:o:c:x:p: flag
do
    case "${flag}" in
        m) mode=${OPTARG};;
        s) sourceFile=${OPTARG};;
        u) remoteUid=${OPTARG};;
        h) remoteHost=${OPTARG};;
        t) targetFile=${OPTARG};;
        j) proxyJump=${OPTARG};;
        b) byteMode=${OPTARG};;
        v) byteValue=${OPTARG};;
        x) metaFile=${OPTARG};;
        o) modTime=${OPTARG};;
        c) checkSum=${OPTARG};;
        p) socksPort=${OPTARG};;
    esac
done

echo ""
echo "mode=$mode"
echo "sourceFile=$sourceFile"
echo "remoteUid=$remoteUid"
echo "remoteHost=$remoteHost"
echo "socksPort=$socksPort"
echo "targetFile=$targetFile"
echo "proxyJump=$proxyJump"
echo "byteMode=$byteMode"
echo "byteValue=$byteValue"
echo "metaFile=$metaFile"
echo "modTime=$modTime"
echo "checkSum=$checkSum"
echo ""

if [[ $mode != "file_md" && $mode != "file" && $mode != "dir" ]] ; then
    failed "invalid mode=$mode! Has to be file_md, file or dir"
fi

if [[ $sourceFile == "" ]] ; then
    failed "sourceFile is empty!"
fi

if [[ $targetFile == "" ]] ; then
    failed "targetFile is empty!"
fi

# SOCKS based transfer
if [[ $socksPort != "" ]] ; then
    proxyCommand='nc -x 127.0.0.1:'"${socksPort}"' %h %p'
    if [[ ${scpOptions} == "" ]] ; then
        failed "scpOptions cannot be empty!" 
    fi
    if [[ $remoteUid == "" || $remoteUid == "__USER__" ]]  ; then
        remoteUH=localhost
    else    
        remoteUH="${remoteUid}@localhost"
    fi  
# no SOCKS
else 
    if [[ $remoteHost == "" ]] ; then
        failed "remoteHost is empty!"
    fi

    if [[ $remoteUid == "" ]] ; then
        failed "remoteUid is empty!"
    fi

    if [[ $remoteUid == "__USER__" ]]  ; then
        remoteUid=${USER}
    fi  
    remoteUH="${remoteUid}@${remoteHost}"
 fi 

if [[ $remoteUH == "" ]] ; then
    failed "remoteUH is empty!"
fi

# proxyjump
if [[ $proxyJump == "__NOJUMP__" ]] ; then
    proxyJump=""
fi

if [[ $proxyJump != "" ]] ; then
    sshProxyJump="-J ${proxyJump}"
    if [[ ${scpOptions} == "" ]] ; then
        failed "scpOptions cannot be empty!" 
    fi
    scpOptions="${scpOptions} -o ProxyJump=${proxyJump}"
fi

# transfer mode
if [[ $mode == "file" ]] ; then
    if [[ $byteMode != "all" && $byteMode != "pos" &&  $byteMode != "last" ]] ; then
        failed "invalid byteMode value! Allowed values: all, pos, last"
    fi

    if [[ $byteValue == "" ]] ; then
        failed "byteValue is empty!"
    fi

    if [[ $byteValue -lt 0 ]] ; then
        failed "byteValue (=${byteValue}) must be >=0!"
    fi

elif [[ $mode == "file_md" ]] ; then
    if [[ $byteMode != "all" && $byteMode != "pos" ]] ; then
        failed "invalid byteMode value! Allowed values are: all, pos"
    fi

    if [[ $byteValue == "" ]] ; then
        failed "byteValue is empty!"
    fi

    if [[ $byteValue -lt 0 ]] ; then
        failed "byteValue (=${byteValue}) must be >=0!"
    fi

    if [[ $metaFile == "" ]] ; then
        failed "metaFile is empty!"
    fi

    if [[ $byteMode == "pos" ]] ; then
        if [[ $modTime == "" ]] ; then
            failed "modTime is empty!"
        fi
        if [[ $checkSum == "" ]] ; then
            failed "checkSum is empty!"
        fi
    fi

fi

# fetching a file
if [[ $mode == "file" ]] ; then
    
    # transfer the whole file
    if [[ $byteMode == "all" ]] ; then
        get_file_all
    # transfer from a given byte offset
    elif  [[ $byteMode == "pos" ]] ; then
        get_file_pos
    #transfer the last bytes of the file
    elif [[ $byteMode == "last" ]] ; then
        get_file_last
    else
        failed "invalid byteMode=${byteMode} option when mode=${mode}"
    fi

# fetching a file with metadata + checking
elif [[ $mode == "file_md" ]] ; then
    
    # transfer the whole file
    if [[ $byteMode == "all" ]] ; then
        get_file_all_md
    elif [[ $byteMode == "pos" ]] ; then
        get_file_pos_md
    else
        failed "invalid byteMode=${byteMode} option when mode=${mode}"
    fi

# fetching directory listing
else
   get_dir
fi