kqueue watch size change on directory - c

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

Related

Shake update of index files after changes

I use the shake build system and watch to keep files for a web site current. I cannot understand how to assure that an index file for a directory is changed when a file in the directory changes.
The index file for a directory lists all the files in it with their title and includes a link. From this, a HTML file is eventually produced by the shake process.
It requires reconstruction when one of the indexed files in the directory changes.
For each index file, the set of files indexed are marked as needed but this does not force the index file to be reconstructed when a file in the directory changes. I had expected that this would trigger the reconstruction of the index file if any of the needed files changes. This seems not to be a correct understanding.
What is the most effective method, to force a re-shake of the index file when a file in the directory changes. Is it sufficient to touch the index file to trigger the reconstruction? Or is it better to recompute the conversion of the index.md file to the next step (pandoc) and the following processing steps are then triggered by the shake logic? Or anything else?

Update file across multiple folder locations?

I need something that can copy a specified file any and everywhere on my drive (or computer) where that file already exists; i.e. update a file. I tried to search this site, in case I'm not the first, and found this:
CMD command line: copy file to multiple locations at the same time
But not quite the same.
Example:
Say I have a file called CurrentList.txt, and I have copies of it all over my hard drive.  But then I change it and I want all the copies to update.  So I want to copy the newer one over all the others.  It could 'copy if newer', but generally I know it's newer, so it could also just find every instance and copy over it.
I was originally going to use some kind of .bat file that would have to iterate over every folder seeking the file in question, but my batch file programming is limited/rusty.  Then I looked to see if xcopy could do it, but I don't think so...
For how I will use it most, I generally know where those files are going to be, so it actually might be as good or better if I could specify it to (using example), "copy CurrentList.txt, overwriting all other copies wherever found in the C:\Lists folder and all subfolders".
I would really like to be able to have it in a context menu, so I could (from a file explorer) right click on a file or selected files and choose the option to distribute it.
Thanks in advance for any ideas.
Use the "replace" command...
replace CurrentList.txt C:\Lists /s

Using inotify to check if files in a watched folder are overwritten

I have written a C program that uses inotify to watch a particular folder. I want to be notified when a new file is created in this folder, or if an existing file was overwritten (i.e. the folder already contains a.txt, and I copy a newer copy of a.txt into this folder, essentially overwriting it).
I am able to generate a notification when new files are added to this folder using the IN_CREATE mask, but I don't get any notification for the second scenario. I've tried IN_MODIFY and IN_ATTRIB, both didn't work.
Is it possible to be notified of the second scenario?
If you overwrite the file you will not get the IN_MODIFY and IN_ATTRIB event. Because, the file is not modified and also the file attributes are not changed. The overwritten is equal to creating a new file. So, You can try that two events with separate.
IN_MODIFY:-
This event is occurred only when the file is modified. To notify this event, you can try to update or remove some contents in the watching list file.
IN_ATTRIB:-
This event is occurred only when the file attribute is changed. Attribute means file meta data. So, If you want to get this event for a file, You can change the file permission in the watching list file.
Make sure, you want to see this event, you have to add the following events in the event list, When your inotify is initialized.
In_MODIFY
IN_ATTRIB

Rename code in c have a error

i want to move a file from a directory to anther directory with C Coding.
I search and find rename(); function , but when working it doesnt work and have a error:
my code:
#include <stdio.h>
int main() {
if(rename("/root/tmpfile.php", "/home/check-tmp.php"))
perror( NULL );
}
the code well compiled but when running this code showing this error:
Invalid cross-device link
How to move a file from a directory to anther directory without using System for fopen?
Aslo , i finded many codes and ways to do it but doesnt working all codes.
Please say me a way and make sure it will work
Thanks.
Many aspects of the behavior of `rename' are inherently platform-dependent: The rename operation might not be able to move a file from one file system to another , it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists.
In other words, your system does not support rename files cross different partitions (your root partition and your home partition are different.)
So the solution is when it fails, copy the file to the destination and delete the original.
The rename call can only rename and move files within a single disk partition. The error "cross-device link" indicates that you attempted to move a file from one partition to another. (If you were on a Windows system, you can imagine if you tried to rename a file from C: to D:.)
When you use the Unix mv command to move files, it first tries a rename, but if it fails in this way, it falls back and makes a new copy of the file in the new location, then deletes the original. That's what you would have to have your code do in this situation, too.
(Copying a file is easy enough, but there are plenty of library functions out there that will do it for you, and also take care of things like preserving the last-modified time and other file attributes.)

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

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!

Resources