Skip to content
This repository was archived by the owner on Aug 2, 2020. It is now read-only.

Commit d39525b

Browse files
committed
Add lock while downloading cacert.pem to avoid conflicting
1 parent 2f7388b commit d39525b

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/cqhttp/plugins/web/websocket_reverse.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "cqhttp/core/core.h"
77
#include "cqhttp/core/helpers.h"
88
#include "cqhttp/utils/http.h"
9+
#include "cqhttp/utils/mutex.h"
910

1011
using namespace std;
1112
namespace fs = std::filesystem;
@@ -14,6 +15,7 @@ namespace cqhttp::plugins {
1415
static const auto TAG = "反向WS";
1516

1617
using utils::http::download_file;
18+
using utils::mutex::with_file_lock;
1719
using helpers::get_asset_url;
1820

1921
static string check_ws_url(const string &url) {
@@ -27,12 +29,19 @@ namespace cqhttp::plugins {
2729
auto ok = true;
2830

2931
if (!fs::exists(ansi(cacert_file))) {
30-
logging::info(
31-
TAG, u8"正在下载 CA 证书文件,这只会在第一次启动时进行,如果耗时较长,请在配置文件中更换更新源");
32-
if (download_file(get_asset_url("cacert.pem"), cacert_file, true)) {
33-
logging::info_success(TAG, u8"下载 CA 证书文件成功");
34-
} else {
35-
logging::warning(TAG, u8"下载 CA 证书文件失败,可能导致无法 WSS");
32+
try {
33+
with_file_lock(cq::dir::app("tmp") + "~cacert.pem", [&] {
34+
logging::info(
35+
TAG,
36+
u8"正在下载 CA 证书文件,这只会在第一次启动时进行,如果耗时较长,请在配置文件中更换更新源");
37+
if (download_file(get_asset_url("cacert.pem"), cacert_file, true)) {
38+
logging::info_success(TAG, u8"下载 CA 证书文件成功");
39+
} else {
40+
logging::warning(TAG, u8"下载 CA 证书文件失败,可能导致无法 WSS");
41+
ok = false;
42+
}
43+
});
44+
} catch (runtime_error &) {
3645
ok = false;
3746
}
3847
}

src/cqhttp/utils/mutex.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "./mutex.h"
2+
3+
#include <filesystem>
4+
#include <fstream>
5+
6+
using namespace std;
7+
namespace fs = std::filesystem;
8+
9+
namespace cqhttp::utils::mutex {
10+
static bool acquire(string ansi_file_path) {
11+
ofstream f(ansi_file_path);
12+
return f.is_open();
13+
}
14+
15+
void with_file_lock(string file_path, const function<void()> func) {
16+
const auto ansi_file_path = ansi(file_path);
17+
while (fs::exists(ansi_file_path)) {
18+
this_thread::sleep_for(50ms);
19+
}
20+
21+
if (acquire(ansi_file_path)) {
22+
func();
23+
fs::remove(ansi_file_path);
24+
} else {
25+
throw runtime_error("failed to acquire file lock");
26+
}
27+
}
28+
} // namespace cqhttp::utils::mutex

src/cqhttp/utils/mutex.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ namespace cqhttp::utils::mutex {
99
std::unique_lock<std::mutex> lock(m);
1010
func();
1111
}
12+
13+
void with_file_lock(std::string file_path, const std::function<void()> func);
1214
} // namespace cqhttp::utils::mutex

0 commit comments

Comments
 (0)