lib/nfs/Base: use std::string_view

This commit is contained in:
Max Kellermann 2024-05-06 15:21:24 +02:00
parent b6314b4c4b
commit bcc39be784
3 changed files with 12 additions and 18 deletions

View File

@ -3,6 +3,7 @@
#include "Base.hxx" #include "Base.hxx"
#include <algorithm> // for std::copy()
#include <array> #include <array>
#include <cassert> #include <cassert>
@ -13,21 +14,17 @@ static std::array<char, 256> nfs_base_export_name;
static size_t nfs_base_export_name_length; static size_t nfs_base_export_name_length;
void void
nfs_set_base(const char *server, const char *export_name) noexcept nfs_set_base(std::string_view server, std::string_view export_name) noexcept
{ {
assert(server != nullptr); if (server.size() >= nfs_base_server.size() ||
assert(export_name != nullptr); export_name.size() > nfs_base_export_name.size())
const size_t server_length = strlen(server);
const size_t export_name_length = strlen(export_name);
if (server_length >= nfs_base_server.size() ||
export_name_length > nfs_base_export_name.size())
return; return;
memcpy(nfs_base_server.data(), server, server_length + 1); *std::copy(server.begin(), server.end(),
memcpy(nfs_base_export_name.data(), export_name, export_name_length); nfs_base_server.begin()) = '\0';
nfs_base_export_name_length = export_name_length; std::copy(export_name.begin(), export_name.end(),
nfs_base_export_name.begin());
nfs_base_export_name_length = export_name.size();
} }
const char * const char *

View File

@ -1,8 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project // Copyright The Music Player Daemon Project
#ifndef MPD_NFS_BASE_HXX #include <string_view>
#define MPD_NFS_BASE_HXX
/** /**
* Set the "base" NFS server and export name. This will be the * Set the "base" NFS server and export name. This will be the
@ -12,7 +11,7 @@
* This is a kludge that is not truly thread-safe. * This is a kludge that is not truly thread-safe.
*/ */
void void
nfs_set_base(const char *server, const char *export_name) noexcept; nfs_set_base(std::string_view server, std::string_view export_name) noexcept;
/** /**
* Check if the given server and path are inside the "base" * Check if the given server and path are inside the "base"
@ -23,5 +22,3 @@ nfs_set_base(const char *server, const char *export_name) noexcept;
[[gnu::pure]] [[gnu::pure]]
const char * const char *
nfs_check_base(const char *server, const char *path) noexcept; nfs_check_base(const char *server, const char *path) noexcept;
#endif

View File

@ -403,7 +403,7 @@ CreateNfsStorageURI(EventLoop &event_loop, const char *base)
const std::string server(p, mount); const std::string server(p, mount);
nfs_set_base(server.c_str(), mount); nfs_set_base(server, mount);
return std::make_unique<NfsStorage>(event_loop, base, return std::make_unique<NfsStorage>(event_loop, base,
server.c_str(), mount); server.c_str(), mount);