SERVER-117564 Properly handle null device for logpath on Windows (#47618)
GitOrigin-RevId: 035f5404e0ef01c1b454c3b204a7d254e1ef1568
This commit is contained in:
parent
a6ab931c9a
commit
e2fec6eb56
@ -78,6 +78,10 @@
|
||||
#include <boost/move/utility_core.hpp>
|
||||
#include <boost/optional/optional.hpp>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "mongo/logv2/log_util.h"
|
||||
#endif
|
||||
|
||||
#if defined(MONGO_CONFIG_HAVE_HEADER_UNISTD_H)
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
@ -376,9 +380,8 @@ MONGO_INITIALIZER_GENERAL(ServerLogRedirection,
|
||||
fassert(16448, !serverGlobalParams.logWithSyslog);
|
||||
auto [absoluteLogpath, exists] = [&] {
|
||||
#ifdef _WIN32
|
||||
constexpr auto kWindowsNUL = "NUL"_sd;
|
||||
if (serverGlobalParams.logpath == kWindowsNUL) {
|
||||
return std::make_tuple(std::string(kWindowsNUL), true);
|
||||
if (logv2::isLogPathWindowsNul(serverGlobalParams.logpath)) {
|
||||
return std::make_tuple(std::string(logv2::kWindowsNUL), true);
|
||||
}
|
||||
#endif // defined(_WIN32)
|
||||
std::string absolutePath =
|
||||
|
||||
@ -69,6 +69,10 @@
|
||||
#include "mongo/util/string_map.h"
|
||||
#include "mongo/util/time_support.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "mongo/logv2/log_util.h"
|
||||
#endif
|
||||
|
||||
namespace mongo::logv2 {
|
||||
namespace {
|
||||
|
||||
@ -78,12 +82,21 @@ using stream_t = Win32SharedAccessOfstream;
|
||||
using stream_t = std::ofstream;
|
||||
#endif
|
||||
|
||||
bool checkLogFileExists(const std::string& filename) {
|
||||
#ifdef _WIN32
|
||||
if (isLogPathWindowsNul(filename)) {
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return boost::filesystem::exists(filename);
|
||||
}
|
||||
|
||||
StatusWith<boost::shared_ptr<stream_t>> openFile(const std::string& filename, bool append) {
|
||||
std::ios_base::openmode mode = std::ios_base::out;
|
||||
bool exists = false;
|
||||
if (append) {
|
||||
mode |= std::ios_base::app;
|
||||
exists = boost::filesystem::exists(filename);
|
||||
exists = checkLogFileExists(filename);
|
||||
} else
|
||||
mode |= std::ios_base::trunc;
|
||||
auto file = boost::make_shared<stream_t>(filename, mode);
|
||||
|
||||
@ -31,6 +31,7 @@
|
||||
|
||||
#include "mongo/base/status.h"
|
||||
#include "mongo/base/string_data.h"
|
||||
#include "mongo/util/str.h"
|
||||
|
||||
#include <functional>
|
||||
|
||||
@ -42,6 +43,10 @@ namespace mongo::logv2 {
|
||||
constexpr auto kServerLogTag = "server"_sd;
|
||||
constexpr auto kAuditLogTag = "audit"_sd;
|
||||
|
||||
#ifdef _WIN32
|
||||
constexpr auto kWindowsNUL = "nul"_sd;
|
||||
#endif
|
||||
|
||||
using LogRotateCallback = std::function<Status(bool, StringData, std::function<void(Status)>)>;
|
||||
using ShouldEmitLogServiceFn = std::function<bool()>;
|
||||
|
||||
@ -116,4 +121,14 @@ bool shouldEmitLogService();
|
||||
*/
|
||||
void setShouldEmitLogService(ShouldEmitLogServiceFn fn);
|
||||
|
||||
#ifdef _WIN32
|
||||
/**
|
||||
* Checks for the Windows NUL file. Although it's a valid file, boost::filesystem::exists() throws
|
||||
* an exception when checking it so we use this function as a special case.
|
||||
*/
|
||||
inline bool isLogPathWindowsNul(const std::string& path) {
|
||||
return str::equalCaseInsensitive(path, kWindowsNUL);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace mongo::logv2
|
||||
|
||||
Loading…
Reference in New Issue
Block a user