Delphi Indy 中的错误 FTP 列表方法?

Delphi Bug in Indy FTP List method?

我正在尝试生成与特定文件掩码匹配的文件列表,但 Indy 因此错误而失败

EidReplyRFCError with message '.': No such file or directory.

我尝试了几种变体,结果如下:

FTP.List( aFiles, '', true ); => 这行得通

FTP.List( aFiles, '*.*', false ); => 这也有效

FTP.List( aFiles, '*.*', true ); => 这失败了

FTP.List( aFiles, '*.zip', true ); => 这也失败了(尽管它是最新文档中的示例)

FTP.List( '*.*', false ); => 这行得通

FTP.List( '*.*', true ); => 这失败了

我正在使用 Delphi XE5 和 Indy 10.6 版。如果相关,XE8 中也存在同样的问题。

也许功能已经改变并且文档现在是错误的或者它是 Indy 中的错误?

我需要 "details" 这样我也可以比较时间戳和大小。

这不是 TIdFTP 中的错误。这更多是 Indy 文档中的遗漏。

EIdReplyRFCError 表示 FTP 服务器本身正在报告错误以响应 TIdFTP.List() 发送的命令。根据 ADetails 参数和 TIdFTPUseMLIS+CanUseMLS 属性的值,List() 可以发送三种不同命令之一:

ADetails=False:

    NLST [ASpecifier]

ADetails=True:

  TIdFTP.UseMLIS=True and TIdFTP.CanUseMLS=True:

    MLSD [ASpecifier]

  TIdFTP.UseMLIS=False or TIdFTP.CanUseMLS=False:

    LIST [ASpecifier]

因此:

FTP.List( aFiles, '', true ); // this works
// sends either 'LIST' or 'MLSD'

FTP.List( aFiles, '*.*', false ); // this works too
// sends 'NLST *.*'

FTP.List( aFiles, '*.*', true ); // this fails
// sends either 'LIST *.*' or 'MLSD *.*'

FTP.List( aFiles, '*.zip', true ); // this fails too
// sends either 'LIST *.zip' or 'MLSD *.zip'

FTP.List( '*.*', false ); // this works
// sends 'NLST *.*'

FTP.List( '*.*', true ); // this fails
// sends either 'LIST *.*' or 'MLSD *.*'

请注意,所有“失败”的命令都有一些共同点 - 它们可能会发送 MLSD ASpecifier 命令。

根据 RFC 959,它定义了 LISTNLST 命令:

LIST (LIST)

This command causes a list to be sent from the server to the passive DTP. If the pathname specifies a directory or other group of files, the server should transfer a list of files in the specified directory. If the pathname specifies a file then the server should send current information on the file. A null argument implies the user's current working or default directory. ...

NAME LIST (NLST)

This command causes a directory listing to be sent from server to user site. The pathname should specify a directory or other system-specific file group descriptor; a null argument implies the current directory. ...

根据 RFC 3659,它定义了 MLSD 命令:

The MLST and MLSD commands each allow a single optional argument. This argument may be either a directory name or, for MLST only, a file name. For these purposes, a "file name" is the name of any entity in the server NVFS which is not a directory. Where TVFS is supported, any TVFS relative pathname valid in the current working directory, or any TVFS fully qualified pathname, may be given. If a directory name is given then MLSD must return a listing of the contents of the named directory, otherwise it issues a 501 reply, and does not open a data connection. ...

If no argument is given then MLSD must return a listing of the contents of the current working directory, and MLST must return a listing giving information about the current working directory itself. ...

...

If the Client-FTP sends an invalid argument, the server-FTP MUST reply with an error code of 501.

*.**.zip 不是目录名,因此如果 TIdFTP.List() 发送 MLSD *.*MLSD *.zip 命令,服务器将失败。因此,按理说 TIdFTP.UseMLISTIdFTP.CanUseMLS 在您的情况下可能都是 True(UseMLIS 默认为 True,CanUseMLS 在现代 FTP 上通常为 True ] 服务器)。

MLSD 命令不像 LIST/NLST 命令那样支持服务器端过滤。因此,您不能将 *.**.zipMLSD 一起使用。您将不得不检索完整的目录列表,然后忽略您不感兴趣的任何条目。否则,在调用 TIdFTP.List() 之前将 TIdFTP.UseMLIS 设置为 False,但是您 运行 的风险TIdFTP.DirectoryListing 错误地解析了某些服务器的目录列表,因为 LIST 命令使用的格式从未标准化,并且在 Internet 上使用了数百种自定义格式(以及为什么 TIdFTP Indy 10 在使用 LIST 时包含许多列表解析器)。不像 MLSx,它有一个标准化的格式(这就是为什么它首先被引入,以取代 LIST 的缺点)。

因此,这一切归结为 - 当 TIdFTP.UseMLISTIdFTP.CanUseMLS 均为 True 时,ASpecifier 必须 为空或一个目录,不是一个文件掩码。

TIdFTP.List() documentation does state that List() may internally call TIdFTP.ExtListDir() 发送一个 MLSD 命令,但是在那种情况下它没有特别提到对 ASpecifier 参数的这个特殊限制:

If CanUseMLS contains True, the ExtListDir is called to capture and store the results of the FTP MLSD command in the ADest parameter variable instead of the LIST or NLST commands. No additional processing is performed in the List method under this circumstance, and the method is exited.

When ADetails is False, only the file or directory name is returned in the ADest string list using the FTP NLST command. When ADetails is True, List can return FTP server-dependent details including the file size, date modified, and file permissions for the Owner, Group, and User using the FTP LIST command.

TIdFTP.ExtListDir() documentation 确实声明它的输入参数必须是一个目录名,但是:

The MLSD command, supported in ExtListDir, accepts an optional directory name or relative path in Adirectory for the directory listing. If am empty string is passed in ADirectory, the current directory is used for the directory listing operation.

附带说明:TIdFTP.DirFormat 属性 会告诉您 TIdFTP.DirectoryListing 解析结果后检测到的列表格式。或者您可以查看 TListFTP.ListResultDetailsUsedMLS 属性(将其类型转换为 TIdFTPListResult 以访问属性)以推断 [=14 发送了哪个命令=](如果成功)。

另一种解决方案是包括

IdAllFTPListParsers

在您的 uses 子句中禁用 UseMLIS

像这样:

uses
  ....
  IdAllFTPListParsers;
   .....
procedure TForm1.DoThis;
var
  i: integer;
begin
  if not IDFTP1.Connected then IDFTP1.Connect;
  IDFTP1.UseMLIS:= false;
  IDFTP1.List;
  for i:= 0 to IDFTP1.DirectoryListing.Count -1 do begin
    .. process directory items.
    IdFTP1.TransferType:= ftBinary;
    ..Get your files