Skip to content

Commit

Permalink
Convert some to string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
netheril96 committed Feb 27, 2024
1 parent 84d2332 commit c4dcaae
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 87 deletions.
24 changes: 11 additions & 13 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ jobs:
run: sudo apt-get install fuse libfuse-dev
- name: vcpkg build
id: vcpkg
uses: johnwason/vcpkg-action@v4
uses: johnwason/vcpkg-action@v6
with:
manifest-dir: ${{ github.workspace }}
triplet: x64-linux
token: ${{ github.token }}
github-binarycache: true
- name: build-test
env:
VCPKG_DEFAULT_BINARY_CACHE: ${{ github.workspace }}/vcpkg_cache
run: python3 build.py --enable_unit_test --enable_integration_test --vcpkg_root=${{ github.workspace }}/vcpkg --build_root=./build
- name: upload
uses: actions/upload-artifact@v1
Expand All @@ -46,7 +45,7 @@ jobs:
with:
python-version: "3.x"
- name: install
run: sudo apt-get install fuse libfuse-dev libcrypto++-dev libargon2-dev libtclap-dev libabsl-dev libutf8proc-dev libjsoncpp-dev doctest-dev
run: sudo apt-get install fuse libfuse-dev libcrypto++-dev libargon2-dev libtclap-dev libabsl-dev libutf8proc-dev libjsoncpp-dev doctest-dev libsqlite3-dev
- name: configure
run: mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DSECUREFS_USE_VCPKG=OFF ..
- name: build
Expand All @@ -69,15 +68,15 @@ jobs:
- name: install
run: brew install macfuse
- name: vcpkg build
uses: johnwason/vcpkg-action@v4
uses: johnwason/vcpkg-action@v6
id: vcpkg
with:
manifest-dir: ${{ github.workspace }}
triplet: x64-osx
token: ${{ github.token }}
cache-key: macos-11-x64
github-binarycache: true
- name: build
env:
VCPKG_DEFAULT_BINARY_CACHE: ${{ github.workspace }}/vcpkg_cache
run: python3 build.py --enable_unit_test --vcpkg_root=${{ github.workspace }}/vcpkg --triplet=x64-osx --build_root=./build
- name: upload
uses: actions/upload-artifact@v1
Expand All @@ -100,15 +99,15 @@ jobs:
- name: install
run: brew install macfuse
- name: vcpkg build
uses: johnwason/vcpkg-action@v4
uses: johnwason/vcpkg-action@v6
id: vcpkg
with:
manifest-dir: ${{ github.workspace }}
triplet: arm64-osx
token: ${{ github.token }}
cache-key: macos-11-arm64
github-binarycache: true
- name: build
env:
VCPKG_DEFAULT_BINARY_CACHE: ${{ github.workspace }}/vcpkg_cache
run: python3 build.py --vcpkg_root=${{ github.workspace }}/vcpkg --triplet=arm64-osx --build_root=./build --cmake_defines CMAKE_OSX_ARCHITECTURES=arm64
- name: upload
uses: actions/upload-artifact@v1
Expand All @@ -129,16 +128,15 @@ jobs:
- name: install
run: choco install winfsp
- name: vcpkg build
uses: johnwason/vcpkg-action@v4
uses: johnwason/vcpkg-action@v6
id: vcpkg
with:
manifest-dir: ${{ github.workspace }}
triplet: x64-windows-static-md
token: ${{ github.token }}
cache-key: windows
github-binarycache: true
- name: build-test
env:
VCPKG_DEFAULT_BINARY_CACHE: ${{ github.workspace }}/vcpkg_cache
run: python3 build.py --enable_unit_test --enable_integration_test --vcpkg_root=${{ github.workspace }}/vcpkg --triplet=x64-windows-static-md --build_root=./build
- name: upload
uses: actions/upload-artifact@v1
Expand Down
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,14 @@ find_path(TCLAP_INCLUDE_DIRS "tclap/Arg.h")
target_include_directories(securefs-static PUBLIC ${TCLAP_INCLUDE_DIRS})
find_package(absl CONFIG REQUIRED)
target_link_libraries(
securefs-static PUBLIC absl::base absl::inlined_vector absl::flat_hash_map
absl::strings absl::optional)
securefs-static
PUBLIC absl::base
absl::inlined_vector
absl::flat_hash_map
absl::strings
absl::optional
absl::str_format
absl::span)

