mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:47:44 +00:00
Utilities+LibArchive: Don't follow symlinks
This commit is contained in:
parent
415eb17490
commit
83e62e8c4f
3 changed files with 37 additions and 11 deletions
|
@ -165,6 +165,23 @@ void TarOutputStream::add_file(String const& path, mode_t mode, ReadonlyBytes by
|
||||||
VERIFY(m_stream.write_or_error(ReadonlyBytes { &padding, block_size - (n_written % block_size) }));
|
VERIFY(m_stream.write_or_error(ReadonlyBytes { &padding, block_size - (n_written % block_size) }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TarOutputStream::add_link(String const& path, mode_t mode, StringView link_name)
|
||||||
|
{
|
||||||
|
VERIFY(!m_finished);
|
||||||
|
TarFileHeader header {};
|
||||||
|
header.set_size(0);
|
||||||
|
header.set_filename_and_prefix(path);
|
||||||
|
header.set_type_flag(TarFileType::SymLink);
|
||||||
|
header.set_mode(mode);
|
||||||
|
header.set_magic(gnu_magic);
|
||||||
|
header.set_version(gnu_version);
|
||||||
|
header.set_link_name(link_name);
|
||||||
|
header.calculate_checksum();
|
||||||
|
VERIFY(m_stream.write_or_error(Bytes { &header, sizeof(header) }));
|
||||||
|
u8 padding[block_size] = { 0 };
|
||||||
|
VERIFY(m_stream.write_or_error(Bytes { &padding, block_size - sizeof(header) }));
|
||||||
|
}
|
||||||
|
|
||||||
void TarOutputStream::finish()
|
void TarOutputStream::finish()
|
||||||
{
|
{
|
||||||
VERIFY(!m_finished);
|
VERIFY(!m_finished);
|
||||||
|
|
|
@ -57,6 +57,7 @@ class TarOutputStream {
|
||||||
public:
|
public:
|
||||||
TarOutputStream(OutputStream&);
|
TarOutputStream(OutputStream&);
|
||||||
void add_file(String const& path, mode_t, ReadonlyBytes);
|
void add_file(String const& path, mode_t, ReadonlyBytes);
|
||||||
|
void add_link(String const& path, mode_t, StringView);
|
||||||
void add_directory(String const& path, mode_t);
|
void add_directory(String const& path, mode_t);
|
||||||
void finish();
|
void finish();
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
bool gzip = false;
|
bool gzip = false;
|
||||||
bool no_auto_compress = false;
|
bool no_auto_compress = false;
|
||||||
StringView archive_file;
|
StringView archive_file;
|
||||||
|
bool dereference;
|
||||||
StringView directory;
|
StringView directory;
|
||||||
Vector<String> paths;
|
Vector<String> paths;
|
||||||
|
|
||||||
|
@ -45,6 +46,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
args_parser.add_option(no_auto_compress, "Do not use the archive suffix to select the compression algorithm", "no-auto-compress", 0);
|
args_parser.add_option(no_auto_compress, "Do not use the archive suffix to select the compression algorithm", "no-auto-compress", 0);
|
||||||
args_parser.add_option(directory, "Directory to extract to/create from", "directory", 'C', "DIRECTORY");
|
args_parser.add_option(directory, "Directory to extract to/create from", "directory", 'C', "DIRECTORY");
|
||||||
args_parser.add_option(archive_file, "Archive file", "file", 'f', "FILE");
|
args_parser.add_option(archive_file, "Archive file", "file", 'f', "FILE");
|
||||||
|
args_parser.add_option(dereference, "Follow symlinks", "dereference", 'h');
|
||||||
args_parser.add_positional_argument(paths, "Paths", "PATHS", Core::ArgsParser::Required::No);
|
args_parser.add_positional_argument(paths, "Paths", "PATHS", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
|
@ -230,11 +232,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto statbuf_or_error = Core::System::lstat(path);
|
auto statbuf = TRY(Core::System::lstat(path));
|
||||||
if (statbuf_or_error.is_error())
|
|
||||||
return statbuf_or_error.error();
|
|
||||||
|
|
||||||
auto statbuf = statbuf_or_error.value();
|
|
||||||
auto canonicalized_path = LexicalPath::canonicalized_path(path);
|
auto canonicalized_path = LexicalPath::canonicalized_path(path);
|
||||||
tar_stream.add_file(canonicalized_path, statbuf.st_mode, file->read_all());
|
tar_stream.add_file(canonicalized_path, statbuf.st_mode, file->read_all());
|
||||||
if (verbose)
|
if (verbose)
|
||||||
|
@ -243,12 +241,20 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
return {};
|
return {};
|
||||||
};
|
};
|
||||||
|
|
||||||
auto add_directory = [&](String path, auto handle_directory) -> ErrorOr<void> {
|
auto add_link = [&](String path) -> ErrorOr<void> {
|
||||||
auto statbuf_or_error = Core::System::lstat(path);
|
auto statbuf = TRY(Core::System::lstat(path));
|
||||||
if (statbuf_or_error.is_error())
|
|
||||||
return statbuf_or_error.error();
|
auto canonicalized_path = LexicalPath::canonicalized_path(path);
|
||||||
|
tar_stream.add_link(canonicalized_path, statbuf.st_mode, TRY(Core::System::readlink(path)));
|
||||||
|
if (verbose)
|
||||||
|
outln("{}", canonicalized_path);
|
||||||
|
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
|
||||||
|
auto add_directory = [&](String path, auto handle_directory) -> ErrorOr<void> {
|
||||||
|
auto statbuf = TRY(Core::System::lstat(path));
|
||||||
|
|
||||||
auto statbuf = statbuf_or_error.value();
|
|
||||||
auto canonicalized_path = LexicalPath::canonicalized_path(path);
|
auto canonicalized_path = LexicalPath::canonicalized_path(path);
|
||||||
tar_stream.add_directory(canonicalized_path, statbuf.st_mode);
|
tar_stream.add_directory(canonicalized_path, statbuf.st_mode);
|
||||||
if (verbose)
|
if (verbose)
|
||||||
|
@ -257,7 +263,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
|
Core::DirIterator it(path, Core::DirIterator::Flags::SkipParentAndBaseDir);
|
||||||
while (it.has_next()) {
|
while (it.has_next()) {
|
||||||
auto child_path = it.next_full_path();
|
auto child_path = it.next_full_path();
|
||||||
if (!Core::File::is_directory(child_path)) {
|
if (!dereference && Core::File::is_link(child_path)) {
|
||||||
|
TRY(add_link(child_path));
|
||||||
|
} else if (!Core::File::is_directory(child_path)) {
|
||||||
TRY(add_file(child_path));
|
TRY(add_file(child_path));
|
||||||
} else {
|
} else {
|
||||||
TRY(handle_directory(child_path, handle_directory));
|
TRY(handle_directory(child_path, handle_directory));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue