mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 14:48:14 +00:00
truncate: Port to LibMain and use LibCore syscall wrappers :^)
This commit is contained in:
parent
3db9979e40
commit
9eb7030b55
2 changed files with 17 additions and 37 deletions
|
@ -97,6 +97,7 @@ target_link_libraries(test-crypto LibCrypto LibTLS LibLine)
|
||||||
target_link_libraries(test-fuzz LibCore LibGemini LibGfx LibHTTP LibIPC LibJS LibMarkdown LibShell)
|
target_link_libraries(test-fuzz LibCore LibGemini LibGfx LibHTTP LibIPC LibJS LibMarkdown LibShell)
|
||||||
target_link_libraries(test-imap LibIMAP)
|
target_link_libraries(test-imap LibIMAP)
|
||||||
target_link_libraries(test-pthread LibThreading)
|
target_link_libraries(test-pthread LibThreading)
|
||||||
|
target_link_libraries(truncate LibMain)
|
||||||
target_link_libraries(tt LibPthread)
|
target_link_libraries(tt LibPthread)
|
||||||
target_link_libraries(unzip LibArchive LibCompress)
|
target_link_libraries(unzip LibArchive LibCompress)
|
||||||
target_link_libraries(zip LibArchive LibCompress LibCrypto)
|
target_link_libraries(zip LibArchive LibCompress LibCrypto)
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
|
#include <LibMain/Main.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
@ -17,7 +18,7 @@ enum TruncateOperation {
|
||||||
OP_Shrink,
|
OP_Shrink,
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
const char* resize = nullptr;
|
const char* resize = nullptr;
|
||||||
const char* reference = nullptr;
|
const char* reference = nullptr;
|
||||||
|
@ -27,15 +28,15 @@ int main(int argc, char** argv)
|
||||||
args_parser.add_option(resize, "Resize the target file to (or by) this size. Prefix with + or - to expand or shrink the file, or a bare number to set the size exactly", "size", 's', "size");
|
args_parser.add_option(resize, "Resize the target file to (or by) this size. Prefix with + or - to expand or shrink the file, or a bare number to set the size exactly", "size", 's', "size");
|
||||||
args_parser.add_option(reference, "Resize the target file to match the size of this one", "reference", 'r', "file");
|
args_parser.add_option(reference, "Resize the target file to match the size of this one", "reference", 'r', "file");
|
||||||
args_parser.add_positional_argument(file, "File path", "file");
|
args_parser.add_positional_argument(file, "File path", "file");
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
if (!resize && !reference) {
|
if (!resize && !reference) {
|
||||||
args_parser.print_usage(stderr, argv[0]);
|
args_parser.print_usage(stderr, arguments.argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resize && reference) {
|
if (resize && reference) {
|
||||||
args_parser.print_usage(stderr, argv[0]);
|
args_parser.print_usage(stderr, arguments.argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,55 +59,33 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
auto size_opt = str.to_int<off_t>();
|
auto size_opt = str.to_int<off_t>();
|
||||||
if (!size_opt.has_value()) {
|
if (!size_opt.has_value()) {
|
||||||
args_parser.print_usage(stderr, argv[0]);
|
args_parser.print_usage(stderr, arguments.argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
size = size_opt.value();
|
size = size_opt.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reference) {
|
if (reference) {
|
||||||
struct stat st;
|
auto stat = TRY(Core::System::stat(reference));
|
||||||
int rc = stat(reference, &st);
|
size = stat.st_size;
|
||||||
if (rc < 0) {
|
|
||||||
perror("stat");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
size = st.st_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int fd = open(file, O_RDWR | O_CREAT, 0666);
|
auto fd = TRY(Core::System::open(file, O_RDWR | O_CREAT, 0666));
|
||||||
if (fd < 0) {
|
auto stat = TRY(Core::System::fstat(fd));
|
||||||
perror("open");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct stat st;
|
|
||||||
if (fstat(fd, &st) < 0) {
|
|
||||||
perror("fstat");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case OP_Set:
|
case OP_Set:
|
||||||
break;
|
break;
|
||||||
case OP_Grow:
|
case OP_Grow:
|
||||||
size = st.st_size + size;
|
size = stat.st_size + size;
|
||||||
break;
|
break;
|
||||||
case OP_Shrink:
|
case OP_Shrink:
|
||||||
size = st.st_size - size;
|
size = stat.st_size - size;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ftruncate(fd, size) < 0) {
|
TRY(Core::System::ftruncate(fd, size));
|
||||||
perror("ftruncate");
|
TRY(Core::System::close(fd));
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (close(fd) < 0) {
|
|
||||||
perror("close");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue