From 7cd5d576d9ab2fa82d4144a69ae381415e87360c Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Thu, 18 Jan 2018 23:18:55 -0600 Subject: [PATCH] Updated common/config.go's FileExistsLocally implementation to use the LocalDownloader interface for determining the real file path. --- common/config.go | 53 +++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/common/config.go b/common/config.go index a8ec7d16a..e3fd4baf8 100644 --- a/common/config.go +++ b/common/config.go @@ -177,27 +177,38 @@ func ValidatedURL(original string) (string, error) { func FileExistsLocally(original string) bool { // original should be something like file://C:/my/path.iso + u, _ := url.Parse(original) - fileURL, _ := url.Parse(original) - fileExists := false - - if fileURL.Scheme == "file" { - // on windows, correct URI is file:///c:/blah/blah.iso. - // url.Parse will pull out the scheme "file://" and leave the path as - // "/c:/blah/blah/iso". Here we remove this forward slash on absolute - // Windows file URLs before processing - // see https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/ - // for more info about valid windows URIs - filePath := fileURL.Path - if runtime.GOOS == "windows" && len(filePath) > 0 && filePath[0] == '/' { - filePath = filePath[1:] - } - _, err := os.Stat(filePath) - if err != nil { - return fileExists - } else { - fileExists = true - } + // First create a dummy downloader so we can figure out which + // protocol to use. + cli := NewDownloadClient(&DownloadConfig{}) + d, ok := cli.config.DownloaderMap[u.Scheme] + if !ok { + return false + } + + // Check to see that it's got a Local way of doing things. + local, ok := d.(LocalDownloader) + if !ok { + return false + } + + // Figure out where we're at. + wd, err := os.Getwd() + if err != nil { + return false + } + + // Now figure out the real path to the file. + realpath, err := local.toPath(wd, *u) + if err != nil { + return false + } + + // Finally we can seek the truth via os.Stat. + _, err = os.Stat(realpath) + if err != nil { + return false } - return fileExists + return true }