0

I have Connected to the server using SFTP SSH in c++ and i have already copied files from remote to local. Now i'm trying to copy from local to remote, i read about this from this, but its not working. I wrote the following code.

 fr = fopen("C:/Users/Sami/Desktop/we/s.txt", "r");
fseek(fr, 0, SEEK_END);
 lSize = ftell(fr);
rewind(fr);
char * buffer = (char*)malloc(sizeof(char)*lSize);
 result = fread(buffer, 1, lSize, fr);
file = sftp_open(sftp, "/home/serversj/Desktop/sami/s.txt", O_CREAT, 1);
nwritten = sftp_write(file, buffer, result);

Where i opened the local system file using 'fopen' and store the file data into buffer the read the buffer content into result. While debugging i can see the sftp_open function creates a NULL value in 'file', hence the sftp_write shows errors 'No such file'. I'm working on windows. I got the error is in sftp_open , i also tried this answer but issues in the 'sftp_open'. I don't know how to correct this ..I'm stuck in this.

The error is : ‘SFTP server : Permission denied.’.. and ssh_get error is ‘No such file ‘

Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93
  • Does the `/home/serversj/Desktop/sami/` directory exist on your server? – melpomene Sep 18 '18 at 07:12
  • @melpomene: Yes, i used that directory already for copying file from **remote to local** and while debugging the error shows "read access violation. file was nullptr." hence "Permission denied." –  Sep 18 '18 at 07:14
  • In your other question you used `/home/server/Desktop/sa/`, i.e. `server` instead of `serversj` and `sa` instead of `sami`. – melpomene Sep 18 '18 at 07:18
  • Why don't you put proper error checks in your code? A null pointer dereference does not mean "permission denied". – melpomene Sep 18 '18 at 07:19
  • Yes , actually i didn't put here the actual path used in my program , i just renamed those path wile posting the question for some security issues. But the actual path exist. The error i can see is "sftp_open : SFTP server : Permission denied.". Is it related with setting mode in sftp_open (file,"",acces_type,mode); ? and the access_type used here is 'O_CREAT' and mode is 1. is it correct? –  Sep 18 '18 at 07:24
  • Can you upload a file to that path using any standalone SFTP client (e.g. WinSCP)? Show us its log file. – Martin Prikryl Sep 18 '18 at 07:27
  • Show us WinSCP log file. – Martin Prikryl Sep 18 '18 at 07:32
  • @MartinPrikryl : How can i attach the log file here ? the content inside in the log file is large (32kb), so i can't copy the entire content into the comment section. –  Sep 18 '18 at 07:44
  • You can edit the log into your question. Or post it on a site like https://pastebin.com/ and link here. – Martin Prikryl Sep 18 '18 at 07:45
  • @MartinPrikryl : Sorry, i can't attach the log file here due to some security issues.Is that necessary ? I already copied files from remote to local by using sftp c++. The issues is that **sftp_open(sftp,"",O_CREAT,0)**, can't create the file. Hence the **sftp_write()** can'ty find the file for writing. Is that issue with 'mode' parameter in sftp_open ? –  Sep 18 '18 at 09:52
  • I do not think that your question includes enough information to help you. If you are not willing to provide us more, you will hardly get an answer. – Martin Prikryl Sep 18 '18 at 11:15
  • What should I see? – Martin Prikryl Sep 18 '18 at 13:18
  • This is not how this site works. Your question has to stand on its own. You cannot provide information using "hidden" ways. Pay for a support if you cannot post information publicly. – Martin Prikryl Sep 18 '18 at 13:21
  • @MartinPrikryl : I edited the question ..Please check it now –  Sep 18 '18 at 13:24
  • And what about the path in `sftp_open`? Is it really also `/home/rajinreeza/Desktop/samjith/`? + Also include the information about the error you are getting into the question itself (it lost in comments now). + Do you have libssh log file? – Martin Prikryl Sep 18 '18 at 13:38
  • Yes..The same path i used in sftp_open(); .. I have aaded the error in the question .. Please check it.. I’m learning sftp ,so my doubt is in sftp_open(file,””,access_type,mode) , where the mode is stands for permission ,**Mode specifies the permissions to use if a new file is created. It is modified by the process's umask in the usual way: The permissions of the created file are (mode & ~umask)**..Is it related with this ?? Just a doubt, because the i’m in the learning stage –  Sep 18 '18 at 17:46
  • I do not think it's about the "mode" parameter. + Again, show us libssh log. – Martin Prikryl Sep 19 '18 at 05:19

2 Answers2

1

Check your constants:

#define O_RDONLY     0x0000
#define O_WRONLY     0x0001
#define O_RDWR       0x0002

#define O_CREAT      0x0100
#define O_TRUNC      0x0200
#define O_EXCL       0x0400

#define S_IFREG      0x8000          /* regular */
#define S_IREAD      0x0100          /* read permission, owner */
#define S_IWRITE     0x0080          /* write permission, owner */
#define S_IEXEC      0x0040          /* execute/search permission, owner */
#define S_IRWXU      S_IREAD | S_IWRITE | S_IEXEC

int accesstype = O_WRONLY | O_CREAT | O_TRUNC;
mode_t mode = S_IRWXU;
sftp_file file = sftp_open( sftpSession, dstFile, accesstype, mode );
Falo
  • 331
  • 3
  • 5
0

You were using sftp_open(); only for creating the file by using access_type O_CREAT,you need O_WRONLY writing some data into that file.

 fr = fopen("C:/Users/Sami/Desktop/we/s.txt", "r");
    fseek(fr, 0, SEEK_END);
     lSize = ftell(fr);
    rewind(fr);
    char * buffer = (char*)malloc(sizeof(char)*lSize);
     result = fread(buffer, 1, lSize, fr);
    int access_type = O_WRONLY | O_CREAT;
    file = sftp_open(sftp, "/home/serversj/Desktop/sami/s.txt", access_type,S_IRWXU);
    nwritten = sftp_write(file, buffer, result);

I hope this will work..

Learner
  • 23
  • 1
  • 8