Esempio n. 1
0
bool FileOps::CacheFile(void* destination, MythFile &source)
{
  unsigned long long totalLength = source.Length();
  unsigned long long totalRead = 0;

  const long buffersize = RCV_BUF_IMAGE_SIZE;
  char* buffer = new char[buffersize];

  while (totalRead < totalLength)
  {
    int bytes_read = source.Read(buffer, buffersize);
    if (bytes_read <= 0)
      break;

    totalRead += bytes_read;

    char *p = buffer;
    while (bytes_read > 0)
    {
      int bytes_written = XBMC->WriteFile(destination, p, bytes_read);
      if (bytes_written <= 0)
        break;

      bytes_read -= bytes_written;
      p += bytes_written;
    }
  }

  XBMC->CloseFile(destination);
  delete[] buffer;

  if (totalRead < totalLength)
    XBMC->Log(LOG_DEBUG, "%s: Failed to read all data: (%d/%d)", __FUNCTION__, totalRead, totalLength);

  return true;
}
Esempio n. 2
0
bool FileOps::CacheFile(const CStdString &localFilename, MythFile &source)
{
  if (source.IsNull())
  {
    XBMC->Log(LOG_ERROR,"%s: NULL file provided", __FUNCTION__);
    return false;
  }

  if (source.Length() == 0)
  {
    XBMC->Log(LOG_ERROR,"%s: Empty file provided", __FUNCTION__);
    return false;
  }

  // Try to open the file. If it fails, check if we need to create the directory first.
  // This way we avoid checking if the directory exists every time.
  void *file;
  if (!(file = XBMC->OpenFileForWrite(localFilename.c_str(), true)))
  {
    CStdString cacheDirectory = GetDirectoryName(localFilename);
    if (XBMC->DirectoryExists(cacheDirectory.c_str()) || XBMC->CreateDirectory(cacheDirectory.c_str()))
    {
      if (g_bExtraDebug)
        XBMC->Log(LOG_DEBUG, "%s: Created cache directory: %s", __FUNCTION__, cacheDirectory.c_str());

      if (!(file = XBMC->OpenFileForWrite(localFilename.c_str(), true)))
      {
        XBMC->Log(LOG_ERROR, "%s: Failed to create cache file: %s", __FUNCTION__, localFilename.c_str());
        return false;
      }
    }
    else
    {
      XBMC->Log(LOG_ERROR, "%s: Failed to create cache directory: %s", __FUNCTION__, cacheDirectory.c_str());
      return false;
    }
  }

  unsigned long long totalLength = source.Length();
  unsigned long long totalRead = 0;

  const long buffersize = RCV_BUF_IMAGE_SIZE;
  char* buffer = new char[buffersize];

  while (totalRead < totalLength)
  {
    int bytes_read = source.Read(buffer, buffersize);
    if (bytes_read <= 0)
      break;

    totalRead += bytes_read;

    char *p = buffer;
    while (bytes_read > 0)
    {
      int bytes_written = XBMC->WriteFile(file, p, bytes_read);
      if (bytes_written <= 0)
        break;

      bytes_read -= bytes_written;
      p += bytes_written;
    }
  }

  XBMC->CloseFile(file);
  delete[] buffer;

  if (totalRead < totalLength)
  {
    XBMC->Log(LOG_DEBUG, "%s: Failed to read all data: %s (%d/%d)", __FUNCTION__, localFilename.c_str(), totalRead, totalLength);
  }

  return true;
}
Esempio n. 3
0
CStdString fileOps::GetFileFromBackend ( CStdString filenameToGet, CStdString fromStorageGroup )
{
  XBMC->Log(LOG_DEBUG,"%s - Getting File via Myth Protocol - %s",
	    __FUNCTION__,filenameToGet.c_str());
  
  if (filenameToGet.Left(1).compare("/") != 0) 
  {
    filenameToGet = "/" + filenameToGet;
  }
  
  MythFile theFile;
  
  if (fromStorageGroup.CompareNoCase("channels")==0) 
  {
    CStdString chanFilename = "/channels" + filenameToGet;
    CStdString chanSG = "c";
    theFile=mythConP.ConnectPath((char*)chanFilename.c_str(),(char*)chanSG.c_str());
  }
  else 
  {
    theFile=mythConP.ConnectPath((char*)filenameToGet.c_str(),(char*)fromStorageGroup.c_str());
  }

  if (theFile.IsNull()) 
  {
    return "";
  }
  int theFilesLength = theFile.Duration();
  if (theFilesLength <= 0) 
  {
    return "";
  }
  
  CStdString writeFilePath = baseLocalCachepath + fromStorageGroup + filenameToGet;
  checkDirectory(writeFilePath,true);
  
  XFILE::CFile writeFile;
  if (writeFile.OpenForWrite(writeFilePath))
  {
    //char* theFileBuff = new char[theFilesLength];
    int totalRead = 0;
    while (totalRead < theFilesLength)
    {
      char* theFileTmpBuff = new char[theFilesLength];
      int readData = theFile.Read(theFileTmpBuff,theFilesLength-totalRead);
      if (readData <= 0)
      {
        break;
      }
      writeFile.Write((const void*)theFileTmpBuff,readData);
      /*char *filePntr = theFileBuff;
      if (totalRead > 0) {
        filePntr += (totalRead);
      }
      if (readData > (theFilesLength-totalRead)) {
        readData = (theFilesLength-totalRead);
      }
      memcpy((void*)filePntr,(void*)theFileTmpBuff,readData);
      */
      
      totalRead += readData;
    }
    writeFile.Close();
    if (totalRead < theFilesLength) 
    {
    XBMC->Log(LOG_DEBUG,"%s - Did not Read all data - %s - %d - %d",
	      __FUNCTION__,filenameToGet.c_str(),totalRead,theFilesLength);    
    }
    return writeFilePath;
  }
  else 
  {
    return "";
  }
  
}