3 * Copyright (C) 2005-2013 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
23 #ifdef HAS_FILESYSTEM_SFTP
26 #include "threads/CriticalSection.h"
28 #include <libssh/libssh.h>
29 #include <libssh/sftp.h>
32 #include <boost/shared_ptr.hpp>
36 #if LIBSSH_VERSION_INT < SSH_VERSION_INT(0,3,2)
37 #define ssh_session SSH_SESSION
40 #if LIBSSH_VERSION_INT < SSH_VERSION_INT(0,4,0)
41 #define sftp_file SFTP_FILE*
42 #define sftp_session SFTP_SESSION*
43 #define sftp_attributes SFTP_ATTRIBUTES*
44 #define sftp_dir SFTP_DIR*
45 #define ssh_session ssh_session*
48 //five secs timeout for SFTP
49 #define SFTP_TIMEOUT 5
54 CSFTPSession(const CStdString
&host
, unsigned int port
, const CStdString
&username
, const CStdString
&password
);
55 virtual ~CSFTPSession();
57 sftp_file
CreateFileHande(const CStdString
&file
);
58 void CloseFileHandle(sftp_file handle
);
59 bool GetDirectory(const CStdString
&base
, const CStdString
&folder
, CFileItemList
&items
);
60 bool DirectoryExists(const char *path
);
61 bool FileExists(const char *path
);
62 int Stat(const char *path
, struct __stat64
* buffer
);
63 int Seek(sftp_file handle
, uint64_t position
);
64 int Read(sftp_file handle
, void *buffer
, size_t length
);
65 int64_t GetPosition(sftp_file handle
);
68 bool VerifyKnownHost(ssh_session session
);
69 bool Connect(const CStdString
&host
, unsigned int port
, const CStdString
&username
, const CStdString
&password
);
71 bool GetItemPermissions(const char *path
, uint32_t &permissions
);
72 CCriticalSection m_critSect
;
75 ssh_session m_session
;
76 sftp_session m_sftp_session
;
80 typedef boost::shared_ptr
<CSFTPSession
> CSFTPSessionPtr
;
82 class CSFTPSessionManager
85 static CSFTPSessionPtr
CreateSession(const CURL
&url
);
86 static CSFTPSessionPtr
CreateSession(const CStdString
&host
, unsigned int port
, const CStdString
&username
, const CStdString
&password
);
87 static void ClearOutIdleSessions();
88 static void DisconnectAllSessions();
90 static CCriticalSection m_critSect
;
91 static std::map
<CStdString
, CSFTPSessionPtr
> sessions
;
96 class CSFTPFile
: public IFile
100 virtual ~CSFTPFile();
101 virtual void Close();
102 virtual int64_t Seek(int64_t iFilePosition
, int iWhence
= SEEK_SET
);
103 virtual unsigned int Read(void* lpBuf
, int64_t uiBufSize
);
104 virtual bool Open(const CURL
& url
);
105 virtual bool Exists(const CURL
& url
);
106 virtual int Stat(const CURL
& url
, struct __stat64
* buffer
);
107 virtual int Stat(struct __stat64
* buffer
);
108 virtual int64_t GetLength();
109 virtual int64_t GetPosition();
110 virtual int GetChunkSize() {return 1;};
111 virtual int IoControl(EIoControl request
, void* param
);
114 CSFTPSessionPtr m_session
;
115 sftp_file m_sftp_handle
;