aboutsummaryrefslogtreecommitdiffstats
path: root/src/input/plugins
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2014-02-06 07:29:26 +0100
committerMax Kellermann <max@duempel.org>2014-02-06 18:37:27 +0100
commite4322a716c178ffa74bd56542ea6214c4d6845bb (patch)
tree16f81d41faf5a4b5f7e72aedff5497dc3eb8ccfc /src/input/plugins
parent90886c07603854fe2b46e28890ad88222eea6933 (diff)
downloadmpd-e4322a716c178ffa74bd56542ea6214c4d6845bb.tar.gz
mpd-e4322a716c178ffa74bd56542ea6214c4d6845bb.tar.xz
mpd-e4322a716c178ffa74bd56542ea6214c4d6845bb.zip
input/nfs: new plugin using libnfs
Diffstat (limited to '')
-rw-r--r--src/input/plugins/NfsInputPlugin.cxx202
-rw-r--r--src/input/plugins/NfsInputPlugin.hxx25
2 files changed, 227 insertions, 0 deletions
diff --git a/src/input/plugins/NfsInputPlugin.cxx b/src/input/plugins/NfsInputPlugin.cxx
new file mode 100644
index 000000000..0380c8c10
--- /dev/null
+++ b/src/input/plugins/NfsInputPlugin.cxx
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2003-2014 The Music Player Daemon Project
+ * 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 "NfsInputPlugin.hxx"
+#include "../InputStream.hxx"
+#include "../InputPlugin.hxx"
+#include "util/StringUtil.hxx"
+#include "util/Error.hxx"
+#include "util/Domain.hxx"
+
+extern "C" {
+#include <nfsc/libnfs.h>
+}
+
+#include <sys/stat.h>
+#include <fcntl.h>
+
+static constexpr Domain nfs_domain("nfs");
+
+class NfsInputStream {
+ InputStream base;
+
+ nfs_context *ctx;
+ nfsfh *fh;
+
+public:
+ NfsInputStream(const char *uri,
+ Mutex &mutex, Cond &cond,
+ nfs_context *_ctx, nfsfh *_fh,
+ InputStream::offset_type size)
+ :base(input_plugin_nfs, uri, mutex, cond),
+ ctx(_ctx), fh(_fh) {
+ base.ready = true;
+ base.seekable = true;
+ base.size = size;
+ }
+
+ ~NfsInputStream() {
+ nfs_close(ctx, fh);
+ nfs_destroy_context(ctx);
+ }
+
+ InputStream *GetBase() {
+ return &base;
+ }
+
+ bool IsEOF() const {
+ return base.offset >= base.size;
+ }
+
+ size_t Read(void *ptr, size_t size, Error &error) {
+ int nbytes = nfs_read(ctx, fh, size, (char *)ptr);
+ if (nbytes < 0) {
+ error.SetErrno(-nbytes, "nfs_read() failed");
+ nbytes = 0;
+ }
+
+ return nbytes;
+ }
+
+ bool Seek(InputStream::offset_type offset, int whence, Error &error) {
+ uint64_t current_offset;
+ int result = nfs_lseek(ctx, fh, offset, whence, &current_offset);
+ if (result < 0) {
+ error.SetErrno(-result, "smbc_lseek() failed");
+ return false;
+ }
+
+ base.offset = current_offset;
+ return true;
+ }
+};
+
+/*
+ * InputPlugin methods
+ *
+ */
+
+static InputStream *
+input_nfs_open(const char *uri,
+ Mutex &mutex, Cond &cond,
+ Error &error)
+{
+ if (!StringStartsWith(uri, "nfs://"))
+ return nullptr;
+
+ uri += 6;
+
+ const char *slash = strchr(uri, '/');
+ if (slash == nullptr) {
+ error.Set(nfs_domain, "Malformed nfs:// URI");
+ return nullptr;
+ }
+
+ const std::string server(uri, slash);
+
+ uri = slash;
+ slash = strrchr(uri + 1, '/');
+ if (slash == nullptr || slash[1] == 0) {
+ error.Set(nfs_domain, "Malformed nfs:// URI");
+ return nullptr;
+ }
+
+ const std::string mount(uri, slash);
+ uri = slash;
+
+ nfs_context *ctx = nfs_init_context();
+ if (ctx == nullptr) {
+ error.Set(nfs_domain, "nfs_init_context() failed");
+ return nullptr;
+ }
+
+ int result = nfs_mount(ctx, server.c_str(), mount.c_str());
+ if (result < 0) {
+ nfs_destroy_context(ctx);
+ error.SetErrno(-result, "nfs_mount() failed");
+ return nullptr;
+ }
+
+ nfsfh *fh;
+ result = nfs_open(ctx, uri, O_RDONLY, &fh);
+ if (result < 0) {
+ nfs_destroy_context(ctx);
+ error.SetErrno(-result, "nfs_open() failed");
+ return nullptr;
+ }
+
+ struct stat st;
+ result = nfs_fstat(ctx, fh, &st);
+ if (result < 0) {
+ nfs_close(ctx, fh);
+ nfs_destroy_context(ctx);
+ error.SetErrno(-result, "nfs_fstat() failed");
+ return nullptr;
+ }
+
+ auto is = new NfsInputStream(uri, mutex, cond, ctx, fh, st.st_size);
+ return is->GetBase();
+}
+
+static size_t
+input_nfs_read(InputStream *is, void *ptr, size_t size,
+ Error &error)
+{
+ NfsInputStream &s = *(NfsInputStream *)is;
+ return s.Read(ptr, size, error);
+}
+
+static void
+input_nfs_close(InputStream *is)
+{
+ NfsInputStream *s = (NfsInputStream *)is;
+ delete s;
+}
+
+static bool
+input_nfs_eof(InputStream *is)
+{
+ NfsInputStream &s = *(NfsInputStream *)is;
+ return s.IsEOF();
+}
+
+static bool
+input_nfs_seek(InputStream *is,
+ InputPlugin::offset_type offset, int whence,
+ Error &error)
+{
+ NfsInputStream &s = *(NfsInputStream *)is;
+ return s.Seek(offset, whence, error);
+}
+
+const InputPlugin input_plugin_nfs = {
+ "nfs",
+ nullptr,
+ nullptr,
+ input_nfs_open,
+ input_nfs_close,
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ input_nfs_read,
+ input_nfs_eof,
+ input_nfs_seek,
+};
diff --git a/src/input/plugins/NfsInputPlugin.hxx b/src/input/plugins/NfsInputPlugin.hxx
new file mode 100644
index 000000000..d2cc87549
--- /dev/null
+++ b/src/input/plugins/NfsInputPlugin.hxx
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2003-2014 The Music Player Daemon Project
+ * 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.
+ */
+
+#ifndef MPD_INPUT_NFS_H
+#define MPD_INPUT_NFS_H
+
+extern const struct InputPlugin input_plugin_nfs;
+
+#endif