This project is read-only.

Does SftpClient.UploadFile overwrite existing files?

Apr 5, 2011 at 10:03 PM

Seemingly simple question - does the SftpClient.UploadFile() method overwrite existing files on the destination server?

I have tried to test, but get mixed results.  I am not certain whether the issue is with this method and it not overwriting the file, or if the issue is with the server and file permissions.  I only have control over the program at this point, so until I get definitive word from the admins on the server side, I thought I'd post here and see.

Thanks!  Great library, really appreciate the effort!

Apr 14, 2011 at 5:38 PM

Sorry for getting back to you so late, was away for a while and then busy with other work.

 

At a moment, when you try to upload the file it opens it using following mode:

Flags.Write | Flags.CreateNewOrOpen | Flags.Truncate

 

So to answer your question, it will overwrite an existing file.

 

Please let me know if you have any suggestions in this functionality and I will try to incorporate it in a future or actualy please open it as a bug, this way I know to get back to it.

 

Thanks,

Oleg

Aug 26, 2011 at 6:54 AM
Edited Aug 26, 2011 at 6:56 AM

Hi,

I had a issue to  Overwrite the file in hosting server.

RequestOpen(fullPath, Flags.Write | Flags.CreateNewOrOpen | Flag.Truncate);

I am getting exception if i try to overwrite the existing file in server.

i tried to change the flag but not able to success.

Is it possible to overwrite the file in hosting server.

 

Thanks

Rambhopal

Nov 29, 2012 at 11:31 AM
Edited Nov 29, 2012 at 11:32 AM

I'm digging up this question because I kind of encounter the same issue.

The library is set by default when using the OpenFile or UploadFile, to use the Flags.CreateNewOrOpen or Flags.CreateNew, the problem with this approach is that file with a smaller size than the original one doesn't overwrite the existing files.

Since the file already exists and it will use Flags.CreateNewOrOpen, so if you upload a file with a smaller size, you final file will contain the data of the new file and the rest of the data of the previous file.

I think it should default to Flags.CreateNew or Flags.Truncate, and it should be our work to use Flags.CreateNewOrOpen or Flags.Append when we want to "resume" a file.

Dec 29, 2012 at 1:09 AM

Hi,

Sorry for getting back to you that late.

 

I tried to reproduce the situation you described but with no luck.

Also, all upload operations I start with those flags Flags.Write | Flags.Truncate

So it will always truncate the file when uploading.

 

Can you please post the code which will demonstrate uploading file, overwriting it so it will have pieces from previous file in it?

 

Thanks,

Oleg

Dec 31, 2012 at 10:19 AM
Edited Dec 31, 2012 at 10:20 AM

Here is a project that reproduce the problem.

I provided files in order to test, and a final.txt file, that correspond to the result.

You just need to change the connections information and file path.

http://cl.ly/2n2R0I0y0K3K

As you can see, this occurs only when the file uploaded is smaller than the existing file on the server (and with the same name ^^).

As I said before, if I use client.Open(filePath, FileMode.Created, FileAccess.Write) instead of client.OpenWrite(filePath), there is no problem.
But may be this is a wanted behavior.

Jan 3, 2013 at 10:32 PM

Hi,

 

Thanks for project example,

I will try it tomorrow and see what I can find.

 

Thanks,

Oleg

Jan 4, 2013 at 2:52 AM

Hi,

Ok, So I looked at it and to achive what you want to to you need to use Open method instead of OpenWrite.

I tried to implemented OpenWrite method similar to File.OpenWrite.

So its behaviour is the same as if you were to use regular files.

I modified your code and ran this but received the same result.

           client.HostKeyReceived += (s, e) =>
            {
                e.CanTrust = true;
            };
            client.Connect();

            //SftpFileStream _fileStream = client.OpenWrite("final.txt");
            //var _fileStream = client.Open("final.txt", FileMode.OpenOrCreate | FileMode.Truncate);
            FileStream _fileStream = File.OpenWrite(@"final1.txt");
            FileStream _localFileStream = File.Open(@"big.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            int transferred = 0;
            int totalToTransfer = 1495368;
            int lengthTransferred = 0;
            while (transferred < totalToTransfer)
            {

                byte[] readBytes = new byte[65536];
                lengthTransferred = _localFileStream.Read(readBytes, 0, 65536);
                _fileStream.Write(readBytes, 0, lengthTransferred);

                transferred += lengthTransferred;
            }

            _localFileStream.Close();
            _fileStream.Close();

            //_fileStream = client.OpenWrite("final.txt");
            //_fileStream = client.Open("final.txt", FileMode.OpenOrCreate | FileMode.Truncate);
            //_fileStream = File.Open(@"final1.txt", FileMode.OpenOrCreate);
            _fileStream = File.OpenWrite(@"final1.txt");
            _localFileStream = File.Open(@"small.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            transferred = 0;
            totalToTransfer = 128832;
            lengthTransferred = 0;
            while (transferred < totalToTransfer)
            {

                byte[] readBytes = new byte[65536];
                lengthTransferred = _localFileStream.Read(readBytes, 0, 65536);
                _fileStream.Write(readBytes, 0, lengthTransferred);

                transferred += lengthTransferred;
            }

            _localFileStream.Close();
            _fileStream.Close();
        }

If you like, you can use Open method where you can explicitly specify to truncate a new file.

 

Please let me know if you have any more questions or suggestions.

 

Thanks,

Oleg

 

OpenWrite
Jan 4, 2013 at 8:45 AM

Hi!

Thank you for your answer, I already use Open instead of OpenWrite.

I was just wondering if OpenWrite should behave like it behave or like Open with the right parameters. Because when using it I supposed it will behave like that, and maybe other would think it also work like that.

But since there is other ways to achieve what I need, this is ok!

Thanks again!