You are not logged in.

#1 2010-03-23 22:14:24

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

FCM: dead-simple File Change Monitor

Here's a simple change monitor I wrote in C. It takes as arguments a list of files and associated commands; when the file changes, its command is executed.

Example:

$ fcm -f some-file.txt -e 'echo some-file.txt changed!' \
> -E ad -f some-file-2.txt -e 'echo some-file-2.txt changed!'

'-E ad' preceding the second file causes FCM to examine both attribute change and data change.

AUR: http://aur.archlinux.org/packages.php?ID=35877
Mercurial repo: http://peasantoid.org:1024/fcm

Last edited by Peasantoid (2010-03-24 23:50:06)

Offline

#2 2010-03-23 23:22:46

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 857
Website

Re: FCM: dead-simple File Change Monitor

Any reason it doesn't use inotify?

Offline

#3 2010-03-23 23:29:55

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: FCM: dead-simple File Change Monitor

Because I felt like reinventing a few dozen wheels. tongue

But seriously, *slaps self*. Didn't know about inotify, but this works for my purposes, so whatever.

*reads inotify(7)*

Last edited by Peasantoid (2010-03-23 23:31:27)

Offline

#4 2010-03-24 01:50:14

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 857
Website

Re: FCM: dead-simple File Change Monitor

Haha fair enough.  The concept is pretty good, sort of like "watch" for files.  A front-end for FAM would be another way to do it, if you wanted to watch across NFS or some other corner cases.

Offline

#5 2010-03-24 21:11:27

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: FCM: dead-simple File Change Monitor

Rewrote this to use the inotify API (thanks for the heads-up, tavianator). -c option is gone for the moment but will be back later in a different form.

As for FAM, that feels like overkill.

Last edited by Peasantoid (2010-03-24 21:13:43)

Offline

#6 2010-04-02 23:57:10

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: FCM: dead-simple File Change Monitor

Finally fixed an annoying bug in which modifying a file with f.e. vim caused it to be dropped from the watch list. This was because of the way inotify handles a file being moved over another file. Not an inotify bug; more like a vim one, since it doesn't just do the smart thing and directly overwrite the file properly (by opening in truncate mode).

Offline

#7 2010-04-05 04:27:27

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 857
Website

Re: FCM: dead-simple File Change Monitor

Actually vim is doing the smart thing.  Basically this ensures that the save is an atomic operation; two apps could write to it at the same time otherwise.

Offline

#8 2010-04-05 20:16:54

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: FCM: dead-simple File Change Monitor

tavianator wrote:

Actually vim is doing the smart thing.  Basically this ensures that the save is an atomic operation; two apps could write to it at the same time otherwise.

Yeah, I suppose that makes sense. But since it makes my job harder, I call it stupid. tongue

Offline

#9 2010-04-23 06:41:29

thomasd
Member
Registered: 2008-08-11
Posts: 114

Re: FCM: dead-simple File Change Monitor

Hi Peasantoid,

I try to use fcm to print mail by saving a mail to a specific file while having this running:

fcm -f ~/operamail.mbs -e "iconv -t iso8859-1 ~/operamail.mbs | a2ps -=mail -Pprinter"

However, fcm starts two processes, thus I get two printouts:

a2ps: unknown encoding `utf-8', ignored
[stdin (Mail Folder): 2 pages on 1 sheet]
a2ps: unknown encoding `utf-8', ignored
[stdin (Mail Folder): 2 pages on 1 sheet]
request id is printer-132344 (1 file(s))
[Total: 2 pages on 1 sheet] sent to the printer `printer'
request id is printer-132345 (1 file(s))
[Total: 2 pages on 1 sheet] sent to the printer `printer'

Both hardcopies show all text, so my first assumption that the file changes twice during writing is obviously not true. Can I overcome this in some way?

Offline

#10 2010-04-23 07:03:57

MindTooth
Member
From: Norway
Registered: 2008-11-11
Posts: 331

Re: FCM: dead-simple File Change Monitor

Maybe not FAM, but Gamin which superseeds FAM smile

Offline

#11 2010-04-23 07:36:06

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: FCM: dead-simple File Change Monitor

thomasd wrote:

Hi Peasantoid,

