fs/Path: add operator/(Path,Path)
Modeled after std::filesystem::operator/() from C++17.
This commit is contained in:
parent
79e89eb23b
commit
99d5b61698
@ -310,7 +310,7 @@ bool ConfigLoader::TryFile(const AllocatedPath &base_path, Path path)
|
|||||||
{
|
{
|
||||||
if (base_path.IsNull())
|
if (base_path.IsNull())
|
||||||
return false;
|
return false;
|
||||||
auto full_path = AllocatedPath::Build(base_path, path);
|
auto full_path = base_path / path;
|
||||||
return TryFile(full_path);
|
return TryFile(full_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,7 +260,7 @@ glue_state_file_init()
|
|||||||
if (cache_dir.IsNull())
|
if (cache_dir.IsNull())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
path_fs = AllocatedPath::Build(cache_dir, "state");
|
path_fs = cache_dir / Path::FromFS("state");
|
||||||
#else
|
#else
|
||||||
return;
|
return;
|
||||||
#endif
|
#endif
|
||||||
@ -486,7 +486,7 @@ try {
|
|||||||
const auto sdcard = Environment::getExternalStorageDirectory();
|
const auto sdcard = Environment::getExternalStorageDirectory();
|
||||||
if (!sdcard.IsNull()) {
|
if (!sdcard.IsNull()) {
|
||||||
const auto config_path =
|
const auto config_path =
|
||||||
AllocatedPath::Build(sdcard, "mpd.conf");
|
sdcard / Path::FromFS("mpd.conf");
|
||||||
if (FileExists(config_path))
|
if (FileExists(config_path))
|
||||||
ReadConfigFile(config_path);
|
ReadConfigFile(config_path);
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,7 @@ map_uri_fs(const char *uri) noexcept
|
|||||||
if (uri_fs.IsNull())
|
if (uri_fs.IsNull())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
return AllocatedPath::Build(music_dir_fs, uri_fs);
|
return music_dir_fs / uri_fs;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
@ -128,5 +128,5 @@ map_spl_utf8_to_fs(const char *name) noexcept
|
|||||||
if (filename_fs.IsNull())
|
if (filename_fs.IsNull())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
return AllocatedPath::Build(playlist_dir_fs, filename_fs);
|
return playlist_dir_fs / filename_fs;
|
||||||
}
|
}
|
||||||
|
@ -138,7 +138,7 @@ LoadPlaylistFileInfo(PlaylistInfo &info,
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
FileInfo fi;
|
FileInfo fi;
|
||||||
if (!GetFileInfo(AllocatedPath::Build(parent_path_fs, name_fs), fi) ||
|
if (!GetFileInfo(parent_path_fs / name_fs, fi) ||
|
||||||
!fi.IsRegular())
|
!fi.IsRegular())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -81,8 +81,7 @@ handle_listfiles_local(Response &r, Path path_fs)
|
|||||||
if (name_utf8.empty())
|
if (name_utf8.empty())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const AllocatedPath full_fs =
|
const auto full_fs = path_fs / name_fs;
|
||||||
AllocatedPath::Build(path_fs, name_fs);
|
|
||||||
FileInfo fi;
|
FileInfo fi;
|
||||||
if (!GetFileInfo(full_fs, fi, false))
|
if (!GetFileInfo(full_fs, fi, false))
|
||||||
continue;
|
continue;
|
||||||
|
@ -113,7 +113,7 @@ ParsePath(const char *path)
|
|||||||
if (path2.IsNull())
|
if (path2.IsNull())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
return AllocatedPath::Build(home, path2);
|
return home / path2;
|
||||||
} else if (!PathTraitsUTF8::IsAbsolute(path)) {
|
} else if (!PathTraitsUTF8::IsAbsolute(path)) {
|
||||||
throw FormatRuntimeError("not an absolute path: %s", path);
|
throw FormatRuntimeError("not an absolute path: %s", path);
|
||||||
} else {
|
} else {
|
||||||
|
@ -53,8 +53,7 @@ CreateConfiguredDatabase(EventLoop &main_event_loop, EventLoop &io_event_loop,
|
|||||||
if (cache_dir.IsNull())
|
if (cache_dir.IsNull())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
const auto db_file = AllocatedPath::Build(cache_dir,
|
const auto db_file = cache_dir / Path::FromFS(PATH_LITERAL("mpd.db"));
|
||||||
PATH_LITERAL("mpd.db"));
|
|
||||||
const auto db_file_utf8 = db_file.ToUTF8();
|
const auto db_file_utf8 = db_file.ToUTF8();
|
||||||
if (db_file_utf8.empty())
|
if (db_file_utf8.empty())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -428,8 +428,7 @@ SimpleDatabase::Mount(const char *local_uri, const char *storage_uri)
|
|||||||
#ifndef ENABLE_ZLIB
|
#ifndef ENABLE_ZLIB
|
||||||
constexpr bool compress = false;
|
constexpr bool compress = false;
|
||||||
#endif
|
#endif
|
||||||
auto db = new SimpleDatabase(AllocatedPath::Build(cache_path,
|
auto db = new SimpleDatabase(cache_path / name_fs,
|
||||||
name_fs.c_str()),
|
|
||||||
compress);
|
compress);
|
||||||
try {
|
try {
|
||||||
db->Open();
|
db->Open();
|
||||||
|
@ -141,7 +141,7 @@ WatchDirectory::GetUriFS() const noexcept
|
|||||||
if (uri.IsNull())
|
if (uri.IsNull())
|
||||||
return name;
|
return name;
|
||||||
|
|
||||||
return AllocatedPath::Build(uri, name);
|
return uri / name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* we don't look at "." / ".." nor files with newlines in their 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))
|
if (skip_path(ent->d_name))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const auto child_path_fs =
|
const auto name_fs = Path::FromFS(ent->d_name);
|
||||||
AllocatedPath::Build(path_fs, ent->d_name);
|
const auto child_path_fs = path_fs / name_fs;
|
||||||
|
|
||||||
FileInfo fi;
|
FileInfo fi;
|
||||||
try {
|
try {
|
||||||
@ -211,7 +211,7 @@ recursive_watch_subdirectories(WatchDirectory *directory,
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
directory->children.emplace_front(directory,
|
directory->children.emplace_front(directory,
|
||||||
AllocatedPath::FromFS(ent->d_name),
|
name_fs,
|
||||||
ret);
|
ret);
|
||||||
child = &directory->children.front();
|
child = &directory->children.front();
|
||||||
|
|
||||||
@ -262,7 +262,7 @@ mpd_inotify_callback(int wd, unsigned mask,
|
|||||||
|
|
||||||
const auto path_fs = uri_fs.IsNull()
|
const auto path_fs = uri_fs.IsNull()
|
||||||
? root
|
? root
|
||||||
: AllocatedPath::Build(root, uri_fs.c_str());
|
: (root / uri_fs);
|
||||||
|
|
||||||
recursive_watch_subdirectories(directory, path_fs,
|
recursive_watch_subdirectories(directory, path_fs,
|
||||||
directory->GetDepth());
|
directory->GetDepth());
|
||||||
|
@ -43,8 +43,7 @@ try {
|
|||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
try {
|
try {
|
||||||
const auto x = AllocatedPath::Build(path_fs,
|
const auto x = path_fs / Path::FromFS(PathTraitsFS::CURRENT_DIRECTORY);
|
||||||
PathTraitsFS::CURRENT_DIRECTORY);
|
|
||||||
const FileInfo fi2(x);
|
const FileInfo fi2(x);
|
||||||
} catch (const std::system_error &e) {
|
} catch (const std::system_error &e) {
|
||||||
if (IsAccessDenied(e))
|
if (IsAccessDenied(e))
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2003-2017 The Music Player Daemon Project
|
* Copyright 2003-2018 The Music Player Daemon Project
|
||||||
* http://www.musicpd.org
|
* http://www.musicpd.org
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
@ -169,4 +169,12 @@ public:
|
|||||||
const_pointer_type GetSuffix() const noexcept;
|
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
|
#endif
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2003-2017 The Music Player Daemon Project
|
* Copyright 2003-2018 The Music Player Daemon Project
|
||||||
* http://www.musicpd.org
|
* http://www.musicpd.org
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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()));
|
return AllocatedPath::FromFS(PathTraitsFS::GetParent(c_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AllocatedPath
|
||||||
|
operator/(Path a, Path b) noexcept
|
||||||
|
{
|
||||||
|
return AllocatedPath::Build(a, b);
|
||||||
|
}
|
||||||
|
@ -183,16 +183,16 @@ ParseConfigLine(char *line, const char *dir_name, AllocatedPath &result_dir)
|
|||||||
*line_end = 0;
|
*line_end = 0;
|
||||||
|
|
||||||
// build the result path
|
// build the result path
|
||||||
const char *path = line;
|
const auto path_fs = Path::FromFS(line);
|
||||||
|
|
||||||
AllocatedPath result = nullptr;
|
AllocatedPath result = nullptr;
|
||||||
if (home_relative) {
|
if (home_relative) {
|
||||||
auto home = GetHomeDir();
|
auto home = GetHomeDir();
|
||||||
if (home.IsNull())
|
if (home.IsNull())
|
||||||
return true;
|
return true;
|
||||||
result = AllocatedPath::Build(home, path);
|
result = home / path_fs;
|
||||||
} else {
|
} else {
|
||||||
result = AllocatedPath::FromFS(path);
|
result = AllocatedPath(path_fs);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsValidDir(result.c_str())) {
|
if (IsValidDir(result.c_str())) {
|
||||||
@ -209,9 +209,8 @@ try {
|
|||||||
auto config_dir = GetUserConfigDir();
|
auto config_dir = GetUserConfigDir();
|
||||||
if (config_dir.IsNull())
|
if (config_dir.IsNull())
|
||||||
return result;
|
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;
|
char *line;
|
||||||
while ((line = input.ReadLine()) != nullptr)
|
while ((line = input.ReadLine()) != nullptr)
|
||||||
if (ParseConfigLine(line, name, result))
|
if (ParseConfigLine(line, name, result))
|
||||||
@ -237,7 +236,7 @@ GetUserConfigDir() noexcept
|
|||||||
// Check for $HOME/.config
|
// Check for $HOME/.config
|
||||||
auto home = GetHomeDir();
|
auto home = GetHomeDir();
|
||||||
if (!home.IsNull()) {
|
if (!home.IsNull()) {
|
||||||
AllocatedPath fallback = AllocatedPath::Build(home, ".config");
|
auto fallback = home / Path::FromFS(".config");
|
||||||
if (IsValidDir(fallback.c_str()))
|
if (IsValidDir(fallback.c_str()))
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
@ -274,7 +273,7 @@ GetUserCacheDir() noexcept
|
|||||||
// Check for $HOME/.cache
|
// Check for $HOME/.cache
|
||||||
auto home = GetHomeDir();
|
auto home = GetHomeDir();
|
||||||
if (!home.IsNull()) {
|
if (!home.IsNull()) {
|
||||||
AllocatedPath fallback = AllocatedPath::Build(home, ".cache");
|
auto fallback = home / Path::FromFS(".cache");
|
||||||
if (IsValidDir(fallback.c_str()))
|
if (IsValidDir(fallback.c_str()))
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
@ -114,8 +114,7 @@ LocalStorage::MapFSOrThrow(const char *uri_utf8) const
|
|||||||
if (StringIsEmpty(uri_utf8))
|
if (StringIsEmpty(uri_utf8))
|
||||||
return base_fs;
|
return base_fs;
|
||||||
|
|
||||||
return AllocatedPath::Build(base_fs,
|
return base_fs / AllocatedPath::FromUTF8Throw(uri_utf8);
|
||||||
AllocatedPath::FromUTF8Throw(uri_utf8));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AllocatedPath
|
AllocatedPath
|
||||||
@ -176,7 +175,7 @@ LocalDirectoryReader::Read() noexcept
|
|||||||
StorageFileInfo
|
StorageFileInfo
|
||||||
LocalDirectoryReader::GetInfo(bool follow)
|
LocalDirectoryReader::GetInfo(bool follow)
|
||||||
{
|
{
|
||||||
return Stat(AllocatedPath::Build(base_fs, reader.GetEntry()), follow);
|
return Stat(base_fs / reader.GetEntry(), follow);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Storage>
|
std::unique_ptr<Storage>
|
||||||
|
Loading…
Reference in New Issue
Block a user