mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 05:20:30 +00:00
Utilities: Add the listdir utility
This utility uses the Core::DirIterator facility which in turn uses the get_dir_entries syscall. Therefore, this utility lets us to view the actual values for inode numbers, and entry type value for directory entries.
This commit is contained in:
parent
603516e8c0
commit
fc0dbd2334
Notes:
sideshowbarker
2024-07-17 20:22:04 +09:00
Author: https://github.com/supercomputer7 Commit: https://github.com/SerenityOS/serenity/commit/fc0dbd2334 Pull-request: https://github.com/SerenityOS/serenity/pull/22598 Reviewed-by: https://github.com/ADKaster ✅
2 changed files with 109 additions and 0 deletions
43
Base/usr/share/man/man1/listdir.md
Normal file
43
Base/usr/share/man/man1/listdir.md
Normal file
|
@ -0,0 +1,43 @@
|
|||
## Name
|
||||
|
||||
lsdir - list directory entries
|
||||
|
||||
## Synopsis
|
||||
|
||||
```**sh
|
||||
# lsdir [options...] [path...]
|
||||
```
|
||||
|
||||
## Description
|
||||
|
||||
This utility will list all directory entries of a given path (or list of paths)
|
||||
and print their inode number and file type (in either POSIX DT_* format or human readable).
|
||||
|
||||
The utility uses `LibCore` `DirIterator` object and restrict its functionality
|
||||
to the `get_dir_entries` syscall only, to get the raw values of each directory
|
||||
entry.
|
||||
|
||||
## Options
|
||||
|
||||
* `-P`, `--posix-names`: Show POSIX names for file types
|
||||
* `-t`, `--total-entries-count`: Print count of listed entries when traversing a directory
|
||||
|
||||
## Arguments
|
||||
|
||||
* `path`: Directory to list
|
||||
|
||||
## Examples
|
||||
|
||||
```sh
|
||||
# List directory entries of working directory
|
||||
$ lsdir
|
||||
# List directory entries of /proc directory
|
||||
$ lsdir /proc
|
||||
# List directory entries of /proc directory with POSIX names for file types
|
||||
$ lsdir -P /proc
|
||||
# List directory entries of /proc directory and print in the end the count of traversed entries
|
||||
$ lsdir -t /proc
|
||||
```
|
||||
|
||||
## See also
|
||||
* [`ls`(1)](help://man/1/ls)
|
66
Userland/Utilities/listdir.cpp
Normal file
66
Userland/Utilities/listdir.cpp
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Liav A. <liavalb@hotmail.co.il>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/DirIterator.h>
|
||||
#include <LibCore/DirectoryEntry.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
static bool flag_show_unix_posix_file_type = false;
|
||||
static bool flag_show_total_count = false;
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
TRY(Core::System::pledge("stdio rpath"));
|
||||
|
||||
Vector<StringView> paths;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.set_general_help("List Dirent entries in a directory.");
|
||||
args_parser.add_option(flag_show_unix_posix_file_type, "Show POSIX names for file types", "posix-names", 'P');
|
||||
args_parser.add_option(flag_show_total_count, "Show total count for each directory being iterated", "total-entries-count", 't');
|
||||
args_parser.add_positional_argument(paths, "Directory to list", "path", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if (paths.is_empty())
|
||||
paths.append("."sv);
|
||||
|
||||
for (auto& path : paths) {
|
||||
Core::DirIterator di(path, Core::DirIterator::NoStat);
|
||||
if (di.has_error()) {
|
||||
auto error = di.error();
|
||||
warnln("Failed to open {} - {}", path, error);
|
||||
return error;
|
||||
}
|
||||
|
||||
outln("Traversing {}", path);
|
||||
size_t count = 0;
|
||||
|
||||
Function<StringView(Core::DirectoryEntry::Type)> name_from_directory_entry_type;
|
||||
if (flag_show_unix_posix_file_type)
|
||||
name_from_directory_entry_type = Core::DirectoryEntry::posix_name_from_directory_entry_type;
|
||||
else
|
||||
name_from_directory_entry_type = Core::DirectoryEntry::representative_name_from_directory_entry_type;
|
||||
|
||||
while (di.has_next()) {
|
||||
auto dir_entry = di.next();
|
||||
if (dir_entry.has_value()) {
|
||||
outln(" {} (Type: {}, Inode number: {})",
|
||||
dir_entry.value().name,
|
||||
name_from_directory_entry_type(dir_entry.value().type),
|
||||
dir_entry.value().inode_number);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (flag_show_total_count)
|
||||
outln("Directory {} has {} which has being listed during the program runtime", path, count);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue