Serenity Operating System
at hosted 204 lines 7.5 kB view raw
1/* 2 * Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com> 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 <AK/ByteBuffer.h> 28#include <AK/FileSystemPath.h> 29#include <AK/String.h> 30#include <AK/Vector.h> 31#include <LibCore/ArgsParser.h> 32#include <LibCore/DateTime.h> 33#include <LibCore/DirIterator.h> 34#include <LibCore/File.h> 35#include <LibCore/Object.h> 36#include <LibM/math.h> 37#include <limits.h> 38#include <stdio.h> 39#include <string.h> 40#include <sys/stat.h> 41#include <unistd.h> 42 43struct DuOption { 44 enum class TimeType { 45 NotUsed, 46 Modification, 47 Access, 48 Status 49 }; 50 51 bool all = false; 52 bool apparent_size = false; 53 int threshold = 0; 54 TimeType time_type = TimeType::NotUsed; 55 Vector<String> excluded_patterns; 56}; 57 58static int parse_args(int argc, char** argv, Vector<String>& files, DuOption& du_option, int& max_depth); 59static int print_space_usage(const String& path, const DuOption& du_option, int max_depth); 60 61int main(int argc, char** argv) 62{ 63 Vector<String> files; 64 DuOption du_option; 65 int max_depth = INT_MAX; 66 67 if (parse_args(argc, argv, files, du_option, max_depth)) 68 return 1; 69 70 for (const auto& file : files) { 71 if (print_space_usage(file, du_option, max_depth)) 72 return 1; 73 } 74 75 return 0; 76} 77 78int parse_args(int argc, char** argv, Vector<String>& files, DuOption& du_option, int& max_depth) 79{ 80 bool summarize = false; 81 const char* pattern = nullptr; 82 const char* exclude_from = nullptr; 83 Vector<const char*> files_to_process; 84 85 Core::ArgsParser::Option time_option { 86 true, 87 "Show time of type time-type of any file in the directory, or any of its subdirectories. " 88 "Available choices: mtime, modification, ctime, status, use, atime, access", 89 "time", 90 0, 91 "time-type", 92 [&du_option](const char* s) { 93 if (!strcmp(s, "mtime") || !strcmp(s, "modification")) 94 du_option.time_type = DuOption::TimeType::Modification; 95 else if (!strcmp(s, "ctime") || !strcmp(s, "status") || !strcmp(s, "use")) 96 du_option.time_type = DuOption::TimeType::Status; 97 else if (!strcmp(s, "atime") || !strcmp(s, "access")) 98 du_option.time_type = DuOption::TimeType::Access; 99 else 100 return false; 101 102 return true; 103 } 104 }; 105 106 Core::ArgsParser args_parser; 107 args_parser.add_option(du_option.all, "Write counts for all files, not just directories", "all", 'a'); 108 args_parser.add_option(du_option.apparent_size, "Print apparent sizes, rather than disk usage", "apparent-size", 0); 109 args_parser.add_option(max_depth, "Print the total for a directory or file only if it is N or fewer levels below the command line argument", "max-depth", 'd', "N"); 110 args_parser.add_option(summarize, "Display only a total for each argument", "summarize", 's'); 111 args_parser.add_option(du_option.threshold, "Exclude entries smaller than size if positive, or entries greater than size if negative", "threshold", 't', "size"); 112 args_parser.add_option(move(time_option)); 113 args_parser.add_option(pattern, "Exclude files that match pattern", "exclude", 0, "pattern"); 114 args_parser.add_option(exclude_from, "Exclude files that match any pattern in file", "exclude_from", 'X', "file"); 115 args_parser.add_positional_argument(files_to_process, "File to process", "file", Core::ArgsParser::Required::No); 116 args_parser.parse(argc, argv); 117 118 if (summarize) 119 max_depth = 0; 120 121 if (pattern) 122 du_option.excluded_patterns.append(pattern); 123 if (exclude_from) { 124 auto file = Core::File::construct(exclude_from); 125 bool success = file->open(Core::IODevice::ReadOnly); 126 ASSERT(success); 127 if (const auto buff = file->read_all()) { 128 String patterns = String::copy(buff, Chomp); 129 du_option.excluded_patterns.append(patterns.split('\n')); 130 } 131 } 132 133 for (auto* file : files_to_process) { 134 files.append(file); 135 } 136 137 if (files.is_empty()) { 138 files.append("."); 139 } 140 141 return 0; 142} 143 144int print_space_usage(const String& path, const DuOption& du_option, int max_depth) 145{ 146 struct stat path_stat; 147 if (lstat(path.characters(), &path_stat) < 0) { 148 perror("lstat"); 149 return 1; 150 } 151 152 if (--max_depth >= 0 && S_ISDIR(path_stat.st_mode)) { 153 auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir); 154 if (di.has_error()) { 155 fprintf(stderr, "DirIterator: %s\n", di.error_string()); 156 return 1; 157 } 158 while (di.has_next()) { 159 const auto child_path = di.next_full_path(); 160 if (du_option.all || Core::File::is_directory(child_path)) { 161 if (print_space_usage(child_path, du_option, max_depth)) 162 return 1; 163 } 164 } 165 } 166 167 const auto basename = FileSystemPath(path).basename(); 168 for (const auto& pattern : du_option.excluded_patterns) { 169 if (basename.matches(pattern, CaseSensitivity::CaseSensitive)) 170 return 0; 171 } 172 173 long long size = path_stat.st_size; 174 if (du_option.apparent_size) { 175 const auto block_size = 512; 176 size = path_stat.st_blocks * block_size; 177 } 178 179 if ((du_option.threshold > 0 && size < du_option.threshold) || (du_option.threshold < 0 && size > -du_option.threshold)) 180 return 0; 181 182 const long long block_size = 1024; 183 size = size / block_size + (size % block_size != 0); 184 185 if (du_option.time_type == DuOption::TimeType::NotUsed) 186 printf("%lld\t%s\n", size, path.characters()); 187 else { 188 auto time = path_stat.st_mtime; 189 switch (du_option.time_type) { 190 case DuOption::TimeType::Access: 191 time = path_stat.st_atime; 192 break; 193 case DuOption::TimeType::Status: 194 time = path_stat.st_ctime; 195 default: 196 break; 197 } 198 199 const auto formatted_time = Core::DateTime::from_timestamp(time).to_string(); 200 printf("%lld\t%s\t%s\n", size, formatted_time.characters(), path.characters()); 201 } 202 203 return 0; 204}