// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Mobius Forensic Toolkit
// Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022 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 <mobius/partition/partition_system.h>
#include <mobius/core/log.h>
#include "disk.h"
#include "disk_impl_base.h"
#include "disk_impl_null.h"
#include "disk_impl_imagefile.h"
#include "disk_impl_reader.h"

#ifdef HAVE_LIBUDEV_H
#include "disk_impl_device.h"
#endif

namespace mobius
{
namespace disk
{
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief default constructor
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
disk::disk ()
  : impl_ (std::make_shared <disk_impl_null> ())
{
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief constructor from implementation pointer
//! \param impl implementation pointer
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
disk::disk (std::shared_ptr <disk_impl_base> impl)
  : is_valid_ (true), impl_ (impl)
{
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief Get filesystems from disk
//! \return List of filesystems detected
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
std::vector <mobius::vfs::filesystem::filesystem>
disk::get_filesystems () const
{
  std::vector <mobius::vfs::filesystem::filesystem> filesystems;
  auto reader = new_reader ();

  // check if disk has a single filesystem
  mobius::vfs::filesystem::filesystem fs (reader, 0);
  if (fs)
    filesystems.push_back (fs);

  // scan partitions
  mobius::partition::partition_system psystem (*this);

  for (const auto& p : psystem.get_partitions ())
    {
      try
        {
          mobius::vfs::filesystem::filesystem fs (reader, p.get_starting_address ());

          if (fs)
            filesystems.push_back (fs);
        }
      catch (const std::exception& e)
        {
          mobius::core::log log (__FILE__, __FUNCTION__);
          log.warning (__LINE__, e.what ());
        }
    }

  return filesystems;
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief create new disk from URL
//! \param url imagefile URL
//! \param imgtype imagefile type
//! \return new disk
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
disk
new_disk_from_url (const std::string& url, const std::string& imgtype)
{
  return disk (std::make_shared <disk_impl_imagefile> (url, imgtype));
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief create new disk from reader
//! \param reader reader object
//! \param sector_size sector size in bytes
//! \return new disk
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
disk
new_disk_from_reader (mobius::io::reader reader, std::uint32_t sector_size)
{
  return disk (std::make_shared <disk_impl_reader> (reader, sector_size));
}

#ifdef HAVE_LIBUDEV_H
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//! \brief create new disk from device UID
//! \param uid device UID
//! \return new disk
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
disk
new_disk_from_device_uid (const std::string& uid)
{
  return disk (std::make_shared <disk_impl_device> (uid));
}
#endif

} // namespace disk
} // namespace mobius
