sapling/eden/fs/utils/SystemError.h
Xavier Deguillard 6dd4d77e88 utils: properly detect ENOENT on Windows
Summary:
On Windows, system error may be using the Win32ErrorCategory, and not the
generic system_category or generic_category. Thus we also need to test for that
category as well as the Windows error codes.

Reading a bit on the internet how Win32 error codes are supposed to be handled,
in theory, using the system_category should be the preferred method. However,
this would also mean that printing exceptions would also contain a \r at the
end, which feels suboptimal. Maybe xlog should just be fixed to never try to
print \r characters to avoid this issue?

Reviewed By: genevievehelsel

Differential Revision: D37189378

fbshipit-source-id: 03ca35c38a05bc35c8a184a6069b1ab41b6852c0
2022-06-16 15:57:28 -07:00

42 lines
1.2 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#pragma once
#include <eden/common/utils/WinError.h>
#include <system_error>
namespace facebook::eden {
/**
* Return true if this exception contains an errno value in ex.code().value()
*/
inline bool isErrnoError(const std::system_error& ex) {
// std::generic_category is the correct category to represent errno values.
// However folly/Exception.h unfortunately throws errno values as
// std::system_category for now.
return (
ex.code().category() == std::generic_category() ||
ex.code().category() == std::system_category());
}
/**
* Return true if this exception is equivalent to an ENOENT error code.
*/
inline bool isEnoent(const std::system_error& ex) {
auto ret = isErrnoError(ex) && ex.code().value() == ENOENT;
#ifdef _WIN32
ret = ret ||
(ex.code().category() == Win32ErrorCategory::get() &&
(ex.code().value() == ERROR_PATH_NOT_FOUND ||
ex.code().value() == ERROR_FILE_NOT_FOUND));
#endif
return ret;
}
} // namespace facebook::eden