mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 08:27:35 +00:00
Userland: Use CFile inside sysctl
Also add a StringView overload to CIODevice::write
This commit is contained in:
parent
092f06a719
commit
e74b5975e4
2 changed files with 18 additions and 34 deletions
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/ByteBuffer.h>
|
#include <AK/ByteBuffer.h>
|
||||||
|
#include <AK/StringView.h>
|
||||||
#include <LibCore/CObject.h>
|
#include <LibCore/CObject.h>
|
||||||
|
|
||||||
class CIODevice : public CObject {
|
class CIODevice : public CObject {
|
||||||
|
@ -32,6 +33,7 @@ public:
|
||||||
ByteBuffer read_all();
|
ByteBuffer read_all();
|
||||||
|
|
||||||
bool write(const byte*, int size);
|
bool write(const byte*, int size);
|
||||||
|
bool write(const AK::StringView& v) { return write((const byte*)v.characters(), v.length()); }
|
||||||
|
|
||||||
// FIXME: I would like this to be const but currently it needs to call populate_read_buffer().
|
// FIXME: I would like this to be const but currently it needs to call populate_read_buffer().
|
||||||
bool can_read_line();
|
bool can_read_line();
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/CArgsParser.h>
|
#include <LibCore/CArgsParser.h>
|
||||||
#include <LibCore/CDirIterator.h>
|
#include <LibCore/CDirIterator.h>
|
||||||
|
#include <LibCore/CFile.h>
|
||||||
|
|
||||||
static String read_var(const String& name)
|
static String read_var(const String& name)
|
||||||
{
|
{
|
||||||
|
@ -16,19 +17,17 @@ static String read_var(const String& name)
|
||||||
builder.append("/proc/sys/");
|
builder.append("/proc/sys/");
|
||||||
builder.append(name);
|
builder.append(name);
|
||||||
auto path = builder.to_string();
|
auto path = builder.to_string();
|
||||||
int fd = open(path.characters(), O_RDONLY);
|
CFile f(path);
|
||||||
if (fd < 0) {
|
if (!f.open(CIODevice::ReadOnly)) {
|
||||||
perror("open");
|
fprintf(stderr, "open: %s", f.error_string());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
char buffer[1024];
|
const auto& b = f.read_all();
|
||||||
int nread = read(fd, buffer, sizeof(buffer));
|
if (f.error() < 0) {
|
||||||
close(fd);
|
fprintf(stderr, "read: %s", f.error_string());
|
||||||
if (nread < 0) {
|
|
||||||
perror("read");
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
return String(buffer, nread, Chomp);
|
return String((const char*)b.pointer(), b.size(), Chomp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_var(const String& name, const String& value)
|
static void write_var(const String& name, const String& value)
|
||||||
|
@ -37,17 +36,16 @@ static void write_var(const String& name, const String& value)
|
||||||
builder.append("/proc/sys/");
|
builder.append("/proc/sys/");
|
||||||
builder.append(name);
|
builder.append(name);
|
||||||
auto path = builder.to_string();
|
auto path = builder.to_string();
|
||||||
int fd = open(path.characters(), O_WRONLY);
|
CFile f(path);
|
||||||
if (fd < 0) {
|
if (!f.open(CIODevice::WriteOnly)) {
|
||||||
perror("open");
|
fprintf(stderr, "open: %s", f.error_string());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
int nwritten = write(fd, value.characters(), value.length());
|
f.write(value.view());
|
||||||
if (nwritten < 0) {
|
if (f.error() < 0) {
|
||||||
perror("read");
|
fprintf(stderr, "write: %s", f.error_string());
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
close(fd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -61,24 +59,8 @@ static int handle_show_all()
|
||||||
|
|
||||||
char pathbuf[PATH_MAX];
|
char pathbuf[PATH_MAX];
|
||||||
while (di.has_next()) {
|
while (di.has_next()) {
|
||||||
String name = di.next_path();
|
String variable_name = di.next_path();
|
||||||
sprintf(pathbuf, "/proc/sys/%s", name.characters());
|
printf("%s = %s\n", variable_name.characters(), read_var(variable_name).characters());
|
||||||
int fd = open(pathbuf, O_RDONLY);
|
|
||||||
if (fd < 0) {
|
|
||||||
perror("open");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
char buffer[1024];
|
|
||||||
int nread = read(fd, buffer, sizeof(buffer));
|
|
||||||
close(fd);
|
|
||||||
if (nread < 0) {
|
|
||||||
perror("read");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
buffer[nread] = '\0';
|
|
||||||
printf("%s = %s", name.characters(), buffer);
|
|
||||||
if (nread && buffer[nread - 1] != '\n')
|
|
||||||
printf("\n");
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue