1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-07-02 14:04:22 +00:00

Implement label and uuid retrieval for the disk being scanned.

This commit is contained in:
King_DuckZ 2016-06-03 20:26:54 +02:00
parent 29c98c0154
commit 742d024bfb
3 changed files with 57 additions and 1 deletions

View file

@ -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}")

View file

@ -35,6 +35,10 @@
# include <iostream>
#endif
#include <cstdint>
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range_core.hpp>
namespace fs = boost::filesystem;
namespace mchlib {
namespace {
@ -167,6 +171,41 @@ namespace mchlib {
return static_cast<LinuxDeviceTypes>(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

View file

@ -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