2014-02-05 19:23:38 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 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 "config.h"
|
|
|
|
#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"
|
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"
|
2015-11-06 09:37:07 +01:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/StringCompare.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) {}
|
|
|
|
|
|
|
|
virtual ~SmbclientDirectoryReader();
|
|
|
|
|
|
|
|
/* virtual methods from class StorageDirectoryReader */
|
2014-10-02 10:07:46 +02:00
|
|
|
const char *Read() override;
|
2015-02-28 20:50:15 +01:00
|
|
|
bool GetInfo(bool follow, StorageFileInfo &info,
|
|
|
|
Error &error) override;
|
2014-02-05 19:23:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class SmbclientStorage final : public Storage {
|
|
|
|
const std::string base;
|
|
|
|
|
|
|
|
SMBCCTX *const ctx;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SmbclientStorage(const char *_base, SMBCCTX *_ctx)
|
|
|
|
:base(_base), ctx(_ctx) {}
|
|
|
|
|
|
|
|
virtual ~SmbclientStorage() {
|
2014-02-06 22:19:59 +01:00
|
|
|
smbclient_mutex.lock();
|
2014-02-05 19:23:38 +01:00
|
|
|
smbc_free_context(ctx, 1);
|
2014-02-06 22:19:59 +01:00
|
|
|
smbclient_mutex.unlock();
|
2014-02-05 19:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual methods from class Storage */
|
2015-02-28 20:50:15 +01:00
|
|
|
bool GetInfo(const char *uri_utf8, bool follow, StorageFileInfo &info,
|
2014-10-02 10:07:46 +02:00
|
|
|
Error &error) override;
|
2014-02-05 19:23:38 +01:00
|
|
|
|
2014-10-02 10:07:46 +02:00
|
|
|
StorageDirectoryReader *OpenDirectory(const char *uri_utf8,
|
|
|
|
Error &error) override;
|
2014-02-05 19:23:38 +01:00
|
|
|
|
2014-10-02 10:07:46 +02:00
|
|
|
std::string MapUTF8(const char *uri_utf8) const override;
|
2014-02-07 19:01:06 +01:00
|
|
|
|
2014-10-02 10:07:46 +02:00
|
|
|
const char *MapToRelativeUTF8(const char *uri_utf8) const override;
|
2014-02-05 19:23:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string
|
|
|
|
SmbclientStorage::MapUTF8(const char *uri_utf8) const
|
|
|
|
{
|
|
|
|
assert(uri_utf8 != nullptr);
|
|
|
|
|
2015-11-06 09:37:07 +01:00
|
|
|
if (StringIsEmpty(uri_utf8))
|
2014-02-05 19:23:38 +01:00
|
|
|
return base;
|
|
|
|
|
|
|
|
return PathTraitsUTF8::Build(base.c_str(), uri_utf8);
|
|
|
|
}
|
|
|
|
|
2014-02-07 19:01:06 +01:00
|
|
|
const char *
|
|
|
|
SmbclientStorage::MapToRelativeUTF8(const char *uri_utf8) const
|
|
|
|
{
|
|
|
|
return PathTraitsUTF8::Relative(base.c_str(), uri_utf8);
|
|
|
|
}
|
|
|
|
|
2014-02-05 19:23:38 +01:00
|
|
|
static bool
|
2015-02-28 20:50:15 +01:00
|
|
|
GetInfo(const char *path, StorageFileInfo &info, Error &error)
|
2014-02-05 19:23:38 +01:00
|
|
|
{
|
|
|
|
struct stat st;
|
2014-02-06 22:19:59 +01:00
|
|
|
smbclient_mutex.lock();
|
|
|
|
bool success = smbc_stat(path, &st) == 0;
|
|
|
|
smbclient_mutex.unlock();
|
|
|
|
if (!success) {
|
2014-02-05 19:23:38 +01:00
|
|
|
error.SetErrno();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
info.mtime = st.st_mtime;
|
|
|
|
info.device = st.st_dev;
|
|
|
|
info.inode = st.st_ino;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SmbclientStorage::GetInfo(const char *uri_utf8, gcc_unused bool follow,
|
2015-02-28 20:50:15 +01:00
|
|
|
StorageFileInfo &info, Error &error)
|
2014-02-05 19:23:38 +01:00
|
|
|
{
|
|
|
|
const std::string mapped = MapUTF8(uri_utf8);
|
|
|
|
return ::GetInfo(mapped.c_str(), info, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageDirectoryReader *
|
|
|
|
SmbclientStorage::OpenDirectory(const char *uri_utf8, Error &error)
|
|
|
|
{
|
|
|
|
std::string mapped = MapUTF8(uri_utf8);
|
2014-02-06 22:19:59 +01:00
|
|
|
smbclient_mutex.lock();
|
2014-02-05 19:23:38 +01:00
|
|
|
int handle = smbc_opendir(mapped.c_str());
|
2014-02-06 22:19:59 +01:00
|
|
|
smbclient_mutex.unlock();
|
2014-02-05 19:23:38 +01:00
|
|
|
if (handle < 0) {
|
|
|
|
error.SetErrno();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SmbclientDirectoryReader(std::move(mapped.c_str()), handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
static bool
|
|
|
|
SkipNameFS(const char *name)
|
|
|
|
{
|
|
|
|
return name[0] == '.' &&
|
|
|
|
(name[1] == 0 ||
|
|
|
|
(name[1] == '.' && name[2] == 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
SmbclientDirectoryReader::~SmbclientDirectoryReader()
|
|
|
|
{
|
2014-02-06 22:19:59 +01:00
|
|
|
smbclient_mutex.lock();
|
2014-02-05 19:23:38 +01:00
|
|
|
smbc_close(handle);
|
2014-02-06 22:19:59 +01:00
|
|
|
smbclient_mutex.unlock();
|
2014-02-05 19:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
SmbclientDirectoryReader::Read()
|
|
|
|
{
|
2014-02-06 22:19:59 +01:00
|
|
|
const ScopeLock protect(smbclient_mutex);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-02-28 20:50:15 +01:00
|
|
|
SmbclientDirectoryReader::GetInfo(gcc_unused bool follow,
|
|
|
|
StorageFileInfo &info,
|
2014-02-05 19:23:38 +01:00
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
const std::string path = PathTraitsUTF8::Build(base.c_str(), name);
|
|
|
|
return ::GetInfo(path.c_str(), info, error);
|
|
|
|
}
|
|
|
|
|
2014-02-05 19:23:02 +01:00
|
|
|
static Storage *
|
2014-10-07 19:45:40 +02:00
|
|
|
CreateSmbclientStorageURI(gcc_unused EventLoop &event_loop, const char *base,
|
|
|
|
Error &error)
|
2014-02-05 19:23:38 +01:00
|
|
|
{
|
2014-02-05 19:23:02 +01:00
|
|
|
if (memcmp(base, "smb://", 6) != 0)
|
|
|
|
return nullptr;
|
|
|
|
|
2016-09-05 11:32:20 +02:00
|
|
|
SmbclientInit();
|
2014-02-05 19:23:38 +01:00
|
|
|
|
2014-02-06 22:19:59 +01:00
|
|
|
const ScopeLock protect(smbclient_mutex);
|
2014-02-05 19:23:38 +01:00
|
|
|
SMBCCTX *ctx = smbc_new_context();
|
|
|
|
if (ctx == nullptr) {
|
|
|
|
error.SetErrno("smbc_new_context() failed");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SMBCCTX *ctx2 = smbc_init_context(ctx);
|
|
|
|
if (ctx2 == nullptr) {
|
|
|
|
error.SetErrno("smbc_init_context() failed");
|
|
|
|
smbc_free_context(ctx, 1);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SmbclientStorage(base, ctx2);
|
|
|
|
}
|
2014-02-05 19:23:02 +01:00
|
|
|
|
|
|
|
const StoragePlugin smbclient_storage_plugin = {
|
|
|
|
"smbclient",
|
|
|
|
CreateSmbclientStorageURI,
|
|
|
|
};
|