sapling/eden/fs/inodes/DiffContext.cpp
Adam Simpkins a3c582d708 check for std::generic_category when looking for errno errors
Summary:
Add a new utils/SystemError.h header with helper functions to check if a
`std::system_error` contains an errno value.

Most of the code in Eden previously only checked for `std::system_category`
when looking for errno values.  `std::generic_category` is the correct category
to use for errno exceptions, but folly/Exception.h incorrectly throws them as
`std::system_category` today.  This change makes Eden treat either error type
as errno values for now.

Reviewed By: yfeldblum

Differential Revision: D7329999

fbshipit-source-id: 67a3c3ea10371c53a2e34236b7575deac4cbd53a
2018-03-20 13:38:45 -07:00

91 lines
2.8 KiB
C++

/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include <folly/File.h>
#include <folly/FileUtil.h>
#include <folly/experimental/logging/xlog.h>
#include "eden/fs/fuse/privhelper/PrivHelper.h"
#include "eden/fs/fuse/privhelper/UserInfo.h"
#include "eden/fs/inodes/DiffContext.h"
#include "eden/fs/inodes/InodeDiffCallback.h"
#include "eden/fs/store/ObjectStore.h"
#include "eden/fs/utils/SystemError.h"
namespace facebook {
namespace eden {
constexpr folly::StringPiece DiffContext::kSystemWideIgnoreFileName;
DiffContext::DiffContext(
InodeDiffCallback* cb,
bool listIgnored,
const ObjectStore* os,
const UserInfo& userInfo)
: callback{cb}, store{os}, listIgnored{listIgnored} {
initOwnedIgnores(
tryIngestFile(AbsolutePathPiece{kSystemWideIgnoreFileName}),
tryIngestFile(constructUserIgnoreFileName(userInfo)));
}
DiffContext::DiffContext(
InodeDiffCallback* cb,
bool listIgnored,
const ObjectStore* os,
folly::StringPiece systemWideIgnoreFileContents,
folly::StringPiece userIgnoreFileContents)
: callback{cb}, store{os}, listIgnored{listIgnored} {
// Load the system-wide ignore settings and user-specific
// ignore settings into rootIgnore_.
initOwnedIgnores(systemWideIgnoreFileContents, userIgnoreFileContents);
}
AbsolutePath DiffContext::constructUserIgnoreFileName(
const UserInfo& userInfo) {
return userInfo.getHomeDirectory() + PathComponentPiece{".gitignore"};
}
std::string DiffContext::tryIngestFile(AbsolutePathPiece fileName) {
std::string contents;
try {
auto in =
folly::File(fileName.stringPiece()); // throws if file does not exist
if (!folly::readFile(in.fd(), contents)) {
contents.clear();
}
} catch (const std::system_error& ex) {
if (!isEnoent(ex)) {
XLOG(WARNING) << "error reading gitignore file " << fileName
<< folly::exceptionStr(ex);
}
} catch (const std::exception& ex) {
XLOG(WARNING) << "error reading gitignore file " << fileName
<< folly::exceptionStr(ex);
}
return contents;
}
void DiffContext::initOwnedIgnores(
folly::StringPiece systemWideIgnoreFileContents,
folly::StringPiece userIgnoreFileContents) {
pushFrameIfAvailable(systemWideIgnoreFileContents);
pushFrameIfAvailable(userIgnoreFileContents);
}
void DiffContext::pushFrameIfAvailable(folly::StringPiece ignoreFileContents) {
if (folly::trimWhitespace(ignoreFileContents).size() > 0) {
ownedIgnores_.push_back(std::make_unique<GitIgnoreStack>(
getToplevelIgnore(), ignoreFileContents));
}
}
} // namespace eden
} // namespace facebook