| | |
| | | #include <sstream> |
| | | #include "../os.h" |
| | | #include "../../base/logger.h" |
| | | #include "../../base/string_utils.h" |
| | | |
| | | #include <mntent.h> |
| | | #include <dirent.h> |
| | | #include <sys/utsname.h> |
| | | #ifndef NDEBUG |
| | | #include <valgrind/memcheck.h> |
| | | #endif |
| | | // #ifndef NDEBUG |
| | | // #include <valgrind/memcheck.h> |
| | | // #endif |
| | | |
| | | //#ifdef USE_DISK_MODEL |
| | | ///#define PARSE_ID_FUNC parse_disk_id |
| | |
| | | #ifdef USE_DBUS |
| | | #include <dbus-1.0/dbus/dbus.h> |
| | | #endif |
| | | |
| | | using namespace license; |
| | | |
| | | /** |
| | | *Usually uuid are hex number separated by "-". this method read up to 8 hex |
| | |
| | | |
| | | FUNCTION_RETURN parse_blkid(const std::string &blkid_file_content, std::vector<DiskInfo> &diskInfos_out, |
| | | std::unordered_map<std::string, int> &disk_by_uuid) { |
| | | DiskInfo diskInfo; |
| | | DiskInfo diskInfo = {}; |
| | | int diskNum = 0; |
| | | for (std::size_t oldpos = 0, pos = 0; (pos = blkid_file_content.find("</device>", oldpos)) != std::string::npos; |
| | | oldpos = pos + 1) { |
| | | std::string cur_dev = blkid_file_content.substr(oldpos, pos); |
| | | diskInfo.id = diskNum++; |
| | | std::string device = cur_dev.substr(cur_dev.find_last_of(">") + 1); |
| | | strncpy(diskInfo.device, device.c_str(), MAX_PATH); |
| | | mstrlcpy(diskInfo.device, device.c_str(), MAX_PATH); |
| | | std::string label = getAttribute(cur_dev, "PARTLABEL"); |
| | | strncpy(diskInfo.label, label.c_str(), 255); |
| | | mstrlcpy(diskInfo.label, label.c_str(), 255); |
| | | std::string disk_sn = getAttribute(cur_dev, "UUID"); |
| | | parseUUID(disk_sn.c_str(), diskInfo.disk_sn, sizeof(diskInfo.disk_sn)); |
| | | std::string disk_type = getAttribute(cur_dev, "TYPE"); |
| | | disk_by_uuid.insert(std::pair<std::string, int>(disk_sn, diskInfo.id)); |
| | | diskInfo.label_initialized = true; |
| | | diskInfo.sn_initialized = true; |
| | | // unlikely that somebody put the swap on a removable disk. |
| | | // this is a first rough guess on what can be a preferred disk for blkid devices |
| | | // just in case /etc/fstab can't be accessed or it is not up to date. |
| | |
| | | bool found = false; |
| | | for (auto &diskInfo : disk_infos) { |
| | | if (((int)(sym_stat.st_ino)) == diskInfo.id) { |
| | | strncpy(diskInfo.label, dir->d_name, 255 - 1); |
| | | mstrlcpy(diskInfo.label, dir->d_name, 255); |
| | | diskInfo.label_initialized = true; |
| | | LOG_DEBUG("Label for disk ino %d device %s, set to %s", sym_stat.st_ino, diskInfo.device, |
| | | diskInfo.label); |
| | |
| | | if (pos != std::string::npos) { |
| | | device_name_s = device_name_s.substr(pos + 1); |
| | | } |
| | | strncpy(tmpDiskInfo.device, device_name_s.c_str(), sizeof(tmpDiskInfo.device)); |
| | | mstrlcpy(tmpDiskInfo.device, device_name_s.c_str(), sizeof(tmpDiskInfo.device)); |
| | | PARSE_ID_FUNC(dir->d_name, tmpDiskInfo.disk_sn, sizeof(tmpDiskInfo.disk_sn)); |
| | | tmpDiskInfo.sn_initialized = true; |
| | | tmpDiskInfo.label_initialized = false; |
| | |
| | | } |
| | | } else { |
| | | LOG_DEBUG("fstab device %s found, but no corresponding diskInfo", ent->mnt_fsname); |
| | | } |
| | | } else if (strncmp("LABEL=", ent->mnt_fsname, 6) == 0) { |
| | | // fstab entry is uuid |
| | | device_name_s = device_name_s.substr(6); |
| | | for (auto &disk_info : diskInfos) { |
| | | if (device_name_s == disk_info.label) { |
| | | disk_info.preferred = true; |
| | | LOG_DEBUG("Disk %d device %s set as preferred", disk_info.id, disk_info.device); |
| | | break; |
| | | } |
| | | } |
| | | } else { |
| | | // fstab entry is a device |
| | |
| | | strcat(proc_path, "/exe"); |
| | | |
| | | int ch = readlink(proc_path, path, MAX_PATH - 1); |
| | | if (ch != -1) { |
| | | path[ch] = '\0'; |
| | | strncpy(buffer, path, ch); |
| | | result = FUNC_RET_OK; |
| | | } else { |
| | | if (ch > MAX_PATH || ch < 0) { |
| | | result = FUNC_RET_ERROR; |
| | | } else { |
| | | mstrlcpy(buffer, path, ch + 1); |
| | | result = FUNC_RET_OK; |
| | | } |
| | | return result; |
| | | } |