I try to use fcm to print mail by saving a mail to a specific file while having this running:

fcm -f ~/operamail.mbs -e "iconv -t iso8859-1 ~/operamail.mbs | a2ps -=mail -Pprinter"

However, fcm starts two processes, thus I get two printouts:

a2ps: unknown encoding `utf-8', ignored
[stdin (Mail Folder): 2 pages on 1 sheet]
a2ps: unknown encoding `utf-8', ignored
[stdin (Mail Folder): 2 pages on 1 sheet]
request id is printer-132344 (1 file(s))
[Total: 2 pages on 1 sheet] sent to the printer `printer'
request id is printer-132345 (1 file(s))
[Total: 2 pages on 1 sheet] sent to the printer `printer'

Both hardcopies show all text, so my first assumption that the file changes twice during writing is obviously not true. Can I overcome this in some way?

Hrm. It may have something to do with me using inotify's IN_MODIFY instead of IN_CLOSE_WRITE. Run this in the source tree:

sed -i s/IN_MODIFY/IN_CLOSE_WRITE/ fcm.c

Then recompile and post back on whether it works.

# edit - clarity

Last edited by Peasantoid (2010-04-23 07:39:19)

Offline

#12 2010-04-23 12:00:20

thomasd
Member
Registered: 2008-08-11
Posts: 114

Re: FCM: dead-simple File Change Monitor

That fixes it. Thanks for the quick reply.

Offline

#13 2010-04-23 12:28:59

thomasd
Member
Registered: 2008-08-11
Posts: 114

Re: FCM: dead-simple File Change Monitor

A little patch that allows to install to nonstandard locations by "make PREFIX=/usr/local install":

# HG changeset patch
# User Thomas Dahms <thdahms@gmx.de>
# Date 1272025507 -7200
# Node ID 568bffa076982c3f687f8fc4bfabb9588acc48c1
# Parent  564ddf614ec48c0eeba70e119432e1d5ae7d0fec
add PREFIX to Makefile.

diff -r 564ddf614ec4 -r 568bffa07698 Makefile
--- a/Makefile  Fri Apr 02 19:44:48 2010 -0400
+++ b/Makefile  Fri Apr 23 14:25:07 2010 +0200
@@ -6,8 +6,9 @@
 
 MAN = fcm.1
 
-BINDIR = $(DESTDIR)/usr/bin
-MANDIR = $(DESTDIR)/usr/share/man/man1
+PREFIX=/usr
+BINDIR = $(DESTDIR)/$(PREFIX)/bin
+MANDIR = $(DESTDIR)/$(PREFIX)/share/man/man1
 
 .PHONY: all
 all: $(BIN)

Offline

#14 2010-04-23 18:57:11

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: FCM: dead-simple File Change Monitor

thomasd wrote:

That fixes it. Thanks for the quick reply.

Okay, I'll commit that to the codebase then.

thomasd wrote:

A little patch that allows to install to nonstandard locations by "make PREFIX=/usr/local install":

# HG changeset patch
# User Thomas Dahms <thdahms@gmx.de>
# Date 1272025507 -7200
# Node ID 568bffa076982c3f687f8fc4bfabb9588acc48c1
# Parent  564ddf614ec48c0eeba70e119432e1d5ae7d0fec
add PREFIX to Makefile.

diff -r 564ddf614ec4 -r 568bffa07698 Makefile
--- a/Makefile  Fri Apr 02 19:44:48 2010 -0400
+++ b/Makefile  Fri Apr 23 14:25:07 2010 +0200
@@ -6,8 +6,9 @@
 
 MAN = fcm.1
 
-BINDIR = $(DESTDIR)/usr/bin
-MANDIR = $(DESTDIR)/usr/share/man/man1
+PREFIX=/usr
+BINDIR = $(DESTDIR)/$(PREFIX)/bin
+MANDIR = $(DESTDIR)/$(PREFIX)/share/man/man1
 
 .PHONY: all
 all: $(BIN)

Thanks. For aesthetic reasons, I slightly modified the change so as to not include extraneous slashes in the paths. So instead of 'make PREFIX=/something install', you'd do 'make PREFIX=something install'. Note the missing slash.

(Changes have been committed and pushed.)

Offline

Board footer

Powered by FluxBB