int CSFTPSession::Stat(const char *path, struct __stat64* buffer) { if(m_connected) { P8PLATFORM::CLockObject lock(m_lock); m_LastActive = P8PLATFORM::GetTimeMs(); sftp_attributes attributes = sftp_stat(m_sftp_session, CorrectPath(path).c_str()); if (attributes) { memset(buffer, 0, sizeof(struct __stat64)); buffer->st_size = attributes->size; buffer->st_mtime = attributes->mtime; buffer->st_atime = attributes->atime; if S_ISDIR(attributes->permissions) buffer->st_mode = S_IFDIR; else if S_ISREG(attributes->permissions) buffer->st_mode = S_IFREG; sftp_attributes_free(attributes); return 0; } else {
sftp_file CSFTPSession::CreateFileHande(const std::string& file) { if (m_connected) { P8PLATFORM::CLockObject lock(m_lock); m_LastActive = P8PLATFORM::GetTimeMs(); sftp_file handle = sftp_open(m_sftp_session, CorrectPath(file).c_str(), O_RDONLY, 0); if (handle) { sftp_file_set_blocking(handle); return handle; } else kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Was connected but couldn't create filehandle for '%s'", file.c_str()); } else kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Not connected and can't create file handle for '%s'", file.c_str()); return NULL; }
bool CSFTPSession::GetDirectory(const std::string& base, const std::string& folder, std::vector<kodi::vfs::CDirEntry>& items) { int sftp_error = SSH_FX_OK; if (m_connected) { sftp_dir dir = NULL; P8PLATFORM::CLockObject lock(m_lock); m_LastActive = P8PLATFORM::GetTimeMs(); dir = sftp_opendir(m_sftp_session, CorrectPath(folder).c_str()); //Doing as little work as possible within the critical section if (!dir) sftp_error = sftp_get_error(m_sftp_session); lock.Unlock(); if (!dir) { kodi::Log(ADDON_LOG_ERROR, "%s: %s for '%s'", __FUNCTION__, SFTPErrorText(sftp_error), folder.c_str()); } else { bool read = true; while (read) { sftp_attributes attributes = NULL; lock.Lock(); read = sftp_dir_eof(dir) == 0; attributes = sftp_readdir(m_sftp_session, dir); lock.Unlock(); if (attributes && (attributes->name == NULL || strcmp(attributes->name, "..") == 0 || strcmp(attributes->name, ".") == 0)) { lock.Lock(); sftp_attributes_free(attributes); lock.Unlock(); continue; } if (attributes) { std::string itemName = attributes->name; std::string localPath = folder; localPath.append(itemName); if (attributes->type == SSH_FILEXFER_TYPE_SYMLINK) { lock.Lock(); sftp_attributes_free(attributes); attributes = sftp_stat(m_sftp_session, CorrectPath(localPath).c_str()); lock.Unlock(); if (attributes == NULL) continue; } kodi::vfs::CDirEntry entry; entry.SetLabel(itemName); if (itemName[0] == '.') entry.AddProperty("file:hidden", "true"); entry.SetDateTime(attributes->mtime64); if (attributes->type & SSH_FILEXFER_TYPE_DIRECTORY) { localPath.append("/"); entry.SetFolder(true); } else entry.SetSize(attributes->size); entry.SetPath(base+localPath); items.push_back(entry); lock.Lock(); sftp_attributes_free(attributes); lock.Unlock(); } else read = false; } lock.Lock(); sftp_closedir(dir); lock.Unlock(); return true; } } else kodi::Log(ADDON_LOG_ERROR, "SFTPSession: Not connected, can't list directory '%s'", folder.c_str()); return false; }
int _tmain(int argc, _TCHAR* argv[]) { _tsetlocale(LC_ALL, _T("")); // fixes VS11DP quirk // open csv file RCSVFileRO file; if (argc == 2) { if (!file.Open(CorrectPath(argv[1]))) {_putts(_T("Unable to open file: ") + CorrectPath(argv[1])); PAUSERETURN(-1);} } else if (argc == 1) { if (!file.Open(CorrectPath(_T("strings.csv")))) {_putts(_T("Please supply the name of CSV-file.")); PAUSERETURN(-2);} } else {_putts(_T("Invalid argument count.")); PAUSERETURN(-3);} if (file.GetLineCount() == 0) {_putts(_T("Empty file.")); PAUSERETURN(1);} if (file.GetLineCount() < 2) {_putts(_T("File must contain at least two lines.")); PAUSERETURN(-4);} if (file.GetFieldCount() < 2) {_putts(_T("File must contain at least two columns.")); PAUSERETURN(-5);} // generate header file contents RString str; str += _T("// strings.h : Defines the strings to be used throughout the application.\r\n"); str += _T("//\r\n"); str += _T("\r\n"); int n = 1000; int i, j; for (i = 1; i < file.GetLineCount(); i++) if (_tcscmp(file.GetField(i, 0), _T("")) != 0) str += RString(_T("#define ")) + file.GetField(i, 0) + _T(" ") + NumberToString(n++) + _T("\r\n"); str += _T("\r\n"); str += _T("inline void LoadStrings()\r\n"); str += _T("{\r\n"); str += _T("\tINT_PTR nLanguage;\r\n"); for (i = 1; i < file.GetFieldCount(); i++) { str += _T("\r\n"); str += (RString)_T("\tnLanguage = GetLangMgr()->GetLanguage(_T(\"") + file.GetField(0, i) + _T("\"));\r\n"); str += (RString)_T("\tif (nLanguage == -1)\r\n"); str += (RString)_T("\t\tnLanguage = GetLangMgr()->AddLanguage(_T(\"") + file.GetField(0, i) + _T("\"), _T(\"") + file.GetField(1, i) + _T("\"), _T(\"\"));\r\n\r\n"); for (j = 1; j < file.GetLineCount(); j++) { if (_tcscmp(file.GetField(j, 0), _T("")) == 0) continue; // skip empty ids str += (RString)_T("\tGetLangMgr()->SetString(nLanguage, ") + NumberToString(j-1) + _T(", _T(\"") + file.GetField(j, i) + _T("\"), false);\r\n"); } } str += _T("}\r\n"); // write to file RString strFilePath = CorrectPath(argc == 2 ? argv[1] : _T("strings.csv")); strFilePath = strFilePath.Left(strFilePath.ReverseFind(_T('\\'))+1) + _T("strings.h"); RArray<BYTE> data = StringToData(str, CHARSET_ANSI); if (!DataToFile(data, strFilePath)) {_tprintf(_T("Failed to open %s for writing.\n"), (LPCTSTR)strFilePath); PAUSERETURN(-6);} _tprintf(_T("Created %s succesfully.\n"), (LPCTSTR)strFilePath); PAUSERETURN(0); }