2009-09-25 18:32:00 +02:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2009-09-25 18:32:00 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2013-01-02 19:22:15 +01:00
|
|
|
#include "InotifyQueue.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "InotifyDomain.hxx"
|
2014-01-29 20:16:43 +01:00
|
|
|
#include "Service.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2018-12-28 14:02:41 +01:00
|
|
|
#include "protocol/Ack.hxx" // for class ProtocolError
|
2015-11-06 09:37:07 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2013-10-20 13:51:55 +02:00
|
|
|
/**
|
|
|
|
* Wait this long after the last change before calling
|
2018-12-28 14:00:10 +01:00
|
|
|
* UpdateService::Enqueue(). This increases the probability that
|
|
|
|
* updates can be bundled.
|
2013-10-20 13:51:55 +02:00
|
|
|
*/
|
2020-10-08 19:35:36 +02:00
|
|
|
static constexpr Event::Duration INOTIFY_UPDATE_DELAY =
|
2016-12-27 23:06:34 +01:00
|
|
|
std::chrono::seconds(5);
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2013-04-08 23:14:07 +02:00
|
|
|
void
|
2018-12-28 13:46:47 +01:00
|
|
|
InotifyQueue::OnDelay() noexcept
|
2009-09-25 18:32:00 +02:00
|
|
|
{
|
|
|
|
unsigned id;
|
|
|
|
|
2013-01-14 10:10:21 +01:00
|
|
|
while (!queue.empty()) {
|
|
|
|
const char *uri_utf8 = queue.front().c_str();
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2018-12-28 13:47:18 +01:00
|
|
|
try {
|
2018-12-28 14:02:41 +01:00
|
|
|
try {
|
|
|
|
id = update.Enqueue(uri_utf8, false);
|
|
|
|
} catch (const ProtocolError &e) {
|
|
|
|
if (e.GetCode() == ACK_ERROR_UPDATE_ALREADY) {
|
|
|
|
/* retry later */
|
|
|
|
delay_event.Schedule(INOTIFY_UPDATE_DELAY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-12-28 13:47:18 +01:00
|
|
|
} catch (...) {
|
|
|
|
FormatError(std::current_exception(),
|
|
|
|
"Failed to enqueue '%s'", uri_utf8);
|
|
|
|
queue.pop_front();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(inotify_domain, "updating '%s' job=%u",
|
|
|
|
uri_utf8, id);
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2013-01-14 10:10:21 +01:00
|
|
|
queue.pop_front();
|
2009-09-25 18:32:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 09:49:39 +01:00
|
|
|
gcc_pure
|
2009-09-25 18:32:00 +02:00
|
|
|
static bool
|
2017-05-08 14:44:49 +02:00
|
|
|
path_in(const char *path, const char *possible_parent) noexcept
|
2009-09-25 18:32:00 +02:00
|
|
|
{
|
2015-11-06 09:49:39 +01:00
|
|
|
if (StringIsEmpty(path))
|
|
|
|
return true;
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2015-11-06 09:49:39 +01:00
|
|
|
auto rest = StringAfterPrefix(path, possible_parent);
|
|
|
|
return rest != nullptr &&
|
|
|
|
(StringIsEmpty(rest) || rest[0] == '/');
|
2009-09-25 18:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-01 13:58:22 +02:00
|
|
|
InotifyQueue::Enqueue(const char *uri_utf8) noexcept
|
2009-09-25 18:32:00 +02:00
|
|
|
{
|
2017-08-29 16:38:50 +02:00
|
|
|
delay_event.Schedule(INOTIFY_UPDATE_DELAY);
|
2013-01-14 10:10:21 +01:00
|
|
|
|
|
|
|
for (auto i = queue.begin(), end = queue.end(); i != end;) {
|
2013-01-03 09:40:37 +01:00
|
|
|
const char *current_uri = i->c_str();
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2013-01-03 09:40:37 +01:00
|
|
|
if (path_in(uri_utf8, current_uri))
|
2009-09-25 18:32:00 +02:00
|
|
|
/* 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 */
|
2013-01-14 10:10:21 +01:00
|
|
|
i = queue.erase(i);
|
2009-09-25 18:32:00 +02:00
|
|
|
else
|
2013-01-03 09:40:37 +01:00
|
|
|
++i;
|
2009-09-25 18:32:00 +02:00
|
|
|
}
|
|
|
|
|
2013-01-14 10:10:21 +01:00
|
|
|
queue.emplace_back(uri_utf8);
|
2009-09-25 18:32:00 +02:00
|
|
|
}
|