Skip to content

Commit

Permalink
opendir -> DirHandle
Browse files Browse the repository at this point in the history
Summary:
Apply our C++ conventions to opendir. Now it's DirHandle, and both
UnixDirHandle.cpp and WinDirHandle.cpp are included in the build on
every platform.

Reviewed By: zhengchaol

Differential Revision: D31348996

fbshipit-source-id: e980319ba0fa9d39e996a71ec14009ef97324c63
  • Loading branch information
chadaustin authored and facebook-github-bot committed Oct 14, 2021
1 parent cffe7ba commit 24c4764
Show file tree
Hide file tree
Showing 21 changed files with 112 additions and 97 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ watchman/Pipe.cpp
watchman/ThreadPool.cpp
watchman/WatchmanConfig.cpp
watchman/bser.cpp
watchman/opendir.cpp
watchman/UnixDirHandle.cpp
watchman/WinDirHandle.cpp
watchman/stream.cpp
watchman/stream_unix.cpp
watchman/root/dir.cpp
Expand Down Expand Up @@ -629,16 +630,17 @@ watchman/SignalHandler.cpp
watchman/SymlinkTargets.cpp
watchman/ThreadPool.cpp
watchman/TriggerCommand.cpp
watchman/UnixDirHandle.cpp
watchman/UserDir.cpp
watchman/WatchmanConfig.cpp
watchman/WinDirHandle.cpp
watchman/bser.cpp
watchman/clientmode.cpp
# hash.cpp (in libhash)
watchman/launchd.cpp
watchman/listener-user.cpp
watchman/listener.cpp
watchman/main.cpp
watchman/opendir.cpp
watchman/sockname.cpp
watchman/state.cpp
watchman/stream.cpp
Expand Down Expand Up @@ -720,7 +722,6 @@ watchman/watcher/win32.cpp
watchman/winbuild/errmap.cpp
watchman/winbuild/pathmap.cpp
watchman/winbuild/mkdir.cpp
watchman/winbuild/dir.cpp
watchman/winbuild/time.cpp
watchman/winbuild/backtrace.cpp
watchman/winbuild/getopt_long.cpp
Expand Down
37 changes: 37 additions & 0 deletions watchman/DirHandle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <memory>
#include "watchman/FileInformation.h"

namespace watchman {

struct DirEntry {
bool has_stat;
char* d_name;
FileInformation stat;
};

class DirHandle {
public:
virtual ~DirHandle() = default;
virtual const DirEntry* readDir() = 0;
#ifndef _WIN32
virtual int getFd() const = 0;
#endif
};

/**
* Returns a dir handle to path.
* Does not follow symlinks if strict == true.
* Throws std::system_error if the dir could not be opened.
*/
std::unique_ptr<DirHandle> openDir(const char* path, bool strict = true);

} // namespace watchman
6 changes: 3 additions & 3 deletions watchman/InMemoryView.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <utility>
#include "watchman/ContentHash.h"
#include "watchman/CookieSync.h"
#include "watchman/DirHandle.h"
#include "watchman/PendingCollection.h"
#include "watchman/PerfSample.h"
#include "watchman/QueryableView.h"
Expand All @@ -21,7 +22,6 @@
#include "watchman/SymlinkTargets.h"
#include "watchman/WatchmanConfig.h"
#include "watchman/query/FileResult.h"
#include "watchman/watchman_opendir.h"
#include "watchman/watchman_string.h"
#include "watchman/watchman_system.h"

Expand Down Expand Up @@ -238,7 +238,7 @@ class InMemoryView final : public QueryableView {
ViewDatabase& view,
PendingChanges& coll,
const PendingChange& pending,
const watchman_dir_ent* pre_stat);
const DirEntry* pre_stat);

/** Recursively walks files under a specified dir */
void dirGenerator(
Expand Down Expand Up @@ -286,7 +286,7 @@ class InMemoryView final : public QueryableView {
ViewDatabase& view,
PendingChanges& coll,
const PendingChange& pending,
const watchman_dir_ent* pre_stat);
const DirEntry* pre_stat);

bool propagateToParentDirIfAppropriate(
const RootConfig& root,
Expand Down
43 changes: 23 additions & 20 deletions watchman/opendir.cpp → watchman/UnixDirHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
* LICENSE file in the root directory of this source tree.
*/

#include "watchman/DirHandle.h"

#include <folly/String.h>
#include <system_error>
#include "watchman/FileDescriptor.h"
#include "watchman/FileSystem.h"
#include "watchman/Logging.h"
#include "watchman/WatchmanConfig.h"

#ifndef _WIN32
#include <dirent.h>
#endif

#ifdef __APPLE__
#include <sys/attr.h> // @manual
#include <sys/utsname.h> // @manual
#include <sys/vnode.h> // @manual
#endif
#include <folly/String.h>
#include "watchman/FileDescriptor.h"
#include "watchman/FileSystem.h"
#include "watchman/Logging.h"
#include "watchman/WatchmanConfig.h"
#include "watchman/watchman_opendir.h"

using namespace watchman;
using watchman::FileDescriptor;
using watchman::OpenFileHandleOptions;
namespace watchman {

#ifdef HAVE_GETATTRLISTBULK
// The ordering of these fields is defined by the ordering of the
Expand Down Expand Up @@ -59,7 +60,7 @@ typedef struct {
#endif

#ifndef _WIN32
class DirHandle : public watchman_dir_handle {
class UnixDirHandle : public DirHandle {
#ifdef HAVE_GETATTRLISTBULK
std::string dirName_;
FileDescriptor fd_;
Expand All @@ -69,12 +70,12 @@ class DirHandle : public watchman_dir_handle {
char* cursor_{nullptr};
#endif
DIR* d_{nullptr};
struct watchman_dir_ent ent_;
struct DirEntry ent_;

public:
explicit DirHandle(const char* path, bool strict);
~DirHandle() override;
const watchman_dir_ent* readDir() override;
explicit UnixDirHandle(const char* path, bool strict);
~UnixDirHandle() override;
const DirEntry* readDir() override;
int getFd() const override;
};
#endif
Expand Down Expand Up @@ -127,8 +128,8 @@ static bool use_bulkstat_by_default() {
#endif

#ifndef _WIN32
std::unique_ptr<watchman_dir_handle> w_dir_open(const char* path, bool strict) {
return std::make_unique<DirHandle>(path, strict);
std::unique_ptr<DirHandle> openDir(const char* path, bool strict) {
return std::make_unique<UnixDirHandle>(path, strict);
}

#ifdef HAVE_GETATTRLISTBULK
Expand Down Expand Up @@ -171,7 +172,7 @@ static const std::unordered_map<uint32_t, const char*> commonLabels = {
};
#endif

DirHandle::DirHandle(const char* path, bool strict)
UnixDirHandle::UnixDirHandle(const char* path, bool strict)
#ifdef HAVE_GETATTRLISTBULK
: dirName_(path)
#endif
Expand Down Expand Up @@ -216,7 +217,7 @@ DirHandle::DirHandle(const char* path, bool strict)
}
}

const watchman_dir_ent* DirHandle::readDir() {
const DirEntry* UnixDirHandle::readDir() {
#ifdef HAVE_GETATTRLISTBULK
if (fd_) {
bulk_attr_item* item;
Expand Down Expand Up @@ -396,13 +397,13 @@ const watchman_dir_ent* DirHandle::readDir() {
return &ent_;
}

DirHandle::~DirHandle() {
UnixDirHandle::~UnixDirHandle() {
if (d_) {
closedir(d_);
}
}

int DirHandle::getFd() const {
int UnixDirHandle::getFd() const {
#ifdef HAVE_GETATTRLISTBULK
if (cfg_get_bool("_use_bulkstat", use_bulkstat_by_default())) {
return fd_.fd();
Expand All @@ -412,5 +413,7 @@ int DirHandle::getFd() const {
}
#endif

} // namespace watchman

/* vim:ts=2:sw=2:et:
*/
27 changes: 16 additions & 11 deletions watchman/winbuild/dir.cpp → watchman/WinDirHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
* LICENSE file in the root directory of this source tree.
*/

#include "watchman/DirHandle.h"

#include <folly/ScopeGuard.h>
#include "watchman/FileDescriptor.h"
#include "watchman/FileSystem.h"
#include "watchman/watchman_opendir.h"
#include "watchman/watchman_string.h"
#include "watchman/watchman_system.h"

using watchman::FileDescriptor;
using watchman::FileInformation;
using watchman::OpenFileHandleOptions;
namespace watchman {

#ifdef _WIN32

namespace {
class WinDirHandle : public watchman_dir_handle {
class WinDirHandle : public DirHandle {
std::wstring dirWPath_;
FileDescriptor h_;
bool win7_{false};
FILE_FULL_DIR_INFO* info_{nullptr};
char __declspec(align(8)) buf_[64 * 1024];
HANDLE hDirFind_{nullptr};
char nameBuf_[WATCHMAN_NAME_MAX];
struct watchman_dir_ent ent_;
DirEntry ent_;

public:
~WinDirHandle() {
Expand All @@ -49,15 +50,15 @@ class WinDirHandle : public watchman_dir_handle {
win7_ = true;
}

ent_ = watchman_dir_ent();
ent_ = DirEntry();
ent_.d_name = nameBuf_;
ent_.has_stat = true;
if (path[1] == ':') {
ent_.stat.dev = tolower(path[0]) - 'a';
}
}

const watchman_dir_ent* readDir() override {
const DirEntry* readDir() override {
if (win7_) {
return readDirWin7();
}
Expand All @@ -75,7 +76,7 @@ class WinDirHandle : public watchman_dir_handle {
}

private:
const watchman_dir_ent* readDirWin8() {
const DirEntry* readDirWin8() {
if (!info_) {
if (!GetFileInformationByHandleEx(
(HANDLE)h_.handle(), FileFullDirectoryInfo, buf_, sizeof(buf_))) {
Expand Down Expand Up @@ -123,7 +124,7 @@ class WinDirHandle : public watchman_dir_handle {
return &ent_;
}

const watchman_dir_ent* readDirWin7() {
const DirEntry* readDirWin7() {
// FileFullDirectoryInfo is not supported prior to Windows 8
WIN32_FIND_DATAW findFileData;
bool success;
Expand Down Expand Up @@ -181,6 +182,10 @@ class WinDirHandle : public watchman_dir_handle {
};
} // namespace

std::unique_ptr<watchman_dir_handle> w_dir_open(const char* path, bool strict) {
std::unique_ptr<DirHandle> openDir(const char* path, bool strict) {
return std::make_unique<WinDirHandle>(path, strict);
}

#endif

} // namespace watchman
6 changes: 3 additions & 3 deletions watchman/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "watchman/ChildProcess.h"
#include "watchman/Clock.h"
#include "watchman/DirHandle.h"
#include "watchman/FileSystem.h"
#include "watchman/GroupLookup.h"
#include "watchman/LogConfig.h"
Expand All @@ -33,7 +34,6 @@
#include "watchman/sockname.h"
#include "watchman/state.h"
#include "watchman/watchman_cmd.h"
#include "watchman/watchman_opendir.h"
#include "watchman/watchman_stream.h"

#ifdef _WIN32
Expand Down Expand Up @@ -582,8 +582,8 @@ static void verify_dir_ownership(const std::string& state_dir) {
"sock_access", false /* write bits */, true /* execute bits */) |
S_ISGID;

auto dirp = w_dir_open(
state_dir.c_str(), false /* don't need strict symlink rules */);
auto dirp =
openDir(state_dir.c_str(), false /* don't need strict symlink rules */);

dir_fd = dirp->getFd();
if (dir_fd == -1) {
Expand Down
4 changes: 2 additions & 2 deletions watchman/root/crawler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void InMemoryView::crawler(
/* Start watching and open the dir for crawling.
* Whether we open the dir prior to watching or after is watcher specific,
* so the operations are rolled together in our abstraction */
std::unique_ptr<watchman_dir_handle> osdir;
std::unique_ptr<DirHandle> osdir;

try {
osdir = watcher_->startWatchDir(root, dir, path);
Expand Down Expand Up @@ -133,7 +133,7 @@ void InMemoryView::crawler(
}

try {
while (const watchman_dir_ent* dirent = osdir->readDir()) {
while (const DirEntry* dirent = osdir->readDir()) {
// Don't follow parent/self links
if (dirent->d_name[0] == '.' &&
(!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, ".."))) {
Expand Down
4 changes: 2 additions & 2 deletions watchman/root/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*/

#include <folly/String.h>
#include "watchman/DirHandle.h"
#include "watchman/Logging.h"
#include "watchman/QueryableView.h"
#include "watchman/TriggerCommand.h"
#include "watchman/root/Root.h"
#include "watchman/root/watchlist.h"
#include "watchman/watchman_opendir.h"

namespace watchman {

Expand Down Expand Up @@ -257,7 +257,7 @@ Root::Root(

// This just opens and releases the dir. If an exception is thrown
// it will bubble up.
w_dir_open(root_path.c_str());
(void)openDir(root_path.c_str());
inner.view_ = std::move(view);

inner.last_cmd_timestamp = std::chrono::steady_clock::now();
Expand Down
2 changes: 1 addition & 1 deletion watchman/root/iothread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ void InMemoryView::processPath(
ViewDatabase& view,
PendingChanges& coll,
const PendingChange& pending,
const watchman_dir_ent* pre_stat) {
const DirEntry* pre_stat) {
w_assert(
pending.path.size() >= rootPath_.size(),
"full_path must be a descendant of the root directory\n");
Expand Down
2 changes: 1 addition & 1 deletion watchman/root/stat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void InMemoryView::statPath(
ViewDatabase& view,
PendingChanges& coll,
const PendingChange& pending,
const watchman_dir_ent* pre_stat) {
const DirEntry* pre_stat) {
bool recursive = pending.flags.contains(W_PENDING_RECURSIVE);
const bool via_notify = pending.flags.contains(W_PENDING_VIA_NOTIFY);
const PendingFlags desynced_flag = pending.flags & W_PENDING_IS_DESYNCED;
Expand Down
Loading

0 comments on commit 24c4764

Please sign in to comment.