From patchwork Wed Nov 23 19:00:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric DeCosta X-Patchwork-Id: 13054131 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CEBDC4332F for ; Wed, 23 Nov 2022 19:00:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239194AbiKWTAw (ORCPT ); Wed, 23 Nov 2022 14:00:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238335AbiKWTAv (ORCPT ); Wed, 23 Nov 2022 14:00:51 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D52CE91C26 for ; Wed, 23 Nov 2022 11:00:49 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id g12so30793602wrs.10 for ; Wed, 23 Nov 2022 11:00:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=dsP2VTFNE7ywAKYOQ/VT+mldhWLES67uQlxqgONwgL4=; b=Hrr+KMh6x4BduZvmUOn5ZtsWphy6M3RDmgHVTAdgzCHg7wOfh6RjsZlvENwlXRbvU2 7H+w+ipmpU1DLx6sfs2DNFGdooaW9l+cfX0QZ92NflZct5g0oJ1yg7X/GOBACTzrRuCR rx6ckXI7jAAh1O5bfIQcpqud/feQOS+Rno+WHs4NRCTkipdmzXnNpwqaQ4T+LsmKtUVm NVOAMc6QPeqYvcdIMOItRBzE0Ky7tuAXoy1lVxf7Ym/YfAXpGttzs6DmVl4mBFPh267P sAAQVNZM8HtI9V14nhanluVh7qGu2NxuFKR+8yiKUqHpuZLfMlCf+HOQ95qCSh+rXotx zFFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dsP2VTFNE7ywAKYOQ/VT+mldhWLES67uQlxqgONwgL4=; b=C5jfKNUP45WQSyXOajpVmX/Ks4f3fj5krhkuuqlgtRU2HcbGLTv38FLnPR2SLqzkA/ M60PTTa2bmmaDHsuoU69Yp2clZl93qvc6YrjZhWCGZNQ+jhFrqeVowKH6blHxtak/uql BsWO/VouHr8sC+2nqVVqt4lFiCYixW3IZxZr0OUVhFpsJ/2oJDz8ClBaWCfhVdMhNqzK RO4daEc5LLzd5NoAD7iicxLmILwOe52InmIxdwTSMwZw82tEgq6oR4tLw7x9bYdliDMx TP9nC9am/KTd30W1avwCV5vKFFUja2kuf6RUeVxc8lF+D0485P/evG7kbhbfJb0N4XsW Iu/A== X-Gm-Message-State: ANoB5plpNA9sfL6HAaRBEwd58pfLpPoRC8bAa0Lt7iHbksN3e76F/Dhp ZW9Fo9l4sXGuCBb1X0t/zXeUaM3UBMA= X-Google-Smtp-Source: AA0mqf5JNf73CXkeI8AVxkEGAGkaEpQ3rFEL2ir6ZnvFCWspwlduIkB6H9AkQCGM0wDFBXO+JPrF6g== X-Received: by 2002:adf:db85:0:b0:241:bfd7:de7 with SMTP id u5-20020adfdb85000000b00241bfd70de7mr15670628wri.254.1669230048028; Wed, 23 Nov 2022 11:00:48 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t13-20020a05600c198d00b003cf54b77bfesm3794634wmq.28.2022.11.23.11.00.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Nov 2022 11:00:47 -0800 (PST) Message-Id: <99d684c7bdfac9db9cbf74bae1d21f9dc20ac825.1669230044.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 23 Nov 2022 19:00:39 +0000 Subject: [PATCH v4 1/6] fsmonitor: prepare to share code between Mac OS and Linux Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnI=?= =?utf-8?b?w7A=?= Bjarmason , Glen Choo , Johannes Schindelin , Taylor Blau , Eric DeCosta , Eric DeCosta Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Eric DeCosta From: Eric DeCosta Linux and Mac OS can share some of the code originally developed for Mac OS. Mac OS and Linux can share fsm-ipc-unix.c and fsm-settings-unix.c Signed-off-by: Eric DeCosta --- compat/fsmonitor/fsm-health-linux.c | 24 ++++++++++ compat/fsmonitor/fsm-ipc-darwin.c | 53 +--------------------- compat/fsmonitor/fsm-ipc-linux.c | 1 + compat/fsmonitor/fsm-ipc-unix.c | 52 +++++++++++++++++++++ compat/fsmonitor/fsm-settings-darwin.c | 63 +------------------------- compat/fsmonitor/fsm-settings-linux.c | 1 + compat/fsmonitor/fsm-settings-unix.c | 61 +++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 114 deletions(-) create mode 100644 compat/fsmonitor/fsm-health-linux.c create mode 100644 compat/fsmonitor/fsm-ipc-linux.c create mode 100644 compat/fsmonitor/fsm-ipc-unix.c create mode 100644 compat/fsmonitor/fsm-settings-linux.c create mode 100644 compat/fsmonitor/fsm-settings-unix.c diff --git a/compat/fsmonitor/fsm-health-linux.c b/compat/fsmonitor/fsm-health-linux.c new file mode 100644 index 00000000000..b9f709e8548 --- /dev/null +++ b/compat/fsmonitor/fsm-health-linux.c @@ -0,0 +1,24 @@ +#include "cache.h" +#include "config.h" +#include "fsmonitor.h" +#include "fsm-health.h" +#include "fsmonitor--daemon.h" + +int fsm_health__ctor(struct fsmonitor_daemon_state *state) +{ + return 0; +} + +void fsm_health__dtor(struct fsmonitor_daemon_state *state) +{ + return; +} + +void fsm_health__loop(struct fsmonitor_daemon_state *state) +{ + return; +} + +void fsm_health__stop_async(struct fsmonitor_daemon_state *state) +{ +} diff --git a/compat/fsmonitor/fsm-ipc-darwin.c b/compat/fsmonitor/fsm-ipc-darwin.c index d67b0ee50d3..4c3c92081ee 100644 --- a/compat/fsmonitor/fsm-ipc-darwin.c +++ b/compat/fsmonitor/fsm-ipc-darwin.c @@ -1,52 +1 @@ -#include "cache.h" -#include "config.h" -#include "strbuf.h" -#include "fsmonitor.h" -#include "fsmonitor-ipc.h" -#include "fsmonitor-path-utils.h" - -static GIT_PATH_FUNC(fsmonitor_ipc__get_default_path, "fsmonitor--daemon.ipc") - -const char *fsmonitor_ipc__get_path(struct repository *r) -{ - static const char *ipc_path = NULL; - git_SHA_CTX sha1ctx; - char *sock_dir = NULL; - struct strbuf ipc_file = STRBUF_INIT; - unsigned char hash[GIT_MAX_RAWSZ]; - - if (!r) - BUG("No repository passed into fsmonitor_ipc__get_path"); - - if (ipc_path) - return ipc_path; - - - /* By default the socket file is created in the .git directory */ - if (fsmonitor__is_fs_remote(r->gitdir) < 1) { - ipc_path = fsmonitor_ipc__get_default_path(); - return ipc_path; - } - - git_SHA1_Init(&sha1ctx); - git_SHA1_Update(&sha1ctx, r->worktree, strlen(r->worktree)); - git_SHA1_Final(hash, &sha1ctx); - - repo_config_get_string(r, "fsmonitor.socketdir", &sock_dir); - - /* Create the socket file in either socketDir or $HOME */ - if (sock_dir && *sock_dir) { - strbuf_addf(&ipc_file, "%s/.git-fsmonitor-%s", - sock_dir, hash_to_hex(hash)); - } else { - strbuf_addf(&ipc_file, "~/.git-fsmonitor-%s", hash_to_hex(hash)); - } - free(sock_dir); - - ipc_path = interpolate_path(ipc_file.buf, 1); - if (!ipc_path) - die(_("Invalid path: %s"), ipc_file.buf); - - strbuf_release(&ipc_file); - return ipc_path; -} +#include "fsm-ipc-unix.c" diff --git a/compat/fsmonitor/fsm-ipc-linux.c b/compat/fsmonitor/fsm-ipc-linux.c new file mode 100644 index 00000000000..4c3c92081ee --- /dev/null +++ b/compat/fsmonitor/fsm-ipc-linux.c @@ -0,0 +1 @@ +#include "fsm-ipc-unix.c" diff --git a/compat/fsmonitor/fsm-ipc-unix.c b/compat/fsmonitor/fsm-ipc-unix.c new file mode 100644 index 00000000000..d67b0ee50d3 --- /dev/null +++ b/compat/fsmonitor/fsm-ipc-unix.c @@ -0,0 +1,52 @@ +#include "cache.h" +#include "config.h" +#include "strbuf.h" +#include "fsmonitor.h" +#include "fsmonitor-ipc.h" +#include "fsmonitor-path-utils.h" + +static GIT_PATH_FUNC(fsmonitor_ipc__get_default_path, "fsmonitor--daemon.ipc") + +const char *fsmonitor_ipc__get_path(struct repository *r) +{ + static const char *ipc_path = NULL; + git_SHA_CTX sha1ctx; + char *sock_dir = NULL; + struct strbuf ipc_file = STRBUF_INIT; + unsigned char hash[GIT_MAX_RAWSZ]; + + if (!r) + BUG("No repository passed into fsmonitor_ipc__get_path"); + + if (ipc_path) + return ipc_path; + + + /* By default the socket file is created in the .git directory */ + if (fsmonitor__is_fs_remote(r->gitdir) < 1) { + ipc_path = fsmonitor_ipc__get_default_path(); + return ipc_path; + } + + git_SHA1_Init(&sha1ctx); + git_SHA1_Update(&sha1ctx, r->worktree, strlen(r->worktree)); + git_SHA1_Final(hash, &sha1ctx); + + repo_config_get_string(r, "fsmonitor.socketdir", &sock_dir); + + /* Create the socket file in either socketDir or $HOME */ + if (sock_dir && *sock_dir) { + strbuf_addf(&ipc_file, "%s/.git-fsmonitor-%s", + sock_dir, hash_to_hex(hash)); + } else { + strbuf_addf(&ipc_file, "~/.git-fsmonitor-%s", hash_to_hex(hash)); + } + free(sock_dir); + + ipc_path = interpolate_path(ipc_file.buf, 1); + if (!ipc_path) + die(_("Invalid path: %s"), ipc_file.buf); + + strbuf_release(&ipc_file); + return ipc_path; +} diff --git a/compat/fsmonitor/fsm-settings-darwin.c b/compat/fsmonitor/fsm-settings-darwin.c index 6abbc7af3ab..14baf9f0603 100644 --- a/compat/fsmonitor/fsm-settings-darwin.c +++ b/compat/fsmonitor/fsm-settings-darwin.c @@ -1,62 +1 @@ -#include "config.h" -#include "fsmonitor.h" -#include "fsmonitor-ipc.h" -#include "fsmonitor-settings.h" -#include "fsmonitor-path-utils.h" - - /* - * For the builtin FSMonitor, we create the Unix domain socket for the - * IPC in the .git directory. If the working directory is remote, - * then the socket will be created on the remote file system. This - * can fail if the remote file system does not support UDS file types - * (e.g. smbfs to a Windows server) or if the remote kernel does not - * allow a non-local process to bind() the socket. (These problems - * could be fixed by moving the UDS out of the .git directory and to a - * well-known local directory on the client machine, but care should - * be taken to ensure that $HOME is actually local and not a managed - * file share.) - * - * FAT32 and NTFS working directories are problematic too. - * - * The builtin FSMonitor uses a Unix domain socket in the .git - * directory for IPC. These Windows drive formats do not support - * Unix domain sockets, so mark them as incompatible for the daemon. - * - */ -static enum fsmonitor_reason check_uds_volume(struct repository *r) -{ - struct fs_info fs; - const char *ipc_path = fsmonitor_ipc__get_path(r); - struct strbuf path = STRBUF_INIT; - strbuf_add(&path, ipc_path, strlen(ipc_path)); - - if (fsmonitor__get_fs_info(dirname(path.buf), &fs) == -1) { - strbuf_release(&path); - return FSMONITOR_REASON_ERROR; - } - - strbuf_release(&path); - - if (fs.is_remote || - !strcmp(fs.typename, "msdos") || - !strcmp(fs.typename, "ntfs")) { - free(fs.typename); - return FSMONITOR_REASON_NOSOCKETS; - } - - free(fs.typename); - return FSMONITOR_REASON_OK; -} - -enum fsmonitor_reason fsm_os__incompatible(struct repository *r, int ipc) -{ - enum fsmonitor_reason reason; - - if (ipc) { - reason = check_uds_volume(r); - if (reason != FSMONITOR_REASON_OK) - return reason; - } - - return FSMONITOR_REASON_OK; -} +#include "fsm-settings-unix.c" diff --git a/compat/fsmonitor/fsm-settings-linux.c b/compat/fsmonitor/fsm-settings-linux.c new file mode 100644 index 00000000000..14baf9f0603 --- /dev/null +++ b/compat/fsmonitor/fsm-settings-linux.c @@ -0,0 +1 @@ +#include "fsm-settings-unix.c" diff --git a/compat/fsmonitor/fsm-settings-unix.c b/compat/fsmonitor/fsm-settings-unix.c new file mode 100644 index 00000000000..a6ed32575a9 --- /dev/null +++ b/compat/fsmonitor/fsm-settings-unix.c @@ -0,0 +1,61 @@ +#include "config.h" +#include "fsmonitor.h" +#include "fsmonitor-ipc.h" +#include "fsmonitor-path-utils.h" + + /* + * For the builtin FSMonitor, we create the Unix domain socket for the + * IPC in the .git directory. If the working directory is remote, + * then the socket will be created on the remote file system. This + * can fail if the remote file system does not support UDS file types + * (e.g. smbfs to a Windows server) or if the remote kernel does not + * allow a non-local process to bind() the socket. (These problems + * could be fixed by moving the UDS out of the .git directory and to a + * well-known local directory on the client machine, but care should + * be taken to ensure that $HOME is actually local and not a managed + * file share.) + * + * FAT32 and NTFS working directories are problematic too. + * + * The builtin FSMonitor uses a Unix domain socket in the .git + * directory for IPC. These Windows drive formats do not support + * Unix domain sockets, so mark them as incompatible for the daemon. + * + */ +static enum fsmonitor_reason check_uds_volume(struct repository *r) +{ + struct fs_info fs; + const char *ipc_path = fsmonitor_ipc__get_path(r); + struct strbuf path = STRBUF_INIT; + strbuf_addstr(&path, ipc_path); + + if (fsmonitor__get_fs_info(dirname(path.buf), &fs) == -1) { + strbuf_release(&path); + return FSMONITOR_REASON_ERROR; + } + + strbuf_release(&path); + + if (fs.is_remote || + !strcmp(fs.typename, "msdos") || + !strcmp(fs.typename, "ntfs")) { + free(fs.typename); + return FSMONITOR_REASON_NOSOCKETS; + } + + free(fs.typename); + return FSMONITOR_REASON_OK; +} + +enum fsmonitor_reason fsm_os__incompatible(struct repository *r, int ipc) +{ + enum fsmonitor_reason reason; + + if (ipc) { + reason = check_uds_volume(r); + if (reason != FSMONITOR_REASON_OK) + return reason; + } + + return FSMONITOR_REASON_OK; +} From patchwork Wed Nov 23 19:00:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric DeCosta X-Patchwork-Id: 13054133 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1A523C3A59F for ; Wed, 23 Nov 2022 19:00:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239378AbiKWTAy (ORCPT ); Wed, 23 Nov 2022 14:00:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239159AbiKWTAw (ORCPT ); Wed, 23 Nov 2022 14:00:52 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C05491C3E for ; Wed, 23 Nov 2022 11:00:51 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id 83-20020a1c0256000000b003d03017c6efso1517615wmc.4 for ; Wed, 23 Nov 2022 11:00:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=WlX8fRoMkPnEolpDAbYR1NXBm53dpHMaf0+jydPyO8c=; b=emaLUmtqkoChOqRcbodtoAaY37x1SZSrHq0eDIrvFAbhIUkWIsSA+UHguqiczIaud0 NMr6llJuLPz6K2sBlmjKB420rUyD7uPOxALLp8bwO610TdzKSJeNevInbSOL9IG5mntR Tr5OB+tkpFHIXpPwJX8N+wg0owv/yMZQvS4Eul/surWEEsg7WB0UHVxPz5/2OrCmU6tD wsSiXuz+/CjQc+HAq8r4bdzTA25l8p/Fpk092MDszwQBbwY8mC9itMYsist/3JdZ5rs+ DhvUpR3Jxz0+2nHBq1+brCRwXy6vUM6lhMAa411/mtA1c7aquv6V7Fh5TzGHvZkOfMOv TIFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WlX8fRoMkPnEolpDAbYR1NXBm53dpHMaf0+jydPyO8c=; b=exGq/ThC0DDHUwr/Kv/5zrfLxVeJdkKoJy3OJSauhQXZUUnM+IZQFLI6PLGvMYxR8e hJGfPuTOsAX+7NGps183adCiBfUW6NQ7n0DzvKP8Wl4ofnWqfMoiXWOMeBIYntSjoIAh oxm2hNezKzZbP2BWcxP6vSfyuiR6VxL7bzcQ/mk6VwRTgrKTczZpKEuwetC581tnUmHq RMThcLyh4CCy8SXLM24ZNao4kbvNMta2Ij1K3xTjlBybaBHd0yfOp/yGokKJ4QUiq04z sYR5rpZbjjApYQmdQKK4Vpw6U8RRZN2wIziGRb7NBIes49y1TUxMZ6BTDA84bERFqngo Unpw== X-Gm-Message-State: ANoB5pkA3/CBYR0rfmoB3mWCBo8WCZ8GGZHGIv6wsPPSi7+IT9vg7tvq 6yFGSI+gv8ug89a+5nxMCWw3EiZwmbo= X-Google-Smtp-Source: AA0mqf5UjVrv8BM2Xtkdq37PCaLiZrB6WvMxpiyfGpM9/LyTpqmBMdE/cvOe2OMmOV02bTFVsXxsgQ== X-Received: by 2002:a05:600c:3c96:b0:3cf:a457:2d89 with SMTP id bg22-20020a05600c3c9600b003cfa4572d89mr24138186wmb.20.1669230049495; Wed, 23 Nov 2022 11:00:49 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id iw8-20020a05600c54c800b003cfbe1da539sm3115993wmb.36.2022.11.23.11.00.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Nov 2022 11:00:48 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 23 Nov 2022 19:00:40 +0000 Subject: [PATCH v4 2/6] fsmonitor: determine if filesystem is local or remote Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnI=?= =?utf-8?b?w7A=?= Bjarmason , Glen Choo , Johannes Schindelin , Taylor Blau , Eric DeCosta , Eric DeCosta Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Eric DeCosta From: Eric DeCosta Compare the given path to the mounted filesystems. Find the mount that is the longest prefix of the path (if any) and determine if that mount is on a local or remote filesystem. Signed-off-by: Eric DeCosta --- compat/fsmonitor/fsm-path-utils-linux.c | 186 ++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 compat/fsmonitor/fsm-path-utils-linux.c diff --git a/compat/fsmonitor/fsm-path-utils-linux.c b/compat/fsmonitor/fsm-path-utils-linux.c new file mode 100644 index 00000000000..d3281422ebc --- /dev/null +++ b/compat/fsmonitor/fsm-path-utils-linux.c @@ -0,0 +1,186 @@ +#include "fsmonitor.h" +#include "fsmonitor-path-utils.h" +#include +#include +#include +#include +#include + +static int is_remote_fs(const char* path) { + struct statfs fs; + + if (statfs(path, &fs)) { + error_errno(_("statfs('%s') failed"), path); + return -1; + } + + switch (fs.f_type) { + case 0x61636673: /* ACFS */ + case 0x5346414F: /* AFS */ + case 0x00C36400: /* CEPH */ + case 0xFF534D42: /* CIFS */ + case 0x73757245: /* CODA */ + case 0x19830326: /* FHGFS */ + case 0x1161970: /* GFS */ + case 0x47504653: /* GPFS */ + case 0x013111A8: /* IBRIX */ + case 0x6B414653: /* KAFS */ + case 0x0BD00BD0: /* LUSTRE */ + case 0x564C: /* NCP */ + case 0x6969: /* NFS */ + case 0x6E667364: /* NFSD */ + case 0x7461636f: /* OCFS2 */ + case 0xAAD7AAEA: /* PANFS */ + case 0x517B: /* SMB */ + case 0xBEEFDEAD: /* SNFS */ + case 0xFE534D42: /* SMB2 */ + case 0xBACBACBC: /* VMHGFS */ + case 0xA501FCF5: /* VXFS */ + return 1; + default: + break; + } + + return 0; +} + +static int find_mount(const char *path, const struct statvfs *fs, + struct mntent *ent) +{ + const char *const mounts = "/proc/mounts"; + const char *rp = real_pathdup(path, 1); + struct mntent *ment = NULL; + struct statvfs mntfs; + FILE *fp; + int found = 0; + int dlen, plen, flen = 0; + + ent->mnt_fsname = NULL; + ent->mnt_dir = NULL; + ent->mnt_type = NULL; + + fp = setmntent(mounts, "r"); + if (!fp) { + error_errno(_("setmntent('%s') failed"), mounts); + return -1; + } + + plen = strlen(rp); + + /* read all the mount information and compare to path */ + while ((ment = getmntent(fp)) != NULL) { + if (statvfs(ment->mnt_dir, &mntfs)) { + switch (errno) { + case EPERM: + case ESRCH: + case EACCES: + continue; + default: + error_errno(_("statvfs('%s') failed"), ment->mnt_dir); + endmntent(fp); + return -1; + } + } + + /* is mount on the same filesystem and is a prefix of the path */ + if ((fs->f_fsid == mntfs.f_fsid) && + !strncmp(ment->mnt_dir, rp, strlen(ment->mnt_dir))) { + dlen = strlen(ment->mnt_dir); + if (dlen > plen) + continue; + /* + * root is always a potential match; otherwise look for + * directory prefix + */ + if ((dlen == 1 && ment->mnt_dir[0] == '/') || + (dlen > flen && (!rp[dlen] || rp[dlen] == '/'))) { + flen = dlen; + /* + * https://man7.org/linux/man-pages/man3/getmntent.3.html + * + * The pointer points to a static area of memory which is + * overwritten by subsequent calls to getmntent(). + */ + found = 1; + free(ent->mnt_fsname); + free(ent->mnt_dir); + free(ent->mnt_type); + ent->mnt_fsname = xstrdup(ment->mnt_fsname); + ent->mnt_dir = xstrdup(ment->mnt_dir); + ent->mnt_type = xstrdup(ment->mnt_type); + } + } + } + endmntent(fp); + + if (!found) + return -1; + + return 0; +} + +int fsmonitor__get_fs_info(const char *path, struct fs_info *fs_info) +{ + struct mntent ment; + struct statvfs fs; + + if (statvfs(path, &fs)) + return error_errno(_("statvfs('%s') failed"), path); + + + if (find_mount(path, &fs, &ment) < 0) { + free(ment.mnt_fsname); + free(ment.mnt_dir); + free(ment.mnt_type); + return -1; + } + + trace_printf_key(&trace_fsmonitor, + "statvfs('%s') [flags 0x%08lx] '%s' '%s'", + path, fs.f_flag, ment.mnt_type, ment.mnt_fsname); + + fs_info->is_remote = is_remote_fs(ment.mnt_dir); + fs_info->typename = ment.mnt_fsname; + free(ment.mnt_dir); + free(ment.mnt_type); + + if (fs_info->is_remote < 0) { + free(ment.mnt_fsname); + return -1; + } + + trace_printf_key(&trace_fsmonitor, + "'%s' is_remote: %d", + path, fs_info->is_remote); + + return 0; +} + +int fsmonitor__is_fs_remote(const char *path) +{ + struct fs_info fs; + + if (fsmonitor__get_fs_info(path, &fs)) + return -1; + + free(fs.typename); + + return fs.is_remote; +} + +/* + * No-op for now. + */ +int fsmonitor__get_alias(const char *path, struct alias_info *info) +{ + return 0; +} + +/* + * No-op for now. + */ +char *fsmonitor__resolve_alias(const char *path, + const struct alias_info *info) +{ + return NULL; +} From patchwork Wed Nov 23 19:00:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric DeCosta X-Patchwork-Id: 13054134 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80C16C4332F for ; Wed, 23 Nov 2022 19:01:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239357AbiKWTBI (ORCPT ); Wed, 23 Nov 2022 14:01:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239374AbiKWTAy (ORCPT ); Wed, 23 Nov 2022 14:00:54 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE01C922F7 for ; Wed, 23 Nov 2022 11:00:52 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id m7-20020a05600c090700b003cf8a105d9eso1948549wmp.5 for ; Wed, 23 Nov 2022 11:00:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=NWVyY03VyOHrfSqJa6fkgcP3t9WmHSfFLgDykLbO4hQ=; b=FRKYt2mWxT9a2FWkFZwrHg+eNnonRVayeDb6pNc9IbSXRrZmwT9pnyB8lH52cLGROy DZoYH6klDNBC4ZipX+2j+8GJdbaKFYkEibjaIqas0mk+NJp36C/9WTTu6FkLdfGKgz2h xR5LAGnnHiu8AUGJMG93ogIadQvq8+4dIHtA8mI9TCDKYwgEZLbN+2vmK2krFOLr4hKO VRVjR716WEFKJOajLTDxQEhAEmbnjQrFz4WV70deIfyGHkAI6mkVexrS4UQY3nEZDFPk zUgM7315aBpuaowUT6gr14H+FoS/kKf3cpYkEm+3sJL4CtPw2boR5Cg43OUwk8mtE8a/ HpmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NWVyY03VyOHrfSqJa6fkgcP3t9WmHSfFLgDykLbO4hQ=; b=4aAXFqyhePmw6hcw23caDFoJ3Fp3bypg58CrJQIJmY/iKyOg+ITlDM1lD0DYYXOjOH m8rsoDno35owMHYd7RlIgC5qquufuXgaWc/RnVghMM9HvNMAdS+orJFmTIACkB/H4wci CEZJRM+0Yspli/azTHsXdWH0PgE8lF/7bOQazS7bU36NoxRPu2T+qpMcTArXCWl/n5Lt 2cVDS9Q/Ho6dv+GLoDr6e5bYRKr2j6BT5s/d7pKeXt0x8ctgPPgkR4O3lTpm8PST/+Xp PkGVR5LSFUIu8T9ozFnky3p5lj5HOKXHm/DWn2sW92+Oi/SDFYM6hOLLurFtrwL/NwiQ lNdg== X-Gm-Message-State: ANoB5plj5sfqTH+lSIJr+T3xPN0fk9uebdMFqPDYqHjyX7zTvagK/bUS bjVMAGa+T3h9kodziu6xOa2UnT8GbvA= X-Google-Smtp-Source: AA0mqf4PAl7GptxOdk9IbY8Jxqzi5Dm1OxCQP3L1A6yIk+Ev5N3JwNbgKR+nRIYtsfv53rf18l1FDQ== X-Received: by 2002:a05:600c:3542:b0:3cf:6c2f:950c with SMTP id i2-20020a05600c354200b003cf6c2f950cmr13955254wmq.146.1669230050952; Wed, 23 Nov 2022 11:00:50 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c3-20020adffb43000000b00225307f43fbsm17212597wrs.44.2022.11.23.11.00.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Nov 2022 11:00:50 -0800 (PST) Message-Id: <80282efef5779728022a317388da6790e5db44e2.1669230044.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 23 Nov 2022 19:00:41 +0000 Subject: [PATCH v4 3/6] fsmonitor: implement filesystem change listener for Linux Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnI=?= =?utf-8?b?w7A=?= Bjarmason , Glen Choo , Johannes Schindelin , Taylor Blau , Eric DeCosta , Eric DeCosta Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Eric DeCosta From: Eric DeCosta Implement a filesystem change listener for Linux based on the inotify API: https://man7.org/linux/man-pages/man7/inotify.7.html inotify requires registering a watch on every directory in the worktree and special handling of moves/renames. Signed-off-by: Eric DeCosta --- compat/fsmonitor/fsm-listen-linux.c | 676 ++++++++++++++++++++++++++++ 1 file changed, 676 insertions(+) create mode 100644 compat/fsmonitor/fsm-listen-linux.c diff --git a/compat/fsmonitor/fsm-listen-linux.c b/compat/fsmonitor/fsm-listen-linux.c new file mode 100644 index 00000000000..e8548e4e009 --- /dev/null +++ b/compat/fsmonitor/fsm-listen-linux.c @@ -0,0 +1,676 @@ +#include "cache.h" +#include "fsmonitor.h" +#include "fsm-listen.h" +#include "fsmonitor--daemon.h" +#include +#include +#include +#include + +/* + * Safe value to bitwise OR with rest of mask for + * kernels that do not support IN_MASK_CREATE + */ +#ifndef IN_MASK_CREATE +#define IN_MASK_CREATE 0x00000000 +#endif + +enum shutdown_reason { + SHUTDOWN_CONTINUE = 0, + SHUTDOWN_STOP, + SHUTDOWN_ERROR, + SHUTDOWN_FORCE +}; + +struct watch_entry { + struct hashmap_entry ent; + int wd; + uint32_t cookie; + const char *dir; +}; + +struct rename_entry { + struct hashmap_entry ent; + time_t whence; + uint32_t cookie; + const char *dir; +}; + +struct fsm_listen_data { + int fd_inotify; + enum shutdown_reason shutdown; + struct hashmap watches; + struct hashmap renames; + struct hashmap revwatches; +}; + +static int watch_entry_cmp(const void *cmp_data, + const struct hashmap_entry *eptr, + const struct hashmap_entry *entry_or_key, + const void *keydata) +{ + const struct watch_entry *e1, *e2; + + e1 = container_of(eptr, const struct watch_entry, ent); + e2 = container_of(eptr, const struct watch_entry, ent); + return e1->wd != e2->wd; +} + +static int revwatches_entry_cmp(const void *cmp_data, + const struct hashmap_entry *eptr, + const struct hashmap_entry *entry_or_key, + const void *keydata) +{ + const struct watch_entry *e1, *e2; + + e1 = container_of(eptr, const struct watch_entry, ent); + e2 = container_of(eptr, const struct watch_entry, ent); + return strcmp(e1->dir, e2->dir); +} + +static int rename_entry_cmp(const void *cmp_data, + const struct hashmap_entry *eptr, + const struct hashmap_entry *entry_or_key, + const void *keydata) +{ + const struct rename_entry *e1, *e2; + + e1 = container_of(eptr, const struct rename_entry, ent); + e2 = container_of(eptr, const struct rename_entry, ent); + return e1->cookie != e2->cookie; +} + +/* + * Register an inotify watch, add watch descriptor to path mapping + * and the reverse mapping. + */ +static int add_watch(const char *path, struct fsm_listen_data *data) +{ + const char *interned = strintern(path); + struct watch_entry *w1, *w2; + + /* add the inotify watch, don't allow watches to be modified */ + int wd = inotify_add_watch(data->fd_inotify, interned, + (IN_ALL_EVENTS | IN_ONLYDIR | IN_MASK_CREATE) + ^ IN_ACCESS ^ IN_CLOSE ^ IN_OPEN); + if (wd < 0) + return error_errno("inotify_add_watch('%s') failed", interned); + + /* add watch descriptor -> directory mapping */ + CALLOC_ARRAY(w1, 1); + w1->wd = wd; + w1->dir = interned; + hashmap_entry_init(&w1->ent, memhash(&w1->wd, sizeof(int))); + hashmap_add(&data->watches, &w1->ent); + + /* add directory -> watch descriptor mapping */ + CALLOC_ARRAY(w2, 1); + w2->wd = wd; + w2->dir = interned; + hashmap_entry_init(&w2->ent, memhash(w2->dir, strlen(w2->dir))); + hashmap_add(&data->revwatches, &w2->ent); + + return 0; +} + +/* + * Remove the inotify watch, the watch descriptor to path mapping + * and the reverse mapping. + */ +static void remove_watch(struct watch_entry *w, + struct fsm_listen_data *data) +{ + struct watch_entry k1, k2, *w1, *w2; + + /* remove watch, ignore error if kernel already did it */ + if (inotify_rm_watch(data->fd_inotify, w->wd) && errno != EINVAL) + error_errno("inotify_rm_watch() failed"); + + hashmap_entry_init(&k1.ent, memhash(&w->wd, sizeof(int))); + w1 = hashmap_remove_entry(&data->watches, &k1, ent, NULL); + if (!w1) + BUG("Double remove of watch for '%s'", w->dir); + + if (w1->cookie) + BUG("Removing watch for '%s' which has a pending rename", w1->dir); + + hashmap_entry_init(&k2.ent, memhash(w->dir, strlen(w->dir))); + w2 = hashmap_remove_entry(&data->revwatches, &k2, ent, NULL); + if (!w2) + BUG("Double remove of reverse watch for '%s'", w->dir); + + /* w1->dir and w2->dir are interned strings, we don't own them */ + free(w1); + free(w2); +} + +/* + * Check for stale directory renames. + * + * https://man7.org/linux/man-pages/man7/inotify.7.html + * + * Allow for some small timeout to account for the fact that insertion of the + * IN_MOVED_FROM+IN_MOVED_TO event pair is not atomic, and the possibility that + * there may not be any IN_MOVED_TO event. + * + * If the IN_MOVED_TO event is not received within the timeout then events have + * been missed and the monitor is in an inconsistent state with respect to the + * filesystem. + */ +static int check_stale_dir_renames(struct hashmap *renames, time_t max_age) +{ + struct rename_entry *re; + struct hashmap_iter iter; + + hashmap_for_each_entry(renames, &iter, re, ent) { + if (re->whence <= max_age) + return -1; + } + return 0; +} + +/* + * Track pending renames. + * + * Tracking is done via a event cookie to watch descriptor mapping. + * + * A rename is not complete until matching a IN_MOVED_TO event is received + * for a corresponding IN_MOVED_FROM event. + */ +static void add_dir_rename(uint32_t cookie, const char *path, + struct fsm_listen_data *data) +{ + struct watch_entry k, *w; + struct rename_entry *re; + + /* lookup the watch descriptor for the given path */ + hashmap_entry_init(&k.ent, memhash(path, strlen(path))); + w = hashmap_get_entry(&data->revwatches, &k, ent, NULL); + if (!w) /* should never happen */ + BUG("No watch for '%s'", path); + w->cookie = cookie; + + /* add the pending rename to match against later */ + CALLOC_ARRAY(re, 1); + re->dir = w->dir; + re->cookie = w->cookie; + re->whence = time(NULL); + hashmap_entry_init(&re->ent, memhash(&re->cookie, sizeof(uint32_t))); + hashmap_add(&data->renames, &re->ent); +} + +/* + * Handle directory renames + * + * Once a IN_MOVED_TO event is received, lookup the rename tracking information + * via the event cookie and use this information to update the watch. + */ +static void rename_dir(uint32_t cookie, const char *path, + struct fsm_listen_data *data) +{ + struct rename_entry rek, *re; + struct watch_entry k, *w; + + /* lookup a pending rename to match */ + rek.cookie = cookie; + hashmap_entry_init(&rek.ent, memhash(&rek.cookie, sizeof(uint32_t))); + re = hashmap_get_entry(&data->renames, &rek, ent, NULL); + if (re) { + k.dir = re->dir; + hashmap_entry_init(&k.ent, memhash(k.dir, strlen(k.dir))); + w = hashmap_get_entry(&data->revwatches, &k, ent, NULL); + if (w) { + w->cookie = 0; /* rename handled */ + remove_watch(w, data); + add_watch(path, data); + } else { + BUG("No matching watch"); + } + } else { + BUG("No matching cookie"); + } +} + +/* + * Recursively add watches to every directory under path + */ +static int register_inotify(const char *path, + struct fsmonitor_daemon_state *state, + struct fsmonitor_batch *batch) +{ + DIR *dir; + const char *rel; + struct strbuf current = STRBUF_INIT; + struct dirent *de; + struct stat fs; + int ret = -1; + + dir = opendir(path); + if (!dir) + return error_errno("opendir('%s') failed", path); + + while ((de = readdir_skip_dot_and_dotdot(dir)) != NULL) { + strbuf_reset(¤t); + strbuf_addf(¤t, "%s/%s", path, de->d_name); + if (lstat(current.buf, &fs)) { + error_errno("lstat('%s') failed", current.buf); + goto failed; + } + + /* recurse into directory */ + if (S_ISDIR(fs.st_mode)) { + if (add_watch(current.buf, state->listen_data)) + goto failed; + if (register_inotify(current.buf, state, batch)) + goto failed; + } else if (batch) { + rel = current.buf + state->path_worktree_watch.len + 1; + trace_printf_key(&trace_fsmonitor, "explicitly adding '%s'", rel); + fsmonitor_batch__add_path(batch, rel); + } + } + ret = 0; + +failed: + strbuf_release(¤t); + if (closedir(dir) < 0) + return error_errno("closedir('%s') failed", path); + return ret; +} + +static int em_rename_dir_from(u_int32_t mask) +{ + return ((mask & IN_ISDIR) && (mask & IN_MOVED_FROM)); +} + +static int em_rename_dir_to(u_int32_t mask) +{ + return ((mask & IN_ISDIR) && (mask & IN_MOVED_TO)); +} + +static int em_remove_watch(u_int32_t mask) +{ + return (mask & IN_DELETE_SELF); +} + +static int em_dir_renamed(u_int32_t mask) +{ + return ((mask & IN_ISDIR) && (mask & IN_MOVE)); +} + +static int em_dir_created(u_int32_t mask) +{ + return ((mask & IN_ISDIR) && (mask & IN_CREATE)); +} + +static int em_dir_deleted(uint32_t mask) +{ + return ((mask & IN_ISDIR) && (mask & IN_DELETE)); +} + +static int em_force_shutdown(u_int32_t mask) +{ + return (mask & IN_UNMOUNT) || (mask & IN_Q_OVERFLOW); +} + +static int em_ignore(u_int32_t mask) +{ + return (mask & IN_IGNORED) || (mask & IN_MOVE_SELF); +} + +static void log_mask_set(const char *path, u_int32_t mask) +{ + struct strbuf msg = STRBUF_INIT; + + if (mask & IN_ACCESS) + strbuf_addstr(&msg, "IN_ACCESS|"); + if (mask & IN_MODIFY) + strbuf_addstr(&msg, "IN_MODIFY|"); + if (mask & IN_ATTRIB) + strbuf_addstr(&msg, "IN_ATTRIB|"); + if (mask & IN_CLOSE_WRITE) + strbuf_addstr(&msg, "IN_CLOSE_WRITE|"); + if (mask & IN_CLOSE_NOWRITE) + strbuf_addstr(&msg, "IN_CLOSE_NOWRITE|"); + if (mask & IN_OPEN) + strbuf_addstr(&msg, "IN_OPEN|"); + if (mask & IN_MOVED_FROM) + strbuf_addstr(&msg, "IN_MOVED_FROM|"); + if (mask & IN_MOVED_TO) + strbuf_addstr(&msg, "IN_MOVED_TO|"); + if (mask & IN_CREATE) + strbuf_addstr(&msg, "IN_CREATE|"); + if (mask & IN_DELETE) + strbuf_addstr(&msg, "IN_DELETE|"); + if (mask & IN_DELETE_SELF) + strbuf_addstr(&msg, "IN_DELETE_SELF|"); + if (mask & IN_MOVE_SELF) + strbuf_addstr(&msg, "IN_MOVE_SELF|"); + if (mask & IN_UNMOUNT) + strbuf_addstr(&msg, "IN_UNMOUNT|"); + if (mask & IN_Q_OVERFLOW) + strbuf_addstr(&msg, "IN_Q_OVERFLOW|"); + if (mask & IN_IGNORED) + strbuf_addstr(&msg, "IN_IGNORED|"); + if (mask & IN_ISDIR) + strbuf_addstr(&msg, "IN_ISDIR|"); + + trace_printf_key(&trace_fsmonitor, "inotify_event: '%s', mask=%#8.8x %s", + path, mask, msg.buf); + + strbuf_release(&msg); +} + +int fsm_listen__ctor(struct fsmonitor_daemon_state *state) +{ + int fd; + int ret = 0; + struct fsm_listen_data *data; + + CALLOC_ARRAY(data, 1); + state->listen_data = data; + state->listen_error_code = -1; + data->shutdown = SHUTDOWN_ERROR; + + fd = inotify_init1(O_NONBLOCK); + if (fd < 0) + return error_errno("inotify_init1() failed"); + + data->fd_inotify = fd; + + hashmap_init(&data->watches, watch_entry_cmp, NULL, 0); + hashmap_init(&data->renames, rename_entry_cmp, NULL, 0); + hashmap_init(&data->revwatches, revwatches_entry_cmp, NULL, 0); + + if (add_watch(state->path_worktree_watch.buf, data)) + ret = -1; + else if (register_inotify(state->path_worktree_watch.buf, state, NULL)) + ret = -1; + else if (state->nr_paths_watching > 1) { + if (add_watch(state->path_gitdir_watch.buf, data)) + ret = -1; + else if (register_inotify(state->path_gitdir_watch.buf, state, NULL)) + ret = -1; + } + + if (!ret) { + state->listen_error_code = 0; + data->shutdown = SHUTDOWN_CONTINUE; + } + + return ret; +} + +void fsm_listen__dtor(struct fsmonitor_daemon_state *state) +{ + struct fsm_listen_data *data; + struct hashmap_iter iter; + struct watch_entry *w; + int fd; + + if (!state || !state->listen_data) + return; + + data = state->listen_data; + fd = data->fd_inotify; + + hashmap_for_each_entry(&data->watches, &iter, w, ent) { + w->cookie = 0; /* ignore any pending renames */ + remove_watch(w, data); + } + hashmap_clear(&data->watches); + + hashmap_clear(&data->revwatches); /* remove_watch freed the entries */ + + hashmap_clear_and_free(&data->renames, struct rename_entry, ent); + + FREE_AND_NULL(state->listen_data); + + if (fd && (close(fd) < 0)) + error_errno(_("closing inotify file descriptor failed")); +} + +void fsm_listen__stop_async(struct fsmonitor_daemon_state *state) +{ + if (!state->listen_data->shutdown) + state->listen_data->shutdown = SHUTDOWN_STOP; +} + +/* + * Process a single inotify event and queue for publication. + */ +static int process_event(const char *path, + const struct inotify_event *event, + struct fsmonitor_batch *batch, + struct string_list *cookie_list, + struct fsmonitor_daemon_state *state) +{ + const char *rel; + const char *last_sep; + + switch (fsmonitor_classify_path_absolute(state, path)) { + case IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX: + case IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX: + /* Use just the filename of the cookie file. */ + last_sep = find_last_dir_sep(path); + string_list_append(cookie_list, + last_sep ? last_sep + 1 : path); + break; + case IS_INSIDE_DOT_GIT: + case IS_INSIDE_GITDIR: + break; + case IS_DOT_GIT: + case IS_GITDIR: + /* + * If .git directory is deleted or renamed away, + * we have to quit. + */ + if (em_dir_deleted(event->mask)) { + trace_printf_key(&trace_fsmonitor, + "event: gitdir removed"); + state->listen_data->shutdown = SHUTDOWN_FORCE; + goto done; + } + + if (em_dir_renamed(event->mask)) { + trace_printf_key(&trace_fsmonitor, + "event: gitdir renamed"); + state->listen_data->shutdown = SHUTDOWN_FORCE; + goto done; + } + break; + case IS_WORKDIR_PATH: + /* normal events in the working directory */ + if (trace_pass_fl(&trace_fsmonitor)) + log_mask_set(path, event->mask); + + rel = path + state->path_worktree_watch.len + 1; + fsmonitor_batch__add_path(batch, rel); + + if (em_dir_deleted(event->mask)) + break; + + /* received IN_MOVE_FROM, add tracking for expected IN_MOVE_TO */ + if (em_rename_dir_from(event->mask)) + add_dir_rename(event->cookie, path, state->listen_data); + + /* received IN_MOVE_TO, update watch to reflect new path */ + if (em_rename_dir_to(event->mask)) { + rename_dir(event->cookie, path, state->listen_data); + if (register_inotify(path, state, batch)) { + state->listen_data->shutdown = SHUTDOWN_ERROR; + goto done; + } + } + + if (em_dir_created(event->mask)) { + if (add_watch(path, state->listen_data)) { + state->listen_data->shutdown = SHUTDOWN_ERROR; + goto done; + } + if (register_inotify(path, state, batch)) { + state->listen_data->shutdown = SHUTDOWN_ERROR; + goto done; + } + } + break; + case IS_OUTSIDE_CONE: + default: + trace_printf_key(&trace_fsmonitor, + "ignoring '%s'", path); + break; + } + return 0; +done: + return -1; +} + +/* + * Read the inotify event stream and pre-process events before further + * processing and eventual publishing. + */ +static void handle_events(struct fsmonitor_daemon_state *state) +{ + /* See https://man7.org/linux/man-pages/man7/inotify.7.html */ + char buf[4096] + __attribute__ ((aligned(__alignof__(struct inotify_event)))); + + struct hashmap watches = state->listen_data->watches; + struct fsmonitor_batch *batch = NULL; + struct string_list cookie_list = STRING_LIST_INIT_DUP; + struct watch_entry k, *w; + struct strbuf path; + const struct inotify_event *event; + int fd = state->listen_data->fd_inotify; + ssize_t len; + char *ptr, *p; + + strbuf_init(&path, PATH_MAX); + + for(;;) { + len = read(fd, buf, sizeof(buf)); + if (len == -1 && errno != EAGAIN) { + error_errno(_("reading inotify message stream failed")); + state->listen_data->shutdown = SHUTDOWN_ERROR; + goto done; + } + + /* nothing to read */ + if (len <= 0) + goto done; + + /* Loop over all events in the buffer. */ + for (ptr = buf; ptr < buf + len; + ptr += sizeof(struct inotify_event) + event->len) { + + event = (const struct inotify_event *) ptr; + + if (em_ignore(event->mask)) + continue; + + /* File system was unmounted or event queue overflowed */ + if (em_force_shutdown(event->mask)) { + if (trace_pass_fl(&trace_fsmonitor)) + log_mask_set("Forcing shutdown", event->mask); + state->listen_data->shutdown = SHUTDOWN_FORCE; + goto done; + } + + hashmap_entry_init(&k.ent, memhash(&event->wd, sizeof(int))); + k.wd = event->wd; + + w = hashmap_get_entry(&watches, &k, ent, NULL); + if (!w) /* should never happen */ + BUG("No watch for '%s'", event->name); + + /* directory watch was removed */ + if (em_remove_watch(event->mask)) { + remove_watch(w, state->listen_data); + continue; + } + + strbuf_reset(&path); + strbuf_add(&path, w->dir, strlen(w->dir)); + strbuf_addch(&path, '/'); + strbuf_addstr(&path, event->name); + + p = fsmonitor__resolve_alias(path.buf, &state->alias); + if (!p) + p = strbuf_detach(&path, NULL); + + if (!batch) + batch = fsmonitor_batch__new(); + + if (process_event(p, event, batch, &cookie_list, state)) { + free(p); + goto done; + } + free(p); + } + strbuf_reset(&path); + fsmonitor_publish(state, batch, &cookie_list); + string_list_clear(&cookie_list, 0); + batch = NULL; + } +done: + strbuf_release(&path); + fsmonitor_batch__free_list(batch); + string_list_clear(&cookie_list, 0); +} + +/* + * Non-blocking read of the inotify events stream. The inotify fd is polled + * frequently to help minimize the number of queue overflows. + */ +void fsm_listen__loop(struct fsmonitor_daemon_state *state) +{ + int poll_num; + const int interval = 1000; + time_t checked = time(NULL); + struct pollfd fds[1]; + fds[0].fd = state->listen_data->fd_inotify; + fds[0].events = POLLIN; + + for(;;) { + switch (state->listen_data->shutdown) { + case SHUTDOWN_CONTINUE: + poll_num = poll(fds, 1, 1); + if (poll_num == -1) { + if (errno == EINTR) + continue; + error_errno(_("polling inotify message stream failed")); + state->listen_data->shutdown = SHUTDOWN_ERROR; + continue; + } + + if ((time(NULL) - checked) >= interval) { + checked = time(NULL); + if (check_stale_dir_renames(&state->listen_data->renames, + checked - interval)) { + trace_printf_key(&trace_fsmonitor, + "Missed IN_MOVED_TO events, forcing shutdown"); + state->listen_data->shutdown = SHUTDOWN_FORCE; + continue; + } + } + + if (poll_num > 0 && (fds[0].revents & POLLIN)) + handle_events(state); + + continue; + case SHUTDOWN_ERROR: + state->listen_error_code = -1; + ipc_server_stop_async(state->ipc_server_data); + break; + case SHUTDOWN_FORCE: + state->listen_error_code = 0; + ipc_server_stop_async(state->ipc_server_data); + break; + case SHUTDOWN_STOP: + default: + state->listen_error_code = 0; + break; + } + return; + } +} From patchwork Wed Nov 23 19:00:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric DeCosta X-Patchwork-Id: 13054135 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A6328C4332F for ; Wed, 23 Nov 2022 19:01:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239381AbiKWTBV (ORCPT ); Wed, 23 Nov 2022 14:01:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238833AbiKWTBD (ORCPT ); Wed, 23 Nov 2022 14:01:03 -0500 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB18E91C3E for ; Wed, 23 Nov 2022 11:00:53 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id n7so3216726wrr.13 for ; Wed, 23 Nov 2022 11:00:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=ng/xg6yasgRwguK3X1vnGO3/VVD9jc7oLvVlSGtH/ws=; b=ZwglMKUkwA2AHb8mOeZI1l6nxfStvD5rJzNBptY+U2x/4Tjrc/Yc0PcEfDI/RespCe sNuiqtdS+klJBc1PyDyvsQd4a6vrwY2jZcuUSY1a3m5TwpS0uxbVLzP33TjITvkZKEjt fLVubaEm3Dthu4V+WFduVN/TT9NDSt5vQRmzoz0NqMKO/GrHhXGMKYJylfBsYqanOiGg 7/qrETr616z1SKdCSces1ja8WQpDUDS+WbGA+SFLjo9U6vyPidus1ytsGbsS4eI6nNgA t66a8vUvZOIHanWJ/+3YHozYOMlbCUJm0ubZoDe8GJ+X9U/S3RwGikltqpoZ2oV7UeCz Txbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ng/xg6yasgRwguK3X1vnGO3/VVD9jc7oLvVlSGtH/ws=; b=xJu6bodU2mBC8rAHhKpFuwX+9d5kujlgFc/4uqL3iCv2d+YJMWHvJLDVb3Rd5d+WkU 3ts0Xcqpm8M3Y4gmUdUKl+gg3FF8E138MrkxLoQXFCImgQPU+zEOsBp3hJhZnfASrrSd xh10r0eBLBmcLMyQp49KGWhQKreZfm5vyT6hjTQyrh1H2Nv3xVKP/HVLUy+eBmep5y77 Fh+k9RUwZr6F5IdigU5jG8IGv0QqJPPz++iKItjJ9jkBgosSHRslA/psDbfnDoEeevfV 7n7YmAq9UDupv7NwKhGkfyJjNcSsi4kD07ts5E4ryGUkAUx3ppPKyPw8IClxGE23jORZ yVqw== X-Gm-Message-State: ANoB5pkIzc7frySzfTVB3eNRAnMMbLon///KMLUzlAH4wjN/edjEUBKD BOdfwj//wOabtmXUuu+ZlN5CfxYEfns= X-Google-Smtp-Source: AA0mqf5AxGnBo/GhSwVYSaci7IRvcPEufXZ2rNOcRGaQTDsOPUbzYAMA44Pbqyze4liKmEmvU6cOLA== X-Received: by 2002:adf:f944:0:b0:236:8f54:f1f4 with SMTP id q4-20020adff944000000b002368f54f1f4mr18341711wrr.654.1669230052206; Wed, 23 Nov 2022 11:00:52 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id ay2-20020a05600c1e0200b003cf7055c014sm3348282wmb.1.2022.11.23.11.00.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Nov 2022 11:00:51 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 23 Nov 2022 19:00:42 +0000 Subject: [PATCH v4 4/6] fsmonitor: enable fsmonitor for Linux Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnI=?= =?utf-8?b?w7A=?= Bjarmason , Glen Choo , Johannes Schindelin , Taylor Blau , Eric DeCosta , Eric DeCosta Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Eric DeCosta From: Eric DeCosta Update build to enable fsmonitor for Linux. Signed-off-by: Eric DeCosta --- config.mak.uname | 8 ++++++++ contrib/buildsystems/CMakeLists.txt | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/config.mak.uname b/config.mak.uname index d63629fe807..5890d810463 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -68,6 +68,14 @@ ifeq ($(uname_S),Linux) ifneq ($(findstring .el7.,$(uname_R)),) BASIC_CFLAGS += -std=c99 endif + # The builtin FSMonitor on Linux builds upon Simple-IPC. Both require + # Unix domain sockets and PThreads. + ifndef NO_PTHREADS + ifndef NO_UNIX_SOCKETS + FSMONITOR_DAEMON_BACKEND = linux + FSMONITOR_OS_SETTINGS = linux + endif + endif endif ifeq ($(uname_S),GNU/kFreeBSD) HAVE_ALLOCA_H = YesPlease diff --git a/contrib/buildsystems/CMakeLists.txt b/contrib/buildsystems/CMakeLists.txt index 3957e4cf8cd..f058c3d19b4 100644 --- a/contrib/buildsystems/CMakeLists.txt +++ b/contrib/buildsystems/CMakeLists.txt @@ -304,7 +304,16 @@ else() endif() if(SUPPORTS_SIMPLE_IPC) - if(CMAKE_SYSTEM_NAME STREQUAL "Windows") + if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + add_compile_definitions(HAVE_FSMONITOR_DAEMON_BACKEND) + list(APPEND compat_SOURCES compat/fsmonitor/fsm-listen-linux.c) + list(APPEND compat_SOURCES compat/fsmonitor/fsm-health-linux.c) + list(APPEND compat_SOURCES compat/fsmonitor/fsm-ipc-linux.c) + list(APPEND compat_SOURCES compat/fsmonitor/fsm-path-utils-linux.c) + + add_compile_definitions(HAVE_FSMONITOR_OS_SETTINGS) + list(APPEND compat_SOURCES compat/fsmonitor/fsm-settings-linux.c) + elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows") add_compile_definitions(HAVE_FSMONITOR_DAEMON_BACKEND) list(APPEND compat_SOURCES compat/fsmonitor/fsm-listen-win32.c) list(APPEND compat_SOURCES compat/fsmonitor/fsm-health-win32.c) From patchwork Wed Nov 23 19:00:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric DeCosta X-Patchwork-Id: 13054136 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45567C433FE for ; Wed, 23 Nov 2022 19:01:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239612AbiKWTBY (ORCPT ); Wed, 23 Nov 2022 14:01:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239338AbiKWTBE (ORCPT ); Wed, 23 Nov 2022 14:01:04 -0500 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0665F92097 for ; Wed, 23 Nov 2022 11:00:55 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id z4so16244926wrr.3 for ; Wed, 23 Nov 2022 11:00:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=FGeYWHawFT/RkYnEHv+xVJgDFjPFKnvI5O3/C52qS/w=; b=qJgCaRNa83wXXcgB00WpEqULmGSfo6PKPz9QWPoqiDUpvI1gV6bc6fSeivGHuEv+D8 udW78AFjOqgVLFgSGqkOIIm5Ys9wdFYxZ6XAnuQHMXv8D+kAdqOuWorcu64C5bQgv5fV wAR0vrenl/ECWDUgUtI542XN0xqLBusslCixdTEXt5DQzP4ODGPlG0AQpIKOA2VUCPZR eT4cLXwLLLRQUF6Q2TFeHwZBP6C85IPt5CGqSfcdPZSBcFB177AIIAqGEXzigvW1Clpj elJcg2pw7K0rblNZHKIgGiY3905X2ghE3AZi0MlRiy9MfiEVQxooCS3J88RYHrX2qEGw RGbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FGeYWHawFT/RkYnEHv+xVJgDFjPFKnvI5O3/C52qS/w=; b=Q0wlFkkG4XI3s4bx+aug1u01eSLsVqiVJPTooL/AMj1rZz3Aj4JMxHKvofIQgyU/yy 0ptOcUfaxNsZj3kDLRP9eqI7JPxv7rAZozfR6mSW8xkb71/TdtN/95LZhe+CTqvsVwO6 fONXnOeLnkN1MYVaXaeQj/1Aa0umm7UAEMgJ9KayZ+ogkE8MW8tckec3g6emC3wlZB1I 00r2fscjHKg58arvYKjXcW8n+MUiTqixlMn40kOUMTV3HtWkL5ST3cBMBqqsKlNIp0Rj PnMKIm7kzjWnYaRj52FvElLSo6yF//AeTcdkp6wO/y8zH2lWlu/16JIBP+/ojlo6g1Ka CfEQ== X-Gm-Message-State: ANoB5pkcbicpacdyR9CGf7iiCiQRW85J+P/FRlbtu3p1WSGprzC1wtDL i5u8yP3T6zLG96PSBWaSne19hCBiFW4= X-Google-Smtp-Source: AA0mqf7nCMpl7R3pvitku7zSAVMkdzJBTqbc/VwM3fCUcsN/Hosbh+XVcK1+x9vF1Et1QtcpsdH1nw== X-Received: by 2002:adf:ed8b:0:b0:241:d375:88b6 with SMTP id c11-20020adfed8b000000b00241d37588b6mr10331984wro.88.1669230053303; Wed, 23 Nov 2022 11:00:53 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p15-20020a05600c468f00b003cfaae07f68sm212874wmo.17.2022.11.23.11.00.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Nov 2022 11:00:52 -0800 (PST) Message-Id: <8d9d469b3566fe45c43cfd348a3a1e2dbd6dc5dd.1669230044.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 23 Nov 2022 19:00:43 +0000 Subject: [PATCH v4 5/6] fsmonitor: test updates Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnI=?= =?utf-8?b?w7A=?= Bjarmason , Glen Choo , Johannes Schindelin , Taylor Blau , Eric DeCosta , Eric DeCosta Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Eric DeCosta From: Eric DeCosta t7527-builtin-fsmonitor was leaking fsmonitor--daemon processes in some cases. Accomodate slight difference in the number of events generated on Linux. On lower-powered systems, spin a little to give the daemon time to respond to and log filesystem events. Signed-off-by: Eric DeCosta --- t/t7527-builtin-fsmonitor.sh | 94 ++++++++++++++++++++++++++++++------ 1 file changed, 80 insertions(+), 14 deletions(-) diff --git a/t/t7527-builtin-fsmonitor.sh b/t/t7527-builtin-fsmonitor.sh index 4abc74db2bb..951374231b7 100755 --- a/t/t7527-builtin-fsmonitor.sh +++ b/t/t7527-builtin-fsmonitor.sh @@ -13,7 +13,7 @@ fi stop_daemon_delete_repo () { r=$1 && test_might_fail git -C $r fsmonitor--daemon stop && - rm -rf $1 + rm -rf $r } start_daemon () { @@ -72,6 +72,34 @@ start_daemon () { ) } +IMPLICIT_TIMEOUT=5 + +wait_for_update () { + func=$1 && + file=$2 && + sz=$(wc -c < "$file") && + last=0 && + $func && + k=0 && + while test "$k" -lt $IMPLICIT_TIMEOUT + do + nsz=$(wc -c < "$file") + if test "$nsz" -gt "$sz" + then + if test "$last" -eq "$nsz" + then + cat "$file" && + return 0 + fi + last=$nsz + fi + sleep 1 + k=$(( $k + 1 )) + done && + cat "$file" && + return 0 +} + # Is a Trace2 data event present with the given catetory and key? # We do not care what the value is. # @@ -137,7 +165,6 @@ test_expect_success 'implicit daemon start' ' # machines (where it might take a moment to wake and reschedule the # daemon process) to avoid false alarms during test runs.) # -IMPLICIT_TIMEOUT=5 verify_implicit_shutdown () { r=$1 && @@ -373,6 +400,15 @@ create_files () { echo 3 >dir2/new } +rename_directory () { + mv dirtorename dirrenamed +} + +rename_directory_file () { + mv dirtorename dirrenamed && + echo 1 > dirrenamed/new +} + rename_files () { mv rename renamed && mv dir1/rename dir1/renamed && @@ -427,10 +463,12 @@ test_expect_success 'edit some files' ' start_daemon --tf "$PWD/.git/trace" && - edit_files && + wait_for_update edit_files "$PWD/.git/trace" && test-tool fsmonitor-client query --token 0 && + test_might_fail git fsmonitor--daemon stop && + grep "^event: dir1/modified$" .git/trace && grep "^event: dir2/modified$" .git/trace && grep "^event: modified$" .git/trace && @@ -442,10 +480,12 @@ test_expect_success 'create some files' ' start_daemon --tf "$PWD/.git/trace" && - create_files && + wait_for_update create_files "$PWD/.git/trace" && test-tool fsmonitor-client query --token 0 && + test_might_fail git fsmonitor--daemon stop && + grep "^event: dir1/new$" .git/trace && grep "^event: dir2/new$" .git/trace && grep "^event: new$" .git/trace @@ -456,10 +496,12 @@ test_expect_success 'delete some files' ' start_daemon --tf "$PWD/.git/trace" && - delete_files && + wait_for_update delete_files "$PWD/.git/trace" && test-tool fsmonitor-client query --token 0 && + test_might_fail git fsmonitor--daemon stop && + grep "^event: dir1/delete$" .git/trace && grep "^event: dir2/delete$" .git/trace && grep "^event: delete$" .git/trace @@ -470,10 +512,12 @@ test_expect_success 'rename some files' ' start_daemon --tf "$PWD/.git/trace" && - rename_files && + wait_for_update rename_files "$PWD/.git/trace" && test-tool fsmonitor-client query --token 0 && + test_might_fail git fsmonitor--daemon stop && + grep "^event: dir1/rename$" .git/trace && grep "^event: dir2/rename$" .git/trace && grep "^event: rename$" .git/trace && @@ -487,23 +531,42 @@ test_expect_success 'rename directory' ' start_daemon --tf "$PWD/.git/trace" && - mv dirtorename dirrenamed && + wait_for_update rename_directory "$PWD/.git/trace" && test-tool fsmonitor-client query --token 0 && + test_might_fail git fsmonitor--daemon stop && + grep "^event: dirtorename/*$" .git/trace && grep "^event: dirrenamed/*$" .git/trace ' +test_expect_success 'rename directory file' ' + test_when_finished clean_up_repo_and_stop_daemon && + + start_daemon --tf "$PWD/.git/trace" && + + wait_for_update rename_directory_file "$PWD/.git/trace" && + + test-tool fsmonitor-client query --token 0 && + + test_might_fail git fsmonitor--daemon stop && + + grep "^event: dirtorename/*$" .git/trace && + grep "^event: dirrenamed/*$" .git/trace && + grep "^event: dirrenamed/new$" .git/trace +' test_expect_success 'file changes to directory' ' test_when_finished clean_up_repo_and_stop_daemon && start_daemon --tf "$PWD/.git/trace" && - file_to_directory && + wait_for_update file_to_directory "$PWD/.git/trace" && test-tool fsmonitor-client query --token 0 && + test_might_fail git fsmonitor--daemon stop && + grep "^event: delete$" .git/trace && grep "^event: delete/new$" .git/trace ' @@ -513,10 +576,12 @@ test_expect_success 'directory changes to a file' ' start_daemon --tf "$PWD/.git/trace" && - directory_to_file && + wait_for_update directory_to_file "$PWD/.git/trace" && test-tool fsmonitor-client query --token 0 && + test_might_fail git fsmonitor--daemon stop && + grep "^event: dir1$" .git/trace ' @@ -561,7 +626,7 @@ test_expect_success 'flush cached data' ' test-tool -C test_flush fsmonitor-client query --token "builtin:test_00000002:0" >actual_2 && nul_to_q actual_q2 && - grep "^builtin:test_00000002:0Q$" actual_q2 && + grep "^builtin:test_00000002:[0-1]Q$" actual_q2 && >test_flush/file_3 && @@ -732,7 +797,8 @@ u_values="$u1 $u2" for u in $u_values do test_expect_success "unicode in repo root path: $u" ' - test_when_finished "stop_daemon_delete_repo $u" && + test_when_finished \ + "stop_daemon_delete_repo `echo "$u" | sed 's:x:\\\\\\\\\\\\\\x:g'`" && git init "$u" && echo 1 >"$u"/file1 && @@ -818,8 +884,7 @@ test_expect_success 'submodule setup' ' ' test_expect_success 'submodule always visited' ' - test_when_finished "git -C super fsmonitor--daemon stop; \ - rm -rf super; \ + test_when_finished "rm -rf super; \ rm -rf sub" && create_super super && @@ -887,7 +952,8 @@ have_t2_error_event () { } test_expect_success "stray submodule super-prefix warning" ' - test_when_finished "rm -rf super; \ + test_when_finished "git -C super/dir_1/dir_2/sub fsmonitor--daemon stop; \ + rm -rf super; \ rm -rf sub; \ rm super-sub.trace" && From patchwork Wed Nov 23 19:00:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric DeCosta X-Patchwork-Id: 13054137 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A72D4C4332F for ; Wed, 23 Nov 2022 19:01:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239338AbiKWTB0 (ORCPT ); Wed, 23 Nov 2022 14:01:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239427AbiKWTBF (ORCPT ); Wed, 23 Nov 2022 14:01:05 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2BE1922E4 for ; Wed, 23 Nov 2022 11:00:56 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id e11so17920999wru.8 for ; Wed, 23 Nov 2022 11:00:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=Oi1JhA2Y49B3vsv8LyQZ/nUzDuStw5WBLGMA8BZqLVA=; b=UmAFrMfHBOhFifoeTxJB/lTArTwZWGYEFJu2bnZuW5LtEyTvf0xn/E3rPzbReneijI aMKyBZdeb2e91yrJOW2bI5o+iYrpUyLf8NoHU1gluRyMWEbDaBPzwV9DTCx4+tZwd9ag QresmKH+n551uYgUnidxbRordFZPfWYM/OlROspRc2nPL00KxITLJhsurJcX6/T6C4QE F2negrB78T5MRD0maWGSWXVJOSQDgEta6EdFKOXiUdeK+iCLJ37mCw3WBAwPDR1AmNT7 3oCuuL5YIbLIpMRsYCj///0HLl1OrYUrZ/30N7nQod8nUe5Xq02xJYlcS0DkhEXVRYVr qTgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Oi1JhA2Y49B3vsv8LyQZ/nUzDuStw5WBLGMA8BZqLVA=; b=Y7XhESvTA7P4qEHURK7wRbAFz3xAL73rONr3YViYBGNTYBZwKP0WKsU2f30RDXftxP kfpsbCKHVMtwmoR83p8GsYoOhETRl+wYhMlrQPttiMeEtaouAWWupwal+5QK2WusYKPy MvRFmnTksN+NlLus4od0ZOR0YlW/bvY1Vgq2SQW/i1Z9rzTrqfOzFMvxeozR3RelcSR0 aCfy2DWoJc+fpdsWAnvurzF+qmSUKQQGE/aiFm8zsK4HvdTcZeW2gO1DGpuPBJiiVOtU OJOPxvB8x7cRZa7fU04cPWqnkNcsOy7d4jn4TgHVN+E47y0+opZhLankm8ZSvVp/RF20 /WRw== X-Gm-Message-State: ANoB5pk+DjxKxGKM6joR9ZPjMhWsMLw5fZUTBodEN4OG9jJ0WfPvIN3V CXkCt4bByJuOVuPbrY8kW2R9vlbwPVo= X-Google-Smtp-Source: AA0mqf4rpT2h8PmZBU34/2/WM6+AcXQrFAjszIETg0jIUGj7Z9Strm3BSrTtRdDtpeSPHO2fn6UxIQ== X-Received: by 2002:adf:e305:0:b0:236:6089:cc5e with SMTP id b5-20020adfe305000000b002366089cc5emr9394907wrj.118.1669230054373; Wed, 23 Nov 2022 11:00:54 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id e19-20020a05600c219300b003c6bd12ac27sm2960179wme.37.2022.11.23.11.00.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Nov 2022 11:00:53 -0800 (PST) Message-Id: <5afd03fa6ca3a790eb06213573d9be3c40dee492.1669230044.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 23 Nov 2022 19:00:44 +0000 Subject: [PATCH v4 6/6] fsmonitor: update doc for Linux Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Eric Sunshine , =?utf-8?b?w4Z2YXIgQXJuZmrDtnI=?= =?utf-8?b?w7A=?= Bjarmason , Glen Choo , Johannes Schindelin , Taylor Blau , Eric DeCosta , Eric DeCosta Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Eric DeCosta From: Eric DeCosta Update the documentation for Linux. Signed-off-by: Eric DeCosta --- Documentation/config/fsmonitor--daemon.txt | 4 ++-- Documentation/git-fsmonitor--daemon.txt | 24 ++++++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Documentation/config/fsmonitor--daemon.txt b/Documentation/config/fsmonitor--daemon.txt index c225c6c9e74..2cafb040d96 100644 --- a/Documentation/config/fsmonitor--daemon.txt +++ b/Documentation/config/fsmonitor--daemon.txt @@ -4,8 +4,8 @@ fsmonitor.allowRemote:: behavior. Only respected when `core.fsmonitor` is set to `true`. fsmonitor.socketDir:: - This Mac OS-specific option, if set, specifies the directory in + Mac OS and Linux-specific option. If set, specifies the directory in which to create the Unix domain socket used for communication between the fsmonitor daemon and various Git commands. The directory must - reside on a native Mac OS filesystem. Only respected when `core.fsmonitor` + reside on a native filesystem. Only respected when `core.fsmonitor` is set to `true`. diff --git a/Documentation/git-fsmonitor--daemon.txt b/Documentation/git-fsmonitor--daemon.txt index 8238eadb0e1..c2b08229c74 100644 --- a/Documentation/git-fsmonitor--daemon.txt +++ b/Documentation/git-fsmonitor--daemon.txt @@ -76,23 +76,31 @@ repositories; this may be overridden by setting `fsmonitor.allowRemote` to correctly with all network-mounted repositories and such use is considered experimental. -On Mac OS, the inter-process communication (IPC) between various Git +On Linux and Mac OS, the inter-process communication (IPC) between various Git commands and the fsmonitor daemon is done via a Unix domain socket (UDS) -- a -special type of file -- which is supported by native Mac OS filesystems, -but not on network-mounted filesystems, NTFS, or FAT32. Other filesystems -may or may not have the needed support; the fsmonitor daemon is not guaranteed -to work with these filesystems and such use is considered experimental. +special type of file -- which is supported by many native Linux and Mac OS +filesystems, but not on network-mounted filesystems, NTFS, or FAT32. Other +filesystems may or may not have the needed support; the fsmonitor daemon is not +guaranteed to work with these filesystems and such use is considered +experimental. By default, the socket is created in the `.git` directory, however, if the `.git` directory is on a network-mounted filesystem, it will be instead be created at `$HOME/.git-fsmonitor-*` unless `$HOME` itself is on a network-mounted filesystem in which case you must set the configuration -variable `fsmonitor.socketDir` to the path of a directory on a Mac OS native +variable `fsmonitor.socketDir` to the path of a directory on a native filesystem in which to create the socket file. If none of the above directories (`.git`, `$HOME`, or `fsmonitor.socketDir`) -is on a native Mac OS file filesystem the fsmonitor daemon will report an -error that will cause the daemon and the currently running command to exit. +is on a native Linux or Mac OS filesystem the fsmonitor daemon will report +an error that will cause the daemon to exit and the currently running command +to issue a warning. + +On Linux, the fsmonitor daemon registers a watch for each directory in the +repository. The default per-user limit for the number of watches on most Linux +systems is 8192. This may not be sufficient for large repositories or if +multiple instances of the fsmonitor daemon are running. +See https://watchexec.github.io/docs/inotify-limits.html[Linux inotify limits] for more information. CONFIGURATION -------------