From 4ded1ae67b6dd49c7f6e3f21e7f3b5a308d38111 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max.kellermann@gmail.com>
Date: Mon, 28 Nov 2022 14:13:10 +0100
Subject: [PATCH] fs/FileSystem: add CreateDirectoryNoThrow()

---
 src/fs/FileSystem.hxx        | 10 ++++++++++
 src/fs/StandardDirectory.cxx |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/fs/FileSystem.hxx b/src/fs/FileSystem.hxx
index 0a742df9c..ebe80004f 100644
--- a/src/fs/FileSystem.hxx
+++ b/src/fs/FileSystem.hxx
@@ -67,6 +67,16 @@ StatFile(Path file, struct stat &buf, bool follow_symlinks = true)
 
 #endif
 
+static inline bool
+CreateDirectoryNoThrow(Path path) noexcept
+{
+#ifdef _WIN32
+	return CreateDirectory(path.c_str(), nullptr);
+#else
+	return mkdir(path.c_str(), 0777);
+#endif
+}
+
 /**
  * Truncate a file that exists already.  Throws std::system_error on
  * error.
diff --git a/src/fs/StandardDirectory.cxx b/src/fs/StandardDirectory.cxx
index ec2e3af04..2a31d170b 100644
--- a/src/fs/StandardDirectory.cxx
+++ b/src/fs/StandardDirectory.cxx
@@ -314,7 +314,7 @@ GetAppRuntimeDir() noexcept
 #ifdef USE_XDG
 	if (const auto user_dir = GetUserRuntimeDir(); !user_dir.IsNull()) {
 		auto dir = user_dir / app_filename;
-		mkdir(dir.c_str(), 0777);
+		CreateDirectoryNoThrow(dir);
 		return dir;
 	}
 #endif