2009-09-24 21:39:46 +02:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 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.
|
|
|
|
*/
|
|
|
|
|
2009-11-11 14:13:24 +01:00
|
|
|
#include "config.h" /* must be first for large file support */
|
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-27 11:05:21 +01:00
|
|
|
#include "db/PlaylistVector.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"
|
2014-01-23 23:30:12 +01:00
|
|
|
#include "playlist/PlaylistRegistry.hxx"
|
2013-01-03 09:51:35 +01:00
|
|
|
#include "ExcludeList.hxx"
|
2014-01-24 00:20:01 +01:00
|
|
|
#include "config/ConfigGlobal.hxx"
|
|
|
|
#include "config/ConfigOption.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-02-07 19:01:06 +01:00
|
|
|
#include "fs/Charset.hxx"
|
2014-01-29 18:14:57 +01:00
|
|
|
#include "storage/FileInfo.hxx"
|
2014-01-07 23:33:46 +01:00
|
|
|
#include "util/Alloc.hxx"
|
2013-04-08 23:30:21 +02:00
|
|
|
#include "util/UriUtil.hxx"
|
2014-01-29 18:14:57 +01:00
|
|
|
#include "util/Error.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2009-09-24 21:39:46 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
2014-01-07 23:33:46 +01:00
|
|
|
#include <stdlib.h>
|
2009-09-24 21:39:46 +02:00
|
|
|
#include <errno.h>
|
2014-02-06 23:08:24 +01:00
|
|
|
#include <memory>
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-02-05 17:50:04 +01:00
|
|
|
UpdateWalk::UpdateWalk(EventLoop &_loop, DatabaseListener &_listener,
|
2014-02-05 17:03:43 +01:00
|
|
|
Storage &_storage)
|
2014-02-27 18:04:24 +01:00
|
|
|
:cancel(false),
|
|
|
|
storage(_storage),
|
2014-01-29 18:14:57 +01:00
|
|
|
editor(_loop, _listener)
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-29 18:14:57 +01:00
|
|
|
directory_set_stat(Directory &dir, const FileInfo &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;
|
2013-10-19 18:48:38 +02:00
|
|
|
dir.have_stat = true;
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
inline void
|
|
|
|
UpdateWalk::RemoveExcludedFromDirectory(Directory &directory,
|
|
|
|
const ExcludeList &exclude_list)
|
2009-10-16 18:11:43 +02:00
|
|
|
{
|
2012-01-31 22:12:14 +01:00
|
|
|
db_lock();
|
|
|
|
|
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){
|
|
|
|
assert(song.parent == &directory);
|
|
|
|
|
|
|
|
const auto name_fs = AllocatedPath::FromUTF8(song.uri);
|
|
|
|
if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
|
|
|
|
editor.DeleteSong(directory, &song);
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
});
|
2012-01-31 22:12:14 +01:00
|
|
|
|
|
|
|
db_unlock();
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
inline void
|
|
|
|
UpdateWalk::PurgeDeletedFromDirectory(Directory &directory)
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2014-06-10 21:15:40 +02:00
|
|
|
directory.ForEachChildSafe([&](Directory &child){
|
|
|
|
if (DirectoryExists(storage, child))
|
|
|
|
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,
|
|
|
|
song.uri)) {
|
|
|
|
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;) {
|
2014-01-29 18:14:57 +01:00
|
|
|
if (!directory_child_is_regular(storage, directory,
|
|
|
|
i->name.c_str())) {
|
2012-02-13 19:25:03 +01:00
|
|
|
db_lock();
|
2013-10-19 18:48:38 +02:00
|
|
|
i = directory.playlists.erase(i);
|
2012-02-13 19:25:03 +01:00
|
|
|
db_unlock();
|
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
|
|
|
}
|
|
|
|
|
2013-10-15 08:33:44 +02:00
|
|
|
#ifndef WIN32
|
2014-01-29 18:14:57 +01:00
|
|
|
static bool
|
2014-02-05 17:03:43 +01:00
|
|
|
update_directory_stat(Storage &storage, Directory &directory)
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2014-01-29 18:14:57 +01:00
|
|
|
FileInfo info;
|
|
|
|
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,
|
|
|
|
unsigned inode, unsigned device)
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2013-10-15 08:33:44 +02: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-01-29 18:14:57 +01:00
|
|
|
if (!parent->have_stat &&
|
|
|
|
!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::UpdatePlaylistFile(Directory &directory,
|
|
|
|
const char *name, const char *suffix,
|
2014-01-29 18:14:57 +01:00
|
|
|
const FileInfo &info)
|
2012-06-13 21:33:23 +02:00
|
|
|
{
|
|
|
|
if (!playlist_suffix_supported(suffix))
|
|
|
|
return false;
|
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
PlaylistInfo pi(name, info.mtime);
|
2013-01-02 22:04:03 +01:00
|
|
|
|
2012-06-13 21:33:23 +02:00
|
|
|
db_lock();
|
2013-10-19 18:48:38 +02:00
|
|
|
if (directory.playlists.UpdateOrInsert(std::move(pi)))
|
2012-06-13 21:33:23 +02:00
|
|
|
modified = true;
|
|
|
|
db_unlock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
inline bool
|
|
|
|
UpdateWalk::UpdateRegularFile(Directory &directory,
|
2014-01-29 18:14:57 +01:00
|
|
|
const char *name, const FileInfo &info)
|
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,
|
2014-01-29 18:14:57 +01:00
|
|
|
const char *name, const FileInfo &info)
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
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;
|
|
|
|
|
2012-01-31 22:12:14 +01:00
|
|
|
db_lock();
|
2013-10-19 18:48:38 +02:00
|
|
|
Directory *subdir = directory.MakeChild(name);
|
2012-01-31 22:12:14 +01:00
|
|
|
db_unlock();
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(&directory == subdir->parent);
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
if (!UpdateDirectory(*subdir, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we don't look at "." / ".." nor 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
|
|
|
|
skip_path(const char *name_utf8)
|
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,
|
|
|
|
const char *utf8_name) const
|
2009-09-24 21:39:46 +02: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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-05 10:58:07 +02:00
|
|
|
const char *target_str = target.c_str();
|
|
|
|
|
2013-12-04 22:53:43 +01:00
|
|
|
if (PathTraitsFS::IsAbsolute(target_str)) {
|
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 */
|
2014-02-07 19:01:06 +01:00
|
|
|
const auto target_utf8 = PathToUTF8(target_str);
|
|
|
|
if (target_utf8.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const char *relative =
|
|
|
|
storage.MapToRelativeUTF8(target_utf8.c_str());
|
|
|
|
return relative != nullptr
|
2011-07-20 07:05:10 +02:00
|
|
|
? !follow_inside_symlinks
|
|
|
|
: !follow_outside_symlinks;
|
|
|
|
}
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2013-05-05 10:58:07 +02:00
|
|
|
const char *p = target_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 */
|
|
|
|
return !follow_outside_symlinks;
|
|
|
|
}
|
|
|
|
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*/
|
|
|
|
return !follow_inside_symlinks;
|
|
|
|
#else
|
|
|
|
/* no symlink checking on WIN32 */
|
|
|
|
|
|
|
|
(void)directory;
|
|
|
|
(void)utf8_name;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
bool
|
2014-01-29 18:14:57 +01:00
|
|
|
UpdateWalk::UpdateDirectory(Directory &directory, const FileInfo &info)
|
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
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
Error error;
|
2014-02-06 23:08:24 +01:00
|
|
|
const std::auto_ptr<StorageDirectoryReader> reader(storage.OpenDirectory(directory.GetPath(), error));
|
|
|
|
if (reader.get() == nullptr) {
|
2014-01-29 18:14:57 +01:00
|
|
|
LogError(error);
|
2009-09-24 21:39:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-03 10:01:34 +01:00
|
|
|
ExcludeList exclude_list;
|
2014-01-29 18:14:57 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
const auto exclude_path_fs =
|
|
|
|
storage.MapChildFS(directory.GetPath(), ".mpdignore");
|
|
|
|
if (!exclude_path_fs.IsNull())
|
|
|
|
exclude_list.LoadFile(exclude_path_fs);
|
|
|
|
}
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2013-01-03 10:01:34 +01:00
|
|
|
if (!exclude_list.IsEmpty())
|
2014-01-29 20:16:43 +01:00
|
|
|
RemoveExcludedFromDirectory(directory, 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);
|
|
|
|
if (name_fs.IsNull() || exclude_list.Check(name_fs))
|
|
|
|
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
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
FileInfo info2;
|
|
|
|
if (!GetInfo(*reader, info2)) {
|
|
|
|
modified |= editor.DeleteNameIn(directory, name_utf8);
|
2009-09-24 21:39:46 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
UpdateDirectoryChild(directory, 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,
|
|
|
|
const char *name_utf8)
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
2012-01-31 22:12:14 +01:00
|
|
|
db_lock();
|
2013-10-19 18:48:38 +02:00
|
|
|
Directory *directory = parent.FindChild(name_utf8);
|
2012-01-31 22:12:14 +01:00
|
|
|
db_unlock();
|
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
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
FileInfo info;
|
|
|
|
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 */
|
2012-01-31 22:12:14 +01:00
|
|
|
db_lock();
|
2013-10-19 18:48:38 +02:00
|
|
|
Song *conflicting = parent.FindSong(name_utf8);
|
2009-09-24 21:39:46 +02:00
|
|
|
if (conflicting)
|
2014-01-29 20:16:43 +01:00
|
|
|
editor.DeleteSong(parent, conflicting);
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
directory = parent.CreateChild(name_utf8);
|
2012-01-31 22:12:14 +01:00
|
|
|
db_unlock();
|
|
|
|
|
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 *
|
2014-02-04 08:57:40 +01:00
|
|
|
UpdateWalk::DirectoryMakeUriParentChecked(Directory &root, const char *uri)
|
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;
|
|
|
|
|
2012-01-24 19:07:11 +01:00
|
|
|
if (*name_utf8 == 0)
|
|
|
|
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
|
2014-02-04 08:57:40 +01:00
|
|
|
UpdateWalk::UpdateUri(Directory &root, const char *uri)
|
2009-09-24 21:39:46 +02:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo info;
|
|
|
|
if (!GetInfo(storage, uri, info)) {
|
|
|
|
modified |= editor.DeleteNameIn(*parent, name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateDirectoryChild(*parent, name, info);
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-02-04 08:57:40 +01:00
|
|
|
UpdateWalk::Walk(Directory &root, const char *path, bool discard)
|
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 {
|
2014-01-29 18:14:57 +01:00
|
|
|
FileInfo info;
|
|
|
|
if (!GetInfo(storage, "", info))
|
|
|
|
return false;
|
2009-09-24 21:39:46 +02:00
|
|
|
|
2014-01-29 18:14:57 +01:00
|
|
|
UpdateDirectory(root, info);
|
2009-09-24 21:39:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|