FtpWebResponse 无法为 Ubuntu 服务器上特定目录名称的 WebRequestMethods.Ftp.MakeDirectory 方法创建目录

FtpWebResponse fails to create directory for WebRequestMethods.Ftp.MakeDirectory method for a specific directory name on Ubuntu server

我正在尝试压力测试 我的 .NET 应用程序通过 FTP(s) 协议上传文件的功能。此功能使用.NET 的内置 FtpWebResponse class(以防用户的服务器不支持 SSH 连接。)我正在使用以下代码尝试创建 "test up1/archive: * OR | or $ or < and >." 目录在我用户目录中的 Ubuntu 服务器上:

//First call succeeds
string strPath = "ftp://webhost.com/%2F/UserDir/" +
    Uri.EscapeDataString("test up1") + "/";
createDirViaFTP(strPath);

//but then this call fails
strPath += Uri.EscapeDataString("archive: * OR | or $ or < and >.") + "/";
createDirViaFTP(strPath);


static bool createDirViaFTP(string strURI)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(strURI);
    request.EnableSsl = bUseSsl;   //can be either false or true
    request.Credentials =  = new NetworkCredential(strUsrName, secUsrPwd);
    request.UseBinary = true;
    request.Timeout = -1;

    request.Method = WebRequestMethods.Ftp.MakeDirectory;

    try
    {
        using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
        {
            if (response.StatusCode == FtpStatusCode.PathnameCreated)
            {
                //Created OK
                return true;
            }
        }
    }
    catch(Exception ex)
    {
        //Failed
        Console.WriteLine("EXCEPTION: Path=" + strURI + "\n" + ex.Message);
    }

    return false;
}

当我尝试创建 "archive: * OR | or $ or < and >." 目录时,对 createDirViaFTP 的第二次调用抛出以下异常:

EXCEPTION: Path=ftp://webhost.com/%2F/UserDir/test%20up1/archive%3A%20*%20OR%20%7C%20or%20%24%20or%20%3C%20and%20%3E./
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

但是为什么呢?我在这里做错了什么?第二个目录名称中的所有这些符号都应该是合法的 Linux 文件名。我可以通过 SSH shell.

创建相同的目录

我无法在 *nix ProFTPD FTP 服务器上创建此类文件夹,即使我可以在 shell.

中的同一系统上创建此类文件夹

在FTP中,我得到

550 archive: * OR | or $ or < and >.: Invalid directory name

这可能也是 FtpWebRequest 得到的。它只是盲目地将任何 550 错误转换为 "File unavailable".

所以我认为这不是您的代码问题,而是 FTP 服务器对 file/directory 名称的限制。

值得注意的是,ProFTPD 无条件禁止在目录名称中使用星号。

core_mkd function:

MODRET core_mkd(cmd_rec *cmd) {
  int res;
  char *decoded_path, *dir;

  CHECK_CMD_MIN_ARGS(cmd, 2);

  /* XXX Why is there a check to prevent the creation of any directory
   * name containing an asterisk?
   */
  if (strchr(cmd->arg, '*')) {
    pr_response_add_err(R_550, _("%s: Invalid directory name"), cmd->arg);

    pr_cmd_set_errno(cmd, EINVAL);
    errno = EINVAL;
    return PR_ERROR(cmd);
  }
  ...

事实上,如果我从目录名称中删除 *,创建就会成功。所以我想,您还使用 ProFTPD FTP 服务器进行测试。