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 <algorithm> // for std::copy()
#include <array>
#include <cassert>
@ -13,21 +14,17 @@ static std::array<char, 256> nfs_base_export_name;
static size_t nfs_base_export_name_length;
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);
assert(export_name != nullptr);
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())
if (server.size() >= nfs_base_server.size() ||
export_name.size() > nfs_base_export_name.size())
return;
memcpy(nfs_base_server.data(), server, server_length + 1);
memcpy(nfs_base_export_name.data(), export_name, export_name_length);
nfs_base_export_name_length = export_name_length;
*std::copy(server.begin(), server.end(),
nfs_base_server.begin()) = '\0';
std::copy(export_name.begin(), export_name.end(),
nfs_base_export_name.begin());
nfs_base_export_name_length = export_name.size();
}
const char *

View File

@ -1,8 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
#ifndef MPD_NFS_BASE_HXX
#define MPD_NFS_BASE_HXX
#include <string_view>
/**
* 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.
*/
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"
@ -23,5 +22,3 @@ nfs_set_base(const char *server, const char *export_name) noexcept;
[[gnu::pure]]
const char *
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);
nfs_set_base(server.c_str(), mount);
nfs_set_base(server, mount);
return std::make_unique<NfsStorage>(event_loop, base,
server.c_str(), mount);