Skip to content

Add a skip keyword to walkdir function #50531

Open
@mgalenb

Description

@mgalenb

Ive run into errors using Base.walkdir. These errors occur when I've tried to open directories that I don't have permissions for.

I modified walkdir() bellow:

function walkdir(root; topdown=true, follow_symlinks=false, onerror=throw, skip = ['#','.'])
    function _walkdir(chnl, root)
        tryf(f, p) = try
                if any(i->i==first(basename(p)),skip)
                    return
                else
                    f(p)
                end
            catch err
                isa(err, IOError) || rethrow()
                try
                    onerror(err)
                catch err2
                    close(chnl, err2)
                end
                return
            end
        content = tryf(readdir, root)
        content === nothing && return
        dirs = Vector{eltype(content)}()
        files = Vector{eltype(content)}()
        for name in content
            path = joinpath(root, name)

            # If we're not following symlinks, then treat all symlinks as files
            if (!follow_symlinks && something(tryf(islink, path), true)) || !something(tryf(isdir, path), false)
                push!(files, name)
            else
                push!(dirs, name)
            end
        end

        if topdown
            push!(chnl, (root, dirs, files))
        end
        for dir in dirs
            _walkdir(chnl, joinpath(root, dir))
        end
        if !topdown
            push!(chnl, (root, dirs, files))
        end
        nothing
    end
    return Channel{Tuple{String,Vector{String},Vector{String}}}(chnl -> _walkdir(chnl, root))
end

If this is not helpful then plz ignore.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureIndicates new feature / enhancement requestsfilesystemUnderlying file system and functions that use it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions