Skip to content

Commit

Permalink
Uplift of #10363 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-browser-releases authored and spylogsster committed Oct 12, 2021
1 parent ab18288 commit 938fe51
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/services/ipfs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ static_library("ipfs") {
"//brave/components/child_process_monitor",
"//brave/components/services/ipfs/public/mojom",
"//mojo/public/cpp/bindings",
"//third_party/re2",
]
}
3 changes: 3 additions & 0 deletions components/services/ipfs/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_rules = [
"+third_party/re2"
]
12 changes: 12 additions & 0 deletions components/services/ipfs/ipfs_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
#include "base/files/file_util.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/version.h"
#include "brave/components/services/ipfs/ipfs_service_utils.h"

namespace {

const char kRepoLockFile[] = "repo.lock";
const char kVersionWithAgentSuffixSupport[] = "0.10.0";

bool LaunchProcessAndExit(const base::FilePath& path,
std::initializer_list<std::string> args,
Expand Down Expand Up @@ -157,10 +159,20 @@ void IpfsServiceImpl::Launch(mojom::IpfsConfigPtr config,

// Launch IPFS daemon.
base::CommandLine args(config->binary_path);

args.AppendArg("daemon");
args.AppendArg("--migrate=true");
args.AppendArg("--enable-gc");
args.AppendArg("--routing=dhtclient");

auto version = ipfs::GetVersionFromNodeFilename(
config->binary_path.BaseName().MaybeAsASCII());

if (!version.empty() &&
base::Version(version) >= base::Version(kVersionWithAgentSuffixSupport)) {
args.AppendArg("--agent-version-suffix=brave");
}

base::Process ipfs_process = base::LaunchProcess(args, options);
bool result = ipfs_process.IsValid();

Expand Down
22 changes: 22 additions & 0 deletions components/services/ipfs/ipfs_service_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@

#include <memory>
#include <utility>
#include <vector>

#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "brave/components/services/ipfs/public/mojom/ipfs_service.mojom.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/re2/src/re2/re2.h"

namespace {

// RegEx to extract version from node name
constexpr char kExecutableRegEx[] =
"go-ipfs_v(\\d+\\.\\d+\\.\\d+)(-rc\\d+)?\\_\\w+-\\w+";

} // namespace

namespace ipfs {

Expand Down Expand Up @@ -55,4 +65,16 @@ bool UpdateConfigJSON(const std::string& source,
return true;
}

std::string GetVersionFromNodeFilename(const std::string& filename) {
static const RE2 version_pattern(kExecutableRegEx);
std::vector<re2::StringPiece> matched_groups(
version_pattern.NumberOfCapturingGroups() + 1);
if (!version_pattern.Match(filename, 0, filename.size(), RE2::ANCHOR_START,
matched_groups.data(), matched_groups.size()) ||
matched_groups.size() < 2) {
return std::string();
}
return matched_groups[1].as_string();
}

} // namespace ipfs
3 changes: 3 additions & 0 deletions components/services/ipfs/ipfs_service_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class IpfsConfig;
bool UpdateConfigJSON(const std::string& source,
const ipfs::mojom::IpfsConfig* config,
std::string* result);
// Parses version from go-ipfs node filename as it has strong format like:
// go-ipfs_v0.9.0-rc1_windows-amd64
std::string GetVersionFromNodeFilename(const std::string& filename);

} // namespace ipfs

Expand Down
41 changes: 41 additions & 0 deletions components/services/ipfs/ipfs_service_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,45 @@ TEST_F(IPFSServiceUtils, UpdateConfigJSONTest) {
EXPECT_EQ(updated, "");
}

TEST_F(IPFSServiceUtils, GetVerisonFromNodeFilename) {
EXPECT_EQ(
ipfs::GetVersionFromNodeFilename("go-ipfs_v0.9.0-rc1_windows-amd64"),
"0.9.0");
EXPECT_EQ(
ipfs::GetVersionFromNodeFilename("go-ipfs_v0.9.0-rc12_windows-amd64"),
"0.9.0");
EXPECT_EQ(ipfs::GetVersionFromNodeFilename("go-ipfs_v0.9.0_windows-amd64"),
"0.9.0");

EXPECT_EQ(
ipfs::GetVersionFromNodeFilename("go-ipfs_v0.10.0-rc1_darwin-amd64"),
"0.10.0");
EXPECT_EQ(
ipfs::GetVersionFromNodeFilename("go-ipfs_v0.10.0-rc12_darwin-amd64"),
"0.10.0");
EXPECT_EQ(ipfs::GetVersionFromNodeFilename("go-ipfs_v0.10.0_darwin-amd64"),
"0.10.0");

EXPECT_EQ(
ipfs::GetVersionFromNodeFilename("go-ipfs_v0.10.0-rc1_darwin-arm64"),
"0.10.0");
EXPECT_EQ(
ipfs::GetVersionFromNodeFilename("go-ipfs_v0.10.0-rc12_darwin-arm64"),
"0.10.0");
EXPECT_EQ(ipfs::GetVersionFromNodeFilename("go-ipfs_v0.10.0_darwin-arm64"),
"0.10.0");

EXPECT_EQ(ipfs::GetVersionFromNodeFilename("go-ipfs_v0.10.0-rc1_linux-amd64"),
"0.10.0");
EXPECT_EQ(
ipfs::GetVersionFromNodeFilename("go-ipfs_v0.10.0-rc12_linux-amd64"),
"0.10.0");
EXPECT_EQ(ipfs::GetVersionFromNodeFilename("go-ipfs_v0.10.0_linux-amd64"),
"0.10.0");

EXPECT_EQ(ipfs::GetVersionFromNodeFilename("go-ipfs_v0.10.0_linux"), "");
EXPECT_EQ(ipfs::GetVersionFromNodeFilename(""), "");
EXPECT_EQ(ipfs::GetVersionFromNodeFilename("ipfs.exe"), "");
}

} // namespace ipfs

0 comments on commit 938fe51

Please sign in to comment.