From 37863013ab0b0b09a97f8cbffeab2a2f703f1b2c Mon Sep 17 00:00:00 2001 From: John Klug Date: Thu, 14 Sep 2023 10:15:14 -0500 Subject: Use stream.is_open to test if file open works and use strerror to print errno string --- src/Device/Device.cpp | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/Device/Device.cpp b/src/Device/Device.cpp index 5f0e281..1a2aca5 100644 --- a/src/Device/Device.cpp +++ b/src/Device/Device.cpp @@ -500,11 +500,10 @@ void Device::show(std::string name) { if (code == 0) { printf("%s", fileData.c_str()); exitHandler(0); - } else { - printError("cat: can't open %s%s: No such file or directory", - SYSFS_PLATFORM, name.c_str()); - exitHandler(99); } + printError("cat: can't open %s%s: %s", + SYSFS_PLATFORM, name.c_str(),strerror(errno)); + exitHandler(99); } void Device::showTrigger(std::string name) { @@ -514,11 +513,10 @@ void Device::showTrigger(std::string name) { if (code == 0) { printf("%s", fileData.c_str()); exitHandler(0); - } else { - printError("Can't not open %s%s/trigger': No such file or directory", - LEDS_GPIO_DIR, name.c_str()); - exitHandler(99); } + printError("Can't open %s%s/trigger: %s", + LEDS_GPIO_DIR, name.c_str(),strerror(errno)); + exitHandler(99); } void Device::store(std::string name, std::string value) { @@ -528,15 +526,19 @@ void Device::store(std::string name, std::string value) { } printDebug("Setting %s to %s", name.c_str(), value.c_str()); std::ofstream fileToWrite(SYSFS_PLATFORM + name); - if (!fileToWrite.bad()) { + if (fileToWrite.is_open()) { fileToWrite << value; + if (fileToWrite.bad()) { + printError("Can't write %s%s: %s", SYSFS_PLATFORM, + name.c_str(),strerror(errno)); + exitHandler(98); + } fileToWrite.close(); exitHandler(0); - } else { - printError("Can't open %s%s: No such file or directory", SYSFS_PLATFORM, - name.c_str()); - exitHandler(99); } + printError("Can't open %s%s: %s", SYSFS_PLATFORM, + name.c_str(),strerror(errno)); + exitHandler(99); } void Device::storeTrigger(std::string name, std::string value) { @@ -546,15 +548,19 @@ void Device::storeTrigger(std::string name, std::string value) { } printDebug("Setting %s to %s", name.c_str(), value.c_str()); std::ofstream fileToWrite(LEDS_GPIO_DIR + name + "/trigger"); - if (!fileToWrite.bad()) { + if (fileToWrite.is_open()) { fileToWrite << value; + if(fileToWrite.bad()) { + printError("Can't write %s%s/trigger: %s", + LEDS_GPIO_DIR, name.c_str(), strerror(errno)); + exitHandler(98); + } fileToWrite.close(); exitHandler(0); - } else { - printError("Can't not open %s%s/trigger': No such file or directory", - LEDS_GPIO_DIR, name.c_str()); - exitHandler(99); } + printError("Can't open %s%s/trigger: %s", + LEDS_GPIO_DIR, name.c_str(), strerror(errno)); + exitHandler(99); } void Device::writeJson() { @@ -563,7 +569,7 @@ void Device::writeJson() { deviceInfo.Accept(writer); std::ofstream os(DEVICE_INFO_FILE); if (!os) { - printError("Can't write to %s", DEVICE_INFO_FILE); + printError("Can't write to %s: %s", DEVICE_INFO_FILE,strerror(errno)); exitHandler(99); } else { os << buffer.GetString(); -- cgit v1.2.3