Commons Net
  1. Commons Net
  2. NET-449

listFiles bug with folder that begins with "-"

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 3.1
    • Fix Version/s: 3.2
    • Component/s: FTP
    • Labels:
      None

      Description

      FTP Server status:

      root@xxx-srv:/data/Library# tree -A
      .
      ├── -dash
      │   ├── -dash.txt
      │   ├── file1.txt
      │   └── file2.txt
      └── test
          ├── file2.txt
          └── file.txt
      

      Test code:

       
      final FTPClient ftp = new FTPClient();
      ftp.connect(host, port);
      ftp.login(login, pwd);
      System.out.println("PWD: " + ftp.printWorkingDirectory());
      final FTPFile[] listFiles = ftp.listFiles();
      for (int i = 0; i < listFiles.length; i++) {
         System.out.println("[" + i + "] " + listFiles[i]);
      }
      
      System.out.println("Files in /-dash");
      final FTPFile[] listFiles2 = ftp.listFiles("/-dash");
      for (int i = 0; i < listFiles2.length; i++) {
         System.out.println("[" + i + "] " + listFiles2[i]);
      }
      
      System.out.println("Files in -dash");
      final FTPFile[] listFiles3 = ftp.listFiles("-dash");
      for (int i = 0; i < listFiles3.length; i++) {
         System.out.println("[" + i + "] " + listFiles3[i]);
      }
      

      results:

       
      PWD: /
      [0] -dash
      [1] test
      Files in /-dash
      [0] -dash.txt
      [1] file1.txt
      [2] file2.txt
      Files in -dash
      [0] -dash
      [1] .
      [2] ..
      [3] test
      

      When listing "-dash", it list the current directory instead of the destination one.
      If I do the same test with the folder test, this time it works as expected.

        Activity

        Hide
        Sebb added a comment - - edited

        Not sure there's anything Net can do about this.

        The parameter to the LIST command is supposed to be a pathname, and it is up to the server to determine if it is a file or a directory.

        However, the "-" prefix is used by some servers for supporting qualifiers, e.g. -a, -l

        This obviously has the potential to be confused with a valid pathname.

        If the server fails to recognise "-data" as a pathname, then it seems to me that this is a bug in the server.
        It ought to behave according to the RFCs.

        Show
        Sebb added a comment - - edited Not sure there's anything Net can do about this. The parameter to the LIST command is supposed to be a pathname, and it is up to the server to determine if it is a file or a directory. However, the "-" prefix is used by some servers for supporting qualifiers, e.g. -a, -l This obviously has the potential to be confused with a valid pathname. If the server fails to recognise "-data" as a pathname, then it seems to me that this is a bug in the server. It ought to behave according to the RFCs.
        Hide
        Bogdan Drozdowski added a comment -

        Some servers (most?) probably pass the argument to the "ls" command, hence the results. One workaround is provided in the listing above - use absolute paths. Another workaround is: if the path to be listed starts with a dash, prepend a "./" to it, but this will work only on Unix-like servers or those which implement TVFS. So, there would be need to call SYST and FEAT on every connect, but what if the server doesn't support FEAT, TVFS or SYST (or replies that it's not a Unix)?
        I think it should be the user who should decide to use any of these workarounds (they can do it themselves), but the documentation (at least the Javadoc comment) should mention this case.

        Show
        Bogdan Drozdowski added a comment - Some servers (most?) probably pass the argument to the "ls" command, hence the results. One workaround is provided in the listing above - use absolute paths. Another workaround is: if the path to be listed starts with a dash, prepend a "./" to it, but this will work only on Unix-like servers or those which implement TVFS. So, there would be need to call SYST and FEAT on every connect, but what if the server doesn't support FEAT, TVFS or SYST (or replies that it's not a Unix)? I think it should be the user who should decide to use any of these workarounds (they can do it themselves), but the documentation (at least the Javadoc comment) should mention this case.
        Hide
        Sebb added a comment -

        Fix code alignment; simplify.

        Show
        Sebb added a comment - Fix code alignment; simplify.
        Hide
        Sebb added a comment -

        Fixed by updating Javadoc.

        URL: http://svn.apache.org/viewvc?rev=1361541&view=rev
        Log:
        NET-449 listFiles bug with folder that begins with "-". Clarify Javadoc.

        Modified:
        commons/proper/net/trunk/src/changes/changes.xml
        commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java

        Show
        Sebb added a comment - Fixed by updating Javadoc. URL: http://svn.apache.org/viewvc?rev=1361541&view=rev Log: NET-449 listFiles bug with folder that begins with "-". Clarify Javadoc. Modified: commons/proper/net/trunk/src/changes/changes.xml commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java
        Hide
        Sebb added a comment -

        Note: some servers interpret "--" to mean end of options.

        In which case, you can use:

        ftp.listFiles("-- -dash");

        [I added this to the Javadoc]

        Show
        Sebb added a comment - Note: some servers interpret "--" to mean end of options. In which case, you can use: ftp.listFiles("-- -dash"); [I added this to the Javadoc]

          People

          • Assignee:
            Unassigned
            Reporter:
            Stéphane Verger
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development