DevicesModel.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "DevicesModel.h"
  27. #include <AK/JsonArray.h>
  28. #include <AK/JsonObject.h>
  29. #include <AK/JsonValue.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <LibCore/File.h>
  32. #include <sys/stat.h>
  33. NonnullRefPtr<DevicesModel> DevicesModel::create()
  34. {
  35. return adopt(*new DevicesModel);
  36. }
  37. DevicesModel::DevicesModel()
  38. {
  39. }
  40. DevicesModel::~DevicesModel()
  41. {
  42. }
  43. int DevicesModel::row_count(const GUI::ModelIndex&) const
  44. {
  45. return m_devices.size();
  46. }
  47. int DevicesModel::column_count(const GUI::ModelIndex&) const
  48. {
  49. return Column::__Count;
  50. }
  51. String DevicesModel::column_name(int column) const
  52. {
  53. switch (column) {
  54. case Column::Device:
  55. return "Device";
  56. case Column::Major:
  57. return "Major";
  58. case Column::Minor:
  59. return "Minor";
  60. case Column::ClassName:
  61. return "Class";
  62. case Column::Type:
  63. return "Type";
  64. default:
  65. ASSERT_NOT_REACHED();
  66. }
  67. }
  68. GUI::Variant DevicesModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  69. {
  70. ASSERT(is_valid(index));
  71. if (role == GUI::ModelRole::TextAlignment) {
  72. switch (index.column()) {
  73. case Column::Device:
  74. return Gfx::TextAlignment::CenterLeft;
  75. case Column::Major:
  76. return Gfx::TextAlignment::CenterRight;
  77. case Column::Minor:
  78. return Gfx::TextAlignment::CenterRight;
  79. case Column::ClassName:
  80. return Gfx::TextAlignment::CenterLeft;
  81. case Column::Type:
  82. return Gfx::TextAlignment::CenterLeft;
  83. }
  84. return {};
  85. }
  86. if (role == GUI::ModelRole::Sort) {
  87. const DeviceInfo& device = m_devices[index.row()];
  88. switch (index.column()) {
  89. case Column::Device:
  90. return device.path;
  91. case Column::Major:
  92. return device.major;
  93. case Column::Minor:
  94. return device.minor;
  95. case Column::ClassName:
  96. return device.class_name;
  97. case Column::Type:
  98. return device.type;
  99. default:
  100. ASSERT_NOT_REACHED();
  101. }
  102. }
  103. if (role == GUI::ModelRole::Display) {
  104. const DeviceInfo& device = m_devices[index.row()];
  105. switch (index.column()) {
  106. case Column::Device:
  107. return device.path;
  108. case Column::Major:
  109. return device.major;
  110. case Column::Minor:
  111. return device.minor;
  112. case Column::ClassName:
  113. return device.class_name;
  114. case Column::Type:
  115. switch (device.type) {
  116. case DeviceInfo::Type::Block:
  117. return "Block";
  118. case DeviceInfo::Type::Character:
  119. return "Character";
  120. default:
  121. ASSERT_NOT_REACHED();
  122. }
  123. default:
  124. ASSERT_NOT_REACHED();
  125. }
  126. }
  127. return {};
  128. }
  129. void DevicesModel::update()
  130. {
  131. auto proc_devices = Core::File::construct("/proc/devices");
  132. if (!proc_devices->open(Core::IODevice::OpenMode::ReadOnly))
  133. ASSERT_NOT_REACHED();
  134. auto json = JsonValue::from_string(proc_devices->read_all());
  135. ASSERT(json.has_value());
  136. m_devices.clear();
  137. json.value().as_array().for_each([this](auto& value) {
  138. JsonObject device = value.as_object();
  139. DeviceInfo device_info;
  140. device_info.major = device.get("major").to_uint();
  141. device_info.minor = device.get("minor").to_uint();
  142. device_info.class_name = device.get("class_name").to_string();
  143. String type_str = device.get("type").to_string();
  144. if (type_str == "block")
  145. device_info.type = DeviceInfo::Type::Block;
  146. else if (type_str == "character")
  147. device_info.type = DeviceInfo::Type::Character;
  148. else
  149. ASSERT_NOT_REACHED();
  150. m_devices.append(move(device_info));
  151. });
  152. auto fill_in_paths_from_dir = [this](const String& dir) {
  153. Core::DirIterator dir_iter { dir, Core::DirIterator::Flags::SkipDots };
  154. while (dir_iter.has_next()) {
  155. auto name = dir_iter.next_path();
  156. auto path = String::formatted("{}/{}", dir, name);
  157. struct stat statbuf;
  158. if (lstat(path.characters(), &statbuf) != 0) {
  159. ASSERT_NOT_REACHED();
  160. }
  161. if (!S_ISBLK(statbuf.st_mode) && !S_ISCHR(statbuf.st_mode))
  162. continue;
  163. unsigned _major = major(statbuf.st_rdev);
  164. unsigned _minor = minor(statbuf.st_rdev);
  165. auto it = m_devices.find_if([_major, _minor](const auto& device_info) {
  166. return device_info.major == _major && device_info.minor == _minor;
  167. });
  168. if (it != m_devices.end()) {
  169. (*it).path = move(path);
  170. }
  171. }
  172. };
  173. fill_in_paths_from_dir("/dev");
  174. fill_in_paths_from_dir("/dev/pts");
  175. did_update();
  176. }