1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-08-14 14:49:48 +00:00

Implement autodetect set type.

Only supports HardDisk, Directory, CdRom, DVD, BluRay for now.
This commit is contained in:
King_DuckZ 2015-12-08 13:56:46 +00:00
parent 87bc031e65
commit fe2ea40c4f
13 changed files with 499 additions and 17 deletions

View file

@ -18,7 +18,7 @@
#ifndef id48D6E1D45238460F99C2BCBFDE920791
#define id48D6E1D45238460F99C2BCBFDE920791
#define PROGRAM_NAME "@PROJECT_NAME@"
#define PROGRAM_NAME "@bare_name@"
#define VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define VERSION_MINOR @PROJECT_VERSION_MINOR@
#define VERSION_BETA @PROJECT_VERSION_BETA@

View file

@ -20,6 +20,7 @@
#include <stdlib.h>
#include <iso646.h>
#include <sys/stat.h>
#include <unistd.h>
#include "dindexerConfig.h"
#include "helpers/lengthof.h"

View file

@ -115,6 +115,7 @@ static size_t foreach_avail_action(int(*parFunc)(char*, char*), char** parList,
return z;
}
}
return z;
}
static int printf_stderr (char* parMsg, char* parUnused) {

View file

@ -1,5 +1,7 @@
project(${bare_name}-scan CXX C)
find_package(blkid REQUIRED)
add_executable(${PROJECT_NAME}
main.cpp
filesearcher.cpp
@ -10,12 +12,17 @@ add_executable(${PROJECT_NAME}
dbbackend.cpp
settings.cpp
commandline.cpp
discinfo.cpp
)
target_include_directories(${PROJECT_NAME}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/..
)
target_include_directories(${PROJECT_NAME} SYSTEM
PRIVATE ${BLKID_INCLUDE_DIRS}
)
target_link_libraries(${PROJECT_NAME}
PRIVATE ${bare_name}-if
PRIVATE ${BLKID_LIBRARIES}
)

View file

@ -30,14 +30,14 @@ namespace po = boost::program_options;
namespace din {
namespace {
const char g_allowed_types[] = {
'C', //CD-Rom
'D', //Directory
'V', //DVD
'B', //BluRay
'F', //Floppy Disk
'H', //Hard Disk
'Z', //Iomega Zip
'O' //Other
static_cast<char>(SetSourceType_CDRom),
static_cast<char>(SetSourceType_Directory),
static_cast<char>(SetSourceType_DVD),
static_cast<char>(SetSourceType_BluRay),
static_cast<char>(SetSourceType_FloppyDisk),
static_cast<char>(SetSourceType_HardDisk),
static_cast<char>(SetSourceType_IomegaZip),
static_cast<char>(SetSourceType_Other)
};
const char* const g_version_string =
PROGRAM_NAME " v"
@ -60,6 +60,7 @@ namespace din {
oss << ", " << g_allowed_types[z];
}
oss << '.';
oss << " Default is 'autodetect'.";
type_param_help = oss.str();
}
@ -75,7 +76,7 @@ namespace din {
po::options_description set_options("Set options");
set_options.add_options()
("setname,n", po::value<std::string>()->default_value("New set"), "Name to be given to the new set being scanned.")
("type,t", po::value<char>()->default_value('V'), type_param_help.c_str())
("type,t", po::value<char>(), type_param_help.c_str())
;
po::options_description positional_options("Positional options");
positional_options.add_options()
@ -120,7 +121,7 @@ namespace din {
if (parVarMap.count("search-path") == 0) {
throw std::invalid_argument("No search path specified");
}
if (g_allowed_types + lengthof(g_allowed_types) == std::find(g_allowed_types, g_allowed_types + lengthof(g_allowed_types), parVarMap["type"].as<char>())) {
if (parVarMap.count("type") and g_allowed_types + lengthof(g_allowed_types) == std::find(g_allowed_types, g_allowed_types + lengthof(g_allowed_types), parVarMap["type"].as<char>())) {
throw std::invalid_argument("Invalid value for parameter \"type\"");
}
return false;

View file

@ -21,6 +21,17 @@
#include <boost/program_options/variables_map.hpp>
namespace din {
enum SetSourceTypes {
SetSourceType_CDRom = 'C',
SetSourceType_Directory = 'D',
SetSourceType_DVD = 'V',
SetSourceType_BluRay = 'B',
SetSourceType_FloppyDisk = 'F',
SetSourceType_HardDisk = 'H',
SetSourceType_IomegaZip = 'Z',
SetSourceType_Other = 'O'
};
bool parse_commandline ( int parArgc, char* parArgv[], boost::program_options::variables_map& parVarMap );
} //namespace din

325
src/scan/discinfo.cpp Normal file
View file

@ -0,0 +1,325 @@
/* Copyright 2015, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" 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 3 of the License, or
* (at your option) any later version.
*
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#include "discinfo.hpp"
#include "pathname.hpp"
#include "helpers/lengthof.h"
#include <map>
#include <fstream>
#include <boost/tokenizer.hpp>
#include <ciso646>
#include <blkid/blkid.h>
#include <linux/limits.h>
#include <sys/stat.h>
#include <memory>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <scsi/sg.h>
#if !defined(NDEBUG)
# include <iostream>
#endif
#include <cstdint>
namespace din {
namespace {
using MountpointsMap = std::map<std::string, std::string>;
//See: http://lkml.iu.edu/hypermail/linux/kernel/0602.1/1295.html
enum LinuxDeviceTypes {
LinuxDeviceType_Disk = 0x00,
LinuxDeviceType_Tape = 0x01,
LinuxDeviceType_Printer = 0x02,
LinuxDeviceType_Processor = 0x03,
LinuxDeviceType_Worm = 0x04,
LinuxDeviceType_ROM = 0x05,
LinuxDeviceType_Scanner = 0x06,
LinuxDeviceType_Mod = 0x07,
LinuxDeviceType_MediumChanger = 0x08,
LinuxDeviceType_Comm = 0x09,
LinuxDeviceType_Raid = 0x0c,
LinuxDeviceType_Enclosure = 0x0d,
LinuxDeviceType_RBC = 0x0e,
LinuxDeviceType_NoLun = 0x7f,
LinuxDeviceType_Unknown = 0xff
};
//From cdrecord's source (cdrecord/scsi_mmc.c)
enum DiscTypes {
DiscType_Reserved = 0x0000, //"Reserved"
DiscType_NonRemovable = 0x0001, //"Non -removable Disk"
DiscType_Removable = 0x0002, //"Removable Disk"
DiscType_MOErasable = 0x0003, //"MO Erasable"
DiscType_MOWriteOnce = 0x0004, //"MO Write Once"
DiscType_ASMO = 0x0005, //"AS-MO"
/* 0x06..0x07 is reserved */
DiscType_CDROM = 0x0008, //"CD-ROM"
DiscType_CDR = 0x0009, //"CD-R"
DiscType_CDRW = 0x000A, //"CD-RW"
/* 0x0B..0x0F is reserved */
DiscType_DVDROM = 0x0010, //"DVD-ROM"
DiscType_DVDR = 0x0011, //"DVD-R sequential recording"
DiscType_DVDRAM = 0x0012, //"DVD-RAM"
DiscType_DVDRWRestricted = 0x0013, //"DVD-RW restricted overwrite"
DiscType_DVDRW = 0x0014, //"DVD-RW sequential recording"
DiscType_DVDRDL = 0x0015, //"DVD-R/DL sequential recording"
DiscType_DVDRDLJump = 0x0016, //"DVD-R/DL layer jump recording"
DiscType_DVDRWDL = 0x0017, //"DVD-RW/DL"
/* 0x18..0x19 is reserved */
DiscType_DVDPRW = 0x001A, //"DVD+RW"
DiscType_DVDPR = 0x001B, //"DVD+R"
DiscType_DDCDROM = 0x0020, //"DDCD-ROM"
DiscType_DDCDR = 0x0021, //"DDCD-R"
DiscType_DDCDRW = 0x0022, //"DDCD-RW"
DiscType_DVDPRWDL = 0x002A, //"DVD+RW/DL"
DiscType_DVDPRDL = 0x002B, //"DVD+R/DL"
DiscType_BDROM = 0x0040, //"BD-ROM"
DiscType_BDR = 0x0041, //"BD-R sequential recording"
DiscType_BDRRandom = 0x0042, //"BD-R random recording"
DiscType_BDRE = 0x0043, //"BD-RE"
/* 0x44..0x4F is reserved */
DiscType_HDDVDROM = 0x0050, //"HD DVD-ROM"
DiscType_HDDVDR = 0x0051, //"HD DVD-R"
DiscType_HDDVDRAM = 0x0052, //"HD DVD-RAM"
DiscType_HDDVDRW = 0x0053, //"HD DVD-RW"
/* 0x54..0x57 is reserved */
DiscType_HDDVDRDL = 0x0058, //"HD DVD-R/DL"
DiscType_HDDVDRWDL = 0x005A, //"HD DVD-RW/DL"
DiscType_NoStandardProfile = 0xFFFF, //"No standard Profile"
};
MountpointsMap list_mountpoints() {
std::ifstream rd("/proc/mounts");
std::string line;
MountpointsMap retmap;
boost::char_separator<char> sep(" ");
while (std::getline(rd, line)) {
boost::tokenizer<boost::char_separator<char>> tok(line, sep);
auto it_token = tok.begin();
std::string dev(*it_token);
++it_token;
std::string mount(*it_token);
retmap[std::move(mount)] = std::move(dev);
}
return std::move(retmap);
}
LinuxDeviceTypes get_dev_type (const std::string& parDev) {
const char dev_prefix[] = "/dev/";
const std::size_t dev_prefix_len = lengthof(dev_prefix) - 1;
int retval = LinuxDeviceType_Unknown;
struct stat st;
if (stat(parDev.c_str(), &st) or not S_ISBLK(st.st_mode)) {
return LinuxDeviceType_Unknown;
}
if (parDev.size() > dev_prefix_len and std::equal(dev_prefix, dev_prefix + dev_prefix_len, parDev.begin())) {
std::unique_ptr<char[]> dev_buff(new char[PATH_MAX + 1]);
dev_t disk = 0;
if (blkid_devno_to_wholedisk(st.st_rdev, dev_buff.get(), PATH_MAX + 1, &disk)) {
return LinuxDeviceType_Unknown;
}
std::string sys_type_path("/sys/block/");
sys_type_path += dev_buff.get();
sys_type_path += "/device/type";
std::ifstream rd(sys_type_path);
rd >> retval;
}
return static_cast<LinuxDeviceTypes>(retval);
}
} //unnamed namespace
DiscInfo::DiscInfo (std::string&& parPath) :
m_initial_path(std::move(parPath)),
m_mountpoint(),
m_device()
{
PathName input_path((std::string(m_initial_path)));
auto mounts = list_mountpoints();
do {
auto curr_path = input_path.path();
auto it_found = mounts.find(curr_path);
if (mounts.end() != it_found) {
m_mountpoint = std::move(curr_path);
m_device = it_found->second;
break;
}
input_path.pop_right();
} while(input_path.atom_count() > 0);
}
bool DiscInfo::mountpoint_found() const {
return not m_device.empty();
}
DriveTypes DiscInfo::drive_type() const {
auto dev_type = get_dev_type(m_device);
switch (dev_type) {
case LinuxDeviceType_Disk:
return DriveType_HardDisk;
case LinuxDeviceType_ROM:
return DriveType_Optical;
case LinuxDeviceType_Tape:
case LinuxDeviceType_Printer:
case LinuxDeviceType_Processor:
case LinuxDeviceType_Worm:
case LinuxDeviceType_Scanner:
case LinuxDeviceType_Mod:
case LinuxDeviceType_MediumChanger:
case LinuxDeviceType_Comm:
case LinuxDeviceType_Raid:
case LinuxDeviceType_Enclosure:
case LinuxDeviceType_RBC:
case LinuxDeviceType_NoLun:
return DriveType_Other;
case LinuxDeviceType_Unknown:
default:
return DriveType_Unknown;
}
}
OpticalTypes DiscInfo::optical_type() const {
struct FileDesc {
FileDesc ( void ) : m_fd(0) {}
FileDesc ( int parFD ) : m_fd(parFD) {}
FileDesc ( std::nullptr_t ) : m_fd(0) {}
operator int() { return m_fd; }
bool operator== ( const FileDesc& parOther ) const { return m_fd == parOther.m_fd; }
bool operator!= ( const FileDesc& parOther ) const { return m_fd != parOther.m_fd; }
int m_fd;
};
struct FileCloser {
typedef FileDesc pointer;
void operator() ( FileDesc parFile ) const { close(parFile.m_fd); }
};
using AutoFile = std::unique_ptr<FileDesc, FileCloser>;
const auto drive_type = this->drive_type();
if (drive_type != DriveType_Optical) {
return OpticalType_NotOptical;
}
char out_data[8] {};
unsigned char scsi_command[10] = {'F', '\0', '\0', '\0', '\0', '\0', '\0', '\0', lengthof(out_data), '\0'};
unsigned char sense_buffer[16] {};
AutoFile f_dev(open(m_device.c_str(), O_RDONLY));
sg_io_hdr_t sg_io {};
//See: http://www.tldp.org/HOWTO/SCSI-Generic-HOWTO/sg_io_hdr_t.html
sg_io.interface_id = 'S';
sg_io.dxfer_direction = SG_DXFER_FROM_DEV;
sg_io.dxfer_len = lengthof(out_data);
sg_io.dxferp = out_data;
sg_io.timeout = 40000;
sg_io.flags |= SG_FLAG_DIRECT_IO;
sg_io.mx_sb_len = lengthof(sense_buffer);
sg_io.cmdp = scsi_command;
sg_io.cmd_len = lengthof(scsi_command);
sg_io.sbp = sense_buffer;
const int ioctl_ret = ioctl(f_dev.get(), SG_IO, &sg_io);
#if !defined(NDEBUG)
switch (ioctl_ret) {
case EBADF:
std::cerr << "fd is not a valid descriptor.\n";
return OpticalType_Unknown;
case EFAULT:
std::cerr << "argp references an inaccessible memory area.\n";
return OpticalType_Unknown;
case EINVAL:
std::cerr << "request or argp is not valid.\n";
return OpticalType_Unknown;
case ENOTTY:
std::cerr << "fd is not associated with a character special device.\n";
return OpticalType_Unknown;
}
#else
if (ioctl_ret) {
return OpticalType_Unknown;
}
#endif
const DiscTypes detected_type = static_cast<DiscTypes>(
static_cast<uint16_t>((static_cast<uint16_t>(out_data[7]) & 0xFF)) | (static_cast<uint16_t>(out_data[6]) << 8 & 0xFF00)
);
switch (detected_type) {
case DiscType_NonRemovable: //"Non -removable Disk"
case DiscType_Removable: //"Removable Disk"
case DiscType_MOErasable: //"MO Erasable"
case DiscType_MOWriteOnce: //"MO Write Once"
case DiscType_ASMO: //"AS-MO"
return OpticalType_NotOptical;
case DiscType_CDROM: //"CD-ROM"
case DiscType_CDR: //"CD-R"
case DiscType_CDRW: //"CD-RW"
return OpticalType_CDRom;
case DiscType_DVDROM: //"DVD-ROM"
case DiscType_DVDR: //"DVD-R sequential recording"
case DiscType_DVDRAM: //"DVD-RAM"
case DiscType_DVDRWRestricted: //"DVD-RW restricted overwrite"
case DiscType_DVDRW: //"DVD-RW sequential recording"
case DiscType_DVDRDL: //"DVD-R/DL sequential recording"
case DiscType_DVDRDLJump: //"DVD-R/DL layer jump recording"
case DiscType_DVDRWDL: //"DVD-RW/DL"
case DiscType_DVDPRW: //"DVD+RW"
case DiscType_DVDPR: //"DVD+R"
case DiscType_DVDPRWDL: //"DVD+RW/DL"
case DiscType_DVDPRDL: //"DVD+R/DL"
return OpticalType_DVD;
case DiscType_DDCDROM: //"DDCD-ROM"
case DiscType_DDCDR: //"DDCD-R"
case DiscType_DDCDRW: //"DDCD-RW"
return OpticalType_DDCDRom;
case DiscType_BDROM: //"BD-ROM"
case DiscType_BDR: //"BD-R sequential recording"
case DiscType_BDRRandom: //"BD-R random recording"
case DiscType_BDRE: //"BD-RE"
return OpticalType_BluRay;
case DiscType_HDDVDROM: //"HD DVD-ROM"
case DiscType_HDDVDR: //"HD DVD-R"
case DiscType_HDDVDRAM: //"HD DVD-RAM"
case DiscType_HDDVDRW: //"HD DVD-RW"
case DiscType_HDDVDRDL: //"HD DVD-R/DL"
case DiscType_HDDVDRWDL: //"HD DVD-RW/DL"
return OpticalType_DVD;
case DiscType_Reserved: //"Reserved"
case DiscType_NoStandardProfile: //"No standard Profile"
default:
return OpticalType_Unknown;
};
}
} //namespace din

58
src/scan/discinfo.hpp Normal file
View file

@ -0,0 +1,58 @@
/* Copyright 2015, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" 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 3 of the License, or
* (at your option) any later version.
*
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idED32F82A21AE430EA2E253EC308F872C
#define idED32F82A21AE430EA2E253EC308F872C
#include <string>
namespace din {
enum OpticalTypes {
OpticalType_DVD,
OpticalType_CDRom,
OpticalType_DDCDRom,
OpticalType_BluRay,
OpticalType_NotOptical,
OpticalType_Unknown
};
enum DriveTypes {
DriveType_Optical,
DriveType_HardDisk,
DriveType_Other,
DriveType_Unknown
};
class DiscInfo {
public:
explicit DiscInfo ( std::string&& parPath );
~DiscInfo ( void ) noexcept = default;
const std::string& mountpoint ( void ) const { return m_mountpoint; }
const std::string& device ( void ) const { return m_device; }
OpticalTypes optical_type ( void ) const;
bool mountpoint_found ( void ) const;
DriveTypes drive_type ( void ) const;
private:
const std::string m_initial_path;
std::string m_mountpoint;
std::string m_device;
};
} //namespace din
#endif

View file

@ -220,7 +220,7 @@ namespace din {
m_local_data->db_settings = parDBSettings;
}
Indexer::~Indexer() {
Indexer::~Indexer() noexcept {
}
std::size_t Indexer::total_items() const {

View file

@ -31,11 +31,13 @@
# include <condition_variable>
#endif
#include <wordexp.h>
#include "discinfo.hpp"
#include "dindexerConfig.h"
#include "filesearcher.hpp"
#include "indexer.hpp"
#include "settings.hpp"
#include "commandline.hpp"
#include "pathname.hpp"
namespace {
void run_hash_calculation ( din::Indexer& parIndexer, bool parShowProgress );
@ -68,11 +70,51 @@ int main (int parArgc, char* parArgv[]) {
{
const bool loaded = din::load_settings(expand(CONFIG_FILE_PATH), settings);
if (not loaded) {
std::cerr << "Can't load settings from dindexerrc.yml, quitting\n";
std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n";
return 1;
}
}
char set_type;
if (0 == vm.count("type")) {
std::cout << "Analyzing disc... ";
din::DiscInfo info((std::string(search_path)));
const din::DriveTypes drive_type = info.drive_type();
if (din::DriveType_HardDisk == drive_type) {
if (info.mountpoint() == din::PathName(search_path).path())
set_type = din::SetSourceType_HardDisk;
else
set_type = din::SetSourceType_Directory;
}
else if (din::DriveType_Optical == drive_type) {
switch (info.optical_type()) {
case din::OpticalType_DVD:
set_type = din::SetSourceType_DVD;
break;
case din::OpticalType_CDRom:
set_type = din::SetSourceType_CDRom;
break;
case din::OpticalType_BluRay:
set_type = din::SetSourceType_BluRay;
break;
default:
std::cerr << "Set autodetect failed because this media type is unknown, please specify the set type manually\n";
return 1;
}
}
else {
std::cerr << "Can't autodetect set type, please specify it manually\n";
return 1;
}
}
else {
set_type = vm["type"].as<char>();
}
std::cout << "Setting type to " << set_type << '\n';
return 0;
std::cout << "constructing...\n";
din::Indexer indexer(settings);
fastf::FileSearcher searcher(search_path);
fastf::FileSearcher::ConstCharVecType ext, ignore;
@ -92,7 +134,7 @@ int main (int parArgc, char* parArgv[]) {
if (verbose) {
std::cout << "Writing to database...\n";
}
if (not indexer.add_to_db(vm["setname"].as<std::string>(), vm["type"].as<char>())) {
if (not indexer.add_to_db(vm["setname"].as<std::string>(), set_type)) {
std::cerr << "Not written to DB, likely because a set with the same hash already exists\n";
}
}