summaryrefslogtreecommitdiffstats
path: root/xbmc/filesystem/UDFBlockInput.cpp
blob: b4eb26563decd9bcad9ab518ec77f06251241607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "UDFBlockInput.h"

#include "filesystem/File.h"

#include <mutex>

#include <udfread/udfread.h>

int CUDFBlockInput::Close(udfread_block_input* bi)
{
  auto m_bi = reinterpret_cast<UDF_BI*>(bi);

  m_bi->fp->Close();

  return 0;
}

uint32_t CUDFBlockInput::Size(udfread_block_input* bi)
{
  auto m_bi = reinterpret_cast<UDF_BI*>(bi);

  return static_cast<uint32_t>(m_bi->fp->GetLength() / UDF_BLOCK_SIZE);
}

int CUDFBlockInput::Read(
    udfread_block_input* bi, uint32_t lba, void* buf, uint32_t blocks, int flags)
{
  auto m_bi = reinterpret_cast<UDF_BI*>(bi);
  std::unique_lock<CCriticalSection> lock(m_bi->lock);

  int64_t pos = static_cast<int64_t>(lba) * UDF_BLOCK_SIZE;

  if (m_bi->fp->Seek(pos, SEEK_SET) != pos)
    return -1;

  ssize_t size = blocks * UDF_BLOCK_SIZE;
  ssize_t read = m_bi->fp->Read(buf, size);
  if (read > 0)
    return static_cast<int>(read / UDF_BLOCK_SIZE);

  return static_cast<int>(read);
}

udfread_block_input* CUDFBlockInput::GetBlockInput(const std::string& file)
{
  auto fp = std::make_shared<XFILE::CFile>();

  if (fp->Open(file))
  {
    m_bi = std::make_unique<UDF_BI>();
    if (m_bi)
    {
      m_bi->fp = fp;
      m_bi->bi.close = CUDFBlockInput::Close;
      m_bi->bi.read = CUDFBlockInput::Read;
      m_bi->bi.size = CUDFBlockInput::Size;

      return &m_bi->bi;
    }

    fp->Close();
  }

  return nullptr;
}