Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow state directory based in $HOME. #1236

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ if(NOT WIN32)
target. Disabling this is useful in the case where the CMAKE_INSTALL_PREFIX
is owned by a non-privileged user but where the WATCHMAN_STATE_DIR requires
administrative rights to create and set its permissions.")
option(WATCHMAN_USE_XDG_STATE_HOME
"If enabled, use $XDG_STATE_HOME/watchman as the Watchman state directory. \
XDG_STATE_HOME defaults to $HOME/.local/state."
OFF
)
else()
set(WATCHMAN_STATE_DIR)
set(INSTALL_WATCHMAN_STATE_DIR)
Expand Down Expand Up @@ -189,6 +194,9 @@ if(BUILD_INFO)
config_h("#define WATCHMAN_BUILD_INFO \"${BUILD_INFO}\"")
endif ()

if(WATCHMAN_USE_XDG_STATE_HOME)
config_h("#define WATCHMAN_USE_XDG_STATE_HOME 1")
endif()

# While most of these tests are not strictly needed on windows, it is vital
# that we probe for and find strtoll in order for the jansson build to use
Expand Down
47 changes: 46 additions & 1 deletion watchman/UserDir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <eden/common/utils/StringConv.h>
#include <fmt/core.h>
#include <folly/String.h>
#include <pwd.h>
#include <unistd.h>
#include "watchman/Logging.h"
#include "watchman/Options.h"
#include "watchman/fs/FileSystem.h"
Expand Down Expand Up @@ -131,11 +133,52 @@ std::string computeUserName() {
throw std::logic_error("unreachable");
}

std::string computeHomeDirectory() {
const char* homeDir = getenv("HOME");

if (!homeDir || *homeDir == 0) {
uid_t uid = getuid();
struct passwd* pw = getpwuid(uid);

if (!pw) {
log(FATAL,
"getpwuid(",
uid,
") failed: ",
folly::errnoStr(errno),
". I don't know who you are\n");
}
homeDir = pw->pw_dir;
}

return homeDir;
}

const std::string& getHomeDirectory() {
static std::string homeDir = computeHomeDirectory();
return homeDir;
}

const std::string& getTemporaryDirectory() {
static std::string tmpdir = computeTemporaryDirectory();
return tmpdir;
}

std::string computeXDGStateHomeDirectory() {
char* xdgStateHome = getenv("XDG_STATE_HOME");

if (!xdgStateHome || *xdgStateHome == 0) {
return fmt::format("{}/.local/state", getHomeDirectory());
}

return xdgStateHome;
}

const std::string& getXDGStateHomeDirectory() {
static std::string xdgStateHome = computeXDGStateHomeDirectory();
return xdgStateHome;
}

std::string computeWatchmanStateDirectory(const std::string& user) {
if (!flags.test_state_dir.empty()) {
return fmt::format("{}/{}-state", flags.test_state_dir, user);
Expand All @@ -145,7 +188,9 @@ std::string computeWatchmanStateDirectory(const std::string& user) {
return getCachedWatchmanAppDataPath();
#else
auto state_parent =
#ifdef WATCHMAN_STATE_DIR
#if defined(WATCHMAN_USE_XDG_STATE_HOME)
fmt::format("{}/watchman", getXDGStateHomeDirectory())
#elif defined(WATCHMAN_STATE_DIR)
WATCHMAN_STATE_DIR
#else
getTemporaryDirectory().c_str()
Expand Down
6 changes: 5 additions & 1 deletion watchman/UserDir.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ std::string computeUserName();
*/
const std::string& getTemporaryDirectory();

/**
* Returns a cached reference to the current user's home directory.
*/
const std::string& getHomeDirectory();

/**
* Computes the Watchman state directory corresponding to the given user name.
*/
std::string computeWatchmanStateDirectory(const std::string& user);

} // namespace watchman
17 changes: 2 additions & 15 deletions watchman/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,36 +473,23 @@ static SpawnResult spawn_via_launchd() {
uint32_t size = sizeof(watchman_path);
char plist_path[WATCHMAN_NAME_MAX];
FILE* fp;
struct passwd* pw;
uid_t uid;

close_random_fds();

if (_NSGetExecutablePath(watchman_path, &size) == -1) {
log(FATAL, "_NSGetExecutablePath: path too long; size ", size, "\n");
}

uid = getuid();
pw = getpwuid(uid);
if (!pw) {
log(FATAL,
"getpwuid(",
uid,
") failed: ",
folly::errnoStr(errno),
". I don't know who you are\n");
}

snprintf(
plist_path, sizeof(plist_path), "%s/Library/LaunchAgents", pw->pw_dir);
plist_path, sizeof(plist_path), "%s/Library/LaunchAgents", getHomeDirectory().c_str());
// Best effort attempt to ensure that the agents dir exists. We'll detect
// and report the failure in the fopen call below.
mkdir(plist_path, 0755);
snprintf(
plist_path,
sizeof(plist_path),
"%s/Library/LaunchAgents/com.github.facebook.watchman.plist",
pw->pw_dir);
getHomeDirectory().c_str());

if (access(plist_path, R_OK) == 0) {
// Unload any that may already exist, as it is likely wrong
Expand Down
Loading