Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'pr36'. closes #36

  • Loading branch information...
commit ce193255c02e132a23eefe128d1f1ac8e7901ae3 2 parents 1963ed9 + 0d3032b
@opdenkamp authored
View
3  addons/pvr.mediaportal.tvserver/Makefile.am
@@ -29,6 +29,5 @@ libmediaportal_addon_la_SOURCES = src/Cards.cpp \
src/utils.cpp \
src/lib/tsreader/FileReader.cpp \
src/lib/tsreader/MultiFileReader.cpp \
- src/lib/tsreader/TSReader.cpp \
- src/lib/tsreader/posix/FileSMB.cpp
+ src/lib/tsreader/TSReader.cpp
View
2  addons/pvr.mediaportal.tvserver/project/VS2010Express/XBMC_MediaportalTVserver.vcxproj
@@ -127,7 +127,6 @@
<ClCompile Include="..\..\src\lib\tsreader\FileReader.cpp" />
<ClCompile Include="..\..\src\lib\tsreader\MultiFileReader.cpp" />
<ClCompile Include="..\..\src\lib\tsreader\TSReader.cpp" />
- <ClCompile Include="..\..\src\lib\tsreader\windows\File.cpp" />
<ClCompile Include="..\..\src\pvrclient-mediaportal.cpp" />
<ClCompile Include="..\..\src\recordings.cpp" />
<ClCompile Include="..\..\src\Socket.cpp" />
@@ -148,7 +147,6 @@
<ClInclude Include="..\..\src\lib\tsreader\IFile.h" />
<ClInclude Include="..\..\src\lib\tsreader\MultiFileReader.h" />
<ClInclude Include="..\..\src\lib\tsreader\TSReader.h" />
- <ClInclude Include="..\..\src\lib\tsreader\windows\File.h" />
<ClInclude Include="..\..\src\pvrclient-mediaportal.h" />
<ClInclude Include="..\..\src\recordings.h" />
<ClInclude Include="..\..\src\Socket.h" />
View
6 ...s/pvr.mediaportal.tvserver/project/VS2010Express/XBMC_MediaportalTVserver.vcxproj.filters
@@ -77,9 +77,6 @@
<ClCompile Include="..\..\src\lib\tsreader\TSReader.cpp">
<Filter>Source Files\lib%255ctsreader</Filter>
</ClCompile>
- <ClCompile Include="..\..\src\lib\tsreader\windows\File.cpp">
- <Filter>Source Files\lib%255ctsreader\windows</Filter>
- </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\Cards.h">
@@ -133,8 +130,5 @@
<ClInclude Include="..\..\src\lib\tsreader\TSReader.h">
<Filter>Header Files\lib%255ctsreader</Filter>
</ClInclude>
- <ClInclude Include="..\..\src\lib\tsreader\windows\File.h">
- <Filter>Header Files\lib%255ctsreader\windows</Filter>
- </ClInclude>
</ItemGroup>
</Project>
View
45 addons/pvr.mediaportal.tvserver/src/lib/tsreader/FileReader.cpp
@@ -28,14 +28,29 @@
#include "FileReader.h"
#include "client.h" //for XBMC->Log
+#include "platform/threads/threads.h"
#include <algorithm> //std::min, std::max
#include "platform/util/timeutils.h" // for usleep
using namespace ADDON;
-using namespace PLATFORM;
+
+/* indicate that caller can handle truncated reads, where function returns before entire buffer has been filled */
+#define READ_TRUNCATED 0x01
+
+/* indicate that that caller support read in the minimum defined chunk size, this disables internal cache then */
+#define READ_CHUNKED 0x02
+
+/* use cache to access this file */
+#define READ_CACHED 0x04
+
+/* open without caching. regardless to file type. */
+#define READ_NO_CACHE 0x08
+
+/* calcuate bitrate for file while reading */
+#define READ_BITRATE 0x10
FileReader::FileReader() :
- m_hFile(),
+ m_hFileHandle(NULL),
m_pFileName(0),
m_fileSize(0),
m_llBufferPointer(0),
@@ -106,10 +121,14 @@ long FileReader::OpenFile()
do
{
- XBMC->Log(LOG_INFO, "FileReader::OpenFile() %s %s.", m_pFileName, CFile::Exists(m_pFileName) ? "exists" : "not found");
+ XBMC->Log(LOG_INFO, "FileReader::OpenFile() %s.", m_pFileName);
// Open in readonly mode with this filename
- if ( m_hFile.Open(m_pFileName, READ_CHUNKED) )
+ void* fileHandle = XBMC->OpenFile(m_pFileName, READ_CHUNKED);
+ if (fileHandle)
+ {
+ m_hFileHandle = fileHandle;
break;
+ }
usleep(20000);
}
@@ -147,7 +166,11 @@ long FileReader::CloseFile()
return S_OK;
}
- m_hFile.Close();
+ if (m_hFileHandle)
+ {
+ XBMC->CloseFile(m_hFileHandle);
+ m_hFileHandle = NULL;
+ }
m_llBufferPointer = 0;
return S_OK;
@@ -156,7 +179,7 @@ long FileReader::CloseFile()
inline bool FileReader::IsFileInvalid()
{
- return (m_hFile.IsInvalid());
+ return m_hFileHandle == NULL;
}
@@ -170,7 +193,7 @@ long FileReader::GetStartPosition(int64_t *lpllpos)
int64_t FileReader::SetFilePointer(int64_t llDistanceToMove, unsigned long dwMoveMethod)
{
//XBMC->Log(LOG_DEBUG, "%s: distance %d method %d.", __FUNCTION__, llDistanceToMove, dwMoveMethod);
- int64_t rc = m_hFile.Seek(llDistanceToMove, dwMoveMethod);
+ int64_t rc = XBMC->SeekFile(m_hFileHandle, llDistanceToMove, dwMoveMethod);
//XBMC->Log(LOG_DEBUG, "%s: distance %d method %d returns %d.", __FUNCTION__, llDistanceToMove, dwMoveMethod, rc);
return rc;
}
@@ -178,13 +201,13 @@ int64_t FileReader::SetFilePointer(int64_t llDistanceToMove, unsigned long dwMov
int64_t FileReader::GetFilePointer()
{
- return m_hFile.GetPosition();
+ return XBMC->GetFilePosition(m_hFileHandle);
}
long FileReader::Read(unsigned char* pbData, unsigned long lDataLength, unsigned long *dwReadBytes)
{
- *dwReadBytes = m_hFile.Read((void*)pbData, lDataLength);//Read file data into buffer
+ *dwReadBytes = XBMC->ReadFile(m_hFileHandle, (void*)pbData, lDataLength);//Read file data into buffer
//XBMC->Log(LOG_DEBUG, "%s: requested read length %d actually read %d.", __FUNCTION__, lDataLength, *dwReadBytes);
if (*dwReadBytes < lDataLength)
@@ -198,10 +221,10 @@ long FileReader::Read(unsigned char* pbData, unsigned long lDataLength, unsigned
int64_t FileReader::GetFileSize()
{
- return m_hFile.GetLength();
+ return XBMC->GetFileLength(m_hFileHandle);
}
int64_t FileReader::OnChannelChange(void)
{
- return m_hFile.GetPosition();
+ return XBMC->GetFilePosition(m_hFileHandle);
}
View
8 addons/pvr.mediaportal.tvserver/src/lib/tsreader/FileReader.h
@@ -29,12 +29,6 @@
#include "platform/os.h"
-#ifdef TARGET_WINDOWS
-#include "windows/File.h"
-#else
-#include "posix/FileSMB.h"
-#endif
-
class FileReader
{
public:
@@ -56,7 +50,7 @@ class FileReader
virtual int64_t OnChannelChange(void);
protected:
- PLATFORM::CFile m_hFile; // Handle to file for streaming
+ void* m_hFileHandle; // Handle to file for streaming
char* m_pFileName; // The filename where we stream
int64_t m_fileSize;
int64_t m_llBufferPointer;
View
10 addons/pvr.mediaportal.tvserver/src/lib/tsreader/MultiFileReader.cpp
@@ -33,6 +33,7 @@
#include <algorithm>
#include "platform/util/timeutils.h"
#include "platform/util/StdString.h"
+#include "platform/threads/threads.h"
using namespace ADDON;
using namespace PLATFORM;
@@ -608,12 +609,11 @@ long MultiFileReader::GetFileLength(const char* pFilename, int64_t &length)
length = 0;
// Try to open the file
- CFile hFile;
- struct stat64 filestatus;
- if (hFile.Open(pFilename) && hFile.Stat(&filestatus) >= 0)
+ void* hFile;
+ if (((hFile = XBMC->OpenFile(pFilename, 0)) != NULL))
{
- length = (int64_t) filestatus.st_size;
- hFile.Close();
+ length = XBMC->GetFileLength(hFile);
+ XBMC->CloseFile(hFile);
}
else
{
View
1  addons/pvr.mediaportal.tvserver/src/lib/tsreader/MultiFileReader.h
@@ -95,4 +95,3 @@ class MultiFileReader : public FileReader
bool m_bDebugOutput;
int64_t m_cachedFileSize;
};
-
View
417 addons/pvr.mediaportal.tvserver/src/lib/tsreader/posix/FileSMB.cpp
@@ -1,417 +0,0 @@
-/*
- * Copyright (C) 2005-2008 Team XBMC
- * http://www.xbmc.org
- *
- * This Program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- *
- * This Program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with XBMC; see the file COPYING. If not, write to
- * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- */
-
-// FileSmb.cpp: implementation of the CFile class.
-//
-//////////////////////////////////////////////////////////////////////
-
-#include <inttypes.h>
-#include "client.h"
-#include "FileSMB.h"
-#include <libsmbclient.h>
-#include "platform/threads/mutex.h"
-
-using namespace ADDON;
-
-void xb_smbc_log(const char* msg)
-{
- XBMC->Log(LOG_INFO, "%s%s", "smb: ", msg);
-}
-
-void xb_smbc_auth(const char *srv, const char *shr, char *wg, int wglen,
- char *un, int unlen, char *pw, int pwlen)
-{
- return;
-}
-
-smbc_get_cached_srv_fn orig_cache;
-
-SMBCSRV* xb_smbc_cache(SMBCCTX* c, const char* server, const char* share, const char* workgroup, const char* username)
-{
- return orig_cache(c, server, share, workgroup, username);
-}
-
-namespace PLATFORM
-{
-
-CSMB::CSMB()
-{
- m_IdleTimeout = 0;
- m_context = NULL;
-}
-
-CSMB::~CSMB()
-{
- Deinit();
-}
-
-void CSMB::Deinit()
-{
- PLATFORM::CLockObject lock(*this);
-
- /* samba goes loco if deinited while it has some files opened */
- if (m_context)
- {
- try
- {
- smbc_set_context(NULL);
- smbc_free_context(m_context, 1);
- }
- catch(...)
- {
- XBMC->Log(LOG_ERROR,"exception on CSMB::Deinit. errno: %d", errno);
- }
- m_context = NULL;
- }
-}
-
-void CSMB::Init()
-{
- PLATFORM::CLockObject lock(*this);
-
- if (!m_context)
- {
- // Create ~/.smb/smb.conf. This file is used by libsmbclient.
- // http://us1.samba.org/samba/docs/man/manpages-3/libsmbclient.7.html
- // http://us1.samba.org/samba/docs/man/manpages-3/smb.conf.5.html
- char smb_conf[MAX_PATH];
-
- sprintf(smb_conf, "%s/.smb", getenv("HOME"));
- mkdir(smb_conf, 0755);
- sprintf(smb_conf, "%s/.smb/smb.conf", getenv("HOME"));
- FILE* f = fopen(smb_conf, "w");
-
- if (f != NULL)
- {
- fprintf(f, "[global]\n");
-
- // make sure we're not acting like a server
- fprintf(f, "\tpreferred master = no\n");
- fprintf(f, "\tlocal master = no\n");
- fprintf(f, "\tdomain master = no\n");
-
- // use the weaker LANMAN password hash in order to be compatible with older servers
- fprintf(f, "\tclient lanman auth = yes\n");
- fprintf(f, "\tlanman auth = yes\n");
-
- fprintf(f, "\tsocket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
- fprintf(f, "\tlock directory = %s/.smb/\n", getenv("HOME"));
-
- fprintf(f, "\tname resolve order = lmhosts bcast host\n");
-
- fclose(f);
- }
-
- // reads smb.conf so this MUST be after we create smb.conf
- // multiple smbc_init calls are ignored by libsmbclient.
- smbc_init(xb_smbc_auth, 0);
-
- // setup our context
- m_context = smbc_new_context();
- m_context->debug = 10; //g_advancedSettings.m_logLevel == LOG_LEVEL_DEBUG_SAMBA ? 10 : 0;
- m_context->callbacks.auth_fn = xb_smbc_auth;
- orig_cache = m_context->callbacks.get_cached_srv_fn;
- m_context->callbacks.get_cached_srv_fn = xb_smbc_cache;
- m_context->options.one_share_per_server = false;
- m_context->options.browse_max_lmb_count = 0;
- m_context->timeout = 10000; //g_advancedSettings.m_sambaclienttimeout * 1000;
-
- // initialize samba and do some hacking into the settings
- if (smbc_init_context(m_context))
- {
- /* setup old interface to use this context */
- smbc_set_context(m_context);
- }
- else
- {
- smbc_free_context(m_context, 1);
- m_context = NULL;
- }
- }
- m_IdleTimeout = 180;
-}
-
-void CSMB::Purge()
-{
-}
-
-/* This is called from CApplication::ProcessSlow() and is used to tell if smbclient have been idle for too long */
-void CSMB::CheckIfIdle()
-{
- /* We check if there are open connections. This is done without a lock to not halt the mainthread. It should be thread safe as
- worst case scenario is that m_OpenConnections could read 0 and then changed to 1 if this happens it will enter the if wich will lead to another check, wich is locked. */
- if (m_OpenConnections == 0)
- { /* I've set the the maxiumum IDLE time to be 1 min and 30 sec. */
- PLATFORM::CLockObject lock(*this);
- if (m_OpenConnections == 0 /* check again - when locked */ && m_context != NULL)
- {
- if (m_IdleTimeout > 0)
- {
- m_IdleTimeout--;
- }
- else
- {
- XBMC->Log(LOG_NOTICE, "Samba is idle. Closing the remaining connections");
- smb.Deinit();
- }
- }
- }
-}
-
-void CSMB::SetActivityTime()
-{
- /* Since we get called every 500ms from ProcessSlow we limit the tick count to 180 */
- /* That means we have 2 ticks per second which equals 180/2 == 90 seconds */
- m_IdleTimeout = 180;
-}
-
-/* The following two function is used to keep track on how many Opened files/directories there are.
- This makes the idle timer not count if a movie is paused for example */
-void CSMB::AddActiveConnection()
-{
- PLATFORM::CLockObject lock(*this);
- m_OpenConnections++;
-}
-
-void CSMB::AddIdleConnection()
-{
- PLATFORM::CLockObject lock(*this);
- m_OpenConnections--;
- /* If we close a file we reset the idle timer so that we don't have any wierd behaviours if a user
- leaves the movie paused for a long while and then press stop */
- m_IdleTimeout = 180;
-
- /* Temporary hack for this PVR addon because we are running outside XBMC */
- if (m_OpenConnections == 0)
- {
- m_IdleTimeout = 0;
- CSMB::CheckIfIdle();
- }
-}
-
-CSMB smb;
-
-CFile::CFile()
-{
- smb.Init();
- m_fd = -1;
- smb.AddActiveConnection();
-}
-
-CFile::~CFile()
-{
- Close();
- smb.AddIdleConnection();
-}
-
-int64_t CFile::GetPosition()
-{
- if (m_fd == -1)
- return 0;
- smb.Init();
- PLATFORM::CLockObject lock(smb);
- int64_t pos = smbc_lseek(m_fd, 0, SEEK_CUR);
- if ( pos < 0 )
- return 0;
- return pos;
-}
-
-int64_t CFile::GetLength()
-{
- if (m_fd == -1) return 0;
- return m_fileSize;
-}
-
-bool CFile::Open(const CStdString& strFileName, unsigned int flags)
-{
- Close();
-
- m_fileName = strFileName;
-
- m_fd = OpenFile();
-
- XBMC->Log(LOG_DEBUG,"CFile::Open - opened %s, fd=%d", m_fileName.c_str(), m_fd);
- if (m_fd == -1)
- {
- // write error to logfile
- XBMC->Log(LOG_INFO, "FileSmb->Open: Unable to open file : '%s'\nunix_err:'%x' error : '%s'", strFileName.c_str(), errno, strerror(errno));
- return false;
- }
-
- CLockObject lock(smb);
- struct stat tmpBuffer;
- if (smbc_stat(strFileName, &tmpBuffer) < 0)
- {
- smbc_close(m_fd);
- m_fd = -1;
- return false;
- }
-
- m_fileSize = tmpBuffer.st_size;
-
- int64_t ret = smbc_lseek(m_fd, 0, SEEK_SET);
- if ( ret < 0 )
- {
- smbc_close(m_fd);
- m_fd = -1;
- return false;
- }
- // We've successfully opened the file!
- return true;
-}
-
-int CFile::OpenFile()
-{
- int fd = -1;
- smb.Init();
-
- CStdString strPath = m_fileName;
-
- {
- CLockObject lock(smb);
- fd = smbc_open(strPath.c_str(), O_RDONLY, 0);
- }
-
- // file open failed, try to open the directory to force authentication
- if (fd < 0 && errno == EACCES)
- {
- XBMC->Log(LOG_ERROR, "%s: File open on %s failed\n", __FUNCTION__, strPath.c_str());
- }
-
- return fd;
-}
-
-bool CFile::Exists(const CStdString& strFileName, bool bUseCache)
-{
- // we can't open files like smb://file.f or smb://server/file.f
- // if a file matches the if below return false, it can't exist on a samba share.
- //if (!IsValidFile(url.GetFileName())) return false;
-
- smb.Init();
-
- struct stat info;
-
- CLockObject lock(smb);
- int iResult = smbc_stat(strFileName, &info);
-
- if (iResult < 0)
- return false;
- return true;
-}
-
-int CFile::Stat(struct stat64* buffer)
-{
- if (m_fd == -1)
- return -1;
-
- struct stat tmpBuffer = {0};
-
- CLockObject lock(smb);
- int iResult = smbc_fstat(m_fd, &tmpBuffer);
-
- memset(buffer, 0, sizeof(struct stat64));
- buffer->st_dev = tmpBuffer.st_dev;
- buffer->st_ino = tmpBuffer.st_ino;
- buffer->st_mode = tmpBuffer.st_mode;
- buffer->st_nlink = tmpBuffer.st_nlink;
- buffer->st_uid = tmpBuffer.st_uid;
- buffer->st_gid = tmpBuffer.st_gid;
- buffer->st_rdev = tmpBuffer.st_rdev;
- buffer->st_size = tmpBuffer.st_size;
- buffer->st_atime = tmpBuffer.st_atime;
- buffer->st_mtime = tmpBuffer.st_mtime;
- buffer->st_ctime = tmpBuffer.st_ctime;
-
- return iResult;
-}
-
-unsigned long CFile::Read(void* lpBuf, int64_t uiBufSize)
-{
- if (m_fd == -1)
- return 0;
-
- CLockObject lock(smb); // Init not called since it has to be "inited" by now
- smb.SetActivityTime();
- /* work around stupid bug in samba */
- /* some samba servers have a bug in it where the */
- /* 17th bit will be ignored in a request of data */
- /* this can lead to a very small return of data */
- /* also worse, a request of exactly 64k will return */
- /* as if eof, client has a workaround for windows */
- /* thou it seems other servers are affected too */
- if ( uiBufSize >= 64*1024-2 )
- uiBufSize = 64*1024-2;
-
- int bytesRead = smbc_read(m_fd, lpBuf, (int)uiBufSize);
-
- if ( bytesRead < 0 && errno == EINVAL )
- {
- XBMC->Log(LOG_ERROR, "%s - Error( %d, %d, %s ) - Retrying", __FUNCTION__, bytesRead, errno, strerror(errno));
- bytesRead = smbc_read(m_fd, lpBuf, (int)uiBufSize);
- }
-
- if ( bytesRead < 0 )
- {
- XBMC->Log(LOG_ERROR, "%s - Error( %d, %d, %s )", __FUNCTION__, bytesRead, errno, strerror(errno));
- return 0;
- }
-
- return (unsigned int) bytesRead;
-}
-
-int64_t CFile::Seek(int64_t iFilePosition, int iWhence)
-{
- if (m_fd == -1)
- return -1;
-
- CLockObject lock(smb); // Init not called since it has to be "inited" by now
- smb.SetActivityTime();
- int64_t pos = smbc_lseek(m_fd, iFilePosition, iWhence);
-
- if ( pos < 0 )
- {
- XBMC->Log(LOG_ERROR, "%s - Error( %"PRId64", %d, %s )", __FUNCTION__, pos, errno, strerror(errno));
- return -1;
- }
-
- return (int64_t)pos;
-}
-
-void CFile::Close()
-{
- if (m_fd != -1)
- {
- XBMC->Log(LOG_DEBUG,"CFile::Close closing fd %d", m_fd);
- CLockObject lock(smb);
- smbc_close(m_fd);
- }
- m_fd = -1;
-}
-
-bool CFile::IsInvalid()
-{
- if (m_fd < 0)
- return true;
- else
- return false;
-}
-
-}
View
80 addons/pvr.mediaportal.tvserver/src/lib/tsreader/posix/FileSMB.h
@@ -1,80 +0,0 @@
-/*
- * Copyright (C) 2005-2010 Team XBMC
- * http://www.xbmc.org
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#pragma once
-
-#include <iostream>
-#include "../IFile.h"
-#include "platform/threads/mutex.h"
-
-struct _SMBCCTX;
-typedef _SMBCCTX SMBCCTX;
-
-namespace PLATFORM
-{
-
-class CSMB: public CMutex
-{
-public:
- CSMB();
- ~CSMB();
- void Init();
- void Deinit();
- void Purge();
- void CheckIfIdle();
- void SetActivityTime();
- void AddActiveConnection();
- void AddIdleConnection();
- CStdString URLEncode(const CStdString &value);
-
-private:
- SMBCCTX *m_context;
- CStdString m_strLastHost;
- CStdString m_strLastShare;
- int m_OpenConnections;
- unsigned int m_IdleTimeout;
-};
-
-extern CSMB smb;
-
-class CFile: public IFile
-{
-public:
- CFile();
- ~CFile();
-
- bool Open(const CStdString& strFileName, unsigned int flags = 0);
- bool IsInvalid();
- unsigned long Read(void* lpBuf, int64_t uiBufSize);
- int64_t Seek(int64_t iFilePosition, int iWhence = SEEK_SET);
- int64_t GetPosition();
- int64_t GetLength();
- void Close();
- int Stat(struct stat64 *buffer);
- static bool Exists(const CStdString& strFileName, bool bUseCache = true);
-
-private:
- int OpenFile();
-
- int64_t m_fileSize;
- int m_fd;
- CStdString m_fileName;
-
-};
-
-} // namespace PLATFORM
View
188 addons/pvr.mediaportal.tvserver/src/lib/tsreader/windows/File.cpp
@@ -1,188 +0,0 @@
-/*
- * Copyright (C) 2010-2012 Marcel Groothuis
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "client.h" // For XBMC->Log
-#include "File.h"
-#include "utils.h"
-
-using namespace PLATFORM;
-using namespace ADDON;
-using namespace std;
-
-CFile::CFile() :
- m_hFile(INVALID_HANDLE_VALUE),
- m_flags(0),
- m_bReadOnly(true)
-{
-}
-
-
-CFile::~CFile()
-{
- Close();
-}
-
-
-void CFile::Close()
-{
- try
- {
- if (m_hFile != INVALID_HANDLE_VALUE)
- {
- ::CloseHandle(m_hFile);
- m_hFile = INVALID_HANDLE_VALUE; // Invalidate the file
- }
- }
- catch(...)
- {
- XBMC->Log(LOG_ERROR, "%s - Unhandled exception", __FUNCTION__);
- }
- return;
-}
-
-
-int64_t CFile::GetLength()
-{
- // Do not get file size if static file or first time
- if (m_bReadOnly || !m_fileSize)
- {
- LARGE_INTEGER li;
- if (::GetFileSizeEx(m_hFile, &li) == 0)
- {
- DWORD dwErr = GetLastError();
- XBMC->Log(LOG_ERROR, "%s GetFileSizeEx failed. Error %d.", __FUNCTION__, dwErr);
- return E_FAIL;
- }
- m_fileSize = li.QuadPart;
- }
- return m_fileSize;
-}
-
-
-bool CFile::Open(const CStdString& strFileName, unsigned int flags)
-{
- m_flags = flags;
-
- CStdStringW strWFile = UTF8Util::ConvertUTF8ToUTF16(strFileName.c_str());
-
- // Do not try to open a tsbuffer file without SHARE_WRITE so skip this try if we have a buffer file
- if (strstr(strFileName.c_str(), ".ts.tsbuffer") == NULL)
- {
- // Try to open the file
- m_hFile = ::CreateFileW(strWFile, // The filename
- (DWORD) GENERIC_READ, // File access
- (DWORD) FILE_SHARE_READ, // Share access
- NULL, // Security
- (DWORD) OPEN_EXISTING, // Open flags
- (DWORD) 0, // More flags
- NULL); // Template
-
- m_bReadOnly = false;
- if (!IsInvalid())
- return true;
- }
-
- //Test incase file is being recorded to
- m_hFile = ::CreateFileW(strWFile, // The filename
- (DWORD) GENERIC_READ, // File access
- (DWORD) (FILE_SHARE_READ |
- FILE_SHARE_WRITE), // Share access
- NULL, // Security
- (DWORD) OPEN_EXISTING, // Open flags
- (DWORD) FILE_ATTRIBUTE_NORMAL, // More flags
- NULL); // Template
-
- if (IsInvalid())
- {
- DWORD dwErr = GetLastError();
- XBMC->Log(LOG_ERROR, "%s: error opening file '%s'. Error code %d", __FUNCTION__, strFileName.c_str(), dwErr);
- return false;
- }
-
- return true;
-}
-
-
-bool CFile::Exists(const CStdString& strFileName, bool bUseCache /* = true */)
-{
- DWORD dwAttr = GetFileAttributes(strFileName.c_str());
-
- if (dwAttr == 0xffffffff)
- {
- return false;
- }
-
- return true;
-}
-
-
-unsigned long CFile::Read(void *lpBuf, int64_t uiBufSize)
-{
- if (IsInvalid())
- return 0;
-
- // Read file data into buffer
- BOOL ret;
- DWORD dwReadBytes;
-
- ret = ::ReadFile(m_hFile, lpBuf, (DWORD) uiBufSize, &dwReadBytes, NULL);
-
- if (!ret)
- {
- XBMC->Log(LOG_ERROR, "FileReader::Read() read failed - error = %d", HRESULT_FROM_WIN32(GetLastError()));
- return 0;
- }
-
- return dwReadBytes;
-}
-
-
-int64_t CFile::Seek(int64_t iFilePosition, int iWhence)
-{
- // FILE_BEGIN = SEEK_SET: offset relative to the beginning of the file
- // FILE_CURRENT = SEEK_CUR: offset relative to the current position in the file
- // FILE_END = SEEK_END: offset relative to the end of the file
-
- LARGE_INTEGER liNewPos;
- LARGE_INTEGER liSetPos;
-
- liSetPos.QuadPart = iFilePosition;
-
- if (!::SetFilePointerEx(m_hFile, liSetPos, &liNewPos, iWhence) )
- {
- XBMC->Log(LOG_ERROR, "%s: SetFilePointerEx failed with error code %i\n", __FUNCTION__, GetLastError());
- return -1;
- }
-
- return liNewPos.QuadPart;
-}
-
-
-int64_t CFile::GetPosition()
-{
- LARGE_INTEGER li, seekoffset;
- seekoffset.QuadPart = 0;
-
- if (::SetFilePointerEx(m_hFile, seekoffset, &li, FILE_CURRENT) == 0)
- {
- // Error
- XBMC->Log(LOG_ERROR, "%s: ::SetFilePointerEx failed", __FUNCTION__);
- return -1;
- }
-
- return li.QuadPart;
-}
View
53 addons/pvr.mediaportal.tvserver/src/lib/tsreader/windows/File.h
@@ -1,53 +0,0 @@
-/*
- * Copyright (C) 2005-2010 Team XBMC
- * http://www.xbmc.org
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#pragma once
-
-#include <iostream>
-#include "../IFile.h"
-
-namespace PLATFORM
-{
-class CFile: public IFile
-{
-public:
- CFile();
- virtual ~CFile();
-
- bool Open(const CStdString& strFileName, unsigned int flags = 0);
- bool IsInvalid()
- {
- return (m_hFile == INVALID_HANDLE_VALUE);
- };
-
- unsigned long Read(void* lpBuf, int64_t uiBufSize);
- int64_t Seek(int64_t iFilePosition, int iWhence = SEEK_SET);
- int64_t GetPosition();
- int64_t GetLength();
- void Close();
- static bool Exists(const CStdString& strFileName, bool bUseCache = true);
- //int Stat(struct __stat64 *buffer);
-
-private:
- unsigned int m_flags;
- HANDLE m_hFile;
- int64_t m_fileSize;
- bool m_bReadOnly;
-};
-
-} // namespace PLATFORM
Please sign in to comment.
Something went wrong with that request. Please try again.