Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 13 Aug 2022 18:35:21 +0300
From:      Gleb Popov <arrowd@freebsd.org>
To:        meator <meator.dev@gmail.com>
Cc:        freebsd-hackers <freebsd-hackers@freebsd.org>
Subject:   Re: How to monitor a directory in FreeBSD?
Message-ID:  <CALH631m_q2VF8N4M%2ByCQVu4%2BeF6uue3TZ6KY%2BYzGc_zeWVa3bw@mail.gmail.com>
In-Reply-To: <652f3c12-388c-04d0-ebeb-753b76b2b742@gmail.com>
References:  <652f3c12-388c-04d0-ebeb-753b76b2b742@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
--0000000000005a65d605e6212676
Content-Type: text/plain; charset="UTF-8"

On Sat, Aug 13, 2022, 15:16 meator <meator.dev@gmail.com> 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?
>

Yes, there is no native API to monitor directories. You can use libinotify
from ports, which replicates Linux inotify API and it does have ability to
monitor directories.

>

--0000000000005a65d605e6212676
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto"><div><br><br><div class=3D"gmail_quote"><div dir=3D"ltr" =
class=3D"gmail_attr">On Sat, Aug 13, 2022, 15:16 meator &lt;<a href=3D"mail=
to:meator.dev@gmail.com">meator.dev@gmail.com</a>&gt; wrote:<br></div><bloc=
kquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #cc=
c solid;padding-left:1ex">Hello. I&#39;m working on a C program that needs =
to know whether files and <br>
directories in a specific directory were added, modified or deleted. (It <b=
r>
should also be done recursively for all subdirectories, but to keep it <br>
simple I don&#39;t take recursion into account. It shouldn&#39;t be that ha=
rd to <br>
implement it after I will be able to monitor a directory nonrecursively.)<b=
r>
<br>
I don&#39;t have much experience with BSD programming but I know POSIX. I <=
br>
have used inotify before for this purpose, but BSD doesn&#39;t have it so I=
 <br>
started looking for BSD alternatives. The internet lead me to kqueue. I <br=
>
saw some criticism of it, but I don&#39;t need to monitor several thousands=
 <br>
of files, so I hope it will be usable for my use case.<br>
<br>
The EVFILT_VNODE filter documentation in kqueue(2) doesn&#39;t really talk =
<br>
about files and directories, it talks about file descriptors. Inotify on <b=
r>
the other hand is very explicit about handling files in the monitored <br>
directory. Kqueue can still detect creation and deletion of files inside <b=
r>
the monitored directory with NOTE_WRITE for files and NOTE_LINK for <br>
directories (at least I think, I made a little test program to test this).<=
br>
<br>
This is useful, but I don&#39;t see any obvious way to identify a newly <br=
>
created file inside the monitored directory. File creation would result <br=
>
in NOTE_WRITE, but struct kevent doesn&#39;t have any &quot;name&quot; fiel=
d (unlike <br>
inotify) that would show which file was created. I would have to make a <br=
>
list of directories and compare the old state with the current state to <br=
>
see the file which was added.<br>
<br>
Kqueue also doesn&#39;t seem to detect modification of files inside the <br=
>
monitored directory. Does this mean that I would have to monitor every <br>
single file in the directory to make this work?<br>
<br>
I think this shows that kqueue isn&#39;t really meant to be used for <br>
monitoring directory members. Is this true? Have I misunderstood something?=
<br></blockquote></div></div><div dir=3D"auto"><br></div><div dir=3D"auto">=
Yes, there is no native API to monitor directories. You can use libinotify =
from ports, which replicates Linux inotify API and it does have ability to =
monitor directories.</div><div dir=3D"auto"><div class=3D"gmail_quote"><blo=
ckquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #c=
cc solid;padding-left:1ex">
</blockquote></div></div></div>

--0000000000005a65d605e6212676--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CALH631m_q2VF8N4M%2ByCQVu4%2BeF6uue3TZ6KY%2BYzGc_zeWVa3bw>