if(SECUREFS_USE_VCPKG)
find_package(cryptopp CONFIG REQUIRED)
Expand Down
3 changes: 2 additions & 1 deletion sources/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "platform.h"
#include "win_get_proc.h"

#include <absl/strings/str_format.h>
#include <absl/types/optional.h>
#include <argon2.h>
#include <cryptopp/cpu.h>
Expand Down Expand Up @@ -1400,7 +1401,7 @@ class MountCommand : public _SinglePasswordCommandBase
auto rc = fsopt.root->listxattr(".", nullptr, 0);
if (rc < 0)
{
absl::FPrintf(stderr,
absl::FPrintF(stderr,
"Warning: %s has no extended attribute support.\nXattr is disabled\n",
data_dir.getValue());
native_xattr = false;
Expand Down
2 changes: 1 addition & 1 deletion sources/common_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const OSService& OSService::get_default()
return service;
}

std::string OSService::temp_name(const std::string& prefix, const std::string& suffix)
std::string OSService::temp_name(absl::string_view prefix, absl::string_view suffix)
{
byte random[16];
generate_random(random, array_length(random));
Expand Down
8 changes: 4 additions & 4 deletions sources/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ class OSService
std::string m_dir_name;

public:
static bool is_absolute(const std::string& path);
static native_string_type concat_and_norm(const std::string& base_dir, const std::string& path);
native_string_type norm_path(const std::string& path) const
static bool is_absolute(absl::string_view path);
static native_string_type concat_and_norm(absl::string_view base_dir, absl::string_view path);
native_string_type norm_path(absl::string_view path) const
{
return concat_and_norm(m_dir_name, path);
}
Expand Down Expand Up @@ -220,7 +220,7 @@ class OSService
static uint32_t getgid() noexcept;
static int64_t raise_fd_limit() noexcept;

static std::string temp_name(const std::string& prefix, const std::string& suffix);
static std::string temp_name(absl::string_view prefix, absl::string_view suffix);
static const OSService& get_default();
static void get_current_time(fuse_timespec& out);
static void get_current_time_in_tm(struct tm* tm, int* nanoseconds);
Expand Down
16 changes: 9 additions & 7 deletions sources/unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "platform.h"
#include "streams.h"

#include <absl/strings/match.h>
#include <absl/strings/str_cat.h>
#include <absl/strings/str_format.h>

#include <algorithm>
Expand Down Expand Up @@ -242,21 +244,21 @@ class UnixDirectoryTraverser : public DirectoryTraverser
void rewind() override { ::rewinddir(m_dir); }
};

bool OSService::is_absolute(const std::string& path) { return path.size() > 0 && path[0] == '/'; }
bool OSService::is_absolute(absl::string_view path) { return path.size() > 0 && path[0] == '/'; }

native_string_type OSService::concat_and_norm(const std::string& base_dir, const std::string& path)
native_string_type OSService::concat_and_norm(absl::string_view base_dir, absl::string_view path)
{
if (base_dir.empty() || is_absolute(path))
return path.to_string();
return {path.data(), path.size()};
if (!is_absolute(base_dir))
{
throwInvalidArgumentException("base_dir must be absolute, but is " + base_dir);
throwInvalidArgumentException(absl::StrCat("base_dir must be absolute, but is ", base_dir));
}
if (base_dir.ends_with("/"))
if (base_dir.size() > 0 && base_dir.back() == '/')
{
return base_dir + path;
return absl::StrCat(base_dir, path);
}
return base_dir + "/" + path;
return absl::StrCat(base_dir, "/", path);
}

OSService::OSService() { m_dir_fd = AT_FDCWD; }
Expand Down
104 changes: 45 additions & 59 deletions sources/win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,81 +712,67 @@ OSService::OSService() : m_root_handle(INVALID_HANDLE_VALUE) {}

OSService::~OSService() { CloseHandle(m_root_handle); }

static bool is_absolute_path(absl::string_view path)
bool OSService::is_absolute(absl::string_view path)
{
return path.empty() || path[0] == '/' || path[0] == '\\'
|| (path.size() >= 2 && path[1] == ':');
}

bool OSService::is_absolute(const std::string& path)
native_string_type OSService::concat_and_norm(absl::string_view base_dir, absl::string_view path)
{
return is_absolute_path(absl::string_view(path.data(), path.size()));
}

namespace
{
native_string_type concat_and_norm_path(absl::string_view base_dir, absl::string_view path)
if (base_dir.empty() || is_absolute(path))
{
if (base_dir.empty() || is_absolute_path(path))
{
return widen_string(path);
}
if (!is_absolute_path(base_dir))
{
throwInvalidArgumentException(
absl::StrCat("base_dir must be an absolute path, yet we received ", base_dir));
}
std::vector<char> buffer;
buffer.reserve(2 * (base_dir.size() + path.size()) + 15);
buffer.insert(buffer.end(), base_dir.begin(), base_dir.end());
buffer.push_back('\\');
buffer.insert(buffer.end(), path.begin(), path.end());

absl::InlinedVector<absl::string_view, 32> pieces;
for (absl::string_view p :
absl::StrSplit(absl::string_view(buffer.data(), buffer.size()),
absl::ByAnyChar("/\\"),
[](absl::string_view p) { return !p.empty() && p != "."; }))
return widen_string(path);
}
if (!is_absolute(base_dir))
{
throwInvalidArgumentException(
absl::StrCat("base_dir must be an absolute path, yet we received ", base_dir));
}
std::vector<char> buffer;
buffer.reserve(2 * (base_dir.size() + path.size()) + 15);
buffer.insert(buffer.end(), base_dir.begin(), base_dir.end());
buffer.push_back('\\');
buffer.insert(buffer.end(), path.begin(), path.end());

absl::InlinedVector<absl::string_view, 32> pieces;
for (absl::string_view p :
absl::StrSplit(absl::string_view(buffer.data(), buffer.size()),
absl::ByAnyChar("/\\"),
[](absl::string_view p) { return !p.empty() && p != "."; }))
{
if (p == "..")
{
if (p == "..")
if (!pieces.empty())
{
if (!pieces.empty())
{
pieces.pop_back();
}
}
else
{
pieces.push_back(p);
pieces.pop_back();
}
}
size_t offset = buffer.size();

static constexpr absl::string_view LONG_PATH_PREFIX = R"(\\)";

if (!absl::StartsWith(base_dir, LONG_PATH_PREFIX))
{
buffer.push_back('\\');
buffer.push_back('\\');
buffer.push_back('?');
}
else
{
buffer.push_back('\\');
pieces.push_back(p);
}
for (absl::string_view p : pieces)
{
buffer.push_back('\\');
buffer.insert(buffer.end(), p.begin(), p.end());
}
return widen_string(absl::string_view(buffer.data() + offset, buffer.size() - offset));
}
} // namespace
size_t offset = buffer.size();

native_string_type OSService::concat_and_norm(const std::string& base_dir, const std::string& path)
{
return concat_and_norm_path(absl::string_view(base_dir.data(), base_dir.size()),
absl::string_view(path.data(), path.size()));
static constexpr absl::string_view LONG_PATH_PREFIX = R"(\\)";

if (!absl::StartsWith(base_dir, LONG_PATH_PREFIX))
{
buffer.push_back('\\');
buffer.push_back('\\');
buffer.push_back('?');
}
else
{
buffer.push_back('\\');
}
for (absl::string_view p : pieces)
{
buffer.push_back('\\');
buffer.insert(buffer.end(), p.begin(), p.end());
}
return widen_string(absl::string_view(buffer.data() + offset, buffer.size() - offset));
}

OSService::OSService(const std::string& path)
Expand Down

0 comments on commit c4dcaae

Please sign in to comment.