How do I (recursively?) monitor contents of new directories using inotify? - c

Firstly, I want to start by using inotify to monitor a specific directory (the main directory) for files and sub-directories. If a new directory is added into this main directory, how would I make sure to monitor this sub-directory with inotify? How would I monitor a new directory within this sub-directory of the main directory?
I think adding it to the watch is easy by using the inotify_add_watch() function but I do not know how to get the correct relative path address of files and directories within sub-directories (to use for like Dropbox-like syncing in a different location while maintaining the correct directory tree, for example).

Well the fastest to implement (but not the fastest in reality) would be to:
Create the initial tree of directories by recursively exploring the children; An example in C/Linux can be found here:
http://www.lemoda.net/c/recursive-directory/
Add a watch for each subdirectory; When something has been modified or changed you can parse all children recursively and see the differences. Something similar was discussed here:
How to monitor a folder with all subfolders and files inside?
If this solution doesn't appeal to you, you might try to do a polling mechanism such that you must re-check the whole structure using a thread at a certain time interval.
Hope it helps!

Related

modify all files in directory. While keeping file structure the same

I do not know what it is called but this is what I am wanting to do. Imagine that you have a directory named (program) then you have three other directories beneath that named 1,2,3. Each of these have 10 files in them either in python or php or what ever.
What I am wanting to do is change a little bity bit of code in each one using a find and replace in notepage ++. Is there a way to basically have every file combined for this one find and replace then when you are done just save it and have them go back into the individual files and directories like before. Is this even possible, has anyone even wanted to do this?

kqueue watch size change on directory

I'm trying to watch for size change on directory using kqueue, is this possible? The reason for this because I am watching directories and whenever an event triggers, I stat the directory and compare last mod times etc to figure out if contents-modifed, added, removed, or renamed events happend. My goal is to get an even to trigger on directory when contents-modified happens on a file inside the directory, I couldn't accomplish that so we had an idea, we want to detect size change on directory, as if a contents-modified happend on a file within then the size of directory will change. Is this possible?
Thanks
You don't want/need to stat() the directory. You need to read the list of files in the directory each time kqueue says the directory was modified, and compare it to the list as it was the last time you read it. Only then will you know if a new file has appeared, or if a file has been removed, or if a file has been renamed (you will also need to keep track of the inode numbers for each file in the list to detect renames).
If you want to further monitor for changes to each file then you also need to add events for each file in the directory and update this list of events each time the event for the directory file is signalled.
FYI: This command-line utility does what you want, and can be built to use kqueue: https://github.com/emcrisostomo/fswatch

Matlab addpath() does not stick on the path when current directory is changed?

I have matlab_xunit, which is a folder with a bunch of functions used for writing unit tests. It's stored in ../external/matlab_xunit. I want to call runtests which is part of this package. Here is the sample code:
addpath(genpath('../external/matlab_xunit'))
runtests subdirectory
subdirectory is a sub-directory of the current directory. What runtests does is that it will change the current directory to subdirectory, and then it will find the tests in that folder and run the tests. The problem is, as I found out, as soon as the current directory gets changed, matlab_xunit is no longer on the path. Thus, all functions runtests has to call inside become invalid - they just 'vanish'.
Anyway around this shortcoming of addpath()?
Thanks much!
Use a full path. This will probably work:
addpath(genpath(fullfile(pwd,'../external/matlab_xunit')))

Creating a unique temporary directory from pure C in windows

I'd like to create a unique temporary directory in Windows from some C
code (not C++ or C#). I want to do this so that I can put some temp
files in the directory, and then delete them all easily when I'm done
(by removing the directory recursively).
I'm essentially looking for an equivalent of the linux
mkdtemp
function. There is a C# answer here, and responses on this
question
suggest using Boost. But since I'm using C, those solutions don't work
for me.
The best I've been able to come up with so far is to use
GetTempFileName
followed by CreateDirectory,
but the problem there is that if I ask
GetTempFileName
to create a unique file name, it will also create the file (which I
don't want, since I want to make a directory instead).
Relatedly, there's
GetTempPath,
which returns the location of the user's temp folder from environment
variables - but since I want to create my own directory that I can
safely delete later, I still need to create a directory inside any
path it would return.
It looks like if I want a unique directory to be created, I'll have to
create a temp file, get the name, delete it, and then create a
directory with the same name - which sounds very messy.
Any other ideas?
You can use what GetTempPath returns concatenated with a Guid to ensure uniqueness of the directory. You can create a Guid using UuidCreate or CoCreateGuid Function.
To delete recursively the directory, there is an example here in pure C: How to remove directory recursively? based on FindFirstFile, FindNextFile, DeleteFile and RemoveDirectory.
There is also SHFileOperation but it's more heavyweight and is based on the Windows Shell functions, and the Shell DLLs are not always wanted, especially if you're writing server code.
Use GetTempPath then CreateDirectory with a random name under it, optionally retrying if CreateDirectory fails due to it already existing. But if your name generation is good enough, the likelihood of a collision with an existing name is much smaller than the likelihood of a blackhat guessing your password or even your private key, so you might as well ignore it.
Use _tempnam tmpnam_s to create a filename that doesn't exist yet, and then use CreateDirectory to create the directory. There's technically a race condition if you do this, in that another process could potentially create a file or directory with that name in the time in between when you generate the filename and when you create the directory, but the odds of that are rather unlikely. To protect against that, you can loop until you succeed.
For recursively removing a directory tree, you can use SHFileOperation. Alternatively, you can do the directory traversal yourself with FindFirstFile/FindNextFile, DeleteFile, and RemoveDirectory.
If you want to remove the directory automatically upon exiting, register a function using atexit. This will only work for normal program termination (i.e. via the exit function or via returning from main/WinMain). This will not work for abnormal program termination (e.g. via abort, an access violation, someone else calling TerminateProcess, etc.).

Delete all files from pendrive using C program

I am searching for a solution to delete all files from my pendrive using a C program. I don't want the code to detect the pendrive as that's not a concern right now. I would appreciate any links that could help me out on this.
Note: I am working on Windows 7 64-bit and I want to delete the entire contents from my pendrive, which contains .exes and .dlls.
Thanks and Regards,
Radix
The non-hack correct way to do this, is via the Virtual Disk Service.
If you can't use something like rm -rf F:\* and you really do need to implement it yourself in C then I think I'd probably opt for a recursive solution based on FindFirstFile and FindNextFile. These are the native Windows APIs for enumerating directories. Not remotely portable, but it doesn't seem that's a requirement.
Basically the idea is that you write a function, EmptyDir(), say, whose job it is to delete the contents of a directory. The function uses FindFirstFile and FindNextFile to walk the contents of the directory. When a file is encountered it is deleted. When a directory is encountered, EmptyDir() is called recursively. The last job of EmptyDir(), before it returns, is to delete the now empty directory.

Resources