Building a recursive file system watcher for Linux

Published February 20, 2014
Advertisement
[color=rgb(11,25,2)][font=arial]Following my previous update about correcting file path cases, I am now able to load all our maps in Leadwerks. The power of this tool running natively on Linux is starting to show, if I do say so myself:[/font][/color]

[color=rgb(11,25,2)][font=arial]b62aa01c8a43794b9fefaf7756bc9af2_large.png?1392930161[/font][/color]

[color=rgb(11,25,2)][font=arial]The next step is to implement a file system watcher to detect file changes. Leadwerks automatically monitors the current project directory, and will reload assets whenever the file changes. This allows you to keep an image open in an image editor like GIMP, and any time you save your changes will be reflected inside Leadwerks. It's a great workflow for artists and just the kind of feature I wanted to bring to Linux with this project.[/font][/color]

[color=rgb(11,25,2)][font=arial]Linux has a built-in file system watcher class called "inotify". Interestingly, this class was added to Linux in 2005, the same year the iPod was released, but there appears to be no connection. The "i" in "inotify" stands for "inode". Dennis Ritchie explains:[/font][/color]

[color=rgb(11,25,2)][font=arial]In truth, I don't know either. It was just a term that we started to use. "Index" is my best guess, because of the slightly unusual file system structure that stored the access information of files as a flat array on the disk, with all the hierarchical directory information living aside from this. Thus the i-number is an index in this array, the i-node is the selected element of the array. (The "i-" notation was used in the 1st edition manual; its hyphen was gradually dropped.)[/font][/color]

[color=rgb(11,25,2)][font=arial]The inotify system is pretty straightforward to implement, with a procedural C interface. One thing that tripped me up was an odd layout of the inotify_event structure. It actually has a char pointer built into the structure, so technically the structure does not have a determined length. I don't believe I have ever encountered this design before, but I also am usually dealing with std::string classes.[/font][/color]

[color=rgb(11,25,2)][font=arial]One drawback of inotify is that it isn't recursive. While searching for information on the design, I came across this post by Robert Love, one of the two guys who wrote it (the other being John McCutchan). I disagree with his rational for omitting recursion; just because the performance is not as optimal as he would like does not mean the end user's tastes and preferences will change. I can't imagine a scenario when you wouldn't want the system to work recursively, or at least have the option to. In any case, implementing a recursive watch system was fairly easy. The entire file watch system from zero to finished only took about half a day. So we can mark this as one of the features where I overestimated the time and complexity of implementation.[/font][/color]

[color=rgb(11,25,2)][font=arial]File creation, deletion, and modify events are now firing reliably so I am going to plug this code into the editor and start testing it out. Because the editor already works reliably on other Windows and OSX, I don't anticipate any problems. I do not have a firm release date yet, but as you can surmise, we are nearing completion of Leadwerks for Linux.[/font][/color]
4 likes 1 comments

Comments

General Awesome

Interesting article. Thanks for the read!

February 22, 2014 01:32 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement