2014-02-05 19:23:38 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2014-02-05 19:23:38 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SmbclientStorage.hxx"
|
2014-02-05 19:23:02 +01:00
|
|
|
#include "storage/StoragePlugin.hxx"
|
2014-02-05 19:23:38 +01:00
|
|
|
#include "storage/StorageInterface.hxx"
|
|
|
|
#include "storage/FileInfo.hxx"
|
|
|
|
#include "lib/smbclient/Init.hxx"
|
2020-07-20 17:32:40 +02:00
|
|
|
#include "lib/smbclient/Context.hxx"
|
2014-02-06 22:19:59 +01:00
|
|
|
#include "lib/smbclient/Mutex.hxx"
|
2014-10-01 23:38:17 +02:00
|
|
|
#include "fs/Traits.hxx"
|
2014-02-06 22:19:59 +01:00
|
|
|
#include "thread/Mutex.hxx"
|
2016-10-27 08:40:40 +02:00
|
|
|
#include "system/Error.hxx"
|
2018-08-02 10:38:20 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2015-11-06 09:37:07 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2016-10-27 08:40:40 +02:00
|
|
|
#include "util/ScopeExit.hxx"
|
2014-02-05 19:23:38 +01:00
|
|
|
|
|
|
|
#include <libsmbclient.h>
|
|
|
|
|
|
|
|
class SmbclientDirectoryReader final : public StorageDirectoryReader {
|
|
|
|
const std::string base;
|
|
|
|
const unsigned handle;
|
|
|
|
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SmbclientDirectoryReader(std::string &&_base, unsigned _handle)
|
|
|
|
:base(std::move(_base)), handle(_handle) {}
|
|
|
|
|
2020-02-01 04:37:53 +01:00
|
|
|
~SmbclientDirectoryReader() override;
|
2014-02-05 19:23:38 +01:00
|
|
|
|
|
|
|
/* virtual methods from class StorageDirectoryReader */
|
2017-12-18 23:54:21 +01:00
|
|
|
const char *Read() noexcept override;
|
2016-10-27 08:40:40 +02:00
|
|
|
StorageFileInfo GetInfo(bool follow) override;
|
2014-02-05 19:23:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class SmbclientStorage final : public Storage {
|
|
|
|
const std::string base;
|
|
|
|
|
2020-07-20 17:32:40 +02:00
|
|
|
SmbclientContext ctx = SmbclientContext::New();
|
2014-02-05 19:23:38 +01:00
|
|
|
|
|
|
|
public:
|
2020-07-20 17:32:40 +02:00
|
|
|
explicit SmbclientStorage(const char *_base)
|
|
|
|
:base(_base) {}
|
2014-02-05 19:23:38 +01:00
|
|
|
|
|
|
|
/* virtual methods from class Storage */
|
2020-04-03 16:22:39 +02:00
|
|
|
StorageFileInfo GetInfo(std::string_view uri_utf8, bool follow) override;
|
2014-02-05 19:23:38 +01:00
|
|
|
|
2020-04-03 16:22:39 +02:00
|
|
|
std::unique_ptr<StorageDirectoryReader> OpenDirectory(std::string_view uri_utf8) override;
|
2014-02-05 19:23:38 +01:00
|
|
|
|
2020-04-22 00:45:05 +02:00
|
|
|
[[nodiscard]] std::string MapUTF8(std::string_view uri_utf8) const noexcept override;
|
2014-02-07 19:01:06 +01:00
|
|
|
|
2020-04-22 00:45:05 +02:00
|
|
|
[[nodiscard]] std::string_view MapToRelativeUTF8(std::string_view uri_utf8) const noexcept override;
|
2014-02-05 19:23:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string
|
2020-04-03 16:22:39 +02:00
|
|
|
SmbclientStorage::MapUTF8(std::string_view uri_utf8) const noexcept
|
2014-02-05 19:23:38 +01:00
|
|
|
{
|
2020-04-03 16:22:39 +02:00
|
|
|
if (uri_utf8.empty())
|
2014-02-05 19:23:38 +01:00
|
|
|
return base;
|
|
|
|
|
2020-04-03 16:25:08 +02:00
|
|
|
return PathTraitsUTF8::Build(base, uri_utf8);
|
2014-02-05 19:23:38 +01:00
|
|
|
}
|
|
|
|
|
2020-04-03 16:22:39 +02:00
|
|
|
std::string_view
|
|
|
|
SmbclientStorage::MapToRelativeUTF8(std::string_view uri_utf8) const noexcept
|
2014-02-07 19:01:06 +01:00
|
|
|
{
|
2020-04-03 18:58:37 +02:00
|
|
|
return PathTraitsUTF8::Relative(base, uri_utf8);
|
2014-02-07 19:01:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-27 08:40:40 +02:00
|
|
|
static StorageFileInfo
|
|
|
|
GetInfo(const char *path)
|
2014-02-05 19:23:38 +01:00
|
|
|
{
|
|
|
|
struct stat st;
|
2016-10-27 08:40:40 +02:00
|
|
|
|
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(smbclient_mutex);
|
2016-10-27 08:40:40 +02:00
|
|
|
if (smbc_stat(path, &st) != 0)
|
|
|
|
throw MakeErrno("Failed to access file");
|
2014-02-05 19:23:38 +01:00
|
|
|
}
|
|
|
|
|
2016-10-27 08:40:40 +02:00
|
|
|
StorageFileInfo info;
|
2014-02-05 19:23:38 +01:00
|
|
|
if (S_ISREG(st.st_mode))
|
2015-02-28 20:50:15 +01:00
|
|
|
info.type = StorageFileInfo::Type::REGULAR;
|
2014-02-05 19:23:38 +01:00
|
|
|
else if (S_ISDIR(st.st_mode))
|
2015-02-28 20:50:15 +01:00
|
|
|
info.type = StorageFileInfo::Type::DIRECTORY;
|
2014-02-05 19:23:38 +01:00
|
|
|
else
|
2015-02-28 20:50:15 +01:00
|
|
|
info.type = StorageFileInfo::Type::OTHER;
|
2014-02-05 19:23:38 +01:00
|
|
|
|
|
|
|
info.size = st.st_size;
|
2017-02-11 22:47:05 +01:00
|
|
|
info.mtime = std::chrono::system_clock::from_time_t(st.st_mtime);
|
2014-02-05 19:23:38 +01:00
|
|
|
info.device = st.st_dev;
|
|
|
|
info.inode = st.st_ino;
|
2016-10-27 08:40:40 +02:00
|
|
|
return info;
|
2014-02-05 19:23:38 +01:00
|
|
|
}
|
|
|
|
|
2016-10-27 08:40:40 +02:00
|
|
|
StorageFileInfo
|
2020-04-03 16:22:39 +02:00
|
|
|
SmbclientStorage::GetInfo(std::string_view uri_utf8, [[maybe_unused]] bool follow)
|
2014-02-05 19:23:38 +01:00
|
|
|
{
|
|
|
|
const std::string mapped = MapUTF8(uri_utf8);
|
2016-10-27 08:40:40 +02:00
|
|
|
return ::GetInfo(mapped.c_str());
|
2014-02-05 19:23:38 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 11:04:30 +01:00
|
|
|
std::unique_ptr<StorageDirectoryReader>
|
2020-04-03 16:22:39 +02:00
|
|
|
SmbclientStorage::OpenDirectory(std::string_view uri_utf8)
|
2014-02-05 19:23:38 +01:00
|
|
|
{
|
|
|
|
std::string mapped = MapUTF8(uri_utf8);
|
2016-10-27 08:40:40 +02:00
|
|
|
|
|
|
|
int handle;
|
|
|
|
|
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(smbclient_mutex);
|
2016-10-27 08:40:40 +02:00
|
|
|
handle = smbc_opendir(mapped.c_str());
|
|
|
|
if (handle < 0)
|
|
|
|
throw MakeErrno("Failed to open directory");
|
2014-02-05 19:23:38 +01:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:47:28 +01:00
|
|
|
return std::make_unique<SmbclientDirectoryReader>(std::move(mapped),
|
2018-01-21 11:04:30 +01:00
|
|
|
handle);
|
2014-02-05 19:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static bool
|
2017-05-08 14:44:49 +02:00
|
|
|
SkipNameFS(const char *name) noexcept
|
2014-02-05 19:23:38 +01:00
|
|
|
{
|
|
|
|
return name[0] == '.' &&
|
|
|
|
(name[1] == 0 ||
|
|
|
|
(name[1] == '.' && name[2] == 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
SmbclientDirectoryReader::~SmbclientDirectoryReader()
|
|
|
|
{
|
2019-04-25 19:23:14 +02:00
|
|
|
const std::lock_guard<Mutex> lock(smbclient_mutex);
|
2014-02-05 19:23:38 +01:00
|
|
|
smbc_close(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2017-12-18 23:54:21 +01:00
|
|
|
SmbclientDirectoryReader::Read() noexcept
|
2014-02-05 19:23:38 +01:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(smbclient_mutex);
|
2014-02-06 22:19:59 +01:00
|
|
|
|
2014-02-05 19:23:38 +01:00
|
|
|
struct smbc_dirent *e;
|
|
|
|
while ((e = smbc_readdir(handle)) != nullptr) {
|
|
|
|
name = e->name;
|
|
|
|
if (!SkipNameFS(name))
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-27 08:40:40 +02:00
|
|
|
StorageFileInfo
|
2020-03-12 20:56:11 +01:00
|
|
|
SmbclientDirectoryReader::GetInfo([[maybe_unused]] bool follow)
|
2014-02-05 19:23:38 +01:00
|
|
|
{
|
2020-04-03 16:25:08 +02:00
|
|
|
const std::string path = PathTraitsUTF8::Build(base, name);
|
2016-10-27 08:40:40 +02:00
|
|
|
return ::GetInfo(path.c_str());
|
2014-02-05 19:23:38 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 16:11:17 +01:00
|
|
|
static std::unique_ptr<Storage>
|
2020-03-12 20:56:11 +01:00
|
|
|
CreateSmbclientStorageURI([[maybe_unused]] EventLoop &event_loop, const char *base)
|
2014-02-05 19:23:38 +01:00
|
|
|
{
|
2018-08-02 10:38:20 +02:00
|
|
|
if (!StringStartsWithCaseASCII(base, "smb://"))
|
2014-02-05 19:23:02 +01:00
|
|
|
return nullptr;
|
|
|
|
|
2016-09-05 11:32:20 +02:00
|
|
|
SmbclientInit();
|
2014-02-05 19:23:38 +01:00
|
|
|
|
2020-07-20 17:32:40 +02:00
|
|
|
return std::make_unique<SmbclientStorage>(base);
|
2014-02-05 19:23:38 +01:00
|
|
|
}
|
2014-02-05 19:23:02 +01:00
|
|
|
|
|
|
|
const StoragePlugin smbclient_storage_plugin = {
|
|
|
|
"smbclient",
|
|
|
|
CreateSmbclientStorageURI,
|
|
|
|
};
|