Skip to content

Commit 3bb53ee

Browse files
committed
Final version 1.1.0
1 parent f11b29b commit 3bb53ee

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

src/package.cpp

+26-8
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ namespace app {
4343
}
4444

4545
m_pool.queueJob([path = std::move(path), this] {
46+
if(m_shouldStop) return;
47+
4648
if(!processFile(path)) {
4749
m_promise.set_value(false);
50+
m_shouldStop = true;
4851
return;
4952
}
5053

@@ -90,11 +93,17 @@ namespace app {
9093
const auto& output = m_info.outputDir / fs::relative(dirpath, m_info.inputDir);
9194
VERBOSE("Creating directory in '{}'...", output.string());
9295
std::error_code e;
93-
fs::create_directories(output, e);
96+
if(!fs::create_directory(output, e)) {
97+
if(e) {
98+
ERROR("Failed to create directory in '{}'", output.string());
99+
return false;
100+
}
94101

95-
if(e) {
96-
ERROR("Failed to create directory in '{}'", output.string());
97-
return false;
102+
WARNING("Directory located in '{}' already exists", output.string());
103+
} else {
104+
std::unique_lock lock(m_mtx);
105+
m_cachefile.writePath(output);
106+
m_cachefile.flush();
98107
}
99108

100109
return true;
@@ -125,9 +134,9 @@ namespace app {
125134
m_cachefile{cachepath} {
126135
char buf[6];
127136
m_cachefile.read(buf, 6);
128-
if(strcmp(buf, Package::MAGIC_NUMBER) != 0) {
137+
if(strncmp(buf, Package::MAGIC_NUMBER, sizeof(buf)) != 0) {
129138
WARNING("File '{}' isn't a cache", m_cachefile.filepath().string());
130-
return;
139+
return;
131140
}
132141

133142
unsigned flags;
@@ -160,15 +169,24 @@ namespace app {
160169
}
161170

162171
bool UninstallPackage::processDirectory(const fs::path& dirpath) {
163-
// Nothing to do
172+
m_disposeDirs.push(dirpath);
164173
return true;
165174
}
166175

167176
void UninstallPackage::onEndProcess() {
168177
auto f = m_cachefile.filepath();
169178
m_cachefile.close();
170179

171-
fs::remove_all(m_info.outputDir);
180+
for(fs::path p; !m_disposeDirs.empty(); m_disposeDirs.pop()) {
181+
p = m_disposeDirs.top();
182+
VERBOSE("Deleting directory located in '{}'", p.string());
183+
if(std::error_code e; fs::remove(p, e), e) {
184+
WARNING("Failed to delete directory '{}': {}", p.string(), e.message());
185+
}
186+
}
187+
188+
if(std::error_code e; !fs::remove(m_info.outputDir, e))
189+
WARNING("Directory '{}' isn't empty", m_info.outputDir.string());
172190
fs::remove(f);
173191
}
174192
} // namespace app

src/package.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace app {
3535

3636
PackageInfo m_info;
3737
private:
38+
bool m_shouldStop{};
3839
ThreadPool m_pool;
3940
std::mutex m_mtx;
4041
std::promise<bool> m_promise;
@@ -73,5 +74,6 @@ namespace app {
7374

7475
std::queue<fs::path> m_dirs;
7576
utils::File m_cachefile;
77+
std::priority_queue<fs::path> m_disposeDirs;
7678
};
7779
} // namespace app

src/program_options.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,14 @@ namespace app {
163163

164164
std::ostream& operator<<(std::ostream& out, const ProgramOptions& po) {
165165
using namespace std::literals;
166-
utils::table<5> options(1);
166+
utils::table<4> options(1);
167167
options.setTitle("OPTIONS");
168-
options.addRow({"UNIQUE", "REQUIRED", "SHORT VERSION", "LONG VERSION", "DESCRIPTION"});
168+
options.addRow({"UNIQUE", "SHORT VERSION", "LONG VERSION", "DESCRIPTION"});
169169

170170
for(const auto& opt : po.options())
171171
options.addRow(
172172
{
173173
opt.isUniqueOption() ? "*" : "",
174-
opt.doesRequiresValue() ? "*" : "",
175174
opt.aliasChr() != EOF ? "-"s + opt.aliasChr() + (opt.doesRequiresValue() ? " <" + opt.valueName() + ">" : "") : "",
176175
"--" + opt.name() + (opt.doesRequiresValue() ? "=<" + opt.valueName() + ">" : ""),
177176
opt.description()

src/thread_pool.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ namespace app {
2323
job = m_jobs.front();
2424
m_jobs.pop();
2525
}
26-
job();
26+
try {
27+
job();
28+
} catch(...) {
29+
break;
30+
}
2731
}
2832
}
2933

0 commit comments

Comments
 (0)