summaryrefslogtreecommitdiff
path: root/Userland/Utilities/unzip.cpp
blob: 3014ce503ae9d626353632f30148dd6a851b0ab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (c) 2020, Andrés Vieira <anvieiravazquez@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Assertions.h>
#include <AK/DOSPackedTime.h>
#include <AK/NumberFormat.h>
#include <AK/StringUtils.h>
#include <LibArchive/Zip.h>
#include <LibCompress/Deflate.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DeprecatedFile.h>
#include <LibCore/Directory.h>
#include <LibCore/MappedFile.h>
#include <LibCore/System.h>
#include <LibCrypto/Checksum/CRC32.h>
#include <sys/stat.h>

static ErrorOr<void> adjust_modification_time(Archive::ZipMember const& zip_member)
{
    auto time = time_from_packed_dos(zip_member.modification_date, zip_member.modification_time);
    auto seconds = static_cast<time_t>(time.to_seconds());
    struct utimbuf buf {
        .actime = seconds,
        .modtime = seconds
    };

    return Core::System::utime(zip_member.name, buf);
}

static bool unpack_zip_member(Archive::ZipMember zip_member, bool quiet)
{
    if (zip_member.is_directory) {
        if (auto maybe_error = Core::System::mkdir(zip_member.name, 0755); maybe_error.is_error()) {
            warnln("Failed to create directory '{}': {}", zip_member.name, maybe_error.error());
            return false;
        }
        if (!quiet)
            outln(" extracting: {}", zip_member.name);
        return true;
    }
    MUST(Core::Directory::create(LexicalPath(zip_member.name.to_deprecated_string()).parent(), Core::Directory::CreateDirectories::Yes));
    auto new_file = Core::DeprecatedFile::construct(zip_member.name.to_deprecated_string());
    if (!new_file->open(Core::OpenMode::WriteOnly)) {
        warnln("Can't write file {}: {}", zip_member.name, new_file->error_string());
        return false;
    }

    if (!quiet)
        outln(" extracting: {}", zip_member.name);

    Crypto::Checksum::CRC32 checksum;
    switch (zip_member.compression_method) {
    case Archive::ZipCompressionMethod::Store: {
        if (!new_file->write(zip_member.compressed_data.data(), zip_member.compressed_data.size())) {
            warnln("Can't write file contents in {}: {}", zip_member.name, new_file->error_string());
            return false;
        }
        checksum.update({ zip_member.compressed_data.data(), zip_member.compressed_data.size() });
        break;
    }
    case Archive::ZipCompressionMethod::Deflate: {
        auto decompressed_data = Compress::DeflateDecompressor::decompress_all(zip_member.compressed_data);
        if (decompressed_data.is_error()) {
            warnln("Failed decompressing file {}: {}", zip_member.name, decompressed_data.error());
            return false;
        }
        if (decompressed_data.value().size() != zip_member.uncompressed_size) {
            warnln("Failed decompressing file {}", zip_member.name);
            return false;
        }
        if (!new_file->write(decompressed_data.value().data(), decompressed_data.value().size())) {
            warnln("Can't write file contents in {}: {}", zip_member.name, new_file->error_string());
            return false;
        }
        checksum.update({ decompressed_data.value().data(), decompressed_data.value().size() });
        break;
    }
    default:
        VERIFY_NOT_REACHED();
    }

    if (adjust_modification_time(zip_member).is_error()) {
        warnln("Failed setting modification_time for file {}", zip_member.name);
        return false;
    }

    if (!new_file->close()) {
        warnln("Can't close file {}: {}", zip_member.name, new_file->error_string());
        return false;
    }

    if (checksum.digest() != zip_member.crc32) {
        warnln("Failed decompressing file {}: CRC32 mismatch", zip_member.name);
        MUST(Core::DeprecatedFile::remove(zip_member.name, Core::DeprecatedFile::RecursionMode::Disallowed));
        return false;
    }

    return true;
}

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
    StringView zip_file_path;
    bool quiet { false };
    StringView output_directory_path;
    Vector<StringView> file_filters;

    Core::ArgsParser args_parser;
    args_parser.add_option(output_directory_path, "Directory to receive the archive content", "output-directory", 'd', "path");
    args_parser.add_option(quiet, "Be less verbose", "quiet", 'q');
    args_parser.add_positional_argument(zip_file_path, "File to unzip", "path", Core::ArgsParser::Required::Yes);
    args_parser.add_positional_argument(file_filters, "Files or filters in the archive to extract", "files", Core::ArgsParser::Required::No);
    args_parser.parse(arguments);

    struct stat st = TRY(Core::System::stat(zip_file_path));

    // FIXME: Map file chunk-by-chunk once we have mmap() with offset.
    //        This will require mapping some parts then unmapping them repeatedly,
    //        but it would be significantly faster and less syscall heavy than seek()/read() at every read.
    RefPtr<Core::MappedFile> mapped_file;
    ReadonlyBytes input_bytes;
    if (st.st_size > 0) {
        mapped_file = TRY(Core::MappedFile::map(zip_file_path));
        input_bytes = mapped_file->bytes();
    }

    if (!quiet)
        warnln("Archive: {}", zip_file_path);

    auto zip_file = Archive::Zip::try_create(input_bytes);
    if (!zip_file.has_value()) {
        warnln("Invalid zip file {}", zip_file_path);
        return 1;
    }

    if (!output_directory_path.is_null()) {
        TRY(Core::Directory::create(output_directory_path, Core::Directory::CreateDirectories::Yes));
        TRY(Core::System::chdir(output_directory_path));
    }

    Vector<Archive::ZipMember> zip_directories;

    auto success = TRY(zip_file->for_each_member([&](auto zip_member) {
        bool keep_file = false;

        if (!file_filters.is_empty()) {
            for (auto& filter : file_filters) {
                // Convert underscore wildcards (usual unzip convention) to question marks (as used by StringUtils)
                auto string_filter = filter.replace("_"sv, "?"sv, ReplaceMode::All);
                if (zip_member.name.bytes_as_string_view().matches(string_filter, CaseSensitivity::CaseSensitive)) {
                    keep_file = true;
                    break;
                }
            }
        } else {
            keep_file = true;
        }

        if (keep_file) {
            if (!unpack_zip_member(zip_member, quiet))
                return IterationDecision::Break;
            if (zip_member.is_directory)
                zip_directories.append(zip_member);
        }

        return IterationDecision::Continue;
    }));

    if (!success) {
        return 1;
    }

    for (auto& directory : zip_directories) {
        if (adjust_modification_time(directory).is_error()) {
            warnln("Failed setting modification time for directory {}", directory.name);
            return 1;
        }
    }

    return success ? 0 : 1;
}