Database*: move to db/
This commit is contained in:
23
src/db/update/InotifyDomain.cxx
Normal file
23
src/db/update/InotifyDomain.cxx
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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 "InotifyDomain.hxx"
|
||||
#include "util/Domain.hxx"
|
||||
|
||||
const Domain inotify_domain("inotify");
|
||||
25
src/db/update/InotifyDomain.hxx
Normal file
25
src/db/update/InotifyDomain.hxx
Normal file
@@ -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_INOTIFY_DOMAIN_HXX
|
||||
#define MPD_INOTIFY_DOMAIN_HXX
|
||||
|
||||
extern const class Domain inotify_domain;
|
||||
|
||||
#endif
|
||||
89
src/db/update/InotifyQueue.cxx
Normal file
89
src/db/update/InotifyQueue.cxx
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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 "InotifyQueue.hxx"
|
||||
#include "InotifyDomain.hxx"
|
||||
#include "UpdateGlue.hxx"
|
||||
#include "Log.hxx"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Wait this long after the last change before calling
|
||||
* update_enqueue(). This increases the probability that updates can
|
||||
* be bundled.
|
||||
*/
|
||||
static constexpr unsigned INOTIFY_UPDATE_DELAY_S = 5;
|
||||
|
||||
void
|
||||
InotifyQueue::OnTimeout()
|
||||
{
|
||||
unsigned id;
|
||||
|
||||
while (!queue.empty()) {
|
||||
const char *uri_utf8 = queue.front().c_str();
|
||||
|
||||
id = update_enqueue(uri_utf8, false);
|
||||
if (id == 0) {
|
||||
/* retry later */
|
||||
ScheduleSeconds(INOTIFY_UPDATE_DELAY_S);
|
||||
return;
|
||||
}
|
||||
|
||||
FormatDebug(inotify_domain, "updating '%s' job=%u",
|
||||
uri_utf8, id);
|
||||
|
||||
queue.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
path_in(const char *path, const char *possible_parent)
|
||||
{
|
||||
size_t length = strlen(possible_parent);
|
||||
|
||||
return path[0] == 0 ||
|
||||
(memcmp(possible_parent, path, length) == 0 &&
|
||||
(path[length] == 0 || path[length] == '/'));
|
||||
}
|
||||
|
||||
void
|
||||
InotifyQueue::Enqueue(const char *uri_utf8)
|
||||
{
|
||||
ScheduleSeconds(INOTIFY_UPDATE_DELAY_S);
|
||||
|
||||
for (auto i = queue.begin(), end = queue.end(); i != end;) {
|
||||
const char *current_uri = i->c_str();
|
||||
|
||||
if (path_in(uri_utf8, current_uri))
|
||||
/* already enqueued */
|
||||
return;
|
||||
|
||||
if (path_in(current_uri, uri_utf8))
|
||||
/* existing path is a sub-path of the new
|
||||
path; we can dequeue the existing path and
|
||||
update the new path instead */
|
||||
i = queue.erase(i);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
|
||||
queue.emplace_back(uri_utf8);
|
||||
}
|
||||
41
src/db/update/InotifyQueue.hxx
Normal file
41
src/db/update/InotifyQueue.hxx
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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_INOTIFY_QUEUE_HXX
|
||||
#define MPD_INOTIFY_QUEUE_HXX
|
||||
|
||||
#include "event/TimeoutMonitor.hxx"
|
||||
#include "Compiler.h"
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
class InotifyQueue final : private TimeoutMonitor {
|
||||
std::list<std::string> queue;
|
||||
|
||||
public:
|
||||
InotifyQueue(EventLoop &_loop):TimeoutMonitor(_loop) {}
|
||||
|
||||
void Enqueue(const char *uri_utf8);
|
||||
|
||||
private:
|
||||
virtual void OnTimeout() override;
|
||||
};
|
||||
|
||||
#endif
|
||||
114
src/db/update/InotifySource.cxx
Normal file
114
src/db/update/InotifySource.cxx
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* 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 "InotifySource.hxx"
|
||||
#include "InotifyDomain.hxx"
|
||||
#include "util/Error.hxx"
|
||||
#include "system/fd_util.h"
|
||||
#include "system/FatalError.hxx"
|
||||
#include "Log.hxx"
|
||||
|
||||
#include <sys/inotify.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
bool
|
||||
InotifySource::OnSocketReady(gcc_unused unsigned flags)
|
||||
{
|
||||
const auto dest = buffer.Write();
|
||||
if (dest.IsEmpty())
|
||||
FatalError("buffer full");
|
||||
|
||||
ssize_t nbytes = read(Get(), dest.data, dest.size);
|
||||
if (nbytes < 0)
|
||||
FatalSystemError("Failed to read from inotify");
|
||||
if (nbytes == 0)
|
||||
FatalError("end of file from inotify");
|
||||
|
||||
buffer.Append(nbytes);
|
||||
|
||||
while (true) {
|
||||
const char *name;
|
||||
|
||||
auto range = buffer.Read();
|
||||
const struct inotify_event *event =
|
||||
(const struct inotify_event *)
|
||||
range.data;
|
||||
if (range.size < sizeof(*event) ||
|
||||
range.size < sizeof(*event) + event->len)
|
||||
break;
|
||||
|
||||
if (event->len > 0 && event->name[event->len - 1] == 0)
|
||||
name = event->name;
|
||||
else
|
||||
name = nullptr;
|
||||
|
||||
callback(event->wd, event->mask, name, callback_ctx);
|
||||
buffer.Consume(sizeof(*event) + event->len);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline
|
||||
InotifySource::InotifySource(EventLoop &_loop,
|
||||
mpd_inotify_callback_t _callback, void *_ctx,
|
||||
int _fd)
|
||||
:SocketMonitor(_fd, _loop),
|
||||
callback(_callback), callback_ctx(_ctx)
|
||||
{
|
||||
ScheduleRead();
|
||||
|
||||
}
|
||||
|
||||
InotifySource *
|
||||
InotifySource::Create(EventLoop &loop,
|
||||
mpd_inotify_callback_t callback, void *callback_ctx,
|
||||
Error &error)
|
||||
{
|
||||
int fd = inotify_init_cloexec();
|
||||
if (fd < 0) {
|
||||
error.SetErrno("inotify_init() has failed");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return new InotifySource(loop, callback, callback_ctx, fd);
|
||||
}
|
||||
|
||||
int
|
||||
InotifySource::Add(const char *path_fs, unsigned mask, Error &error)
|
||||
{
|
||||
int wd = inotify_add_watch(Get(), path_fs, mask);
|
||||
if (wd < 0)
|
||||
error.SetErrno("inotify_add_watch() has failed");
|
||||
|
||||
return wd;
|
||||
}
|
||||
|
||||
void
|
||||
InotifySource::Remove(unsigned wd)
|
||||
{
|
||||
int ret = inotify_rm_watch(Get(), wd);
|
||||
if (ret < 0 && errno != EINVAL)
|
||||
LogErrno(inotify_domain, "inotify_rm_watch() has failed");
|
||||
|
||||
/* EINVAL may happen here when the file has been deleted; the
|
||||
kernel seems to auto-unregister deleted files */
|
||||
}
|
||||
74
src/db/update/InotifySource.hxx
Normal file
74
src/db/update/InotifySource.hxx
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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_INOTIFY_SOURCE_HXX
|
||||
#define MPD_INOTIFY_SOURCE_HXX
|
||||
|
||||
#include "event/SocketMonitor.hxx"
|
||||
#include "util/FifoBuffer.hxx"
|
||||
|
||||
class Error;
|
||||
|
||||
typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask,
|
||||
const char *name, void *ctx);
|
||||
|
||||
class InotifySource final : private SocketMonitor {
|
||||
mpd_inotify_callback_t callback;
|
||||
void *callback_ctx;
|
||||
|
||||
FifoBuffer<uint8_t, 4096> buffer;
|
||||
|
||||
InotifySource(EventLoop &_loop,
|
||||
mpd_inotify_callback_t callback, void *ctx, int fd);
|
||||
|
||||
public:
|
||||
~InotifySource() {
|
||||
Close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new inotify source and registers it in the GLib main
|
||||
* loop.
|
||||
*
|
||||
* @param a callback invoked for events received from the kernel
|
||||
*/
|
||||
static InotifySource *Create(EventLoop &_loop,
|
||||
mpd_inotify_callback_t callback,
|
||||
void *ctx,
|
||||
Error &error);
|
||||
|
||||
/**
|
||||
* Adds a path to the notify list.
|
||||
*
|
||||
* @return a watch descriptor or -1 on error
|
||||
*/
|
||||
int Add(const char *path_fs, unsigned mask, Error &error);
|
||||
|
||||
/**
|
||||
* Removes a path from the notify list.
|
||||
*
|
||||
* @param wd the watch descriptor returned by mpd_inotify_source_add()
|
||||
*/
|
||||
void Remove(unsigned wd);
|
||||
|
||||
private:
|
||||
virtual bool OnSocketReady(unsigned flags) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
339
src/db/update/InotifyUpdate.cxx
Normal file
339
src/db/update/InotifyUpdate.cxx
Normal file
@@ -0,0 +1,339 @@
|
||||
/*
|
||||
* 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" /* must be first for large file support */
|
||||
#include "InotifyUpdate.hxx"
|
||||
#include "InotifySource.hxx"
|
||||
#include "InotifyQueue.hxx"
|
||||
#include "InotifyDomain.hxx"
|
||||
#include "Mapper.hxx"
|
||||
#include "Main.hxx"
|
||||
#include "fs/AllocatedPath.hxx"
|
||||
#include "fs/FileSystem.hxx"
|
||||
#include "util/Error.hxx"
|
||||
#include "Log.hxx"
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <forward_list>
|
||||
|
||||
#include <assert.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
|
||||
static constexpr unsigned IN_MASK =
|
||||
#ifdef IN_ONLYDIR
|
||||
IN_ONLYDIR|
|
||||
#endif
|
||||
IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF
|
||||
|IN_MOVE|IN_MOVE_SELF;
|
||||
|
||||
struct WatchDirectory {
|
||||
WatchDirectory *parent;
|
||||
|
||||
AllocatedPath name;
|
||||
|
||||
int descriptor;
|
||||
|
||||
std::forward_list<WatchDirectory> children;
|
||||
|
||||
template<typename N>
|
||||
WatchDirectory(WatchDirectory *_parent, N &&_name,
|
||||
int _descriptor)
|
||||
:parent(_parent), name(std::forward<N>(_name)),
|
||||
descriptor(_descriptor) {}
|
||||
|
||||
WatchDirectory(const WatchDirectory &) = delete;
|
||||
WatchDirectory &operator=(const WatchDirectory &) = delete;
|
||||
};
|
||||
|
||||
static InotifySource *inotify_source;
|
||||
static InotifyQueue *inotify_queue;
|
||||
|
||||
static unsigned inotify_max_depth;
|
||||
static WatchDirectory *inotify_root;
|
||||
static std::map<int, WatchDirectory *> inotify_directories;
|
||||
|
||||
static void
|
||||
tree_add_watch_directory(WatchDirectory *directory)
|
||||
{
|
||||
inotify_directories.insert(std::make_pair(directory->descriptor,
|
||||
directory));
|
||||
}
|
||||
|
||||
static void
|
||||
tree_remove_watch_directory(WatchDirectory *directory)
|
||||
{
|
||||
auto i = inotify_directories.find(directory->descriptor);
|
||||
assert(i != inotify_directories.end());
|
||||
inotify_directories.erase(i);
|
||||
}
|
||||
|
||||
static WatchDirectory *
|
||||
tree_find_watch_directory(int wd)
|
||||
{
|
||||
auto i = inotify_directories.find(wd);
|
||||
if (i == inotify_directories.end())
|
||||
return nullptr;
|
||||
|
||||
return i->second;
|
||||
}
|
||||
|
||||
static void
|
||||
disable_watch_directory(WatchDirectory &directory)
|
||||
{
|
||||
tree_remove_watch_directory(&directory);
|
||||
|
||||
for (WatchDirectory &child : directory.children)
|
||||
disable_watch_directory(child);
|
||||
|
||||
inotify_source->Remove(directory.descriptor);
|
||||
}
|
||||
|
||||
static void
|
||||
remove_watch_directory(WatchDirectory *directory)
|
||||
{
|
||||
assert(directory != nullptr);
|
||||
|
||||
if (directory->parent == nullptr) {
|
||||
LogWarning(inotify_domain,
|
||||
"music directory was removed - "
|
||||
"cannot continue to watch it");
|
||||
return;
|
||||
}
|
||||
|
||||
disable_watch_directory(*directory);
|
||||
|
||||
/* remove it from the parent, which effectively deletes it */
|
||||
directory->parent->children.remove_if([directory](const WatchDirectory &child){
|
||||
return &child == directory;
|
||||
});
|
||||
}
|
||||
|
||||
static AllocatedPath
|
||||
watch_directory_get_uri_fs(const WatchDirectory *directory)
|
||||
{
|
||||
if (directory->parent == nullptr)
|
||||
return AllocatedPath::Null();
|
||||
|
||||
const auto uri = watch_directory_get_uri_fs(directory->parent);
|
||||
if (uri.IsNull())
|
||||
return directory->name;
|
||||
|
||||
return AllocatedPath::Build(uri, directory->name);
|
||||
}
|
||||
|
||||
/* we don't look at "." / ".." nor files with newlines in their name */
|
||||
static bool skip_path(const char *path)
|
||||
{
|
||||
return (path[0] == '.' && path[1] == 0) ||
|
||||
(path[0] == '.' && path[1] == '.' && path[2] == 0) ||
|
||||
strchr(path, '\n') != nullptr;
|
||||
}
|
||||
|
||||
static void
|
||||
recursive_watch_subdirectories(WatchDirectory *directory,
|
||||
const AllocatedPath &path_fs, unsigned depth)
|
||||
{
|
||||
Error error;
|
||||
DIR *dir;
|
||||
struct dirent *ent;
|
||||
|
||||
assert(directory != nullptr);
|
||||
assert(depth <= inotify_max_depth);
|
||||
assert(!path_fs.IsNull());
|
||||
|
||||
++depth;
|
||||
|
||||
if (depth > inotify_max_depth)
|
||||
return;
|
||||
|
||||
dir = opendir(path_fs.c_str());
|
||||
if (dir == nullptr) {
|
||||
FormatErrno(inotify_domain,
|
||||
"Failed to open directory %s", path_fs.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
while ((ent = readdir(dir))) {
|
||||
struct stat st;
|
||||
int ret;
|
||||
|
||||
if (skip_path(ent->d_name))
|
||||
continue;
|
||||
|
||||
const auto child_path_fs =
|
||||
AllocatedPath::Build(path_fs, ent->d_name);
|
||||
ret = StatFile(child_path_fs, st);
|
||||
if (ret < 0) {
|
||||
FormatErrno(inotify_domain,
|
||||
"Failed to stat %s",
|
||||
child_path_fs.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!S_ISDIR(st.st_mode))
|
||||
continue;
|
||||
|
||||
ret = inotify_source->Add(child_path_fs.c_str(), IN_MASK,
|
||||
error);
|
||||
if (ret < 0) {
|
||||
FormatError(error,
|
||||
"Failed to register %s",
|
||||
child_path_fs.c_str());
|
||||
error.Clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
WatchDirectory *child = tree_find_watch_directory(ret);
|
||||
if (child != nullptr)
|
||||
/* already being watched */
|
||||
continue;
|
||||
|
||||
directory->children.emplace_front(directory,
|
||||
AllocatedPath::FromFS(ent->d_name),
|
||||
ret);
|
||||
child = &directory->children.front();
|
||||
|
||||
tree_add_watch_directory(child);
|
||||
|
||||
recursive_watch_subdirectories(child, child_path_fs, depth);
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
gcc_pure
|
||||
static unsigned
|
||||
watch_directory_depth(const WatchDirectory *d)
|
||||
{
|
||||
assert(d != nullptr);
|
||||
|
||||
unsigned depth = 0;
|
||||
while ((d = d->parent) != nullptr)
|
||||
++depth;
|
||||
|
||||
return depth;
|
||||
}
|
||||
|
||||
static void
|
||||
mpd_inotify_callback(int wd, unsigned mask,
|
||||
gcc_unused const char *name, gcc_unused void *ctx)
|
||||
{
|
||||
WatchDirectory *directory;
|
||||
|
||||
/*FormatDebug(inotify_domain, "wd=%d mask=0x%x name='%s'", wd, mask, name);*/
|
||||
|
||||
directory = tree_find_watch_directory(wd);
|
||||
if (directory == nullptr)
|
||||
return;
|
||||
|
||||
const auto uri_fs = watch_directory_get_uri_fs(directory);
|
||||
|
||||
if ((mask & (IN_DELETE_SELF|IN_MOVE_SELF)) != 0) {
|
||||
remove_watch_directory(directory);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((mask & (IN_ATTRIB|IN_CREATE|IN_MOVE)) != 0 &&
|
||||
(mask & IN_ISDIR) != 0) {
|
||||
/* a sub directory was changed: register those in
|
||||
inotify */
|
||||
const auto &root = mapper_get_music_directory_fs();
|
||||
|
||||
const auto path_fs = uri_fs.IsNull()
|
||||
? root
|
||||
: AllocatedPath::Build(root, uri_fs.c_str());
|
||||
|
||||
recursive_watch_subdirectories(directory, path_fs,
|
||||
watch_directory_depth(directory));
|
||||
}
|
||||
|
||||
if ((mask & (IN_CLOSE_WRITE|IN_MOVE|IN_DELETE)) != 0 ||
|
||||
/* at the maximum depth, we watch out for newly created
|
||||
directories */
|
||||
(watch_directory_depth(directory) == inotify_max_depth &&
|
||||
(mask & (IN_CREATE|IN_ISDIR)) == (IN_CREATE|IN_ISDIR))) {
|
||||
/* a file was changed, or a directory was
|
||||
moved/deleted: queue a database update */
|
||||
|
||||
if (!uri_fs.IsNull()) {
|
||||
const std::string uri_utf8 = uri_fs.ToUTF8();
|
||||
if (!uri_utf8.empty())
|
||||
inotify_queue->Enqueue(uri_utf8.c_str());
|
||||
}
|
||||
else
|
||||
inotify_queue->Enqueue("");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
mpd_inotify_init(unsigned max_depth)
|
||||
{
|
||||
LogDebug(inotify_domain, "initializing inotify");
|
||||
|
||||
const auto &path = mapper_get_music_directory_fs();
|
||||
if (path.IsNull()) {
|
||||
LogDebug(inotify_domain, "no music directory configured");
|
||||
return;
|
||||
}
|
||||
|
||||
Error error;
|
||||
inotify_source = InotifySource::Create(*main_loop,
|
||||
mpd_inotify_callback, nullptr,
|
||||
error);
|
||||
if (inotify_source == nullptr) {
|
||||
LogError(error);
|
||||
return;
|
||||
}
|
||||
|
||||
inotify_max_depth = max_depth;
|
||||
|
||||
int descriptor = inotify_source->Add(path.c_str(), IN_MASK, error);
|
||||
if (descriptor < 0) {
|
||||
LogError(error);
|
||||
delete inotify_source;
|
||||
inotify_source = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
inotify_root = new WatchDirectory(nullptr, path, descriptor);
|
||||
|
||||
tree_add_watch_directory(inotify_root);
|
||||
|
||||
recursive_watch_subdirectories(inotify_root, path, 0);
|
||||
|
||||
inotify_queue = new InotifyQueue(*main_loop);
|
||||
|
||||
LogDebug(inotify_domain, "watching music directory");
|
||||
}
|
||||
|
||||
void
|
||||
mpd_inotify_finish(void)
|
||||
{
|
||||
if (inotify_source == nullptr)
|
||||
return;
|
||||
|
||||
delete inotify_queue;
|
||||
delete inotify_source;
|
||||
delete inotify_root;
|
||||
inotify_directories.clear();
|
||||
}
|
||||
47
src/db/update/InotifyUpdate.hxx
Normal file
47
src/db/update/InotifyUpdate.hxx
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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_INOTIFY_UPDATE_HXX
|
||||
#define MPD_INOTIFY_UPDATE_HXX
|
||||
|
||||
#include "check.h"
|
||||
|
||||
#ifdef HAVE_INOTIFY_INIT
|
||||
|
||||
void
|
||||
mpd_inotify_init(unsigned max_depth);
|
||||
|
||||
void
|
||||
mpd_inotify_finish(void);
|
||||
|
||||
#else /* !HAVE_INOTIFY_INIT */
|
||||
|
||||
static inline void
|
||||
mpd_inotify_init(gcc_unused unsigned max_depth)
|
||||
{
|
||||
}
|
||||
|
||||
static inline void
|
||||
mpd_inotify_finish(void)
|
||||
{
|
||||
}
|
||||
|
||||
#endif /* !HAVE_INOTIFY_INIT */
|
||||
|
||||
#endif
|
||||
169
src/db/update/UpdateArchive.cxx
Normal file
169
src/db/update/UpdateArchive.cxx
Normal file
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* 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" /* must be first for large file support */
|
||||
#include "UpdateArchive.hxx"
|
||||
#include "UpdateInternal.hxx"
|
||||
#include "UpdateDomain.hxx"
|
||||
#include "db/DatabaseLock.hxx"
|
||||
#include "db/Directory.hxx"
|
||||
#include "db/Song.hxx"
|
||||
#include "Mapper.hxx"
|
||||
#include "fs/AllocatedPath.hxx"
|
||||
#include "archive/ArchiveList.hxx"
|
||||
#include "archive/ArchivePlugin.hxx"
|
||||
#include "archive/ArchiveFile.hxx"
|
||||
#include "archive/ArchiveVisitor.hxx"
|
||||
#include "util/Error.hxx"
|
||||
#include "Log.hxx"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static void
|
||||
update_archive_tree(Directory &directory, const char *name)
|
||||
{
|
||||
const char *tmp = strchr(name, '/');
|
||||
if (tmp) {
|
||||
const std::string child_name(name, tmp);
|
||||
//add dir is not there already
|
||||
db_lock();
|
||||
Directory *subdir =
|
||||
directory.MakeChild(child_name.c_str());
|
||||
subdir->device = DEVICE_INARCHIVE;
|
||||
db_unlock();
|
||||
|
||||
//create directories first
|
||||
update_archive_tree(*subdir, tmp+1);
|
||||
} else {
|
||||
if (strlen(name) == 0) {
|
||||
LogWarning(update_domain,
|
||||
"archive returned directory only");
|
||||
return;
|
||||
}
|
||||
|
||||
//add file
|
||||
db_lock();
|
||||
Song *song = directory.FindSong(name);
|
||||
db_unlock();
|
||||
if (song == nullptr) {
|
||||
song = Song::LoadFile(name, directory);
|
||||
if (song != nullptr) {
|
||||
db_lock();
|
||||
directory.AddSong(song);
|
||||
db_unlock();
|
||||
|
||||
modified = true;
|
||||
FormatDefault(update_domain, "added %s/%s",
|
||||
directory.GetPath(), name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the file listing from an archive file.
|
||||
*
|
||||
* @param parent the parent directory the archive file resides in
|
||||
* @param name the UTF-8 encoded base name of the archive file
|
||||
* @param st stat() information on the archive file
|
||||
* @param plugin the archive plugin which fits this archive type
|
||||
*/
|
||||
static void
|
||||
update_archive_file2(Directory &parent, const char *name,
|
||||
const struct stat *st,
|
||||
const struct archive_plugin *plugin)
|
||||
{
|
||||
db_lock();
|
||||
Directory *directory = parent.FindChild(name);
|
||||
db_unlock();
|
||||
|
||||
if (directory != nullptr && directory->mtime == st->st_mtime &&
|
||||
!walk_discard)
|
||||
/* MPD has already scanned the archive, and it hasn't
|
||||
changed since - don't consider updating it */
|
||||
return;
|
||||
|
||||
const auto path_fs = map_directory_child_fs(parent, name);
|
||||
|
||||
/* open archive */
|
||||
Error error;
|
||||
ArchiveFile *file = archive_file_open(plugin, path_fs.c_str(), error);
|
||||
if (file == nullptr) {
|
||||
LogError(error);
|
||||
return;
|
||||
}
|
||||
|
||||
FormatDebug(update_domain, "archive %s opened", path_fs.c_str());
|
||||
|
||||
if (directory == nullptr) {
|
||||
FormatDebug(update_domain,
|
||||
"creating archive directory: %s", name);
|
||||
db_lock();
|
||||
directory = parent.CreateChild(name);
|
||||
/* mark this directory as archive (we use device for
|
||||
this) */
|
||||
directory->device = DEVICE_INARCHIVE;
|
||||
db_unlock();
|
||||
}
|
||||
|
||||
directory->mtime = st->st_mtime;
|
||||
|
||||
class UpdateArchiveVisitor final : public ArchiveVisitor {
|
||||
Directory *directory;
|
||||
|
||||
public:
|
||||
UpdateArchiveVisitor(Directory *_directory)
|
||||
:directory(_directory) {}
|
||||
|
||||
virtual void VisitArchiveEntry(const char *path_utf8) override {
|
||||
FormatDebug(update_domain,
|
||||
"adding archive file: %s", path_utf8);
|
||||
update_archive_tree(*directory, path_utf8);
|
||||
}
|
||||
};
|
||||
|
||||
UpdateArchiveVisitor visitor(directory);
|
||||
file->Visit(visitor);
|
||||
file->Close();
|
||||
}
|
||||
|
||||
bool
|
||||
update_archive_file(Directory &directory,
|
||||
const char *name, const char *suffix,
|
||||
const struct stat *st)
|
||||
{
|
||||
#ifdef ENABLE_ARCHIVE
|
||||
const struct archive_plugin *plugin =
|
||||
archive_plugin_from_suffix(suffix);
|
||||
if (plugin == nullptr)
|
||||
return false;
|
||||
|
||||
update_archive_file2(directory, name, st, plugin);
|
||||
return true;
|
||||
#else
|
||||
(void)directory;
|
||||
(void)name;
|
||||
(void)suffix;
|
||||
(void)st;
|
||||
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
50
src/db/update/UpdateArchive.hxx
Normal file
50
src/db/update/UpdateArchive.hxx
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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_UPDATE_ARCHIVE_HXX
|
||||
#define MPD_UPDATE_ARCHIVE_HXX
|
||||
|
||||
#include "check.h"
|
||||
#include "Compiler.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
struct Directory;
|
||||
|
||||
#ifdef ENABLE_ARCHIVE
|
||||
|
||||
bool
|
||||
update_archive_file(Directory &directory,
|
||||
const char *name, const char *suffix,
|
||||
const struct stat *st);
|
||||
|
||||
#else
|
||||
|
||||
static inline bool
|
||||
update_archive_file(gcc_unused Directory &directory,
|
||||
gcc_unused const char *name,
|
||||
gcc_unused const char *suffix,
|
||||
gcc_unused const struct stat *st)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
136
src/db/update/UpdateContainer.cxx
Normal file
136
src/db/update/UpdateContainer.cxx
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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" /* must be first for large file support */
|
||||
#include "UpdateContainer.hxx"
|
||||
#include "UpdateInternal.hxx"
|
||||
#include "UpdateDatabase.hxx"
|
||||
#include "UpdateDomain.hxx"
|
||||
#include "db/DatabaseLock.hxx"
|
||||
#include "db/Directory.hxx"
|
||||
#include "db/Song.hxx"
|
||||
#include "decoder/DecoderPlugin.hxx"
|
||||
#include "decoder/DecoderList.hxx"
|
||||
#include "Mapper.hxx"
|
||||
#include "fs/AllocatedPath.hxx"
|
||||
#include "tag/TagHandler.hxx"
|
||||
#include "tag/TagBuilder.hxx"
|
||||
#include "Log.hxx"
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
/**
|
||||
* Create the specified directory object if it does not exist already
|
||||
* or if the #stat object indicates that it has been modified since
|
||||
* the last update. Returns nullptr when it exists already and is
|
||||
* unmodified.
|
||||
*
|
||||
* The caller must lock the database.
|
||||
*/
|
||||
static Directory *
|
||||
make_directory_if_modified(Directory &parent, const char *name,
|
||||
const struct stat *st)
|
||||
{
|
||||
Directory *directory = parent.FindChild(name);
|
||||
|
||||
// directory exists already
|
||||
if (directory != nullptr) {
|
||||
if (directory->mtime == st->st_mtime && !walk_discard) {
|
||||
/* not modified */
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
delete_directory(directory);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
directory = parent.MakeChild(name);
|
||||
directory->mtime = st->st_mtime;
|
||||
return directory;
|
||||
}
|
||||
|
||||
static bool
|
||||
SupportsContainerSuffix(const DecoderPlugin &plugin, const char *suffix)
|
||||
{
|
||||
return plugin.container_scan != nullptr &&
|
||||
plugin.SupportsSuffix(suffix);
|
||||
}
|
||||
|
||||
bool
|
||||
update_container_file(Directory &directory,
|
||||
const char *name,
|
||||
const struct stat *st,
|
||||
const char *suffix)
|
||||
{
|
||||
const DecoderPlugin *_plugin = decoder_plugins_find([suffix](const DecoderPlugin &plugin){
|
||||
return SupportsContainerSuffix(plugin, suffix);
|
||||
});
|
||||
if (_plugin == nullptr)
|
||||
return false;
|
||||
const DecoderPlugin &plugin = *_plugin;
|
||||
|
||||
db_lock();
|
||||
Directory *contdir = make_directory_if_modified(directory, name, st);
|
||||
if (contdir == nullptr) {
|
||||
/* not modified */
|
||||
db_unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
contdir->device = DEVICE_CONTAINER;
|
||||
db_unlock();
|
||||
|
||||
const auto pathname = map_directory_child_fs(directory, name);
|
||||
|
||||
char *vtrack;
|
||||
unsigned int tnum = 0;
|
||||
TagBuilder tag_builder;
|
||||
while ((vtrack = plugin.container_scan(pathname.c_str(), ++tnum)) != nullptr) {
|
||||
Song *song = Song::NewFile(vtrack, *contdir);
|
||||
|
||||
// shouldn't be necessary but it's there..
|
||||
song->mtime = st->st_mtime;
|
||||
|
||||
const auto child_path_fs =
|
||||
map_directory_child_fs(*contdir, vtrack);
|
||||
|
||||
plugin.ScanFile(child_path_fs.c_str(),
|
||||
add_tag_handler, &tag_builder);
|
||||
|
||||
tag_builder.Commit(song->tag);
|
||||
|
||||
db_lock();
|
||||
contdir->AddSong(song);
|
||||
db_unlock();
|
||||
|
||||
modified = true;
|
||||
|
||||
FormatDefault(update_domain, "added %s/%s",
|
||||
directory.GetPath(), vtrack);
|
||||
g_free(vtrack);
|
||||
}
|
||||
|
||||
if (tnum == 1) {
|
||||
db_lock();
|
||||
delete_directory(contdir);
|
||||
db_unlock();
|
||||
return false;
|
||||
} else
|
||||
return true;
|
||||
}
|
||||
36
src/db/update/UpdateContainer.hxx
Normal file
36
src/db/update/UpdateContainer.hxx
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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_UPDATE_CONTAINER_HXX
|
||||
#define MPD_UPDATE_CONTAINER_HXX
|
||||
|
||||
#include "check.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
struct Directory;
|
||||
struct DecoderPlugin;
|
||||
|
||||
bool
|
||||
update_container_file(Directory &directory,
|
||||
const char *name,
|
||||
const struct stat *st,
|
||||
const char *suffix);
|
||||
|
||||
#endif
|
||||
104
src/db/update/UpdateDatabase.cxx
Normal file
104
src/db/update/UpdateDatabase.cxx
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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" /* must be first for large file support */
|
||||
#include "UpdateDatabase.hxx"
|
||||
#include "UpdateRemove.hxx"
|
||||
#include "PlaylistVector.hxx"
|
||||
#include "db/Directory.hxx"
|
||||
#include "db/Song.hxx"
|
||||
#include "db/DatabaseLock.hxx"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void
|
||||
delete_song(Directory &dir, Song *del)
|
||||
{
|
||||
assert(del->parent == &dir);
|
||||
|
||||
/* first, prevent traversers in main task from getting this */
|
||||
dir.RemoveSong(del);
|
||||
|
||||
db_unlock(); /* temporary unlock, because update_remove_song() blocks */
|
||||
|
||||
/* now take it out of the playlist (in the main_task) */
|
||||
update_remove_song(del);
|
||||
|
||||
/* finally, all possible references gone, free it */
|
||||
del->Free();
|
||||
|
||||
db_lock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively remove all sub directories and songs from a directory,
|
||||
* leaving an empty directory.
|
||||
*
|
||||
* Caller must lock the #db_mutex.
|
||||
*/
|
||||
static void
|
||||
clear_directory(Directory &directory)
|
||||
{
|
||||
Directory *child, *n;
|
||||
directory_for_each_child_safe(child, n, directory)
|
||||
delete_directory(child);
|
||||
|
||||
Song *song, *ns;
|
||||
directory_for_each_song_safe(song, ns, directory) {
|
||||
assert(song->parent == &directory);
|
||||
delete_song(directory, song);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
delete_directory(Directory *directory)
|
||||
{
|
||||
assert(directory->parent != nullptr);
|
||||
|
||||
clear_directory(*directory);
|
||||
|
||||
directory->Delete();
|
||||
}
|
||||
|
||||
bool
|
||||
delete_name_in(Directory &parent, const char *name)
|
||||
{
|
||||
bool modified = false;
|
||||
|
||||
db_lock();
|
||||
Directory *directory = parent.FindChild(name);
|
||||
|
||||
if (directory != nullptr) {
|
||||
delete_directory(directory);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
Song *song = parent.FindSong(name);
|
||||
if (song != nullptr) {
|
||||
delete_song(parent, song);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
parent.playlists.erase(name);
|
||||
|
||||
db_unlock();
|
||||
|
||||
return modified;
|
||||
}
|
||||
50
src/db/update/UpdateDatabase.hxx
Normal file
50
src/db/update/UpdateDatabase.hxx
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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_UPDATE_DATABASE_HXX
|
||||
#define MPD_UPDATE_DATABASE_HXX
|
||||
|
||||
#include "check.h"
|
||||
|
||||
struct Directory;
|
||||
struct Song;
|
||||
|
||||
/**
|
||||
* Caller must lock the #db_mutex.
|
||||
*/
|
||||
void
|
||||
delete_song(Directory &parent, Song *song);
|
||||
|
||||
/**
|
||||
* Recursively free a directory and all its contents.
|
||||
*
|
||||
* Caller must lock the #db_mutex.
|
||||
*/
|
||||
void
|
||||
delete_directory(Directory *directory);
|
||||
|
||||
/**
|
||||
* Caller must NOT lock the #db_mutex.
|
||||
*
|
||||
* @return true if the database was modified
|
||||
*/
|
||||
bool
|
||||
delete_name_in(Directory &parent, const char *name);
|
||||
|
||||
#endif
|
||||
23
src/db/update/UpdateDomain.cxx
Normal file
23
src/db/update/UpdateDomain.cxx
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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 "UpdateDomain.hxx"
|
||||
#include "util/Domain.hxx"
|
||||
|
||||
const Domain update_domain("update");
|
||||
25
src/db/update/UpdateDomain.hxx
Normal file
25
src/db/update/UpdateDomain.hxx
Normal file
@@ -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_UPDATE_DOMAIN_HXX
|
||||
#define MPD_UPDATE_DOMAIN_HXX
|
||||
|
||||
extern const class Domain update_domain;
|
||||
|
||||
#endif
|
||||
181
src/db/update/UpdateGlue.cxx
Normal file
181
src/db/update/UpdateGlue.cxx
Normal file
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* 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 "UpdateGlue.hxx"
|
||||
#include "UpdateQueue.hxx"
|
||||
#include "UpdateWalk.hxx"
|
||||
#include "UpdateRemove.hxx"
|
||||
#include "UpdateDomain.hxx"
|
||||
#include "Mapper.hxx"
|
||||
#include "db/DatabaseSimple.hxx"
|
||||
#include "Idle.hxx"
|
||||
#include "GlobalEvents.hxx"
|
||||
#include "util/Error.hxx"
|
||||
#include "Log.hxx"
|
||||
#include "Main.hxx"
|
||||
#include "Instance.hxx"
|
||||
#include "system/FatalError.hxx"
|
||||
#include "thread/Id.hxx"
|
||||
#include "thread/Thread.hxx"
|
||||
#include "thread/Util.hxx"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
static enum update_progress {
|
||||
UPDATE_PROGRESS_IDLE = 0,
|
||||
UPDATE_PROGRESS_RUNNING = 1,
|
||||
UPDATE_PROGRESS_DONE = 2
|
||||
} progress;
|
||||
|
||||
static bool modified;
|
||||
|
||||
static Thread update_thread;
|
||||
|
||||
static const unsigned update_task_id_max = 1 << 15;
|
||||
|
||||
static unsigned update_task_id;
|
||||
|
||||
static UpdateQueueItem next;
|
||||
|
||||
unsigned
|
||||
isUpdatingDB(void)
|
||||
{
|
||||
return next.id;
|
||||
}
|
||||
|
||||
static void
|
||||
update_task(gcc_unused void *ctx)
|
||||
{
|
||||
if (!next.path_utf8.empty())
|
||||
FormatDebug(update_domain, "starting: %s",
|
||||
next.path_utf8.c_str());
|
||||
else
|
||||
LogDebug(update_domain, "starting");
|
||||
|
||||
SetThreadIdlePriority();
|
||||
|
||||
modified = update_walk(next.path_utf8.c_str(), next.discard);
|
||||
|
||||
if (modified || !db_exists()) {
|
||||
Error error;
|
||||
if (!db_save(error))
|
||||
LogError(error, "Failed to save database");
|
||||
}
|
||||
|
||||
if (!next.path_utf8.empty())
|
||||
FormatDebug(update_domain, "finished: %s",
|
||||
next.path_utf8.c_str());
|
||||
else
|
||||
LogDebug(update_domain, "finished");
|
||||
|
||||
progress = UPDATE_PROGRESS_DONE;
|
||||
GlobalEvents::Emit(GlobalEvents::UPDATE);
|
||||
}
|
||||
|
||||
static void
|
||||
spawn_update_task(UpdateQueueItem &&i)
|
||||
{
|
||||
assert(main_thread.IsInside());
|
||||
|
||||
progress = UPDATE_PROGRESS_RUNNING;
|
||||
modified = false;
|
||||
|
||||
next = std::move(i);
|
||||
|
||||
Error error;
|
||||
if (!update_thread.Start(update_task, nullptr, error))
|
||||
FatalError(error);
|
||||
|
||||
FormatDebug(update_domain,
|
||||
"spawned thread for update job id %i", next.id);
|
||||
}
|
||||
|
||||
static unsigned
|
||||
generate_update_id()
|
||||
{
|
||||
unsigned id = update_task_id + 1;
|
||||
if (id > update_task_id_max)
|
||||
id = 1;
|
||||
return id;
|
||||
}
|
||||
|
||||
unsigned
|
||||
update_enqueue(const char *path, bool discard)
|
||||
{
|
||||
assert(main_thread.IsInside());
|
||||
|
||||
if (!db_is_simple() || !mapper_has_music_directory())
|
||||
return 0;
|
||||
|
||||
if (progress != UPDATE_PROGRESS_IDLE) {
|
||||
const unsigned id = generate_update_id();
|
||||
if (!update_queue_push(path, discard, id))
|
||||
return 0;
|
||||
|
||||
update_task_id = id;
|
||||
return id;
|
||||
}
|
||||
|
||||
const unsigned id = update_task_id = generate_update_id();
|
||||
spawn_update_task(UpdateQueueItem(path, discard, id));
|
||||
|
||||
idle_add(IDLE_UPDATE);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called in the main thread after the database update is finished.
|
||||
*/
|
||||
static void update_finished_event(void)
|
||||
{
|
||||
assert(progress == UPDATE_PROGRESS_DONE);
|
||||
assert(next.IsDefined());
|
||||
|
||||
update_thread.Join();
|
||||
next = UpdateQueueItem();
|
||||
|
||||
idle_add(IDLE_UPDATE);
|
||||
|
||||
if (modified)
|
||||
/* send "idle" events */
|
||||
instance->DatabaseModified();
|
||||
|
||||
auto i = update_queue_shift();
|
||||
if (i.IsDefined()) {
|
||||
/* schedule the next path */
|
||||
spawn_update_task(std::move(i));
|
||||
} else {
|
||||
progress = UPDATE_PROGRESS_IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
void update_global_init(void)
|
||||
{
|
||||
GlobalEvents::Register(GlobalEvents::UPDATE, update_finished_event);
|
||||
|
||||
update_remove_global_init();
|
||||
update_walk_global_init();
|
||||
}
|
||||
|
||||
void update_global_finish(void)
|
||||
{
|
||||
update_walk_global_finish();
|
||||
}
|
||||
43
src/db/update/UpdateGlue.hxx
Normal file
43
src/db/update/UpdateGlue.hxx
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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_UPDATE_GLUE_HXX
|
||||
#define MPD_UPDATE_GLUE_HXX
|
||||
|
||||
#include "Compiler.h"
|
||||
|
||||
void update_global_init(void);
|
||||
|
||||
void update_global_finish(void);
|
||||
|
||||
unsigned
|
||||
isUpdatingDB(void);
|
||||
|
||||
/**
|
||||
* Add this path to the database update queue.
|
||||
*
|
||||
* @param path a path to update; if an empty string,
|
||||
* the whole music directory is updated
|
||||
* @return the job id, or 0 on error
|
||||
*/
|
||||
gcc_nonnull_all
|
||||
unsigned
|
||||
update_enqueue(const char *path, bool discard);
|
||||
|
||||
#endif
|
||||
113
src/db/update/UpdateIO.cxx
Normal file
113
src/db/update/UpdateIO.cxx
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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" /* must be first for large file support */
|
||||
#include "UpdateIO.hxx"
|
||||
#include "UpdateDomain.hxx"
|
||||
#include "db/Directory.hxx"
|
||||
#include "Mapper.hxx"
|
||||
#include "fs/AllocatedPath.hxx"
|
||||
#include "fs/FileSystem.hxx"
|
||||
#include "Log.hxx"
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int
|
||||
stat_directory(const Directory &directory, struct stat *st)
|
||||
{
|
||||
const auto path_fs = map_directory_fs(directory);
|
||||
if (path_fs.IsNull())
|
||||
return -1;
|
||||
|
||||
if (!StatFile(path_fs, *st)) {
|
||||
int error = errno;
|
||||
const std::string path_utf8 = path_fs.ToUTF8();
|
||||
FormatErrno(update_domain, error,
|
||||
"Failed to stat %s", path_utf8.c_str());
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
stat_directory_child(const Directory &parent, const char *name,
|
||||
struct stat *st)
|
||||
{
|
||||
const auto path_fs = map_directory_child_fs(parent, name);
|
||||
if (path_fs.IsNull())
|
||||
return -1;
|
||||
|
||||
if (!StatFile(path_fs, *st)) {
|
||||
int error = errno;
|
||||
const std::string path_utf8 = path_fs.ToUTF8();
|
||||
FormatErrno(update_domain, error,
|
||||
"Failed to stat %s", path_utf8.c_str());
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool
|
||||
directory_exists(const Directory &directory)
|
||||
{
|
||||
const auto path_fs = map_directory_fs(directory);
|
||||
if (path_fs.IsNull())
|
||||
/* invalid path: cannot exist */
|
||||
return false;
|
||||
|
||||
return directory.device == DEVICE_INARCHIVE ||
|
||||
directory.device == DEVICE_CONTAINER
|
||||
? FileExists(path_fs)
|
||||
: DirectoryExists(path_fs);
|
||||
}
|
||||
|
||||
bool
|
||||
directory_child_is_regular(const Directory &directory,
|
||||
const char *name_utf8)
|
||||
{
|
||||
const auto path_fs = map_directory_child_fs(directory, name_utf8);
|
||||
if (path_fs.IsNull())
|
||||
return false;
|
||||
|
||||
return FileExists(path_fs);
|
||||
}
|
||||
|
||||
bool
|
||||
directory_child_access(const Directory &directory,
|
||||
const char *name, int mode)
|
||||
{
|
||||
#ifdef WIN32
|
||||
/* CheckAccess() is useless on WIN32 */
|
||||
(void)directory;
|
||||
(void)name;
|
||||
(void)mode;
|
||||
return true;
|
||||
#else
|
||||
const auto path = map_directory_child_fs(directory, name);
|
||||
if (path.IsNull())
|
||||
/* something went wrong, but that isn't a permission
|
||||
problem */
|
||||
return true;
|
||||
|
||||
return CheckAccess(path, mode) || errno != EACCES;
|
||||
#endif
|
||||
}
|
||||
50
src/db/update/UpdateIO.hxx
Normal file
50
src/db/update/UpdateIO.hxx
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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_UPDATE_IO_HXX
|
||||
#define MPD_UPDATE_IO_HXX
|
||||
|
||||
#include "check.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
struct Directory;
|
||||
|
||||
int
|
||||
stat_directory(const Directory &directory, struct stat *st);
|
||||
|
||||
int
|
||||
stat_directory_child(const Directory &parent, const char *name,
|
||||
struct stat *st);
|
||||
|
||||
bool
|
||||
directory_exists(const Directory &directory);
|
||||
|
||||
bool
|
||||
directory_child_is_regular(const Directory &directory,
|
||||
const char *name_utf8);
|
||||
|
||||
/**
|
||||
* Checks if the given permissions on the mapped file are given.
|
||||
*/
|
||||
bool
|
||||
directory_child_access(const Directory &directory,
|
||||
const char *name, int mode);
|
||||
|
||||
#endif
|
||||
28
src/db/update/UpdateInternal.hxx
Normal file
28
src/db/update/UpdateInternal.hxx
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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_UPDATE_INTERNAL_H
|
||||
#define MPD_UPDATE_INTERNAL_H
|
||||
|
||||
#include "check.h"
|
||||
|
||||
extern bool walk_discard;
|
||||
extern bool modified;
|
||||
|
||||
#endif
|
||||
49
src/db/update/UpdateQueue.cxx
Normal file
49
src/db/update/UpdateQueue.cxx
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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 "UpdateQueue.hxx"
|
||||
|
||||
#include <queue>
|
||||
#include <list>
|
||||
|
||||
static constexpr unsigned MAX_UPDATE_QUEUE_SIZE = 32;
|
||||
|
||||
static std::queue<UpdateQueueItem, std::list<UpdateQueueItem>> update_queue;
|
||||
|
||||
bool
|
||||
update_queue_push(const char *path, bool discard, unsigned id)
|
||||
{
|
||||
if (update_queue.size() >= MAX_UPDATE_QUEUE_SIZE)
|
||||
return false;
|
||||
|
||||
update_queue.emplace(path, discard, id);
|
||||
return true;
|
||||
}
|
||||
|
||||
UpdateQueueItem
|
||||
update_queue_shift()
|
||||
{
|
||||
if (update_queue.empty())
|
||||
return UpdateQueueItem();
|
||||
|
||||
auto i = std::move(update_queue.front());
|
||||
update_queue.pop();
|
||||
return i;
|
||||
}
|
||||
48
src/db/update/UpdateQueue.hxx
Normal file
48
src/db/update/UpdateQueue.hxx
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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_UPDATE_QUEUE_HXX
|
||||
#define MPD_UPDATE_QUEUE_HXX
|
||||
|
||||
#include "check.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
struct UpdateQueueItem {
|
||||
std::string path_utf8;
|
||||
unsigned id;
|
||||
bool discard;
|
||||
|
||||
UpdateQueueItem():id(0) {}
|
||||
UpdateQueueItem(const char *_path, bool _discard,
|
||||
unsigned _id)
|
||||
:path_utf8(_path), id(_id), discard(_discard) {}
|
||||
|
||||
bool IsDefined() const {
|
||||
return id != 0;
|
||||
}
|
||||
};
|
||||
|
||||
bool
|
||||
update_queue_push(const char *path, bool discard, unsigned id);
|
||||
|
||||
UpdateQueueItem
|
||||
update_queue_shift();
|
||||
|
||||
#endif
|
||||
97
src/db/update/UpdateRemove.cxx
Normal file
97
src/db/update/UpdateRemove.cxx
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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" /* must be first for large file support */
|
||||
#include "UpdateRemove.hxx"
|
||||
#include "UpdateDomain.hxx"
|
||||
#include "GlobalEvents.hxx"
|
||||
#include "thread/Mutex.hxx"
|
||||
#include "thread/Cond.hxx"
|
||||
#include "db/Song.hxx"
|
||||
#include "db/LightSong.hxx"
|
||||
#include "Main.hxx"
|
||||
#include "Instance.hxx"
|
||||
#include "Log.hxx"
|
||||
|
||||
#ifdef ENABLE_SQLITE
|
||||
#include "sticker/StickerDatabase.hxx"
|
||||
#include "sticker/SongSticker.hxx"
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
static const Song *removed_song;
|
||||
|
||||
static Mutex remove_mutex;
|
||||
static Cond remove_cond;
|
||||
|
||||
/**
|
||||
* Safely remove a song from the database. This must be done in the
|
||||
* main task, to be sure that there is no pointer left to it.
|
||||
*/
|
||||
static void
|
||||
song_remove_event(void)
|
||||
{
|
||||
assert(removed_song != nullptr);
|
||||
|
||||
{
|
||||
const auto uri = removed_song->GetURI();
|
||||
FormatDefault(update_domain, "removing %s", uri.c_str());
|
||||
}
|
||||
|
||||
#ifdef ENABLE_SQLITE
|
||||
/* if the song has a sticker, remove it */
|
||||
if (sticker_enabled())
|
||||
sticker_song_delete(removed_song->Export());
|
||||
#endif
|
||||
|
||||
{
|
||||
const auto uri = removed_song->GetURI();
|
||||
instance->DeleteSong(uri.c_str());
|
||||
}
|
||||
|
||||
/* clear "removed_song" and send signal to update thread */
|
||||
remove_mutex.lock();
|
||||
removed_song = nullptr;
|
||||
remove_cond.signal();
|
||||
remove_mutex.unlock();
|
||||
}
|
||||
|
||||
void
|
||||
update_remove_global_init(void)
|
||||
{
|
||||
GlobalEvents::Register(GlobalEvents::DELETE, song_remove_event);
|
||||
}
|
||||
|
||||
void
|
||||
update_remove_song(const Song *song)
|
||||
{
|
||||
assert(removed_song == nullptr);
|
||||
|
||||
removed_song = song;
|
||||
|
||||
GlobalEvents::Emit(GlobalEvents::DELETE);
|
||||
|
||||
remove_mutex.lock();
|
||||
|
||||
while (removed_song != nullptr)
|
||||
remove_cond.wait(remove_mutex);
|
||||
|
||||
remove_mutex.unlock();
|
||||
}
|
||||
38
src/db/update/UpdateRemove.hxx
Normal file
38
src/db/update/UpdateRemove.hxx
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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_UPDATE_REMOVE_HXX
|
||||
#define MPD_UPDATE_REMOVE_HXX
|
||||
|
||||
#include "check.h"
|
||||
|
||||
struct Song;
|
||||
|
||||
void
|
||||
update_remove_global_init(void);
|
||||
|
||||
/**
|
||||
* Sends a signal to the main thread which will in turn remove the
|
||||
* song: from the sticker database and from the playlist. This
|
||||
* serialized access is implemented to avoid excessive locking.
|
||||
*/
|
||||
void
|
||||
update_remove_song(const Song *song);
|
||||
|
||||
#endif
|
||||
113
src/db/update/UpdateSong.cxx
Normal file
113
src/db/update/UpdateSong.cxx
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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" /* must be first for large file support */
|
||||
#include "UpdateSong.hxx"
|
||||
#include "UpdateInternal.hxx"
|
||||
#include "UpdateIO.hxx"
|
||||
#include "UpdateDatabase.hxx"
|
||||
#include "UpdateContainer.hxx"
|
||||
#include "UpdateDomain.hxx"
|
||||
#include "db/DatabaseLock.hxx"
|
||||
#include "db/Directory.hxx"
|
||||
#include "db/Song.hxx"
|
||||
#include "decoder/DecoderList.hxx"
|
||||
#include "Log.hxx"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
static void
|
||||
update_song_file2(Directory &directory,
|
||||
const char *name, const struct stat *st,
|
||||
const char *suffix)
|
||||
{
|
||||
db_lock();
|
||||
Song *song = directory.FindSong(name);
|
||||
db_unlock();
|
||||
|
||||
if (!directory_child_access(directory, name, R_OK)) {
|
||||
FormatError(update_domain,
|
||||
"no read permissions on %s/%s",
|
||||
directory.GetPath(), name);
|
||||
if (song != nullptr) {
|
||||
db_lock();
|
||||
delete_song(directory, song);
|
||||
db_unlock();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(song != nullptr && st->st_mtime == song->mtime &&
|
||||
!walk_discard) &&
|
||||
update_container_file(directory, name, st, suffix)) {
|
||||
if (song != nullptr) {
|
||||
db_lock();
|
||||
delete_song(directory, song);
|
||||
db_unlock();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (song == nullptr) {
|
||||
FormatDebug(update_domain, "reading %s/%s",
|
||||
directory.GetPath(), name);
|
||||
song = Song::LoadFile(name, directory);
|
||||
if (song == nullptr) {
|
||||
FormatDebug(update_domain,
|
||||
"ignoring unrecognized file %s/%s",
|
||||
directory.GetPath(), name);
|
||||
return;
|
||||
}
|
||||
|
||||
db_lock();
|
||||
directory.AddSong(song);
|
||||
db_unlock();
|
||||
|
||||
modified = true;
|
||||
FormatDefault(update_domain, "added %s/%s",
|
||||
directory.GetPath(), name);
|
||||
} else if (st->st_mtime != song->mtime || walk_discard) {
|
||||
FormatDefault(update_domain, "updating %s/%s",
|
||||
directory.GetPath(), name);
|
||||
if (!song->UpdateFile()) {
|
||||
FormatDebug(update_domain,
|
||||
"deleting unrecognized file %s/%s",
|
||||
directory.GetPath(), name);
|
||||
db_lock();
|
||||
delete_song(directory, song);
|
||||
db_unlock();
|
||||
}
|
||||
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
update_song_file(Directory &directory,
|
||||
const char *name, const char *suffix,
|
||||
const struct stat *st)
|
||||
{
|
||||
if (!decoder_plugins_supports_suffix(suffix))
|
||||
return false;
|
||||
|
||||
update_song_file2(directory, name, st, suffix);
|
||||
return true;
|
||||
}
|
||||
34
src/db/update/UpdateSong.hxx
Normal file
34
src/db/update/UpdateSong.hxx
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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_UPDATE_SONG_HXX
|
||||
#define MPD_UPDATE_SONG_HXX
|
||||
|
||||
#include "check.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
struct Directory;
|
||||
|
||||
bool
|
||||
update_song_file(Directory &directory,
|
||||
const char *name, const char *suffix,
|
||||
const struct stat *st);
|
||||
|
||||
#endif
|
||||
484
src/db/update/UpdateWalk.cxx
Normal file
484
src/db/update/UpdateWalk.cxx
Normal file
@@ -0,0 +1,484 @@
|
||||
/*
|
||||
* 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" /* must be first for large file support */
|
||||
#include "UpdateWalk.hxx"
|
||||
#include "UpdateIO.hxx"
|
||||
#include "UpdateDatabase.hxx"
|
||||
#include "UpdateSong.hxx"
|
||||
#include "UpdateArchive.hxx"
|
||||
#include "UpdateDomain.hxx"
|
||||
#include "db/DatabaseLock.hxx"
|
||||
#include "db/DatabaseSimple.hxx"
|
||||
#include "db/Directory.hxx"
|
||||
#include "db/Song.hxx"
|
||||
#include "PlaylistVector.hxx"
|
||||
#include "playlist/PlaylistRegistry.hxx"
|
||||
#include "Mapper.hxx"
|
||||
#include "ExcludeList.hxx"
|
||||
#include "config/ConfigGlobal.hxx"
|
||||
#include "config/ConfigOption.hxx"
|
||||
#include "fs/AllocatedPath.hxx"
|
||||
#include "fs/Traits.hxx"
|
||||
#include "fs/FileSystem.hxx"
|
||||
#include "fs/DirectoryReader.hxx"
|
||||
#include "util/Alloc.hxx"
|
||||
#include "util/UriUtil.hxx"
|
||||
#include "Log.hxx"
|
||||
|
||||
#include <assert.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
bool walk_discard;
|
||||
bool modified;
|
||||
|
||||
#ifndef WIN32
|
||||
|
||||
static constexpr bool DEFAULT_FOLLOW_INSIDE_SYMLINKS = true;
|
||||
static constexpr bool DEFAULT_FOLLOW_OUTSIDE_SYMLINKS = true;
|
||||
|
||||
static bool follow_inside_symlinks;
|
||||
static bool follow_outside_symlinks;
|
||||
|
||||
#endif
|
||||
|
||||
void
|
||||
update_walk_global_init(void)
|
||||
{
|
||||
#ifndef WIN32
|
||||
follow_inside_symlinks =
|
||||
config_get_bool(CONF_FOLLOW_INSIDE_SYMLINKS,
|
||||
DEFAULT_FOLLOW_INSIDE_SYMLINKS);
|
||||
|
||||
follow_outside_symlinks =
|
||||
config_get_bool(CONF_FOLLOW_OUTSIDE_SYMLINKS,
|
||||
DEFAULT_FOLLOW_OUTSIDE_SYMLINKS);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
update_walk_global_finish(void)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
directory_set_stat(Directory &dir, const struct stat *st)
|
||||
{
|
||||
dir.inode = st->st_ino;
|
||||
dir.device = st->st_dev;
|
||||
dir.have_stat = true;
|
||||
}
|
||||
|
||||
static void
|
||||
remove_excluded_from_directory(Directory &directory,
|
||||
const ExcludeList &exclude_list)
|
||||
{
|
||||
db_lock();
|
||||
|
||||
Directory *child, *n;
|
||||
directory_for_each_child_safe(child, n, directory) {
|
||||
const auto name_fs = AllocatedPath::FromUTF8(child->GetName());
|
||||
|
||||
if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
|
||||
delete_directory(child);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
Song *song, *ns;
|
||||
directory_for_each_song_safe(song, ns, directory) {
|
||||
assert(song->parent == &directory);
|
||||
|
||||
const auto name_fs = AllocatedPath::FromUTF8(song->uri);
|
||||
if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
|
||||
delete_song(directory, song);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
db_unlock();
|
||||
}
|
||||
|
||||
static void
|
||||
purge_deleted_from_directory(Directory &directory)
|
||||
{
|
||||
Directory *child, *n;
|
||||
directory_for_each_child_safe(child, n, directory) {
|
||||
if (directory_exists(*child))
|
||||
continue;
|
||||
|
||||
db_lock();
|
||||
delete_directory(child);
|
||||
db_unlock();
|
||||
|
||||
modified = true;
|
||||
}
|
||||
|
||||
Song *song, *ns;
|
||||
directory_for_each_song_safe(song, ns, directory) {
|
||||
const auto path = map_song_fs(*song);
|
||||
if (path.IsNull() || !FileExists(path)) {
|
||||
db_lock();
|
||||
delete_song(directory, song);
|
||||
db_unlock();
|
||||
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i = directory.playlists.begin(),
|
||||
end = directory.playlists.end();
|
||||
i != end;) {
|
||||
if (!directory_child_is_regular(directory, i->name.c_str())) {
|
||||
db_lock();
|
||||
i = directory.playlists.erase(i);
|
||||
db_unlock();
|
||||
} else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
static int
|
||||
update_directory_stat(Directory &directory)
|
||||
{
|
||||
struct stat st;
|
||||
if (stat_directory(directory, &st) < 0)
|
||||
return -1;
|
||||
|
||||
directory_set_stat(directory, &st);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int
|
||||
find_inode_ancestor(Directory *parent, ino_t inode, dev_t device)
|
||||
{
|
||||
#ifndef WIN32
|
||||
while (parent) {
|
||||
if (!parent->have_stat && update_directory_stat(*parent) < 0)
|
||||
return -1;
|
||||
|
||||
if (parent->inode == inode && parent->device == device) {
|
||||
LogDebug(update_domain, "recursive directory found");
|
||||
return 1;
|
||||
}
|
||||
|
||||
parent = parent->parent;
|
||||
}
|
||||
#else
|
||||
(void)parent;
|
||||
(void)inode;
|
||||
(void)device;
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool
|
||||
update_playlist_file2(Directory &directory,
|
||||
const char *name, const char *suffix,
|
||||
const struct stat *st)
|
||||
{
|
||||
if (!playlist_suffix_supported(suffix))
|
||||
return false;
|
||||
|
||||
PlaylistInfo pi(name, st->st_mtime);
|
||||
|
||||
db_lock();
|
||||
if (directory.playlists.UpdateOrInsert(std::move(pi)))
|
||||
modified = true;
|
||||
db_unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
update_regular_file(Directory &directory,
|
||||
const char *name, const struct stat *st)
|
||||
{
|
||||
const char *suffix = uri_get_suffix(name);
|
||||
if (suffix == nullptr)
|
||||
return false;
|
||||
|
||||
return update_song_file(directory, name, suffix, st) ||
|
||||
update_archive_file(directory, name, suffix, st) ||
|
||||
update_playlist_file2(directory, name, suffix, st);
|
||||
}
|
||||
|
||||
static bool
|
||||
update_directory(Directory &directory, const struct stat *st);
|
||||
|
||||
static void
|
||||
update_directory_child(Directory &directory,
|
||||
const char *name, const struct stat *st)
|
||||
{
|
||||
assert(strchr(name, '/') == nullptr);
|
||||
|
||||
if (S_ISREG(st->st_mode)) {
|
||||
update_regular_file(directory, name, st);
|
||||
} else if (S_ISDIR(st->st_mode)) {
|
||||
if (find_inode_ancestor(&directory, st->st_ino, st->st_dev))
|
||||
return;
|
||||
|
||||
db_lock();
|
||||
Directory *subdir = directory.MakeChild(name);
|
||||
db_unlock();
|
||||
|
||||
assert(&directory == subdir->parent);
|
||||
|
||||
if (!update_directory(*subdir, st)) {
|
||||
db_lock();
|
||||
delete_directory(subdir);
|
||||
db_unlock();
|
||||
}
|
||||
} else {
|
||||
FormatDebug(update_domain,
|
||||
"%s is not a directory, archive or music", name);
|
||||
}
|
||||
}
|
||||
|
||||
/* we don't look at "." / ".." nor files with newlines in their name */
|
||||
gcc_pure
|
||||
static bool skip_path(Path path_fs)
|
||||
{
|
||||
const char *path = path_fs.c_str();
|
||||
return (path[0] == '.' && path[1] == 0) ||
|
||||
(path[0] == '.' && path[1] == '.' && path[2] == 0) ||
|
||||
strchr(path, '\n') != nullptr;
|
||||
}
|
||||
|
||||
gcc_pure
|
||||
static bool
|
||||
skip_symlink(const Directory *directory, const char *utf8_name)
|
||||
{
|
||||
#ifndef WIN32
|
||||
const auto path_fs = map_directory_child_fs(*directory, utf8_name);
|
||||
if (path_fs.IsNull())
|
||||
return true;
|
||||
|
||||
const auto target = ReadLink(path_fs);
|
||||
if (target.IsNull())
|
||||
/* don't skip if this is not a symlink */
|
||||
return errno != EINVAL;
|
||||
|
||||
if (!follow_inside_symlinks && !follow_outside_symlinks) {
|
||||
/* ignore all symlinks */
|
||||
return true;
|
||||
} else if (follow_inside_symlinks && follow_outside_symlinks) {
|
||||
/* consider all symlinks */
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *target_str = target.c_str();
|
||||
|
||||
if (PathTraitsFS::IsAbsolute(target_str)) {
|
||||
/* if the symlink points to an absolute path, see if
|
||||
that path is inside the music directory */
|
||||
const char *relative = map_to_relative_path(target_str);
|
||||
return relative > target_str
|
||||
? !follow_inside_symlinks
|
||||
: !follow_outside_symlinks;
|
||||
}
|
||||
|
||||
const char *p = target_str;
|
||||
while (*p == '.') {
|
||||
if (p[1] == '.' && PathTraitsFS::IsSeparator(p[2])) {
|
||||
/* "../" moves to parent directory */
|
||||
directory = directory->parent;
|
||||
if (directory == nullptr) {
|
||||
/* we have moved outside the music
|
||||
directory - skip this symlink
|
||||
if such symlinks are not allowed */
|
||||
return !follow_outside_symlinks;
|
||||
}
|
||||
p += 3;
|
||||
} else if (PathTraitsFS::IsSeparator(p[1]))
|
||||
/* eliminate "./" */
|
||||
p += 2;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
/* we are still in the music directory, so this symlink points
|
||||
to a song which is already in the database - skip according
|
||||
to the follow_inside_symlinks param*/
|
||||
return !follow_inside_symlinks;
|
||||
#else
|
||||
/* no symlink checking on WIN32 */
|
||||
|
||||
(void)directory;
|
||||
(void)utf8_name;
|
||||
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool
|
||||
update_directory(Directory &directory, const struct stat *st)
|
||||
{
|
||||
assert(S_ISDIR(st->st_mode));
|
||||
|
||||
directory_set_stat(directory, st);
|
||||
|
||||
const auto path_fs = map_directory_fs(directory);
|
||||
if (path_fs.IsNull())
|
||||
return false;
|
||||
|
||||
DirectoryReader reader(path_fs);
|
||||
if (reader.HasFailed()) {
|
||||
int error = errno;
|
||||
const auto path_utf8 = path_fs.ToUTF8();
|
||||
FormatErrno(update_domain, error,
|
||||
"Failed to open directory %s",
|
||||
path_utf8.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
ExcludeList exclude_list;
|
||||
exclude_list.LoadFile(AllocatedPath::Build(path_fs, ".mpdignore"));
|
||||
|
||||
if (!exclude_list.IsEmpty())
|
||||
remove_excluded_from_directory(directory, exclude_list);
|
||||
|
||||
purge_deleted_from_directory(directory);
|
||||
|
||||
while (reader.ReadEntry()) {
|
||||
std::string utf8;
|
||||
struct stat st2;
|
||||
|
||||
const auto entry = reader.GetEntry();
|
||||
|
||||
if (skip_path(entry) || exclude_list.Check(entry))
|
||||
continue;
|
||||
|
||||
utf8 = entry.ToUTF8();
|
||||
if (utf8.empty())
|
||||
continue;
|
||||
|
||||
if (skip_symlink(&directory, utf8.c_str())) {
|
||||
modified |= delete_name_in(directory, utf8.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (stat_directory_child(directory, utf8.c_str(), &st2) == 0)
|
||||
update_directory_child(directory, utf8.c_str(), &st2);
|
||||
else
|
||||
modified |= delete_name_in(directory, utf8.c_str());
|
||||
}
|
||||
|
||||
directory.mtime = st->st_mtime;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static Directory *
|
||||
directory_make_child_checked(Directory &parent, const char *name_utf8)
|
||||
{
|
||||
db_lock();
|
||||
Directory *directory = parent.FindChild(name_utf8);
|
||||
db_unlock();
|
||||
|
||||
if (directory != nullptr)
|
||||
return directory;
|
||||
|
||||
struct stat st;
|
||||
if (stat_directory_child(parent, name_utf8, &st) < 0 ||
|
||||
find_inode_ancestor(&parent, st.st_ino, st.st_dev))
|
||||
return nullptr;
|
||||
|
||||
if (skip_symlink(&parent, name_utf8))
|
||||
return nullptr;
|
||||
|
||||
/* if we're adding directory paths, make sure to delete filenames
|
||||
with potentially the same name */
|
||||
db_lock();
|
||||
Song *conflicting = parent.FindSong(name_utf8);
|
||||
if (conflicting)
|
||||
delete_song(parent, conflicting);
|
||||
|
||||
directory = parent.CreateChild(name_utf8);
|
||||
db_unlock();
|
||||
|
||||
directory_set_stat(*directory, &st);
|
||||
return directory;
|
||||
}
|
||||
|
||||
static Directory *
|
||||
directory_make_uri_parent_checked(const char *uri)
|
||||
{
|
||||
Directory *directory = db_get_root();
|
||||
char *duplicated = xstrdup(uri);
|
||||
char *name_utf8 = duplicated, *slash;
|
||||
|
||||
while ((slash = strchr(name_utf8, '/')) != nullptr) {
|
||||
*slash = 0;
|
||||
|
||||
if (*name_utf8 == 0)
|
||||
continue;
|
||||
|
||||
directory = directory_make_child_checked(*directory,
|
||||
name_utf8);
|
||||
if (directory == nullptr)
|
||||
break;
|
||||
|
||||
name_utf8 = slash + 1;
|
||||
}
|
||||
|
||||
free(duplicated);
|
||||
return directory;
|
||||
}
|
||||
|
||||
static void
|
||||
update_uri(const char *uri)
|
||||
{
|
||||
Directory *parent = directory_make_uri_parent_checked(uri);
|
||||
if (parent == nullptr)
|
||||
return;
|
||||
|
||||
const char *name = PathTraitsUTF8::GetBase(uri);
|
||||
|
||||
struct stat st;
|
||||
if (!skip_symlink(parent, name) &&
|
||||
stat_directory_child(*parent, name, &st) == 0)
|
||||
update_directory_child(*parent, name, &st);
|
||||
else
|
||||
modified |= delete_name_in(*parent, name);
|
||||
}
|
||||
|
||||
bool
|
||||
update_walk(const char *path, bool discard)
|
||||
{
|
||||
walk_discard = discard;
|
||||
modified = false;
|
||||
|
||||
if (path != nullptr && !isRootDirectory(path)) {
|
||||
update_uri(path);
|
||||
} else {
|
||||
Directory *directory = db_get_root();
|
||||
struct stat st;
|
||||
|
||||
if (stat_directory(*directory, &st) == 0)
|
||||
update_directory(*directory, &st);
|
||||
}
|
||||
|
||||
return modified;
|
||||
}
|
||||
37
src/db/update/UpdateWalk.hxx
Normal file
37
src/db/update/UpdateWalk.hxx
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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_UPDATE_WALK_HXX
|
||||
#define MPD_UPDATE_WALK_HXX
|
||||
|
||||
#include "check.h"
|
||||
|
||||
void
|
||||
update_walk_global_init(void);
|
||||
|
||||
void
|
||||
update_walk_global_finish(void);
|
||||
|
||||
/**
|
||||
* Returns true if the database was modified.
|
||||
*/
|
||||
bool
|
||||
update_walk(const char *path, bool discard);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user