Skip to content

Commit

Permalink
Merge pull request #8 from allanliu/libsmartctl
Browse files Browse the repository at this point in the history
libsmartctl: remove intialization exception
  • Loading branch information
allanliu authored Aug 9, 2017
2 parents f41b140 + 000e6ce commit c002e9d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
24 changes: 22 additions & 2 deletions libsmartctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const std::string errStr(ctlerr_t err) {
{GETDEVICERR, "Could not retrieve device information"},
{DEVICEOPENERR, "Could not open device"},
{UNSUPPORTEDDEVICETYPE, "Device type is not supported"},
{CLIENTINITIALIZTIONFAILURE, "libsmartctl client initialization failure"},
};

return errs.at(err);
Expand All @@ -86,13 +87,17 @@ class Client::Impl {
// Initialize interface and check registration
smart_interface::init();
if (!smi()) {
throw std::runtime_error("could not register smart-interface");
up_ = false;
return;
}

// database init has to occur after smart_interface::init();
if (!init_drive_database(false)) {
throw std::runtime_error("could not init drive db");
up_ = false;
return;
}

up_ = true;
}

ctlerr_t initDevice(smart_device_auto_ptr &device, std::string const &devname,
Expand All @@ -119,6 +124,10 @@ class Client::Impl {
public:
CantIdDevResp cantIdDev(std::string const &devname, std::string const &type) {
CantIdDevResp resp;
if (!up_) {
resp.err = CLIENTINITIALIZTIONFAILURE;
return resp;
}

smart_device_auto_ptr device;
ctlerr_t err = initDevice(device, devname, type);
Expand Down Expand Up @@ -149,6 +158,10 @@ class Client::Impl {

DevInfoResp getDevInfo(std::string const &devname, std::string const &type) {
DevInfoResp resp;
if (!up_) {
resp.err = CLIENTINITIALIZTIONFAILURE;
return resp;
}

ata_print_options ataopts;
scsi_print_options scsiopts;
Expand All @@ -174,6 +187,10 @@ class Client::Impl {
DevVendorAttrsResp getDevVendorAttrs(std::string const &devname,
std::string const &type) {
DevVendorAttrsResp resp;
if (!up_) {
resp.err = CLIENTINITIALIZTIONFAILURE;
return resp;
}

ata_print_options ataopts;
scsi_print_options scsiopts;
Expand All @@ -196,6 +213,9 @@ class Client::Impl {

return resp;
}

private:
bool up_;
};

Client::Client() : impl_(Impl::getClient()) {}
Expand Down
1 change: 1 addition & 0 deletions smartctl_errs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum ctlerr_t {
// invalid blk device specificied
DEVICEOPENERR,
UNSUPPORTEDDEVICETYPE,
CLIENTINITIALIZTIONFAILURE,
};

namespace libsmartctl {
Expand Down

0 comments on commit c002e9d

Please sign in to comment.