// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Mobius Forensic Toolkit
// Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022,2023 Eduardo Aguiar
//
// 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 this program. If not, see <http://www.gnu.org/licenses/>.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include "disk_impl_imagefile.h"
#include <mobius/io/uri.h>

namespace mobius
{
namespace disk
{
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief constructor
//! \param url imagefile URL
//! \param imgtype imagefile type
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
disk_impl_imagefile::disk_impl_imagefile (const std::string& url, const std::string& imgtype)
  : url_ (url),
    imgtype_ (imgtype),
    imagefile_ (mobius::vfs::new_imagefile_by_url (url_, imgtype_))
{
  mobius::io::uri uri (url);
  set_name ("imagefile " + uri.get_filename ());

  if (imagefile_.is_available ())
    {
      set_size (imagefile_.get_size ());
      set_sector_size (imagefile_.get_sector_size ());
      imgtype_ = imagefile_.get_type ();
    }
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief check whether disk is available
//! \return true/false
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
bool
disk_impl_imagefile::is_available () const
{
  return imagefile_.is_available ();
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief create new reader
//! \return new reader
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
mobius::io::reader
disk_impl_imagefile::new_reader () const
{
  return imagefile_.new_reader ();
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief (re)scan disk
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void
disk_impl_imagefile::scan ()
{
  imagefile_ = mobius::vfs::new_imagefile_by_url (url_, imgtype_);
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief get metadata
//! \return disk metadata
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
mobius::metadata
disk_impl_imagefile::get_metadata () const
{
  mobius::metadata metadata;
  std::uint32_t sector_size = get_sector_size ();
  std::uint64_t sectors = sector_size ? (get_size () + sector_size - 1) / sector_size : 0;

  for (const auto& attr : imagefile_.get_metadata ())
    {
      if (attr.name == "sectors")
        metadata.add (attr.name, attr.description, "std::uint64_t", std::to_string (sectors));

      else if (attr.name == "sector_size")
        metadata.add (attr.name, attr.description, "std::uint32_t", std::to_string (sector_size));
      
      else 
        metadata.add (attr.name, attr.description, attr.datatype, attr.value);
    }

  return metadata;
}

} // namespace disk
} // namespace mobius
