From 9ae7f186bc43749383594807b1d751b5389161e7 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 29 Jan 2014 18:14:57 +0100 Subject: LocalStorage: new API abstracting filesystem walk Prepare to make this a new plugin API, for example to use a SMB share for the music_directory. --- src/db/update/UpdateIO.cxx | 91 +++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 49 deletions(-) (limited to 'src/db/update/UpdateIO.cxx') diff --git a/src/db/update/UpdateIO.cxx b/src/db/update/UpdateIO.cxx index f91caf359..58b1fe296 100644 --- a/src/db/update/UpdateIO.cxx +++ b/src/db/update/UpdateIO.cxx @@ -21,91 +21,84 @@ #include "UpdateIO.hxx" #include "UpdateDomain.hxx" #include "db/Directory.hxx" -#include "Mapper.hxx" -#include "fs/AllocatedPath.hxx" +#include "storage/FileInfo.hxx" +#include "storage/LocalStorage.hxx" +#include "fs/Traits.hxx" #include "fs/FileSystem.hxx" +#include "util/Error.hxx" #include "Log.hxx" #include #include -int -stat_directory(const Directory &directory, struct stat *st) +bool +GetInfo(LocalStorage &storage, const char *uri_utf8, FileInfo &info) { - 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; + Error error; + bool success = storage.GetInfo(uri_utf8, true, info, error); + if (!success) + LogError(error); + return success; } -int -stat_directory_child(const Directory &parent, const char *name, - struct stat *st) +bool +GetInfo(LocalDirectoryReader &reader, FileInfo &info) { - 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; + Error error; + bool success = reader.GetInfo(true, info, error); + if (!success) + LogError(error); + return success; } bool -directory_exists(const Directory &directory) +DirectoryExists(LocalStorage &storage, const Directory &directory) { - const auto path_fs = map_directory_fs(directory); - if (path_fs.IsNull()) - /* invalid path: cannot exist */ + FileInfo info; + if (!storage.GetInfo(directory.GetPath(), true, info, IgnoreError())) return false; return directory.device == DEVICE_INARCHIVE || directory.device == DEVICE_CONTAINER - ? FileExists(path_fs) - : DirectoryExists(path_fs); + ? info.IsRegular() + : info.IsDirectory(); +} + +static bool +GetDirectoryChildInfo(LocalStorage &storage, const Directory &directory, + const char *name_utf8, FileInfo &info, Error &error) +{ + const auto uri_utf8 = PathTraitsUTF8::Build(directory.GetPath(), + name_utf8); + return storage.GetInfo(uri_utf8.c_str(), true, info, error); } bool -directory_child_is_regular(const Directory &directory, +directory_child_is_regular(LocalStorage &storage, 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); + FileInfo info; + return GetDirectoryChildInfo(storage, directory, name_utf8, info, + IgnoreError()) && + info.IsRegular(); } bool -directory_child_access(const Directory &directory, +directory_child_access(LocalStorage &storage, const Directory &directory, const char *name, int mode) { #ifdef WIN32 /* CheckAccess() is useless on WIN32 */ + (void)storage; (void)directory; (void)name; (void)mode; return true; #else - const auto path = map_directory_child_fs(directory, name); + const auto path = storage.MapChildFS(directory.GetPath(), name); if (path.IsNull()) - /* something went wrong, but that isn't a permission - problem */ + /* does not point to local file: silently ignore the + check */ return true; return CheckAccess(path, mode) || errno != EACCES; -- cgit v1.2.3