diff --git a/CMakeLists.txt b/CMakeLists.txt index b0d7662..b3f19f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,7 @@ if ("${DINDEXER_CONFIG_FILE}" STREQUAL "") endif() message(STATUS "Config file set to \"${DINDEXER_CONFIG_FILE}\"") -find_package(Boost 1.53.0 REQUIRED COMPONENTS program_options) +find_package(Boost 1.53.0 REQUIRED COMPONENTS program_options filesystem) find_package(PostgreSQL 8.3 REQUIRED) find_package(YamlCpp 0.5.1 REQUIRED) import_libpqtypes_project("${PostgreSQL_INCLUDE_DIRS}" "-O3 ${march_flag}") diff --git a/src/machinery/discinfo.cpp b/src/machinery/discinfo.cpp index 3993c30..521da49 100644 --- a/src/machinery/discinfo.cpp +++ b/src/machinery/discinfo.cpp @@ -35,6 +35,10 @@ # include #endif #include +#include +#include + +namespace fs = boost::filesystem; namespace mchlib { namespace { @@ -167,6 +171,41 @@ namespace mchlib { return static_cast(retval); } #endif + + std::string find_with_same_inode (const std::string& parWhat, const char* parWhere) { + using boost::make_iterator_range; + + struct stat st; + if (stat(parWhat.c_str(), &st)) + return std::string(); + + const auto& inode = st.st_ino; + + fs::path p(parWhere); + if (not fs::exists(p) or not fs::is_directory(p)) { + throw std::runtime_error( + std::string("Search path \"") + p.string() + + "\" doesn't exist"); + } + + for (const fs::directory_entry& itm : make_iterator_range(fs::directory_iterator(p), fs::directory_iterator())) { + struct stat curr_st; + if (stat(itm.path().c_str(), &curr_st) and inode == curr_st.st_ino) + return fs::basename(itm); + } + + return std::string(); + } + + //Get disc label by doing the equivalent of: + //find -L /dev/disk/by-label -inum $(stat -c %i /dev/sda1) -print + std::string retrieve_label (const std::string& parMountpoint) { + return find_with_same_inode(parMountpoint, "/dev/disk/by-label"); + } + + std::string retrieve_uuid (const std::string& parMountpoint) { + return find_with_same_inode(parMountpoint, "/dev/disk/by-uuid"); + } } //unnamed namespace DiscInfo::DiscInfo (std::string&& parPath) : @@ -187,6 +226,11 @@ namespace mchlib { } input_path.pop_right(); } while(input_path.atom_count() > 0); + + if (mountpoint_found()) { + m_label = retrieve_label(mountpoint()); + m_uuid = retrieve_uuid(mountpoint()); + } } bool DiscInfo::mountpoint_found() const { @@ -329,4 +373,12 @@ namespace mchlib { }; } #endif + + const std::string& DiscInfo::label() const { + return m_label; + } + + const std::string& DiscInfo::filesystem_uuid() const { + return m_uuid; + } } //namespace mchlib diff --git a/src/machinery/discinfo.hpp b/src/machinery/discinfo.hpp index b4b4e25..0093d1f 100644 --- a/src/machinery/discinfo.hpp +++ b/src/machinery/discinfo.hpp @@ -52,11 +52,15 @@ namespace mchlib { OpticalTypes optical_type ( void ) const; DriveTypes drive_type ( void ) const; #endif + const std::string& label ( void ) const; + const std::string& filesystem_uuid ( void ) const; private: const std::string m_initial_path; std::string m_mountpoint; std::string m_device; + std::string m_label; + std::string m_uuid; }; } //namespace mchlib