Created
October 31, 2012 14:42
-
-
Save aaronwolen/3987414 to your computer and use it in GitHub Desktop.
FTP tree mapper: save an FTP site's directory stucture as a list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' FTP tree mapper | |
#' Save an FTP site's directory stucture as a list. | |
#' @author Aaron Wolen | |
#' | |
#' @example | |
#' url <- 'ftp://ftp.genboree.org/EpigenomeAtlas/Current-Release/experiment-sample' | |
#' roadmap <- map_ftp(url = url, dirs = "Histone_H2BK120ac", recursive = TRUE) | |
map_ftp <- function(url, dirs, recursive = FALSE) { | |
require(RCurl, quietly = TRUE) | |
# Add trailing forward slash | |
add_fslash <- function(x) sub("/*$", "/", x) | |
# Verify required curl options | |
list.opts <- c("dirlistonly", "ftplistonly") | |
curl.opts <- listCurlOptions() | |
if ( any(list.opts %in% curl.opts) ) { | |
opts <- list() | |
opts[[list.opts[list.opts %in% curl.opts][1]]] <- TRUE | |
} else { | |
stop("Your version of curl lacks the listonly option.", call. = FALSE) | |
} | |
# Print file list in FTP directory | |
list_files <- function(url) { | |
url.con <- textConnection(getURL(url, ftp.use.epsv = FALSE, .opts = opts)) | |
url.out <- readLines(url.con) | |
close(url.con) | |
url.out <- url.out[url.out != ""] | |
return(url.out) | |
} | |
url <- add_fslash(url) | |
if (missing(dirs)) dirs <- list_files(url) | |
tree <- list() | |
for(d in dirs) { | |
d.url <- add_fslash(paste(url, d, sep = "")) | |
if (!url.exists(d.url)) next | |
d.out <- list_files(d.url) | |
if(recursive) { | |
# Test if each string represents a file or a new directory | |
d.out.urls <- paste(d.url, d.out, sep = "") | |
is.file <- sapply(d.out.urls, url.exists) | |
is.dir <- sapply(add_fslash(d.out.urls), url.exists) | |
d.files <- d.out[is.file & !is.dir] | |
d.dirs <- setdiff(d.out[is.dir], d.files) | |
tree[[d]] <- d.files | |
if (length(d.dirs) > 0) { | |
tree[[d]] <- c(tree[[d]], ftp_tree(d.url, dirs = d.dirs, recursive)) | |
} | |
} else { | |
tree[[d]] <- d.out | |
} | |
} | |
return(tree) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment