Skip to content

Commit 8d3b03c

Browse files
Utilities: Add a new utility to hold a file descriptor open on a path
It helps checking permissions (RW or read-only) of a path and allows us to keep a file descriptor open on a path, hence keeping the responsible mount busy.
1 parent e4e1956 commit 8d3b03c

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

Userland/Utilities/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(CMD_SOURCES_CPP
22
abench.cpp
3+
access.cpp
34
aconv.cpp
45
adjtime.cpp
56
allocate.cpp
@@ -241,7 +242,7 @@ list(APPEND REQUIRED_TARGETS
241242
)
242243
list(APPEND RECOMMENDED_TARGETS
243244
aconv adjtime aplay abench asctl bt checksum chres cksum copy fortune gzip install keymap lsdev lsirq lsof lspci lzcat man mkfs.fat mknod mktemp
244-
nc netstat notify ntpquery open passwd pixelflut pls printf pro shot strings tar tt unzip wallpaper xzcat zip
245+
nc netstat notify ntpquery open access passwd pixelflut pls printf pro shot strings tar tt unzip wallpaper xzcat zip
245246
)
246247

247248
# FIXME: Support specifying component dependencies for utilities (e.g. WebSocket for telws)

Userland/Utilities/access.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
3+
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
4+
*
5+
* SPDX-License-Identifier: BSD-2-Clause
6+
*/
7+
8+
#include <LibCore/ArgsParser.h>
9+
#include <LibCore/System.h>
10+
#include <LibMain/Main.h>
11+
#include <fcntl.h>
12+
#include <stdint.h>
13+
14+
ErrorOr<int> serenity_main(Main::Arguments arguments)
15+
{
16+
StringView path;
17+
Core::ArgsParser parser;
18+
bool readonly = false;
19+
bool dont_exit_on_success = false;
20+
parser.set_general_help("Open a file in a filesystem in the intention to keep the mount busy.");
21+
parser.add_option(readonly, "Open in read-only mode", "readonly", 'r');
22+
parser.add_option(dont_exit_on_success, "Don't exit on success", "keep-access", 's');
23+
parser.add_positional_argument(path, "file path to open", "path");
24+
parser.parse(arguments);
25+
26+
int fd = -1;
27+
if (readonly)
28+
fd = TRY(Core::System::open(path, O_RDONLY));
29+
else
30+
fd = TRY(Core::System::open(path, O_RDWR));
31+
32+
(void)fd;
33+
34+
if (!dont_exit_on_success)
35+
return 0;
36+
37+
while (true) {
38+
}
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)