* Bug #12976 fixed - getURL returns a file name instead of a file path 53/12853/2
Simon Marchetto [Wed, 30 Oct 2013 11:13:27 +0000 (12:13 +0100)]
Change-Id: I3e8995b607551c64c55ee7739cfde71b7ae453fb

scilab/CHANGES_5.5.X
scilab/modules/fileio/src/c/dlManager.c
scilab/modules/fileio/tests/nonreg_tests/bug_12971.dia.ref
scilab/modules/fileio/tests/nonreg_tests/bug_12971.tst
scilab/modules/fileio/tests/nonreg_tests/bug_12976.dia.ref [new file with mode: 0644]
scilab/modules/fileio/tests/nonreg_tests/bug_12976.tst [new file with mode: 0644]
scilab/modules/fileio/tests/unit_tests/getURL.dia.ref
scilab/modules/fileio/tests/unit_tests/getURL.tst

index e5b305d..355fd2d 100644 (file)
@@ -67,6 +67,8 @@ Scilab Bug Fixes
 
 * Bug #12971 fixed - getURL downloaded file name was wrong.
 
+* Bug #12976 fixed - getURL returns a file name instead of a file path.
+
 * Bug #12978 fixed - exportUI returned an error when input argument was a figure handle.
 
 * Bug #12979 fixed - exportUI did not work with vectorial export.
index 77de90e..ef05210 100644 (file)
@@ -25,6 +25,9 @@
 #include "charEncoding.h"
 #include "localization.h"
 #include "getos.h"
+#include "machine.h"
+#include "scicurdir.h"
+#include "splitpath.h"
 /* ==================================================================== */
 #ifndef HAVE_BASENAME
 static char *Curl_basename(char *path);
@@ -345,6 +348,8 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
     char *filename = NULL;
     FILE *file;
     inputString buffer;
+    char *destdir = NULL;
+    char *destfile = NULL;
 
     curl = curl_easy_init();
     if (curl == NULL)
@@ -362,31 +367,84 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
         return NULL;
     }
 
-    if (dest == NULL)
+    // Get destination directory and filename
+    if (dest != NULL)
     {
-        /* No second argument provided */
-        filename = getFileNameFromURL(url);
+        // Destination is specified in argument
+        char* pathdrive = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
+        char* pathdir = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
+        char* pathfile = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
+        char* pathext = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
+
+        splitpath(dest, TRUE, pathdrive, pathdir, pathfile, pathext);
+
+        if (!isdir(dest))
+        {
+            // Destination is a file
+            destdir = (char *)MALLOC((strlen(pathdrive) + strlen(pathdir) + 1) * sizeof(char));
+            strcpy(destdir, pathdrive);
+            strcat(destdir, pathdir);
+
+            // Get filename
+            destfile = (char *)MALLOC((strlen(pathfile) + strlen(pathext) + 1) * sizeof(char));
+            strcpy(destfile, pathfile);
+            strcat(destfile, pathext);
+        }
+        else
+        {
+            // Destination is a directory
+            destdir = (char *)MALLOC((strlen(pathdrive) + strlen(pathdir) + strlen(pathfile) + strlen(pathext) + strlen(DIR_SEPARATOR) + 1) * sizeof(char));
+            strcpy(destdir, pathdrive);
+            strcat(destdir, pathdir);
+            strcat(destdir, pathfile);
+            strcat(destdir, pathext);
+            strcat(destdir, DIR_SEPARATOR);
+
+            // Retrieve filename from URL
+            destfile = getFileNameFromURL(url);
+        }
+
+        FREE(pathdrive);
+        FREE(pathdir);
+        FREE(pathfile);
+        FREE(pathext);
     }
     else
     {
-        if (isdir(dest))
+        // Destination is not specified in argument
+        // Destination directory is current dir
+        int err = 0;
+        char *currentdir;
+        currentdir = scigetcwd(&err);
+        if (!err)
         {
-            /* The target is a directory. Select the name from the URL */
-            char *name = getFileNameFromURL(url);
-
-            filename = (char *)MALLOC((strlen(name) + strlen("/") + strlen(dest) + 1) * sizeof(char));
-            strcpy(filename, dest);
-            strcat(filename, "/");
-            strcat(filename, name);
-            FREE(name);
+            destdir = (char *)MALLOC((strlen(currentdir) + strlen(DIR_SEPARATOR) + 1) * sizeof(char));
+            strcpy(destdir, currentdir);
+            strcat(destdir, DIR_SEPARATOR);
+            FREE(currentdir);
         }
         else
         {
-            filename = (char *)MALLOC((strlen(dest) + 1) * sizeof(char));
-            strcpy(filename, dest);
+            Scierror(999, _("Failed getting current dir, error code: %d\n"), err);
+            return NULL;
         }
+
+        // Destination filename retrieved from URL
+        destfile = getFileNameFromURL(url);
     }
 
+    if (destfile == NULL)
+    {
+        return NULL;
+    }
+
+    // Build file path
+    filename = (char *)MALLOC((strlen(destdir) + strlen(destfile) + 1) * sizeof(char));
+    strcpy(filename, destdir);
+    strcat(filename, destfile);
+    FREE(destdir);
+    FREE(destfile);
+
     res = curl_easy_setopt(curl, CURLOPT_URL, url);
     if (res != CURLE_OK)
     {
index eb84a80..f528121 100644 (file)
 //
 // <-- Short Description -->
 // getURL() downloaded file name is wrong
-function checkFile(filename, expectedFileName, minimalFileSize)
-    assert_checkequal(filename, expectedFileName)
-    info = fileinfo(filename);
-    assert_checktrue(info(1) > minimalFileSize);
-    deletefile(filename);
-endfunction
 curdir = pwd();
 cd(TMPDIR);
-filename = getURL("www.scilab.org");
-expectedFileName = "index.html";
-assert_checkequal(filename, expectedFileName);
-assert_checktrue(isfile(expectedFileName));
-deletefile(expectedFileName);
+filePath = getURL("www.scilab.org");
+expectedFilePath = fullfile(TMPDIR, "index.html");
+assert_checkequal(filePath, expectedFilePath);
+assert_checktrue(isfile(filePath));
+deletefile(filePath);
 cd(curdir);
index 673cdcf..711ea39 100644 (file)
 // <-- Short Description -->
 // getURL() downloaded file name is wrong
 
-function checkFile(filename, expectedFileName, minimalFileSize)
-    assert_checkequal(filename, expectedFileName)
-    info = fileinfo(filename);
-    assert_checktrue(info(1) > minimalFileSize);
-    deletefile(filename);
-endfunction
-
 curdir = pwd();
 cd(TMPDIR);
 
-filename = getURL("www.scilab.org");
+filePath = getURL("www.scilab.org");
 
-expectedFileName = "index.html";
-assert_checkequal(filename, expectedFileName);
-assert_checktrue(isfile(expectedFileName));
+expectedFilePath = fullfile(TMPDIR, "index.html");
+assert_checkequal(filePath, expectedFilePath);
+assert_checktrue(isfile(filePath));
 
-deletefile(expectedFileName);
+deletefile(filePath);
 cd(curdir);
\ No newline at end of file
diff --git a/scilab/modules/fileio/tests/nonreg_tests/bug_12976.dia.ref b/scilab/modules/fileio/tests/nonreg_tests/bug_12976.dia.ref
new file mode 100644 (file)
index 0000000..fba3b08
--- /dev/null
@@ -0,0 +1,23 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+// <-- CLI SHELL MODE -->
+// <-- Non-regression test for bug 12976 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=12976
+//
+// <-- Short Description -->
+// getURL() returns a file name instead of a file path
+curdir = pwd();
+cd(TMPDIR);
+filepath = getURL("http://www.scilab.org");
+assert_checkequal(filepath, fullfile(TMPDIR, "index.html"));
+deletefile(filepath);
+filepath = getURL("http://www.scilab.org", fullfile(TMPDIR, "scilab_homepage.html"));
+assert_checkequal(filepath, fullfile(TMPDIR, "scilab_homepage.html"));
+deletefile(filepath);
+cd(curdir);
diff --git a/scilab/modules/fileio/tests/nonreg_tests/bug_12976.tst b/scilab/modules/fileio/tests/nonreg_tests/bug_12976.tst
new file mode 100644 (file)
index 0000000..5cb36d6
--- /dev/null
@@ -0,0 +1,29 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+
+// <-- CLI SHELL MODE -->
+
+// <-- Non-regression test for bug 12976 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=12976
+//
+// <-- Short Description -->
+// getURL() returns a file name instead of a file path
+
+curdir = pwd();
+cd(TMPDIR);
+
+filepath = getURL("http://www.scilab.org");
+assert_checkequal(filepath, fullfile(TMPDIR, "index.html"));
+deletefile(filepath);
+
+filepath = getURL("http://www.scilab.org", fullfile(TMPDIR, "scilab_homepage.html"));
+assert_checkequal(filepath, fullfile(TMPDIR, "scilab_homepage.html"));
+deletefile(filepath);
+
+cd(curdir);
index 07cfc0b..5c9c843 100644 (file)
@@ -5,65 +5,67 @@
 //
 //  This file is distributed under the same license as the Scilab package.
 // =============================================================================
-function checkFile(filename, expectedFileName, minimalFileSize)
-    assert_checkequal(filename, expectedFileName)
-    info = fileinfo(filename);
+function checkFile(filePath, expectedFilePath, minimalFileSize)
+    assert_checkequal(filePath, expectedFilePath)
+    info = fileinfo(filePath);
     assert_checktrue(info(1) > minimalFileSize);
-    deletefile(filename);
+    deletefile(filePath);
 endfunction
-function checkFileAndContent(filename, expectedFileName, minimalFileSize, keywordToFind)
-       fd = mopen(filename, "r");
+function checkFileAndContent(filePath, expectedFilePath, minimalFileSize, keywordToFind)
+       fd = mopen(filePath, "r");
        assert_checktrue(grep(mgetl(fd), keywordToFind) <> []);
        mclose(fd);
-       checkFile(filename, expectedFileName, minimalFileSize);
+       checkFile(filePath, expectedFilePath, minimalFileSize);
 endfunction
-function checkContent(filename, content, keyword, keywordToFind)
+function checkContent(filePath, content, keyword, keywordToFind)
        assert_checktrue(length(content) > 10);
        assert_checktrue(grep(content, keywordToFind) <> []);
-       deletefile(filename);
+       deletefile(filePath);
 endfunction
-cd TMPDIR;
+curdir = pwd();
+destdir = fullfile(TMPDIR, "getURL");
+mkdir(destdir);
+cd(destdir);
 // Check downloaded file
-filename = getURL("http://www.scilab.org");
-checkFile(filename, "index.html", 1000);
-filename = getURL("http://www.scilab.org/");
-checkFile(filename, "index.html", 1000);
-filename = getURL("http://www.scilab.org/product/man/derivative.html");
-checkFile(filename, "derivative.html", 1000);
-filename = getURL("www.scilab.org");
-checkFile(filename, "index.html", 1000);
-filename = getURL("www.scilab.org/product/man/derivative.html");
-checkFile(filename, "derivative.html", 1000);
-filename = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README");
-checkFile(filename, "README", 10);
-targetFile = TMPDIR + "/README_Debian";
-filename = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README", targetFile);
-checkFileAndContent(filename, targetFile, 10, "Linux");
-filename = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README", TMPDIR);
-checkFileAndContent(filename, TMPDIR + "/README", 10, "Linux");
+filePath = getURL("http://www.scilab.org");
+checkFile(filePath, fullfile(destdir, "index.html"), 1000);
+filePath = getURL("http://www.scilab.org/");
+checkFile(filePath, fullfile(destdir, "index.html"), 1000);
+filePath = getURL("http://www.scilab.org/product/man/derivative.html");
+checkFile(filePath, fullfile(destdir, "derivative.html"), 1000);
+filePath = getURL("www.scilab.org");
+checkFile(filePath, fullfile(destdir, "index.html"), 1000);
+filePath = getURL("www.scilab.org/product/man/derivative.html");
+checkFile(filePath, fullfile(destdir, "derivative.html"), 1000);
+filePath = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README");
+checkFile(filePath, fullfile(destdir, "README"), 10);
+filePath = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README", fullfile(destdir, "README_Debian"));
+checkFileAndContent(filePath, fullfile(destdir, "README_Debian"), 10, "Linux");
+filePath = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README", destdir);
+checkFileAndContent(filePath, fullfile(destdir, "README"), 10, "Linux");
 // HTTPS
-filename = getURL("https://encrypted.google.com");
-checkFileAndContent(filename, "index.html", 100, "html");
-targetFile = TMPDIR+"/testauth";
-filename = getURL("http://httpbin.org/basic-auth/user/passwd", targetFile, "user", "passwd");
-checkFileAndContent(filename, targetFile, 10, "authenticated");
+filePath = getURL("https://encrypted.google.com");
+checkFileAndContent(filePath, fullfile(destdir, "index.html"), 100, "html");
+filePath = getURL("http://httpbin.org/basic-auth/user/passwd", fullfile(destdir, "testauth"), "user", "passwd");
+checkFileAndContent(filePath, fullfile(destdir, "testauth"), 10, "authenticated");
 // Check returned content
-[f, content] = getURL("http://www.scilab.org:80");
-checkContent(f, content, 1000, "html");
-[f, content] = getURL("http://plop:ae@www.scilab.org:80");
-checkContent(f, content, 1000, "html");
-[f, content] = getURL("http://www.scilab.org/aze");
-checkContent(f, content, 100, "aze");
-[f, content] = getURL("http://www.scilab.org");
-checkContent(f, content, 1000, "html");
-[f, content] = getURL("http://www.scilab.org/");
-checkContent(f, content, 1000, "html");
-[f, content] = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README");
-checkContent(f, content, 10, "Linux");
+[filePath, content] = getURL("http://www.scilab.org:80");
+checkContent(filePath, content, 1000, "html");
+[filePath, content] = getURL("http://plop:ae@www.scilab.org:80");
+checkContent(filePath, content, 1000, "html");
+[filePath, content] = getURL("http://www.scilab.org/aze");
+checkContent(filePath, content, 100, "aze");
+[filePath, content] = getURL("http://www.scilab.org");
+checkContent(filePath, content, 1000, "html");
+[filePath, content] = getURL("http://www.scilab.org/");
+checkContent(filePath, content, 1000, "html");
+[filePath, content] = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README");
+checkContent(filePath, content, 10, "Linux");
 // HTTPS
-[f, content] = getURL("https://encrypted.google.com");
-checkContent(f, content, 100, "html");
-[f, content] = getURL("http://httpbin.org/basic-auth/user/passwd", TMPDIR, "user", "passwd");
-checkContent(f, content, 10, "authenticated");
+[filePath, content] = getURL("https://encrypted.google.com");
+checkContent(filePath, content, 100, "html");
+[filePath, content] = getURL("http://httpbin.org/basic-auth/user/passwd", destdir, "user", "passwd");
+checkContent(filePath, content, 10, "authenticated");
 // Badly formated URL
 assert_checkerror("getURL(''http://plop@ae:www.scilab.org:80'');", [], 999);
+cd(curdir);
index 247e40c..0e37798 100644 (file)
@@ -6,88 +6,90 @@
 //  This file is distributed under the same license as the Scilab package.
 // =============================================================================
 
-function checkFile(filename, expectedFileName, minimalFileSize)
-    assert_checkequal(filename, expectedFileName)
-    info = fileinfo(filename);
+function checkFile(filePath, expectedFilePath, minimalFileSize)
+    assert_checkequal(filePath, expectedFilePath)
+    info = fileinfo(filePath);
     assert_checktrue(info(1) > minimalFileSize);
-    deletefile(filename);
+    deletefile(filePath);
 endfunction
 
-function checkFileAndContent(filename, expectedFileName, minimalFileSize, keywordToFind)
-       fd = mopen(filename, "r");
+function checkFileAndContent(filePath, expectedFilePath, minimalFileSize, keywordToFind)
+       fd = mopen(filePath, "r");
        assert_checktrue(grep(mgetl(fd), keywordToFind) <> []);
        mclose(fd);
-       checkFile(filename, expectedFileName, minimalFileSize);
+       checkFile(filePath, expectedFilePath, minimalFileSize);
 endfunction
 
-function checkContent(filename, content, keyword, keywordToFind)
+function checkContent(filePath, content, keyword, keywordToFind)
        assert_checktrue(length(content) > 10);
        assert_checktrue(grep(content, keywordToFind) <> []);
-       deletefile(filename);
+       deletefile(filePath);
 endfunction
 
-cd TMPDIR;
+curdir = pwd();
+destdir = fullfile(TMPDIR, "getURL");
+mkdir(destdir);
+cd(destdir);
 
 // Check downloaded file
-filename = getURL("http://www.scilab.org");
-checkFile(filename, "index.html", 1000);
+filePath = getURL("http://www.scilab.org");
+checkFile(filePath, fullfile(destdir, "index.html"), 1000);
 
-filename = getURL("http://www.scilab.org/");
-checkFile(filename, "index.html", 1000);
+filePath = getURL("http://www.scilab.org/");
+checkFile(filePath, fullfile(destdir, "index.html"), 1000);
 
-filename = getURL("http://www.scilab.org/product/man/derivative.html");
-checkFile(filename, "derivative.html", 1000);
+filePath = getURL("http://www.scilab.org/product/man/derivative.html");
+checkFile(filePath, fullfile(destdir, "derivative.html"), 1000);
 
-filename = getURL("www.scilab.org");
-checkFile(filename, "index.html", 1000);
+filePath = getURL("www.scilab.org");
+checkFile(filePath, fullfile(destdir, "index.html"), 1000);
 
-filename = getURL("www.scilab.org/product/man/derivative.html");
-checkFile(filename, "derivative.html", 1000);
+filePath = getURL("www.scilab.org/product/man/derivative.html");
+checkFile(filePath, fullfile(destdir, "derivative.html"), 1000);
 
-filename = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README");
-checkFile(filename, "README", 10);
+filePath = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README");
+checkFile(filePath, fullfile(destdir, "README"), 10);
 
-targetFile = TMPDIR + "/README_Debian";
-filename = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README", targetFile);
-checkFileAndContent(filename, targetFile, 10, "Linux");
+filePath = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README", fullfile(destdir, "README_Debian"));
+checkFileAndContent(filePath, fullfile(destdir, "README_Debian"), 10, "Linux");
 
-filename = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README", TMPDIR);
-checkFileAndContent(filename, TMPDIR + "/README", 10, "Linux");
+filePath = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README", destdir);
+checkFileAndContent(filePath, fullfile(destdir, "README"), 10, "Linux");
 
 // HTTPS
-filename = getURL("https://encrypted.google.com");
-checkFileAndContent(filename, "index.html", 100, "html");
+filePath = getURL("https://encrypted.google.com");
+checkFileAndContent(filePath, fullfile(destdir, "index.html"), 100, "html");
 
-targetFile = TMPDIR+"/testauth";
-filename = getURL("http://httpbin.org/basic-auth/user/passwd", targetFile, "user", "passwd");
-checkFileAndContent(filename, targetFile, 10, "authenticated");
+filePath = getURL("http://httpbin.org/basic-auth/user/passwd", fullfile(destdir, "testauth"), "user", "passwd");
+checkFileAndContent(filePath, fullfile(destdir, "testauth"), 10, "authenticated");
 
 // Check returned content
-[f, content] = getURL("http://www.scilab.org:80");
-checkContent(f, content, 1000, "html");
+[filePath, content] = getURL("http://www.scilab.org:80");
+checkContent(filePath, content, 1000, "html");
 
-[f, content] = getURL("http://plop:ae@www.scilab.org:80");
-checkContent(f, content, 1000, "html");
+[filePath, content] = getURL("http://plop:ae@www.scilab.org:80");
+checkContent(filePath, content, 1000, "html");
 
-[f, content] = getURL("http://www.scilab.org/aze");
-checkContent(f, content, 100, "aze");
+[filePath, content] = getURL("http://www.scilab.org/aze");
+checkContent(filePath, content, 100, "aze");
 
-[f, content] = getURL("http://www.scilab.org");
-checkContent(f, content, 1000, "html");
+[filePath, content] = getURL("http://www.scilab.org");
+checkContent(filePath, content, 1000, "html");
 
-[f, content] = getURL("http://www.scilab.org/");
-checkContent(f, content, 1000, "html");
+[filePath, content] = getURL("http://www.scilab.org/");
+checkContent(filePath, content, 1000, "html");
 
-[f, content] = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README");
-checkContent(f, content, 10, "Linux");
+[filePath, content] = getURL("ftp://ftp.free.fr/pub/Distributions_Linux/debian/README");
+checkContent(filePath, content, 10, "Linux");
 
 // HTTPS
-[f, content] = getURL("https://encrypted.google.com");
-checkContent(f, content, 100, "html");
+[filePath, content] = getURL("https://encrypted.google.com");
+checkContent(filePath, content, 100, "html");
 
-[f, content] = getURL("http://httpbin.org/basic-auth/user/passwd", TMPDIR, "user", "passwd");
-checkContent(f, content, 10, "authenticated");
+[filePath, content] = getURL("http://httpbin.org/basic-auth/user/passwd", destdir, "user", "passwd");
+checkContent(filePath, content, 10, "authenticated");
 
 // Badly formated URL
 assert_checkerror("getURL(''http://plop@ae:www.scilab.org:80'');", [], 999);
 
+cd(curdir);