Merge pull request #4594 from FernetMenta/paplayer
[xbmc.git] / xbmc / filesystem / SFTPFile.h
blob72522ba8fbfe7781f828cc339eba524d992031a0
1 #pragma once
2 /*
3 * Copyright (C) 2005-2013 Team XBMC
4 * http://xbmc.org
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)
9 * any later version.
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/>.
22 #include "system.h"
23 #ifdef HAS_FILESYSTEM_SFTP
24 #include "IFile.h"
25 #include "FileItem.h"
26 #include "threads/CriticalSection.h"
28 #include <libssh/libssh.h>
29 #include <libssh/sftp.h>
30 #include <string>
31 #include <map>
32 #include <boost/shared_ptr.hpp>
34 class CURL;
36 #if LIBSSH_VERSION_INT < SSH_VERSION_INT(0,3,2)
37 #define ssh_session SSH_SESSION
38 #endif
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*
46 #endif
48 //five secs timeout for SFTP
49 #define SFTP_TIMEOUT 5
51 class CSFTPSession
53 public:
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);
66 bool IsIdle();
67 private:
68 bool VerifyKnownHost(ssh_session session);
69 bool Connect(const CStdString &host, unsigned int port, const CStdString &username, const CStdString &password);
70 void Disconnect();
71 bool GetItemPermissions(const char *path, uint32_t &permissions);
72 CCriticalSection m_critSect;
74 bool m_connected;
75 ssh_session m_session;
76 sftp_session m_sftp_session;
77 int m_LastActive;
80 typedef boost::shared_ptr<CSFTPSession> CSFTPSessionPtr;
82 class CSFTPSessionManager
84 public:
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();
89 private:
90 static CCriticalSection m_critSect;
91 static std::map<CStdString, CSFTPSessionPtr> sessions;
94 namespace XFILE
96 class CSFTPFile : public IFile
98 public:
99 CSFTPFile();
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);
112 private:
113 CStdString m_file;
114 CSFTPSessionPtr m_session;
115 sftp_file m_sftp_handle;
118 #endif