File: ftpWildcard.tcl

package info (click to toggle)
tclcurl 7.22.0%2Bhg20160822-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,328 kB
  • ctags: 331
  • sloc: ansic: 4,264; tcl: 860; sh: 155; makefile: 30
file content (81 lines) | stat: -rwxr-xr-x 1,528 bytes parent folder | download | duplicates (5)
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
package require TclCurl

proc FtpMatch {pattern filename} {

    puts "Pattern: $pattern - File: $filename"

    # For this example everything matches
    return 0
}

proc FtpCheck {remains} {
    global someVar

    # Lets forget about directories:
    if {($someVar(filetype) eq "directory") || ([regexp {^\.} $someVar(filename)])} {
        return 1
    }

    puts -nonewline "File to download $someVar(filename) ($someVar(size)B) (y/N): "
    flush stdout
    set line [string tolower [gets stdin]]
    if {$line eq y} {
        return 0
    }
    return  1
}

proc FtpSaveFile {readData} {
    global   outFile
    global   openedFile
    global   someVar

    if {$openedFile==0} {
        if {![file exists downloads]} {
            file mkdir downloads
        }
        set outFile [open "downloads/$someVar(filename)" w+]
        fconfigure $outFile -translation binary
    }

    puts -nonewline $outFile $readData

    return 0
}

proc FtpDone {} {
    global outFile
    global openedFile

    puts "Done\n"

    close $outFile
    set openedFile 0

    return 0
}

set openedFile 0
set curlHandle [curl::init]

$curlHandle configure -url ftp://sunsite.rediris.es/sites/metalab.unc.edu/ldp/*
$curlHandle configure -chunkbgnproc  FtpCheck
$curlHandle configure -chunkbgnvar   someVar
$curlHandle configure -chunkendproc  FtpDone
$curlHandle configure -writeproc     FtpSaveFile
$curlHandle configure -wildcardmatch 1
$curlHandle configure -fnmatchproc   FtpMatch

$curlHandle perform

$curlHandle cleanup