c# ftp upload to Linux
Asked Answered
N

1

3

Im trying to check if a directory exists on an FTP server. Before you say "use ListDirectory" or "use PrintWorkingDirectory", they dont always work; for example, I tested if ftp://webserver/Logs existed and both told me it does when it actually doesnt. So Ive gone down the route of uploading a file to the directory and if successful, then the directory exists.

The problem is, the below method doesnt work with GoDaddy's CentOS based server's running vsFTPd 2.0.7.2. Works fine with Microsoft FTP server on IIS7.5.

So I monitored the traffic with Wireshark and used Filezilla to see what it was doing that my App wasnt to make it work. And the only difference is Filezilla is changing the working directory where as I am trying to upload the file with a path before it.

I have a feeling its something to do with the path its uploading to the server and the interpretation by Linux, cause it can be a bit funny with names... :-D Any ideas warmly welcomed?

App code

private bool DirectoryExists(string d)
{
    bool exists = true;
    try
    {
        string file = "directoryexists.test";
        string path = url + homepath + d + "/" + file;

        //Try to save to the directory
        req = (FtpWebRequest)WebRequest.Create(path);
        req.ConnectionGroupName = "conngroup1";
        req.Method = WebRequestMethods.Ftp.UploadFile;
        if (nc != null) req.Credentials = nc;
        if (cbSSL.Checked) req.EnableSsl = true;
        req.Timeout = 10000;

        byte[] fileContents = System.Text.Encoding.Unicode.GetBytes("SAFE TO DELETE");
        req.ContentLength = fileContents.Length;

        Stream s = req.GetRequestStream();
        s.Write(fileContents, 0, fileContents.Length);
        s.Close();

        //Delete file if successful
        req = (FtpWebRequest)WebRequest.Create(path);
        req.ConnectionGroupName = "conngroup1";
        req.Method = WebRequestMethods.Ftp.DeleteFile;
        if (nc != null) req.Credentials = nc;
        if (cbSSL.Checked) req.EnableSsl = true;
        req.Timeout = 10000;

        res = (FtpWebResponse)req.GetResponse();
        res.Close();
    }
    catch (WebException ex)
    {
        exists = false;
    }
    return exists;
}

Filezilla log via Wireshark

Response: 230 Login successful.
Request: CWD /Home/test1
Response: 250 Directory successfully changed.
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,71,209)
Request: LIST
Response: 150 Here comes the directory listing.
FTP Data: 78 bytes
Response: 226 Directory send OK.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,177,1)
Request: STOR directoryexists.txt
Response: 150 Ok to send data.
Response: 226 File receive OK.

App log via Wireshark

Response: 230 Login successful.
Request: OPTS utf8 on
Response: 501 Option not understood.
Request: PWD
Response: 257 "/Home/"
Request: PWD
Response: 257 "/Home/"
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,217,87)
Request: STOR test1/directoryexists.txt
Response: 553 Could not create file.

It creates the folders if they dont exist.

Response: 230 Login successful.
Request: PWD
Response: 257 "/Home/"
Request: PWD
Response: 257 "/Home/"
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,220,60)
Request: STOR Logs/directoryexists.txt
Response: 553 Could not create file.
Request: PWD
Response: 257 "/Home/"
Request: MKD Logs
Response: 257 Create folder operation successful.
Request: TYPE I
Response: 200 Switching to Binary mode.
Request: PASV
Response: 227 Entering Passive Mode (216,69,186,142,255,245)
Request: STOR Logs/LogFiles/directoryexists.txt
Response: 553 Could not create file.
Request: PWD
Response: 257 "/Home/"
Request: MKD Logs/LogFiles
Response: 257 Create folder operation successful.
Neigh answered 2/11, 2011 at 21:58 Comment(4)
Generally 553 errors are permissions related. I see Wireshark picked up STOR test1/directoryexists.txt. Is it possible that you cannot create another nested directory under /Home/ on the FTP server? Some hosting providers limit on how deep you can nest directorie, so you may /not/be/allowed/to/put/a/folder/this/deep or even /this/deep. According to wireshark you are in /Home/ so you should simply just be able to execute STOR directoryexists.txt.Shuttlecock
@Bryan: Thanks for your thoughts but just tested that and worked fine. Response: 257 "/Home/A/B/C/"Neigh
Have you tried setting req.Method to WebRequestMethods.Ftp.MakeDirectory, creating the directory, and then trying to upload the file?Shuttlecock
@Bryan: answered my own question below. Thanks for your input.Neigh
N
4

Linux bites again...

The solution is to set a double slash in the path name so that when it comes to STOR, it has a leading slash... like so:

string url = "ftp://website/";
string homepath = "/Home/";
string d = "test1";
string file = "directoryexists.test";

string path = url + homepath + d + "/" + file;

so the complete path will look like ftp://website//Home/test1/directoryexists.test

req = (FtpWebRequest)WebRequest.Create("ftp://website//Home/test1/directoryexists.test"); 

That way the STOR command will look like

STOR /Home/test1/directoryexists.test

You can get the Home path from StatusDescription

req = (FtpWebRequest)WebRequest.Create(url);
req.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
if (nc != null) req.Credentials = nc;
if (cbSSL.Checked) req.EnableSsl = true;
req.Timeout = 10000;
res = (FtpWebResponse)req.GetResponse();

System.Text.RegularExpressions.Regex regexp = new System.Text.RegularExpressions.Regex("\\s\"([^\"]*)\"\\s");
homepath = regexp.Match(res.StatusDescription).Groups[1].Value;

res.Close();
Neigh answered 2/11, 2011 at 23:51 Comment(3)
Linux isn't an FTP server. But glad you got a solution.Amoritta
Thanks for the obvious Merlyn ;)Neigh
I just had the same problem, and separating the FTP URL from the file path with a double slash worked for me, too. Thanks!Schlesinger

© 2022 - 2024 — McMap. All rights reserved.