fs/Path: add operator/(Path,Path)

Modeled after std::filesystem::operator/() from C++17.
This commit is contained in:
Max Kellermann 2018-07-17 17:01:02 +02:00
parent 79e89eb23b
commit 99d5b61698
14 changed files with 40 additions and 32 deletions

View File

@ -310,7 +310,7 @@ bool ConfigLoader::TryFile(const AllocatedPath &base_path, Path path)
{
if (base_path.IsNull())
return false;
auto full_path = AllocatedPath::Build(base_path, path);
auto full_path = base_path / path;
return TryFile(full_path);
}

View File

@ -260,7 +260,7 @@ glue_state_file_init()
if (cache_dir.IsNull())
return;
path_fs = AllocatedPath::Build(cache_dir, "state");
path_fs = cache_dir / Path::FromFS("state");
#else
return;
#endif
@ -486,7 +486,7 @@ try {
const auto sdcard = Environment::getExternalStorageDirectory();
if (!sdcard.IsNull()) {
const auto config_path =
AllocatedPath::Build(sdcard, "mpd.conf");
sdcard / Path::FromFS("mpd.conf");
if (FileExists(config_path))
ReadConfigFile(config_path);
}

View File

@ -82,7 +82,7 @@ map_uri_fs(const char *uri) noexcept
if (uri_fs.IsNull())
return nullptr;
return AllocatedPath::Build(music_dir_fs, uri_fs);
return music_dir_fs / uri_fs;
}
std::string
@ -128,5 +128,5 @@ map_spl_utf8_to_fs(const char *name) noexcept
if (filename_fs.IsNull())
return nullptr;
return AllocatedPath::Build(playlist_dir_fs, filename_fs);
return playlist_dir_fs / filename_fs;
}

View File

@ -138,7 +138,7 @@ LoadPlaylistFileInfo(PlaylistInfo &info,
return false;
FileInfo fi;
if (!GetFileInfo(AllocatedPath::Build(parent_path_fs, name_fs), fi) ||
if (!GetFileInfo(parent_path_fs / name_fs, fi) ||
!fi.IsRegular())
return false;

View File

@ -81,8 +81,7 @@ handle_listfiles_local(Response &r, Path path_fs)
if (name_utf8.empty())
continue;
const AllocatedPath full_fs =
AllocatedPath::Build(path_fs, name_fs);
const auto full_fs = path_fs / name_fs;
FileInfo fi;
if (!GetFileInfo(full_fs, fi, false))
continue;

View File

@ -113,7 +113,7 @@ ParsePath(const char *path)
if (path2.IsNull())
return nullptr;
return AllocatedPath::Build(home, path2);
return home / path2;
} else if (!PathTraitsUTF8::IsAbsolute(path)) {
throw FormatRuntimeError("not an absolute path: %s", path);
} else {

View File

@ -53,8 +53,7 @@ CreateConfiguredDatabase(EventLoop &main_event_loop, EventLoop &io_event_loop,
if (cache_dir.IsNull())
return nullptr;
const auto db_file = AllocatedPath::Build(cache_dir,
PATH_LITERAL("mpd.db"));
const auto db_file = cache_dir / Path::FromFS(PATH_LITERAL("mpd.db"));
const auto db_file_utf8 = db_file.ToUTF8();
if (db_file_utf8.empty())
return nullptr;

View File

@ -428,8 +428,7 @@ SimpleDatabase::Mount(const char *local_uri, const char *storage_uri)
#ifndef ENABLE_ZLIB
constexpr bool compress = false;
#endif
auto db = new SimpleDatabase(AllocatedPath::Build(cache_path,
name_fs.c_str()),
auto db = new SimpleDatabase(cache_path / name_fs,
compress);
try {
db->Open();

View File

@ -141,7 +141,7 @@ WatchDirectory::GetUriFS() const noexcept
if (uri.IsNull())
return name;
return AllocatedPath::Build(uri, name);
return uri / name;
}
/* we don't look at "." / ".." nor files with newlines in their name */
@ -181,8 +181,8 @@ recursive_watch_subdirectories(WatchDirectory *directory,
if (skip_path(ent->d_name))
continue;
const auto child_path_fs =
AllocatedPath::Build(path_fs, ent->d_name);
const auto name_fs = Path::FromFS(ent->d_name);
const auto child_path_fs = path_fs / name_fs;
FileInfo fi;
try {
@ -211,7 +211,7 @@ recursive_watch_subdirectories(WatchDirectory *directory,
continue;
directory->children.emplace_front(directory,
AllocatedPath::FromFS(ent->d_name),
name_fs,
ret);
child = &directory->children.front();
@ -262,7 +262,7 @@ mpd_inotify_callback(int wd, unsigned mask,
const auto path_fs = uri_fs.IsNull()
? root
: AllocatedPath::Build(root, uri_fs.c_str());
: (root / uri_fs);
recursive_watch_subdirectories(directory, path_fs,
directory->GetDepth());

View File

@ -43,8 +43,7 @@ try {
#ifndef _WIN32
try {
const auto x = AllocatedPath::Build(path_fs,
PathTraitsFS::CURRENT_DIRECTORY);
const auto x = path_fs / Path::FromFS(PathTraitsFS::CURRENT_DIRECTORY);
const FileInfo fi2(x);
} catch (const std::system_error &e) {
if (IsAccessDenied(e))

View File

@ -1,5 +1,5 @@
/*
* Copyright 2003-2017 The Music Player Daemon Project
* Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
@ -169,4 +169,12 @@ public:
const_pointer_type GetSuffix() const noexcept;
};
/**
* Concatenate two path components using the directory separator.
*
* Wrapper for AllocatedPath::Build().
*/
AllocatedPath
operator/(Path a, Path b) noexcept;
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright 2003-2017 The Music Player Daemon Project
* Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
@ -26,3 +26,9 @@ Path::GetDirectoryName() const noexcept
{
return AllocatedPath::FromFS(PathTraitsFS::GetParent(c_str()));
}
AllocatedPath
operator/(Path a, Path b) noexcept
{
return AllocatedPath::Build(a, b);
}

View File

@ -183,16 +183,16 @@ ParseConfigLine(char *line, const char *dir_name, AllocatedPath &result_dir)
*line_end = 0;
// build the result path
const char *path = line;
const auto path_fs = Path::FromFS(line);
AllocatedPath result = nullptr;
if (home_relative) {
auto home = GetHomeDir();
if (home.IsNull())
return true;
result = AllocatedPath::Build(home, path);
result = home / path_fs;
} else {
result = AllocatedPath::FromFS(path);
result = AllocatedPath(path_fs);
}
if (IsValidDir(result.c_str())) {
@ -209,9 +209,8 @@ try {
auto config_dir = GetUserConfigDir();
if (config_dir.IsNull())
return result;
auto dirs_file = AllocatedPath::Build(config_dir, "user-dirs.dirs");
TextFile input(dirs_file);
TextFile input(config_dir / Path::FromFS("user-dirs.dirs"));
char *line;
while ((line = input.ReadLine()) != nullptr)
if (ParseConfigLine(line, name, result))
@ -237,7 +236,7 @@ GetUserConfigDir() noexcept
// Check for $HOME/.config
auto home = GetHomeDir();
if (!home.IsNull()) {
AllocatedPath fallback = AllocatedPath::Build(home, ".config");
auto fallback = home / Path::FromFS(".config");
if (IsValidDir(fallback.c_str()))
return fallback;
}
@ -274,7 +273,7 @@ GetUserCacheDir() noexcept
// Check for $HOME/.cache
auto home = GetHomeDir();
if (!home.IsNull()) {
AllocatedPath fallback = AllocatedPath::Build(home, ".cache");
auto fallback = home / Path::FromFS(".cache");
if (IsValidDir(fallback.c_str()))
return fallback;
}

View File

@ -114,8 +114,7 @@ LocalStorage::MapFSOrThrow(const char *uri_utf8) const
if (StringIsEmpty(uri_utf8))
return base_fs;
return AllocatedPath::Build(base_fs,
AllocatedPath::FromUTF8Throw(uri_utf8));
return base_fs / AllocatedPath::FromUTF8Throw(uri_utf8);
}
AllocatedPath
@ -176,7 +175,7 @@ LocalDirectoryReader::Read() noexcept
StorageFileInfo
LocalDirectoryReader::GetInfo(bool follow)
{
return Stat(AllocatedPath::Build(base_fs, reader.GetEntry()), follow);
return Stat(base_fs / reader.GetEntry(), follow);
}
std::unique_ptr<Storage>