Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251WCHAR pszPath[MAX_PATH] = L"";253if (SHGetSpecialFolderPathW(nullptr, pszPath, nFolder, fCreate)) {254return fs::path(pszPath);255}257LogError("SHGetSpecialFolderPathW() failed, could not obtain requested path.");258return fs::path("");259}260#endif262bool RenameOver(fs::path src, fs::path dest)263{264std::error_code error;265fs::rename(src, dest, error);266return !error;267}269/**270* Ignores exceptions thrown by create_directories if the requested directory exists.271* Specifically handles case where path p exists, but it wasn't possible for the user to272* write to the parent directory.273*/274bool TryCreateDirectories(const fs::path& p)275{276try {277return fs::create_directories(p);278} catch (const fs::filesystem_error&) {279if (!fs::exists(p) || !fs::is_directory(p))280throw;281}283// create_directories didn't create the directory, it had to have existed already284return false;285}287std::string PermsToSymbolicString(fs::perms p)288{289std::string perm_str(9, '-');291auto set_perm = [&](size_t pos, fs::perms required_perm, char letter) {292if ((p & required_perm) != fs::perms::none) {293perm_str[pos] = letter;294}295};297set_perm(0, fs::perms::owner_read, 'r');298set_perm(1, fs::perms::owner_write, 'w');299set_perm(2, fs::perms::owner_exec, 'x');300set_perm(3, fs::perms::group_read, 'r');301set_perm(4, fs::perms::group_write, 'w');302set_perm(5, fs::perms::group_exec, 'x');303set_perm(6, fs::perms::others_read, 'r');304set_perm(7, fs::perms::others_write, 'w');305set_perm(8, fs::perms::others_exec, 'x');307return perm_str;308}310std::optional<fs::perms> InterpretPermString(const std::string& s)311{312if (s == "owner") {313return fs::perms::owner_read | fs::perms::owner_write;314} else if (s == "group") {315return fs::perms::owner_read | fs::perms::owner_write |316fs::perms::group_read;317} else if (s == "all") {318return fs::perms::owner_read | fs::perms::owner_write |319fs::perms::group_read |320fs::perms::others_read;321} else {322return std::nullopt;323}324}326bool IsDirWritable(const fs::path& dir_path)327{328// Attempt to create a tmp file in the directory329if (!fs::is_directory(dir_path)) throw std::runtime_error(strprintf("Path %s is not a directory", fs::PathToString(dir_path)));330FastRandomContext rng;331const auto tmp = dir_path / fs::PathFromString(strprintf(".tmp_%d", rng.rand64()));333const char* mode;334#ifdef __MINGW64__335mode = "w"; // Temporary workaround for https://github.com/bitcoin/bitcoin/issues/30210336#else337mode = "wx";338#endif340if (const auto created{fsbridge::fopen(tmp, mode)}) {341std::fclose(created);342std::error_code ec;343fs::remove(tmp, ec); // clean up, ignore errors344return true;345}346return false;347}349#ifdef __APPLE__350FSType GetFilesystemType(const fs::path& path)351{352if (struct statfs fs_info; statfs(path.c_str(), &fs_info)) {353return FSType::ERROR;354} else if (std::string_view{fs_info.f_fstypename} == "exfat") {355return FSType::EXFAT;356}357return FSType::OTHER;358}359#endif
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.