Unix invoke script when file is moved - unix

I have tons of files dumped into a few different folders. I've tried organizing them several times, unfortunatly, there is no organization structure that consistently makes sense for all of them.
I finally decided to write myself an application that I can add tags to files with, then the organization can be custom to the actual organizational structure.
I want to prevent from getting orphaned data. If I move/rename a file, my tag application should be told about it so it can update the name in the database. I don't want it tagging files that no longer exist, and having to readd tags for files that used to exist.
Is there a way I can write a callback that will hook into the mv command so that if I rename or move my files, they will invoke the script, which will notify my app, which can update its database?
My app is written in Ruby, but I am willing to play with C if necessary.

If you use Linux you can use inotify (manpage) to monitor directories for file events. It seems there is a ruby interface for inotify.
From the Wikipedia:
Some of the events that can be monitored for are:
IN_ACCESS - read of the file
IN_MODIFY - last modification
IN_ATTRIB - attributes of file change
IN_OPEN and IN_CLOSE - open or close of file
IN_MOVED_FROM and IN_MOVED_TO - when the file is moved or renamed
IN_DELETE - a file/directory deleted
IN_CREATE - a file in a watched directory is created
IN_DELETE_SELF - file monitored is deleted
This does not work for Windows (and I think also not for other Unices besides Linux) as inotify does not exist there.

Can you control the path of your users? Place a script or exe and have the path point to it before the standard mv command. Have this script do what you require and then call the standard mv to perform the move.
Alternately an alias in each users profile. Have the alias call your replacement mv command.
Or rename the existing mv command and place a replacement in the same dir, call it mv and have it call your newly renamed mv command after doing what you want.

Related

How to edit a file from within configure.ac?

I have a configure script to set up some paths for my R package during installation. I wish to edit a file based on some conditions. Is there any way to edit a file from within the configure.ac? It would be great if the solution is provided for all operating systems.
Is there any way to edit a file from within the configure.ac?
configure.ac is not executable, but I suppose you mean that you want the configure script generated from it to edit a file. The configure script is a shell script, and you can cause arbitrary shell code to be included in it, more or less just by including that code at the corresponding point in configure.ac.
The question, then, is how you would automate editing a file with a shell script. There is a variety of alternatives, but sed is high on my list. You will find it on every system that can support Autoconf configure scripts, because such scripts use it internally.
On the other hand, this sort of thing is one of the main activities of a configure script, in the form of creating files (especially makefiles, but not limited to those) from templates. You should consider building your target file of interest from a template in this way, instead of making custom-programmed edits to a file packaged in your program distribution. This would involve
setting output variables containing the chosen content for the parts of the file that need to be configured;
designating the target file as one for configure to build; and
providing the template, maybe by taking a complete example file and replacing each variable part with a reference to the appropriate #output_variable#.

Githooks for `git add`?

I've setup a build process using Gruntjs, but don't want anyone to be able to store the output (all contained in a build/ directory) in source control. Don't Repeat Yourself n such.
I've currently got a pre-commit hook setup that cleans the build/ directory and fails the commit if there are any unstaged files (I also stash then unstash so intentionally unstaged files won't interfere with this check).
It would be more user friendly to fail at the point a build file is added: git add build/.... But I can't seem to find an example commit hook for git add. I looked at http://git-scm.com/book/ch7-3.html and http://githooks.com/.
Is this possible? If not, can you think of an improvement over my current setup?
One of those moments where the answer comes to you as you're typing it out.
I can simply setup my .gitignore to forget build/, then also use the clean in the precommit hook to double check that no build files were added.

mkfifo command failing on clearcase vobs

