2009-09-24 21:39:46 +02:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2009-09-24 21:39:46 +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.
|
|
|
|
*/
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
#include "Walk.hxx"
|
2013-01-02 19:22:15 +01:00
|
|
|
#include "UpdateIO.hxx"
|
2014-01-29 20:16:43 +01:00
|
|
|
#include "Editor.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "UpdateDomain.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/DatabaseLock.hxx"
|
2014-01-29 18:05:33 +01:00
|
|
|
#include "db/Uri.hxx"
|
2014-02-26 09:17:41 +01:00
|
|
|
#include "db/plugins/simple/Directory.hxx"
|
|
|
|
#include "db/plugins/simple/Song.hxx"
|
2014-02-05 17:03:43 +01:00
|
|
|
#include "storage/StorageInterface.hxx"
|
2013-01-03 09:51:35 +01:00
|
|
|
#include "ExcludeList.hxx"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2013-10-17 23:23:25 +02:00
|
|
|
#include "fs/Traits.hxx"
|
2013-02-02 15:23:57 +01:00
|
|
|
#include "fs/FileSystem.hxx"
|
2014-01-29 18:14:57 +01:00
|
|
|
#include "storage/FileInfo.hxx"
|
2018-07-06 17:53:57 +02:00
|
|
|
#include "input/InputStream.hxx"
|
|
|
|
#include "input/Error.hxx"
|
2014-01-07 23:33:46 +01:00
|
|
|
#include "util/Alloc.hxx"
|
2015-11-06 09:37:07 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2019-08-09 15:54:13 +02:00
|
|
|
#include "util/UriExtract.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2020-03-12 23:51:16 +01:00
|
|
|
#include <cerrno>
|
2019-07-05 09:59:00 +02:00
|
|
|
#include <exception>
|
2016-09-16 17:05:28 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2009-09-24 21:39:46 +02:00
|
|
|
#include <string.h>
|
2014-01-07 23:33:46 +01:00
|
|
|
#include <stdlib.h>
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2018-07-17 22:39:52 +02:00
|
|
|
UpdateWalk::UpdateWalk(const UpdateConfig &_config,
|
|
|
|
EventLoop &_loop, DatabaseListener &_listener,
|
2018-01-21 11:22:17 +01:00
|
|
|
Storage &_storage) noexcept
|
2018-07-17 22:39:52 +02:00
|
|
|
:config(_config), cancel(false),
|
2014-02-27 18:04:24 +01:00
|
|
|
storage(_storage),
|
2014-01-29 18:14:57 +01:00
|
|
|
editor(_loop, _listener)
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-02-28 20:50:15 +01:00
|
|
|
directory_set_stat(Directory &dir, const StorageFileInfo &info)
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2014-01-29 18:14:57 +01:00
|
|
|
dir.inode = info.inode;
|
|
|
|
dir.device = info.device;
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
inline void
|
|
|
|
UpdateWalk::RemoveExcludedFromDirectory(Directory &directory,
|
2018-01-21 11:22:17 +01:00
|
|
|
const ExcludeList &exclude_list) noexcept
|
2009-10-16 18:11:43 +02:00
|
|
|
{
|
2015-12-15 23:27:05 +01:00
|
|
|
const ScopeDatabaseLock protect;
|
2012-01-31 22:12:14 +01:00
|
|
|
|
2014-06-10 21:15:40 +02:00
|
|
|
directory.ForEachChildSafe([&](Directory &child){
|
|
|
|
const auto name_fs =
|
|
|
|
AllocatedPath::FromUTF8(child.GetName());
|
|
|
|
|
|
|
|
if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
|
|
|
|
editor.DeleteDirectory(&child);
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
directory.ForEachSongSafe([&](Song &song){
|
2014-08-12 15:38:59 +02:00
|
|
|
assert(&song.parent == &directory);
|
2014-06-10 21:15:40 +02:00
|
|
|
|
2020-04-03 15:55:19 +02:00
|
|
|
const auto name_fs = AllocatedPath::FromUTF8(song.filename);
|
2014-06-10 21:15:40 +02:00
|
|
|
if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
|
|
|
|
editor.DeleteSong(directory, &song);
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
});
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
inline void
|
2018-01-21 11:22:17 +01:00
|
|
|
UpdateWalk::PurgeDeletedFromDirectory(Directory &directory) noexcept
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2014-06-10 21:15:40 +02:00
|
|
|
directory.ForEachChildSafe([&](Directory &child){
|
2017-11-25 12:24:27 +01:00
|
|
|
if (child.IsMount() || DirectoryExists(storage, child))
|
2014-06-10 21:15:40 +02:00
|
|
|
return;
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-06-10 21:15:40 +02:00
|
|
|
editor.LockDeleteDirectory(&child);
|
2012-01-31 22:12:14 +01:00
|
|
|
|
2014-06-10 21:15:40 +02:00
|
|
|
modified = true;
|
|
|
|
});
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-06-10 21:15:40 +02:00
|
|
|
directory.ForEachSongSafe([&](Song &song){
|
|
|
|
if (!directory_child_is_regular(storage, directory,
|
2020-04-02 19:48:22 +02:00
|
|
|
song.filename)) {
|
2014-06-10 21:15:40 +02:00
|
|
|
editor.LockDeleteSong(directory, &song);
|
2012-01-31 22:12:14 +01:00
|
|
|
|
2014-06-10 21:15:40 +02:00
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
});
|
2010-07-21 08:58:15 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
for (auto i = directory.playlists.begin(),
|
|
|
|
end = directory.playlists.end();
|
2013-01-02 22:16:52 +01:00
|
|
|
i != end;) {
|
2020-04-02 19:48:22 +02:00
|
|
|
if (!directory_child_is_regular(storage, directory, i->name)) {
|
2015-12-15 23:27:05 +01:00
|
|
|
const ScopeDatabaseLock protect;
|
2013-10-19 18:48:38 +02:00
|
|
|
i = directory.playlists.erase(i);
|
2013-01-02 22:16:52 +01:00
|
|
|
} else
|
|
|
|
++i;
|
2010-07-21 08:58:15 +02:00
|
|
|
}
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifndef _WIN32
|
2014-01-29 18:14:57 +01:00
|
|
|
static bool
|
2018-01-21 11:22:17 +01:00
|
|
|
update_directory_stat(Storage &storage, Directory &directory) noexcept
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2015-02-28 20:50:15 +01:00
|
|
|
StorageFileInfo info;
|
2014-01-29 18:14:57 +01:00
|
|
|
if (!GetInfo(storage, directory.GetPath(), info))
|
|
|
|
return false;
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
directory_set_stat(directory, info);
|
|
|
|
return true;
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
2010-05-19 07:48:52 +02:00
|
|
|
#endif
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-02-09 10:45:38 +01:00
|
|
|
/**
|
|
|
|
* Check the ancestors of the given #Directory and see if there's one
|
|
|
|
* with the same device/inode number, building a loop.
|
|
|
|
*
|
|
|
|
* @return 1 if a loop was found, 0 if not, -1 on I/O error
|
|
|
|
*/
|
2009-09-24 21:39:46 +02:00
|
|
|
static int
|
2014-02-09 10:44:19 +01:00
|
|
|
FindAncestorLoop(Storage &storage, Directory *parent,
|
2018-01-21 11:22:17 +01:00
|
|
|
unsigned inode, unsigned device) noexcept
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifndef _WIN32
|
2014-02-09 10:46:57 +01:00
|
|
|
if (device == 0 && inode == 0)
|
|
|
|
/* can't detect loops if the Storage does not support
|
|
|
|
these numbers */
|
|
|
|
return 0;
|
|
|
|
|
2009-09-24 21:39:46 +02:00
|
|
|
while (parent) {
|
2014-06-16 19:37:33 +02:00
|
|
|
if (parent->device == 0 && parent->inode == 0 &&
|
2014-01-29 18:14:57 +01:00
|
|
|
!update_directory_stat(storage, *parent))
|
2009-09-24 21:39:46 +02:00
|
|
|
return -1;
|
2012-06-13 20:48:30 +02:00
|
|
|
|
2009-09-24 21:39:46 +02:00
|
|
|
if (parent->inode == inode && parent->device == device) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogDebug(update_domain, "recursive directory found");
|
2009-09-24 21:39:46 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2012-06-13 20:48:30 +02:00
|
|
|
|
2009-09-24 21:39:46 +02:00
|
|
|
parent = parent->parent;
|
|
|
|
}
|
2010-05-19 07:48:52 +02:00
|
|
|
#else
|
2014-01-29 18:14:57 +01:00
|
|
|
(void)storage;
|
2010-05-19 07:48:52 +02:00
|
|
|
(void)parent;
|
|
|
|
(void)inode;
|
|
|
|
(void)device;
|
|
|
|
#endif
|
2009-09-24 21:39:46 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
inline bool
|
|
|
|
UpdateWalk::UpdateRegularFile(Directory &directory,
|
2018-01-21 11:22:17 +01:00
|
|
|
const char *name,
|
|
|
|
const StorageFileInfo &info) noexcept
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
|
|
|
const char *suffix = uri_get_suffix(name);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (suffix == nullptr)
|
2012-06-13 21:33:23 +02:00
|
|
|
return false;
|
2010-07-21 08:58:15 +02:00
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
return UpdateSongFile(directory, name, suffix, info) ||
|
|
|
|
UpdateArchiveFile(directory, name, suffix, info) ||
|
|
|
|
UpdatePlaylistFile(directory, name, suffix, info);
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
void
|
|
|
|
UpdateWalk::UpdateDirectoryChild(Directory &directory,
|
2015-09-29 19:39:07 +02:00
|
|
|
const ExcludeList &exclude_list,
|
2018-01-21 11:22:17 +01:00
|
|
|
const char *name, const StorageFileInfo &info) noexcept
|
2015-12-29 12:41:45 +01:00
|
|
|
try {
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(strchr(name, '/') == nullptr);
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
if (info.IsRegular()) {
|
|
|
|
UpdateRegularFile(directory, name, info);
|
|
|
|
} else if (info.IsDirectory()) {
|
2014-02-09 10:44:19 +01:00
|
|
|
if (FindAncestorLoop(storage, &directory,
|
2014-01-29 18:14:57 +01:00
|
|
|
info.inode, info.device))
|
2009-09-24 21:39:46 +02:00
|
|
|
return;
|
|
|
|
|
2015-12-15 23:27:05 +01:00
|
|
|
Directory *subdir;
|
|
|
|
{
|
|
|
|
const ScopeDatabaseLock protect;
|
|
|
|
subdir = directory.MakeChild(name);
|
|
|
|
}
|
2012-01-31 22:12:14 +01:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(&directory == subdir->parent);
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2015-09-29 19:39:07 +02:00
|
|
|
if (!UpdateDirectory(*subdir, exclude_list, info))
|
2014-01-31 22:17:49 +01:00
|
|
|
editor.LockDeleteDirectory(subdir);
|
2009-09-24 21:39:46 +02:00
|
|
|
} else {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(update_domain,
|
|
|
|
"%s is not a directory, archive or music", name);
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
2018-08-09 11:14:40 +02:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 09:02:02 +01:00
|
|
|
/* we don't look at files with newlines in their name */
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_pure
|
2014-01-29 18:14:57 +01:00
|
|
|
static bool
|
2017-05-08 14:44:49 +02:00
|
|
|
skip_path(const char *name_utf8) noexcept
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2014-01-29 18:14:57 +01:00
|
|
|
return strchr(name_utf8, '\n') != nullptr;
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_pure
|
2014-01-29 20:16:43 +01:00
|
|
|
bool
|
|
|
|
UpdateWalk::SkipSymlink(const Directory *directory,
|
2020-04-02 20:10:36 +02:00
|
|
|
std::string_view utf8_name) const noexcept
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifndef _WIN32
|
2014-01-29 18:14:57 +01:00
|
|
|
const auto path_fs = storage.MapChildFS(directory->GetPath(),
|
|
|
|
utf8_name);
|
2013-01-17 00:56:57 +01:00
|
|
|
if (path_fs.IsNull())
|
2014-01-29 18:14:57 +01:00
|
|
|
/* not a local file: don't skip */
|
|
|
|
return false;
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
const auto target = ReadLink(path_fs);
|
2013-05-05 10:58:07 +02:00
|
|
|
if (target.IsNull())
|
2009-09-24 21:39:46 +02:00
|
|
|
/* don't skip if this is not a symlink */
|
|
|
|
return errno != EINVAL;
|
|
|
|
|
2018-07-17 22:36:37 +02:00
|
|
|
if (!config.follow_inside_symlinks &&
|
|
|
|
!config.follow_outside_symlinks) {
|
2009-09-24 21:39:46 +02:00
|
|
|
/* ignore all symlinks */
|
|
|
|
return true;
|
2018-07-17 22:36:37 +02:00
|
|
|
} else if (config.follow_inside_symlinks &&
|
|
|
|
config.follow_outside_symlinks) {
|
2009-09-24 21:39:46 +02:00
|
|
|
/* consider all symlinks */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-21 14:15:01 +02:00
|
|
|
if (target.IsAbsolute()) {
|
2011-07-20 07:05:10 +02:00
|
|
|
/* if the symlink points to an absolute path, see if
|
|
|
|
that path is inside the music directory */
|
2016-04-21 14:15:01 +02:00
|
|
|
const auto target_utf8 = target.ToUTF8();
|
2014-02-07 19:01:06 +01:00
|
|
|
if (target_utf8.empty())
|
|
|
|
return true;
|
|
|
|
|
2020-04-03 16:22:39 +02:00
|
|
|
auto relative =
|
2014-02-07 19:01:06 +01:00
|
|
|
storage.MapToRelativeUTF8(target_utf8.c_str());
|
2020-04-03 16:22:39 +02:00
|
|
|
return relative.data() != nullptr
|
2018-07-17 22:36:37 +02:00
|
|
|
? !config.follow_inside_symlinks
|
|
|
|
: !config.follow_outside_symlinks;
|
2011-07-20 07:05:10 +02:00
|
|
|
}
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2016-04-21 14:15:01 +02:00
|
|
|
const char *p = target.c_str();
|
2009-09-24 21:39:46 +02:00
|
|
|
while (*p == '.') {
|
2013-12-04 22:53:43 +01:00
|
|
|
if (p[1] == '.' && PathTraitsFS::IsSeparator(p[2])) {
|
2009-09-24 21:39:46 +02:00
|
|
|
/* "../" moves to parent directory */
|
|
|
|
directory = directory->parent;
|
2013-10-19 18:19:03 +02:00
|
|
|
if (directory == nullptr) {
|
2009-09-24 21:39:46 +02:00
|
|
|
/* we have moved outside the music
|
|
|
|
directory - skip this symlink
|
|
|
|
if such symlinks are not allowed */
|
2018-07-17 22:36:37 +02:00
|
|
|
return !config.follow_outside_symlinks;
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
p += 3;
|
2013-12-04 22:53:43 +01:00
|
|
|
} else if (PathTraitsFS::IsSeparator(p[1]))
|
2009-09-24 21:39:46 +02:00
|
|
|
/* 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*/
|
2018-07-17 22:36:37 +02:00
|
|
|
return !config.follow_inside_symlinks;
|
2009-09-24 21:39:46 +02:00
|
|
|
#else
|
|
|
|
/* no symlink checking on WIN32 */
|
|
|
|
|
|
|
|
(void)directory;
|
|
|
|
(void)utf8_name;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
bool
|
2015-09-29 19:39:07 +02:00
|
|
|
UpdateWalk::UpdateDirectory(Directory &directory,
|
|
|
|
const ExcludeList &exclude_list,
|
2018-01-21 11:22:17 +01:00
|
|
|
const StorageFileInfo &info) noexcept
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2014-01-29 18:14:57 +01:00
|
|
|
assert(info.IsDirectory());
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
directory_set_stat(directory, info);
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2016-09-16 17:05:28 +02:00
|
|
|
std::unique_ptr<StorageDirectoryReader> reader;
|
|
|
|
|
|
|
|
try {
|
2018-01-21 11:04:30 +01:00
|
|
|
reader = storage.OpenDirectory(directory.GetPath());
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2009-09-24 21:39:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-29 19:39:07 +02:00
|
|
|
ExcludeList child_exclude_list(exclude_list);
|
2014-01-29 18:14:57 +01:00
|
|
|
|
2018-07-06 17:53:57 +02:00
|
|
|
try {
|
|
|
|
Mutex mutex;
|
2020-04-03 16:25:08 +02:00
|
|
|
auto is = InputStream::OpenReady(PathTraitsUTF8::Build(storage.MapUTF8(directory.GetPath()),
|
2018-07-06 17:53:57 +02:00
|
|
|
".mpdignore").c_str(),
|
2018-07-06 19:41:53 +02:00
|
|
|
mutex);
|
2018-07-06 17:53:57 +02:00
|
|
|
child_exclude_list.Load(std::move(is));
|
|
|
|
} catch (...) {
|
|
|
|
if (!IsFileNotFound(std::current_exception()))
|
|
|
|
LogError(std::current_exception());
|
2014-01-29 18:14:57 +01:00
|
|
|
}
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2015-09-29 19:39:07 +02:00
|
|
|
if (!child_exclude_list.IsEmpty())
|
|
|
|
RemoveExcludedFromDirectory(directory, child_exclude_list);
|
2009-10-16 18:11:43 +02:00
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
PurgeDeletedFromDirectory(directory);
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
const char *name_utf8;
|
2014-02-27 16:36:11 +01:00
|
|
|
while (!cancel && (name_utf8 = reader->Read()) != nullptr) {
|
2014-01-29 18:14:57 +01:00
|
|
|
if (skip_path(name_utf8))
|
2009-09-24 21:39:46 +02:00
|
|
|
continue;
|
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
{
|
|
|
|
const auto name_fs = AllocatedPath::FromUTF8(name_utf8);
|
2015-09-29 19:39:07 +02:00
|
|
|
if (name_fs.IsNull() || child_exclude_list.Check(name_fs))
|
2014-01-29 18:14:57 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SkipSymlink(&directory, name_utf8)) {
|
|
|
|
modified |= editor.DeleteNameIn(directory, name_utf8);
|
2009-10-31 18:23:56 +01:00
|
|
|
continue;
|
2014-01-29 18:14:57 +01:00
|
|
|
}
|
2009-10-31 18:23:56 +01:00
|
|
|
|
2015-02-28 20:50:15 +01:00
|
|
|
StorageFileInfo info2;
|
2014-01-29 18:14:57 +01:00
|
|
|
if (!GetInfo(*reader, info2)) {
|
|
|
|
modified |= editor.DeleteNameIn(directory, name_utf8);
|
2009-09-24 21:39:46 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-09-29 19:39:07 +02:00
|
|
|
UpdateDirectoryChild(directory, child_exclude_list, name_utf8, info2);
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
directory.mtime = info.mtime;
|
2009-09-24 21:39:46 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
inline Directory *
|
2014-01-29 18:14:57 +01:00
|
|
|
UpdateWalk::DirectoryMakeChildChecked(Directory &parent,
|
|
|
|
const char *uri_utf8,
|
2020-04-02 20:10:36 +02:00
|
|
|
std::string_view name_utf8) noexcept
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2015-12-15 23:27:05 +01:00
|
|
|
Directory *directory;
|
|
|
|
{
|
|
|
|
const ScopeDatabaseLock protect;
|
|
|
|
directory = parent.FindChild(name_utf8);
|
|
|
|
}
|
2012-06-13 20:48:30 +02:00
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
if (directory != nullptr) {
|
|
|
|
if (directory->IsMount())
|
|
|
|
directory = nullptr;
|
|
|
|
|
2009-09-24 21:39:46 +02:00
|
|
|
return directory;
|
2014-02-26 08:39:44 +01:00
|
|
|
}
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2015-02-28 20:50:15 +01:00
|
|
|
StorageFileInfo info;
|
2014-01-29 18:14:57 +01:00
|
|
|
if (!GetInfo(storage, uri_utf8, info) ||
|
2014-02-09 10:44:19 +01:00
|
|
|
FindAncestorLoop(storage, &parent, info.inode, info.device))
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
if (SkipSymlink(&parent, name_utf8))
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2012-04-04 08:56:45 +02:00
|
|
|
|
2009-09-24 21:39:46 +02:00
|
|
|
/* if we're adding directory paths, make sure to delete filenames
|
|
|
|
with potentially the same name */
|
2015-12-15 23:27:05 +01:00
|
|
|
{
|
|
|
|
const ScopeDatabaseLock protect;
|
|
|
|
Song *conflicting = parent.FindSong(name_utf8);
|
|
|
|
if (conflicting)
|
|
|
|
editor.DeleteSong(parent, conflicting);
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2015-12-15 23:27:05 +01:00
|
|
|
directory = parent.CreateChild(name_utf8);
|
|
|
|
}
|
2012-01-31 22:12:14 +01:00
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
directory_set_stat(*directory, info);
|
2009-09-24 21:39:46 +02:00
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
inline Directory *
|
2018-01-21 11:22:17 +01:00
|
|
|
UpdateWalk::DirectoryMakeUriParentChecked(Directory &root,
|
|
|
|
const char *uri) noexcept
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2014-02-04 08:57:40 +01:00
|
|
|
Directory *directory = &root;
|
2014-01-07 23:33:46 +01:00
|
|
|
char *duplicated = xstrdup(uri);
|
2012-01-24 19:07:11 +01:00
|
|
|
char *name_utf8 = duplicated, *slash;
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
while ((slash = strchr(name_utf8, '/')) != nullptr) {
|
2009-09-24 21:39:46 +02:00
|
|
|
*slash = 0;
|
|
|
|
|
2015-11-06 09:37:07 +01:00
|
|
|
if (StringIsEmpty(name_utf8))
|
2012-01-24 19:07:11 +01:00
|
|
|
continue;
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
directory = DirectoryMakeChildChecked(*directory,
|
2014-01-29 18:14:57 +01:00
|
|
|
duplicated,
|
2014-01-29 20:16:43 +01:00
|
|
|
name_utf8);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (directory == nullptr)
|
2009-09-24 21:39:46 +02:00
|
|
|
break;
|
|
|
|
|
2012-01-24 19:07:11 +01:00
|
|
|
name_utf8 = slash + 1;
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
2014-01-07 23:33:46 +01:00
|
|
|
free(duplicated);
|
2009-09-24 21:39:46 +02:00
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
inline void
|
2018-01-21 11:22:17 +01:00
|
|
|
UpdateWalk::UpdateUri(Directory &root, const char *uri) noexcept
|
2015-12-29 12:41:45 +01:00
|
|
|
try {
|
2014-02-04 08:57:40 +01:00
|
|
|
Directory *parent = DirectoryMakeUriParentChecked(root, uri);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (parent == nullptr)
|
2009-09-24 21:39:46 +02:00
|
|
|
return;
|
|
|
|
|
2013-12-04 22:53:43 +01:00
|
|
|
const char *name = PathTraitsUTF8::GetBase(uri);
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
if (SkipSymlink(parent, name)) {
|
2014-01-29 20:16:43 +01:00
|
|
|
modified |= editor.DeleteNameIn(*parent, name);
|
2014-01-29 18:14:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-28 20:50:15 +01:00
|
|
|
StorageFileInfo info;
|
2014-01-29 18:14:57 +01:00
|
|
|
if (!GetInfo(storage, uri, info)) {
|
|
|
|
modified |= editor.DeleteNameIn(*parent, name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-29 19:39:07 +02:00
|
|
|
ExcludeList exclude_list;
|
|
|
|
|
|
|
|
UpdateDirectoryChild(*parent, exclude_list, name, info);
|
2018-08-09 11:14:40 +02:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-01-21 11:22:17 +01:00
|
|
|
UpdateWalk::Walk(Directory &root, const char *path, bool discard) noexcept
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2009-09-24 21:55:40 +02:00
|
|
|
walk_discard = discard;
|
2009-09-24 21:39:46 +02:00
|
|
|
modified = false;
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (path != nullptr && !isRootDirectory(path)) {
|
2014-02-04 08:57:40 +01:00
|
|
|
UpdateUri(root, path);
|
2009-09-24 21:39:46 +02:00
|
|
|
} else {
|
2015-02-28 20:50:15 +01:00
|
|
|
StorageFileInfo info;
|
2014-01-29 18:14:57 +01:00
|
|
|
if (!GetInfo(storage, "", info))
|
|
|
|
return false;
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2019-10-07 12:23:58 +02:00
|
|
|
if (!info.IsDirectory()) {
|
|
|
|
FormatError(update_domain, "Not a directory: %s",
|
|
|
|
storage.MapUTF8("").c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-29 19:39:07 +02:00
|
|
|
ExcludeList exclude_list;
|
|
|
|
|
|
|
|
UpdateDirectory(root, exclude_list, info);
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|