DevicesModel.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/CDirIterator.h>
  31. #include <LibCore/CFile.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 GModelIndex&) const
  44. {
  45. return m_devices.size();
  46. }
  47. int DevicesModel::column_count(const GModelIndex&) 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. GModel::ColumnMetadata DevicesModel::column_metadata(int column) const
  69. {
  70. switch (column) {
  71. case Column::Device:
  72. return { 70, TextAlignment::CenterLeft };
  73. case Column::Major:
  74. return { 32, TextAlignment::CenterRight };
  75. case Column::Minor:
  76. return { 32, TextAlignment::CenterRight };
  77. case Column::ClassName:
  78. return { 120, TextAlignment::CenterLeft };
  79. case Column::Type:
  80. return { 120, TextAlignment::CenterLeft };
  81. default:
  82. ASSERT_NOT_REACHED();
  83. }
  84. }
  85. GVariant DevicesModel::data(const GModelIndex& index, Role) const
  86. {
  87. ASSERT(is_valid(index));
  88. const DeviceInfo& device = m_devices[index.row()];
  89. switch (index.column()) {
  90. case Column::Device:
  91. return device.path;
  92. case Column::Major:
  93. return device.major;
  94. case Column::Minor:
  95. return device.minor;
  96. case Column::ClassName:
  97. return device.class_name;
  98. case Column::Type:
  99. switch (device.type) {
  100. case DeviceInfo::Type::Block:
  101. return "Block";
  102. case DeviceInfo::Type::Character:
  103. return "Character";
  104. default:
  105. ASSERT_NOT_REACHED();
  106. }
  107. default:
  108. ASSERT_NOT_REACHED();
  109. }
  110. }
  111. void DevicesModel::update()
  112. {
  113. auto proc_devices = CFile::construct("/proc/devices");
  114. if (!proc_devices->open(CIODevice::OpenMode::ReadOnly))
  115. ASSERT_NOT_REACHED();
  116. auto json = JsonValue::from_string(proc_devices->read_all()).as_array();
  117. m_devices.clear();
  118. json.for_each([this](auto& value) {
  119. JsonObject device = value.as_object();
  120. DeviceInfo device_info;
  121. device_info.major = device.get("major").to_uint();
  122. device_info.minor = device.get("minor").to_uint();
  123. device_info.class_name = device.get("class_name").to_string();
  124. String type_str = device.get("type").to_string();
  125. if (type_str == "block")
  126. device_info.type = DeviceInfo::Type::Block;
  127. else if (type_str == "character")
  128. device_info.type = DeviceInfo::Type::Character;
  129. else
  130. ASSERT_NOT_REACHED();
  131. m_devices.append(move(device_info));
  132. });
  133. auto fill_in_paths_from_dir = [this](const String& dir) {
  134. CDirIterator dir_iter { dir, CDirIterator::Flags::SkipDots };
  135. while (dir_iter.has_next()) {
  136. auto name = dir_iter.next_path();
  137. auto path = String::format("%s/%s", dir.characters(), name.characters());
  138. struct stat statbuf;
  139. if (lstat(path.characters(), &statbuf) != 0) {
  140. ASSERT_NOT_REACHED();
  141. }
  142. if (!S_ISBLK(statbuf.st_mode) && !S_ISCHR(statbuf.st_mode))
  143. continue;
  144. unsigned major = ::major(statbuf.st_rdev);
  145. unsigned minor = ::minor(statbuf.st_rdev);
  146. auto it = m_devices.find([major, minor](auto& device_info) {
  147. return device_info.major == major && device_info.minor == minor;
  148. });
  149. if (it != m_devices.end()) {
  150. (*it).path = move(path);
  151. }
  152. }
  153. };
  154. fill_in_paths_from_dir("/dev");
  155. fill_in_paths_from_dir("/dev/pts");
  156. did_update();
  157. }