I am trying to create named pipe in a directory which is created under clearcase's vobs tree (/vobs/something/something) but not checked-in. I am getting this error:
"mkfifo: No such device or address"
I am not able to understand why pipe creation is failing while other files are getting created.
I am using Solaris 10. Is there any way I can create named pipes in vobs?
/vobs/something/something means MVFS path with a view set (as in cleartool setview).
First, try the same operation with the fumm path instead of trying to set a view. As I explain in "Python and ClearCase setview", setting a view creates a sub-shell, with all kinds of side effect for your processes (in term of environment variables and other non-heirted attributes).
So try it in /views/MyView/vobs/something/something.
Second, regarding pipe, check if this thread applies to your case:
Just off the top of my head if you using a pipe and not a file, then it should be specified something like this ..
destination my_pipe pipe("/data/pipes/net_pipe");
rather than
destination my_file file("/data/pipes/net_pipe");
Note that, for ClearCase up to 7.0.x:
ClearCase does not support adding to source control special files such as named pipes, fifos or device files. There are no type mangers available to manage these special files.
Note: Attempts to execute these files in the MVFS is not supported.
WORKAROUNDS:
Keep multiple versions of directories with device files outside of a VOB and versioned directories/symlinks in a VOB to point to correct directory location outside the VOB.
Keep a tar or zip archive of the tree with device files in the VOB, and extract it to a temporary workspace when needed in the development process.

Can you create a batch file in asp.net?

Can you write code to create multiple batch files in asp.net? Can you create it and also have the code write batch file commands into the batch files? What I want to do is create .bat files in a certain location on the computer (or make it create a folder to put the .bat files into) on a button click and have it write all the commands into the .bat file. For example, I have a web form the user inputs data, pushes a button and based off the .bat files it creates, it creates a new batch file every time the button is clicked. Is this possible?
Yes, it is, but it could have a lot of security consequences, so, be careful.
You create a file using methods in the File class, like File.CreateText.
http://msdn.microsoft.com/en-us/library/system.io.file.createtext.aspx
Yes, this is possible but in general, you will only be able to create these files inside your application's folder.
If you need to create the files outside your application's folder, you need to make sure that the App Pool your app runs under, has permissions to write to that folder.
Since batch files are basically executables, I'd be very careful about using them and I would never, under any circumstance take free user input to be placed into those files. You may give the user a set of predefined "commands" to chose from to mitigate your exposure, for example.

How do I scp a file to a Unix host so that a file polling service won't see it before the copy is complete?

I am trying to transfer a file to a remote Unix server using scp. On that server, there is a service which polls the target directory to detect incoming files for processing. I would like to ensure that the polling service does not pick up new files before the copy is complete. Is there a way of doing that?
My file transfer process is a simple scp command embedded in a larger Java program. Ideally, a solution which did not involve changing the Jana would be best (for reasons involving change control processes).
You can scp the file to a different (/tmp) directory and move the
file via ssh after transfer is complete. The different directory needs to be on the same partition as the final destination directory otherwise there will be a copy operation and you'll face a similar problem. Another service on the destination machine can do this move operation.
You can copy the file as hidden (prefix the filename with .) and copy, then move
If you can modify the polling service, you can check active scp processes and ignore files matching scp arguments.
You can check for open files with lsof +d $directory and ignore them in the polling server
I suggest copying the file using rsync instead of scp. rsync already copies new files to temporary filenames, and has many other useful features for file synchronization as well.
$ rsync -a source/path/ remotehost:/target/path/
Of course, you can also copy file-by-file if that's your preference.
If rsync's temporary filenames are sufficient to avoid being picked up by your polling service, then you could simply replace your scp command with a shell script that acts as a wrapper for rsync, eliminating the need to change your Java program.
You would need to know the precise format that your Java program uses to call the scp command, to make sure that the options you feed to rsync do what you expect.
You would also need to figure out how your Java program calls scp. If it does so by full pathname (i.e. /usr/bin/scp), then this solution might put other things at risk on your system that depend on scp (like you, for example, expecting scp to behave as it usually does instead of as a wrapper). Changing a package-installed binary like /usr/bin/scp may also "break" your package registration, making it difficult to install future security updates because a binary has changed to a shell script. And of course, there might be security implications to any change you make.
All in all, I suspect you're better off changing your Java program to make it do precisely what you want, even if that is to launch a shell script to handle aspects of automation that you want to be able to change in the future without modifying your Java.
Good luck!

Resources