Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented proposal: improvements to readdir (any dir, optional details) #122

Open
sylvaticus opened this issue Jul 13, 2023 · 0 comments

Comments

@sylvaticus
Copy link

Hello, as I needed some more info that just item names from the readdir directlry, I have implemented the following customisations to (a) allow the listing of any directory (not just the current one) and (b) the reporting of the details of each element and not just the name.
This is not breaking because by default the function returns the same output as the current version of readdir.
This should answer also similar needs, like the request by @omus in #82 .

Let me know if you would be interested, I'll clean it up a bit, make a docstring and create a pull request if it is the case...

The function:

# Modified to get the info if it is a directory and to list info on any directories, not only current one
function FTP_readdir(ftp::FTP,dir=pwd(ftp);details=false)
    resp = nothing
    try
        resp = FTPClient.ftp_command(ftp.ctxt, "LIST $dir")
    catch err
        if isa(err, FTPClient.FTPClientError)
            err.msg = "Failed to list directories."
        end
        rethrow()
    end
    dir     = split(read(resp.body, String), '\n')
    dir     = filter(x -> !isempty(x), dir)
    
    if(details)
        entries = NamedTuple{(:type, :permissions, :n, :user, :group, :size, :date, :name), Tuple{Char, SubString{String}, SubString{String}, SubString{String}, SubString{String}, SubString{String}, SubString{String}, String}}[]
        for item in dir
            details = split(item)
            details_nt = (type=details[1][1],permissions=details[1][2:end],n=details[2],user=details[3],group=details[4],size=details[5],date=join(details[6:8], ' '),name=join(details[9:end], ' '))
            push!(entries,details_nt)
        end
        return entries
    else
        names = [join(split(line)[9:end], ' ') for line in dir]
        return names
    end
end

An example:

julia> ftp = FTP(hostname = "palantir.boku.ac.at", username = "anonymous", password = "")
URL:       ftp://anonymous@palantir.boku.ac.at/
Transfer:  passive mode
Security:  none

julia> FTP_readdir(ftp,"Public/ImprovedForestCharacteristics")
11-element Vector{String}:
 "Age"
 "Alive_Tree_Carbon"
 "Basal_Area"
 "Biomass"
 "Diameter"
 "Height"
 "Readme.txt"
 "Stand_Density_Index"
 "Stem_Number"
 "Tree_Species_Group"
 "Volume"

julia> FTP_readdir(ftp,"Public/ImprovedForestCharacteristics",details=true)
11-element Vector{NamedTuple{(:type, :permissions, :n, :user, :group, :size, :date, :name), Tuple{Char, SubString{String}, SubString{String}, SubString{String}, SubString{String}, SubString{String}, SubString{String}, String}}}:
 (type = 'd', permissions = "rwxrwx---", n = "2", user = "1000", group = "100", size = "4096", date = "Jan 25 2022", name = "Age")
 (type = 'd', permissions = "rwxrwx---", n = "2", user = "1000", group = "100", size = "4096", date = "Jan 25 2022", name = "Alive_Tree_Carbon")
 (type = 'd', permissions = "rwxrwx---", n = "2", user = "1000", group = "100", size = "4096", date = "Jan 25 2022", name = "Basal_Area")
 (type = 'd', permissions = "rwxrwx---", n = "2", user = "1000", group = "100", size = "4096", date = "Jan 25 2022", name = "Biomass")
 (type = 'd', permissions = "rwxrwx---", n = "2", user = "1000", group = "100", size = "4096", date = "Jan 25 2022", name = "Diameter")
 (type = 'd', permissions = "rwxrwx---", n = "2", user = "1000", group = "100", size = "4096", date = "Jan 25 2022", name = "Height")
 (type = '-', permissions = "rwxrwx---", n = "1", user = "1000", group = "100", size = "2103", date = "Jan 24 2022", name = "Readme.txt")
 (type = 'd', permissions = "rwxrwx---", n = "2", user = "1000", group = "100", size = "4096", date = "Jan 25 2022", name = "Stand_Density_Index")
 (type = 'd', permissions = "rwxrwx---", n = "2", user = "1000", group = "100", size = "4096", date = "Jan 25 2022", name = "Stem_Number")
 (type = 'd', permissions = "rwxrwx---", n = "3", user = "1000", group = "100", size = "4096", date = "Jun 20 2022", name = "Tree_Species_Group")
 (type = 'd', permissions = "rwxrwx---", n = "2", user = "1000", group = "100", size = "4096", date = "Jan 25 2022", name = "Volume")
@sylvaticus sylvaticus changed the title Implemented proposal: improvements to readdir (any dir, oprional details) Implemented proposal: improvements to readdir (any dir, optional details) Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant