summaryrefslogtreecommitdiffstats
path: root/xbmc/filesystem/ISO9660File.cpp
blob: 83de0cc800f5bfe44ec4fc20daef1eec8d8b8d46 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 *  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 "ISO9660File.h"

#include "URL.h"

#include <cmath>

using namespace XFILE;

CISO9660File::CISO9660File() : m_iso(new ISO9660::IFS())
{
}

bool CISO9660File::Open(const CURL& url)
{
  if (m_iso && m_stat)
    return true;

  if (!m_iso->open(url.GetHostName().c_str()))
    return false;

  m_stat.reset(m_iso->stat(url.GetFileName().c_str()));

  if (!m_stat)
    return false;

  if (!m_stat->p_stat)
    return false;

  m_start = m_stat->p_stat->lsn;
  m_current = 0;

  return true;
}

int CISO9660File::Stat(const CURL& url, struct __stat64* buffer)
{
  if (!m_iso)
    return -1;

  if (!m_stat)
    return -1;

  if (!m_stat->p_stat)
    return -1;

  if (!buffer)
    return -1;

  *buffer = {};
  buffer->st_size = m_stat->p_stat->size;

  switch (m_stat->p_stat->type)
  {
    case 2:
      buffer->st_mode = S_IFDIR;
      break;
    case 1:
    default:
      buffer->st_mode = S_IFREG;
      break;
  }

  return 0;
}

ssize_t CISO9660File::Read(void* buffer, size_t size)
{
  const int maxSize = std::min(size, static_cast<size_t>(GetLength()));
  const int blocks = std::ceil(maxSize / ISO_BLOCKSIZE);

  if (m_current > std::ceil(GetLength() / ISO_BLOCKSIZE))
    return -1;

  auto read = m_iso->seek_read(buffer, m_start + m_current, blocks);

  m_current += blocks;

  return read;
}

int64_t CISO9660File::Seek(int64_t filePosition, int whence)
{
  int block = std::floor(filePosition / ISO_BLOCKSIZE);

  switch (whence)
  {
    case SEEK_SET:
      m_current = block;
      break;
    case SEEK_CUR:
      m_current += block;
      break;
    case SEEK_END:
      m_current = std::ceil(GetLength() / ISO_BLOCKSIZE) + block;
      break;
  }

  return m_current * ISO_BLOCKSIZE;
}

int64_t CISO9660File::GetLength()
{
  return m_stat->p_stat->size;
}

int64_t CISO9660File::GetPosition()
{
  return m_current * ISO_BLOCKSIZE;
}

bool CISO9660File::Exists(const CURL& url)
{
  return Open(url);
}

int CISO9660File::GetChunkSize()
{
  return ISO_BLOCKSIZE;
}