From nobody Sat Aug 13 14:28:51 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 4M4jc3224jz4YnGB for ; Sat, 13 Aug 2022 14:29:39 +0000 (UTC) (envelope-from wulf@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [IPv6:2610:1c1:1:606c::24b:4]) (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 4M4jc31SzPz419n for ; Sat, 13 Aug 2022 14:29:39 +0000 (UTC) (envelope-from wulf@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1660400979; 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=x/L3kDS1a8nQlXoBTd0pnwquxfnjVO216PAP0y3NFrk=; b=aoNCVEmoUMhw49srDEHQ/n+xKa5JIYtN65T3FE8BunfoNdHy4EgJ4doBLqxXZHyqLME0nG 2KMTN/K1tau9CxNzBWsWQhGwM7Gyx99IrzoJL5maFQQZ4JNO8c9ptKIt4potaqxgQckfp7 sTMnEFgPJnIO2YbR4P6JThJy89dRohzX0zpQXyYF+MPr6b0sgBlvzGTwdnjSsrk1xzlrht EVdxxrwgDrmWP1oJAc67EmkHZykIXMW1KxOF+6k2G9x/c79xfDRfbRz7F1wmV+UQegEXuP 6eIdPAjGmVfNqCAQMk/Up8pB+UkYt35KQcuEGKi9sr3T4suNfp2ni7gfnW8png== Received: from [192.168.0.30] (unknown [94.45.210.186]) (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: wulf) by smtp.freebsd.org (Postfix) with ESMTPSA id 4M4jc25ZrfzkJl for ; Sat, 13 Aug 2022 14:29:38 +0000 (UTC) (envelope-from wulf@FreeBSD.org) Message-ID: <8e4a993d-795a-158b-d8fc-72fe6475baa6@FreeBSD.org> Date: Sat, 13 Aug 2022 17:28:51 +0300 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.9.0 Subject: Re: How to monitor a directory in FreeBSD? Content-Language: en-US To: freebsd-hackers@freebsd.org References: <652f3c12-388c-04d0-ebeb-753b76b2b742@gmail.com> From: Vladimir Kondratyev 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=1660400979; 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=x/L3kDS1a8nQlXoBTd0pnwquxfnjVO216PAP0y3NFrk=; b=OF38gouGPYmLzj6WP/5EUN8E3HIfEXsgiUiTmoI5QnZgWuke2ikw0zBDTj5nYLm9KwvMMY AmDz/QC3ktzXbtM8QaL2ExIDHUoy4AnO1CWAjYcDKU2+KDT9jJBUG5wFo1a9Z9IBGuTB+f vBeWV64FFvejWV52lP6iAKHfUn3BMKQOV60aUR6ZDQpMF+LTVgNVxunba8RPI+qwzvxBrS LvxQ7mZBVeaP0HuC7RacLCBHoZDCcBgANtcGQ49tWS5z6elaCS6MxriGG2+EYFPNI0cYm3 WmPeSzBw90MapzDVuDDgywbvNYdcIXjj6aJhyMYsshSStBhUisGfTzIjMRkT1Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1660400979; a=rsa-sha256; cv=none; b=NyLBxNXWv5bSeTwTQwYML/mhxjMSRoxScKzVd3QlcGQgQ0jiD0cpCa9jnMjQndx2dYE8ym dKZBrdgwSckZjThu4FL2xRNAPFs5LP44Dg9hJZF8hAThsHK6RUMG1/p6Mq0bW+Z23lMVT5 J3pWIYWgDOM0ZbncAKLjOINaFud/Ns+XDL78uTHX+qwJLW8/GHK1JpJedso4TG3i/s7MJi WVFNHAv0eMccZNpNwM1r5cVij30gpW2A4S75yx3KtJUPPC/oPvPIx8uzrqBmCr2taZx6pA +gbdfSn9fwBIEvHx1PT9nsNQszHQYmegE4xtujIDzvgZjmUfibdeEeDYSaknRg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N On 13.08.2022 15: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? It looks that inotifywait from sysutils/inotify-tools is what you need -- WBR Vladimir Kondratyev