DevicesModel.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DevicesModel.h"
  7. #include <AK/JsonObject.h>
  8. #include <AK/JsonValue.h>
  9. #include <LibCore/DirIterator.h>
  10. #include <LibCore/File.h>
  11. #include <sys/stat.h>
  12. NonnullRefPtr<DevicesModel> DevicesModel::create()
  13. {
  14. return adopt_ref(*new DevicesModel);
  15. }
  16. DevicesModel::DevicesModel()
  17. {
  18. }
  19. DevicesModel::~DevicesModel()
  20. {
  21. }
  22. int DevicesModel::row_count(const GUI::ModelIndex&) const
  23. {
  24. return m_devices.size();
  25. }
  26. int DevicesModel::column_count(const GUI::ModelIndex&) const
  27. {
  28. return Column::__Count;
  29. }
  30. String DevicesModel::column_name(int column) const
  31. {
  32. switch (column) {
  33. case Column::Device:
  34. return "Device";
  35. case Column::Major:
  36. return "Major";
  37. case Column::Minor:
  38. return "Minor";
  39. case Column::ClassName:
  40. return "Class";
  41. case Column::Type:
  42. return "Type";
  43. default:
  44. VERIFY_NOT_REACHED();
  45. }
  46. }
  47. GUI::Variant DevicesModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  48. {
  49. VERIFY(is_valid(index));
  50. if (role == GUI::ModelRole::TextAlignment) {
  51. switch (index.column()) {
  52. case Column::Device:
  53. return Gfx::TextAlignment::CenterLeft;
  54. case Column::Major:
  55. return Gfx::TextAlignment::CenterRight;
  56. case Column::Minor:
  57. return Gfx::TextAlignment::CenterRight;
  58. case Column::ClassName:
  59. return Gfx::TextAlignment::CenterLeft;
  60. case Column::Type:
  61. return Gfx::TextAlignment::CenterLeft;
  62. }
  63. return {};
  64. }
  65. if (role == GUI::ModelRole::Sort) {
  66. const DeviceInfo& device = m_devices[index.row()];
  67. switch (index.column()) {
  68. case Column::Device:
  69. return device.path;
  70. case Column::Major:
  71. return device.major;
  72. case Column::Minor:
  73. return device.minor;
  74. case Column::ClassName:
  75. return device.class_name;
  76. case Column::Type:
  77. return device.type;
  78. default:
  79. VERIFY_NOT_REACHED();
  80. }
  81. }
  82. if (role == GUI::ModelRole::Display) {
  83. const DeviceInfo& device = m_devices[index.row()];
  84. switch (index.column()) {
  85. case Column::Device:
  86. return device.path;
  87. case Column::Major:
  88. return device.major;
  89. case Column::Minor:
  90. return device.minor;
  91. case Column::ClassName:
  92. return device.class_name;
  93. case Column::Type:
  94. switch (device.type) {
  95. case DeviceInfo::Type::Block:
  96. return "Block";
  97. case DeviceInfo::Type::Character:
  98. return "Character";
  99. default:
  100. VERIFY_NOT_REACHED();
  101. }
  102. default:
  103. VERIFY_NOT_REACHED();
  104. }
  105. }
  106. return {};
  107. }
  108. void DevicesModel::invalidate()
  109. {
  110. // FIXME: granularly update this.
  111. auto proc_devices = Core::File::construct("/proc/devices");
  112. if (!proc_devices->open(Core::OpenMode::ReadOnly))
  113. VERIFY_NOT_REACHED();
  114. auto json = JsonValue::from_string(proc_devices->read_all());
  115. VERIFY(json.has_value());
  116. m_devices.clear();
  117. json.value().as_array().for_each([this](auto& value) {
  118. JsonObject device = value.as_object();
  119. DeviceInfo device_info;
  120. device_info.major = device.get("major").to_uint();
  121. device_info.minor = device.get("minor").to_uint();
  122. device_info.class_name = device.get("class_name").to_string();
  123. String type_str = device.get("type").to_string();
  124. if (type_str == "block")
  125. device_info.type = DeviceInfo::Type::Block;
  126. else if (type_str == "character")
  127. device_info.type = DeviceInfo::Type::Character;
  128. else
  129. VERIFY_NOT_REACHED();
  130. m_devices.append(move(device_info));
  131. });
  132. auto fill_in_paths_from_dir = [this](const String& dir) {
  133. Core::DirIterator dir_iter { dir, Core::DirIterator::Flags::SkipDots };
  134. while (dir_iter.has_next()) {
  135. auto path = dir_iter.next_full_path();
  136. struct stat statbuf;
  137. if (lstat(path.characters(), &statbuf) != 0) {
  138. VERIFY_NOT_REACHED();
  139. }
  140. if (!S_ISBLK(statbuf.st_mode) && !S_ISCHR(statbuf.st_mode))
  141. continue;
  142. unsigned _major = major(statbuf.st_rdev);
  143. unsigned _minor = minor(statbuf.st_rdev);
  144. auto it = m_devices.find_if([_major, _minor](const auto& device_info) {
  145. return device_info.major == _major && device_info.minor == _minor;
  146. });
  147. if (it != m_devices.end()) {
  148. (*it).path = move(path);
  149. }
  150. }
  151. };
  152. fill_in_paths_from_dir("/dev");
  153. fill_in_paths_from_dir("/dev/pts");
  154. Model::invalidate();
  155. }