www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples

Archive Index This month's Index

Subject: Re: sftp - file check if it exists

Re: sftp - file check if it exists

From: royconejo <royconejo_at_gmail.com>
Date: Fri, 24 Jul 2009 02:08:46 -0300

Thanks =)

Well, it is easy to know when a file exists because we got a handle or
information about that file.. the problem is when libssh2_sftp_stat() or
sftp_open() fail. I think (maybe I'm confused) that there are some generic
connection errors, but none really tells if the file exist or not.

I mean.. I'd like to know if stat() or open() failed because (let apart
connection errors) *simply the file does not exist*. Given that those
functions return the same connection errors and not a simple (for example)
"LIBSSH2_ERROR_FILE" Is there any way to know if they failed simply because
the file is not there?

And if stat() or open() failed for that reason (there were no connection
errors).. what does libssh2_session_last_errno() returns? *how to tell if
the file is missing?*

Thanks again =)

On Fri, Jul 24, 2009 at 12:30 AM, Phillip Wu <Phillip.Wu_at_lands.nsw.gov.au>wrote:

> You need to do this call:
>
> LIBSSH2_SFTP_ATTRIBUTES fileinfo;
>
> LIBSSH2_SESSION *session;
>
> ……..
>
> session = libssh2_session_init();
>
> sftp_session = libssh2_sftp_init(session);
>
> status= status=libssh2_sftp_stat(sftp_session,”/tmp/myfile”,&fileinfo);
>
>
>
> If status=0 then “/tmp/myfile” exists.
>
>
>
>
>
>
> ------------------------------
>
> *From:* libssh2-devel-bounces_at_cool.haxx.se [mailto:
> libssh2-devel-bounces_at_cool.haxx.se] *On Behalf Of *royconejo
> *Sent:* Friday, 24 July 2009 1:15 PM
> *To:* libssh2-devel_at_cool.haxx.se
> *Subject:* libssh2_sftp_open() error values
>
>
>
> Hello,
>
> How could I know if a given file exists over SFTP? requesting directory
> contents in not a good choice (I suppose) because there'll be hundreds of
> files and retrieving and searching in that list will be way slower than just
> trying to open the file, as one could do in the local filesystem (for
> example with fopen, open, etc)
>
> well, libssh2_sftp_open() returns NULL whenever it fails, but how could I
> discern if it was because of the ?
>
> Shoud I always expect an errno to be set, even when there was no connection
> error? perhaps ERROR_NONE since there was no real error apart from the file
> not being there?
>
> And shoud I expect any reliable behaviour when my local or remote machine
> lost connection? perhaps LIBSSH2_ERROR_SOCKET_SEND when I unplug my local
> machine or LIBSSH2_ERROR_SOCKET_TIMEOUT when the remote host fails?
>
> (Perhaps I'm a little confused and things are not that easy =p)
>
> Thanks in advance =)!
>
> ------------------------------
>
> This message is intended for the addressee named and may contain
> confidential information. If you are not the intended recipient, please
> delete it and notify the sender. Views expressed in this message are those
> of the individual sender, and are not necessarily the views of the
> Department of Lands. This email message has been swept by MIMEsweeper for
> the presence of computer viruses.
>
> ------------------------------
>
> *Please consider the environment before printing this email.*
>
> _______________________________________________
> libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
>
>

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2009-07-24

the libssh2 team