From nobody Sat Aug 13 16:51:32 2022 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4M4mlr0lp3z4ZR8C for ; Sat, 13 Aug 2022 16:51:36 +0000 (UTC) (envelope-from leres@freebsd.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4M4mlr0L5nz3RWM; Sat, 13 Aug 2022 16:51:36 +0000 (UTC) (envelope-from leres@freebsd.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1660409496; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=q0h4jbA54ZtjhlyOLyzoizBGmo8k7McblNoGtk5TCMA=; b=IV0LNNZq9PxY9mMWatUWhNHqQBfoRQ3Lpst6PsUj7Jufhl9BRApaq2FVCnlLw0CjbdIWms bz+LbaoHbucdfteMwTklnTpQQ2qgQ8DWlJ0VP4F0J5RC598VK0s0JlBGH+sZL3IULT0I0A c0U7TVshCju1tXaJq+djPtMw1QtMXYvcaqlf24Pxisvbp2ztzEShksnkvwgyR2aC0/+o1c 5i7I8gkm4rFB10Q6etK+36S1WMWENlJG0OPhKPpq4BRdv0dCY+NgENbsTtj5jzPlqpcFAQ ZSPPJRODTT5m42KdP+jPqvJK9Ell0+h3L1zffblInvk8LcrI+QW7DvDAAwR10w== Received: from [IPV6:fd:1965::2] (unknown [IPv6:2600:1700:a570:e20::38]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: leres) by smtp.freebsd.org (Postfix) with ESMTPSA id 4M4mlq3l41znLb; Sat, 13 Aug 2022 16:51:35 +0000 (UTC) (envelope-from leres@freebsd.org) Message-ID: <754a7f6d-8fbe-aae0-796b-e5c0e782fc4f@freebsd.org> Date: Sat, 13 Aug 2022 09:51:32 -0700 List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: How to monitor a directory in FreeBSD? Content-Language: en-US To: meator , freebsd-hackers@FreeBSD.org References: <652f3c12-388c-04d0-ebeb-753b76b2b742@gmail.com> From: Craig Leres In-Reply-To: <652f3c12-388c-04d0-ebeb-753b76b2b742@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1660409496; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=q0h4jbA54ZtjhlyOLyzoizBGmo8k7McblNoGtk5TCMA=; b=oM5lYxJbjtyjkQzXh5sh3yRiwn8kQZ4XcCoRh3VXe/RZSyl3iM/Ui5PAdKqFGKB2iJqzLH +Wn9cnDBScvdJ6vgOE+ETw9o9Au17ttga6ORmH4Dn0PwH6o+oJh7jW3LsExo2DKSa7UCY9 c/S74i/e9NDk3Nh7FQT3JasSlSMM7yFN/cq6NGI/3sWRvB3i464oKksHNCbwtNj2Co8UKP OETZFK57NkTqgRFN9ed/s7cOqMxyRUa7eNfh/Z+XeZ743dIQFeSkCSsGlLC5eM3PJNG/Kq Bf6cSM1dX9nSjAoMUZsoZbqQZ8oDnnY+bwtk9jX+G5+gXpWXiulRR6rSULUJ0A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1660409496; a=rsa-sha256; cv=none; b=u18PameLv2AuMmy35e3RdKGnNGlVIovlb8KqDaAJTvpAHHn7eNP9SFhfGroPdFErMcL56l qiaqxyEFmizOSqsRLJlrFvOmXOYVtxLzSdKzzT6vs7uZvPjLAZy+DNkXv6Hl7ba55tix48 aGYp2fYB1S9kkQeaX8c2yC3QxPJzTPtvSR6+RIAOSyOIt9iS2Hkg0AuKRZbxY6dmLN2sam +43fg27RG49uEEgW3NDi2QGaz010OVTvyg3hddOIRa579BQymlhrpRxC3IlOYYShvghUbZ D5yG0m0C1Ujc8cBaf0Zj+C219oNbQSYsYGuG2ovcP3bWYAUgYsHuTUJVsr77Jg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N On 8/13/22 05:16, meator wrote: > Hello. I'm working on a C program that needs to know whether files and > directories in a specific directory were added, modified or deleted. (It > should also be done recursively for all subdirectories, but to keep it > simple I don't take recursion into account. It shouldn't be that hard to > implement it after I will be able to monitor a directory nonrecursively.) > > I don't have much experience with BSD programming but I know POSIX. I > have used inotify before for this purpose, but BSD doesn't have it so I > started looking for BSD alternatives. The internet lead me to kqueue. I > saw some criticism of it, but I don't need to monitor several thousands > of files, so I hope it will be usable for my use case. > > The EVFILT_VNODE filter documentation in kqueue(2) doesn't really talk > about files and directories, it talks about file descriptors. Inotify on > the other hand is very explicit about handling files in the monitored > directory. Kqueue can still detect creation and deletion of files inside > the monitored directory with NOTE_WRITE for files and NOTE_LINK for > directories (at least I think, I made a little test program to test this). > > This is useful, but I don't see any obvious way to identify a newly > created file inside the monitored directory. File creation would result > in NOTE_WRITE, but struct kevent doesn't have any "name" field (unlike > inotify) that would show which file was created. I would have to make a > list of directories and compare the old state with the current state to > see the file which was added. > > Kqueue also doesn't seem to detect modification of files inside the > monitored directory. Does this mean that I would have to monitor every > single file in the directory to make this work? > > I think this shows that kqueue isn't really meant to be used for > monitoring directory members. Is this true? Have I misunderstood something? The way I've done it is is to use EVFILT_VNODE/NOTE_WRITE to tell me when the directory has changed and then roll my own opendir()/readdir() code to detect what (if anything) was added or deleted. Craig