Serenity Operating System
at portability 177 lines 5.6 kB view raw
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 27#include "DevicesModel.h" 28#include <AK/JsonArray.h> 29#include <AK/JsonObject.h> 30#include <AK/JsonValue.h> 31#include <LibCore/DirIterator.h> 32#include <LibCore/File.h> 33#include <sys/stat.h> 34 35NonnullRefPtr<DevicesModel> DevicesModel::create() 36{ 37 return adopt(*new DevicesModel); 38} 39 40DevicesModel::DevicesModel() 41{ 42} 43 44DevicesModel::~DevicesModel() 45{ 46} 47 48int DevicesModel::row_count(const GUI::ModelIndex&) const 49{ 50 return m_devices.size(); 51} 52 53int DevicesModel::column_count(const GUI::ModelIndex&) const 54{ 55 return Column::__Count; 56} 57 58String DevicesModel::column_name(int column) const 59{ 60 switch (column) { 61 case Column::Device: 62 return "Device"; 63 case Column::Major: 64 return "Major"; 65 case Column::Minor: 66 return "Minor"; 67 case Column::ClassName: 68 return "Class"; 69 case Column::Type: 70 return "Type"; 71 default: 72 ASSERT_NOT_REACHED(); 73 } 74} 75 76GUI::Model::ColumnMetadata DevicesModel::column_metadata(int column) const 77{ 78 switch (column) { 79 case Column::Device: 80 return { 70, Gfx::TextAlignment::CenterLeft }; 81 case Column::Major: 82 return { 32, Gfx::TextAlignment::CenterRight }; 83 case Column::Minor: 84 return { 32, Gfx::TextAlignment::CenterRight }; 85 case Column::ClassName: 86 return { 120, Gfx::TextAlignment::CenterLeft }; 87 case Column::Type: 88 return { 120, Gfx::TextAlignment::CenterLeft }; 89 default: 90 ASSERT_NOT_REACHED(); 91 } 92} 93 94GUI::Variant DevicesModel::data(const GUI::ModelIndex& index, Role) const 95{ 96 ASSERT(is_valid(index)); 97 98 const DeviceInfo& device = m_devices[index.row()]; 99 switch (index.column()) { 100 case Column::Device: 101 return device.path; 102 case Column::Major: 103 return device.major; 104 case Column::Minor: 105 return device.minor; 106 case Column::ClassName: 107 return device.class_name; 108 case Column::Type: 109 switch (device.type) { 110 case DeviceInfo::Type::Block: 111 return "Block"; 112 case DeviceInfo::Type::Character: 113 return "Character"; 114 default: 115 ASSERT_NOT_REACHED(); 116 } 117 default: 118 ASSERT_NOT_REACHED(); 119 } 120} 121 122void DevicesModel::update() 123{ 124 auto proc_devices = Core::File::construct("/proc/devices"); 125 if (!proc_devices->open(Core::IODevice::OpenMode::ReadOnly)) 126 ASSERT_NOT_REACHED(); 127 128 auto json = JsonValue::from_string(proc_devices->read_all()).as_array(); 129 130 m_devices.clear(); 131 json.for_each([this](auto& value) { 132 JsonObject device = value.as_object(); 133 DeviceInfo device_info; 134 135 device_info.major = device.get("major").to_uint(); 136 device_info.minor = device.get("minor").to_uint(); 137 device_info.class_name = device.get("class_name").to_string(); 138 139 String type_str = device.get("type").to_string(); 140 if (type_str == "block") 141 device_info.type = DeviceInfo::Type::Block; 142 else if (type_str == "character") 143 device_info.type = DeviceInfo::Type::Character; 144 else 145 ASSERT_NOT_REACHED(); 146 147 m_devices.append(move(device_info)); 148 }); 149 150 auto fill_in_paths_from_dir = [this](const String& dir) { 151 Core::DirIterator dir_iter { dir, Core::DirIterator::Flags::SkipDots }; 152 while (dir_iter.has_next()) { 153 auto name = dir_iter.next_path(); 154 auto path = String::format("%s/%s", dir.characters(), name.characters()); 155 struct stat statbuf; 156 if (lstat(path.characters(), &statbuf) != 0) { 157 ASSERT_NOT_REACHED(); 158 } 159 if (!S_ISBLK(statbuf.st_mode) && !S_ISCHR(statbuf.st_mode)) 160 continue; 161 unsigned _major = major(statbuf.st_rdev); 162 unsigned _minor = minor(statbuf.st_rdev); 163 164 auto it = m_devices.find([_major, _minor](auto& device_info) { 165 return device_info.major == _major && device_info.minor == _minor; 166 }); 167 if (it != m_devices.end()) { 168 (*it).path = move(path); 169 } 170 } 171 }; 172 173 fill_in_paths_from_dir("/dev"); 174 fill_in_paths_from_dir("/dev/pts"); 175 176 did_update(); 177}