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
|
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdlib>
#include <filesystem>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility> // std::pair
#include <vector>
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/file_system/object_type.hpp"
#include "src/buildtool/logging/log_config.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/log_sink_cmdline.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/utils/archive/archive_ops.hpp"
#include "src/utils/cpp/tmp_dir.hpp"
namespace {
void SetupDefaultLogging() {
LogConfig::SetLogLimit(LogLevel::Progress);
LogConfig::SetSinks({LogSinkCmdLine::CreateFactory()});
}
using file_t = std::pair</*content*/ std::string, ObjectType>;
using filetree_t = std::unordered_map<std::string, file_t>;
/*
Structure of content tree:
+--root
+--bar
+--foo
+--foo_l
+--baz_l
+--baz
+--bar
+--foo
+--foo_l
foo_l is symlink "baz/foo_l" [non-upwards, pointing to file]
baz_l is symlink "baz" [non-upwards, pointing to tree]
baz/foo_l is symlink "../foo_l" [upwards & confined, pointing to symlink]
*/
auto const kExpected =
filetree_t{{"root", {"", ObjectType::Tree}},
{"root/foo", {"foo", ObjectType::File}},
{"root/bar", {"bar", ObjectType::File}},
{"root/baz", {"", ObjectType::Tree}},
{"root/baz_l", {"baz", ObjectType::Symlink}},
{"root/foo_l", {"foo", ObjectType::Symlink}},
{"root/baz/foo", {"foo", ObjectType::File}},
{"root/baz/bar", {"bar", ObjectType::File}},
{"root/baz/foo_l", {"../foo_l", ObjectType::Symlink}}};
void create_files(std::filesystem::path const& destDir = ".") {
for (auto const& [path, file] : kExpected) {
auto const& [content, type] = file;
switch (type) {
case ObjectType::File: {
if (not FileSystemManager::WriteFile(content, destDir / path)) {
Logger::Log(LogLevel::Error,
"Could not create test file at path {}",
(destDir / path).string());
std::exit(1);
};
} break;
case ObjectType::Tree: {
if (not FileSystemManager::CreateDirectory(destDir / path)) {
Logger::Log(LogLevel::Error,
"Could not create test dir at path {}",
(destDir / path).string());
std::exit(1);
};
} break;
case ObjectType::Symlink: {
if (not FileSystemManager::CreateSymlink(content,
destDir / path)) {
Logger::Log(LogLevel::Error,
"Could not create test symlink at path {}",
(destDir / path).string());
std::exit(1);
};
} break;
default: {
Logger::Log(LogLevel::Error,
"File system failure in creating test archive");
std::exit(1);
}
}
}
}
} // namespace
/// \brief This code will create a zip and a tar.gz archives to be used in
/// tests. The archives are created in a tmp directory and then posted in the
/// current directory. Caller must guarantee write rights in current directory.
auto main() -> int {
// setup logging
SetupDefaultLogging();
// make tmp dir
auto curr_path = FileSystemManager::GetCurrentDirectory();
auto tmp_dir = TmpDir::Create(curr_path);
if (not tmp_dir) {
Logger::Log(LogLevel::Error,
"Could not create tmp dir for test archives at path {}",
curr_path.string());
return 1;
}
// create the content tree
create_files(tmp_dir->GetPath());
// create the archives
// 1. move to correct directory
{
auto anchor = FileSystemManager::ChangeDirectory(tmp_dir->GetPath());
// 2. create the archives wrt to current directory
std::optional<std::string> res{std::nullopt};
res =
ArchiveOps::CreateArchive(ArchiveType::Zip, "zip_repo.zip", "root");
if (res) {
Logger::Log(LogLevel::Error,
"Creating test zip archive failed with:\n{}",
*res);
return 1;
}
res = ArchiveOps::CreateArchive(
ArchiveType::TarGz, "tgz_repo.tar.gz", "root");
if (res) {
Logger::Log(LogLevel::Error,
"Creating test tar.gz archive failed with:\n{}",
*res);
return 1;
}
} // anchor released
// 3. move archives to their correct location
if (not FileSystemManager::Rename(tmp_dir->GetPath() / "zip_repo.zip",
"zip_repo.zip")) {
Logger::Log(LogLevel::Error, "Renaming zip archive failed");
return 1;
}
if (not FileSystemManager::Rename(tmp_dir->GetPath() / "tgz_repo.tar.gz",
"tgz_repo.tar.gz")) {
Logger::Log(LogLevel::Error, "Renaming tar.gz archive failed");
return 1;
}
// Done! Tmp dir will be cleaned automatically
return 0;
}
|