is ruby rm_rf asynchronous? - ruby

if I do
rm_rf('somewhere')
mkdir('somewhere')
every so often, the mkdir throws a Permission Denined. Is this because the rm_rf runs asynchronously and sometimes hasnt finished deleting the dir before the mkdir runs?
How do I make it block until its completed the delete?
running this on Windows btw.
Thanks

Sounds like a feature of NTFS, see Microsoft Suppport:
This file is in a state known as pending deletion. This file has been
deleted, but there are still handles open to it. NTFS will wait until
all handles to this file are closed before updating the index. If an
attempt is made to access the file, however, NTFS will deny the
attempt. Because the file is listed in the index, but is effectively
deleted, you can see the file but you cannot access it.
And looking at the solutions, I don't think there is much to do other than to wait and retry.

rm_rf is synchronous, as any other usual ruby method.
and if it was'nt then you'd probably get a Errno::EEXIST error, but you are getting 'permission denied'.
as a workaround try to insert small delay, like sleep(0.1) between rm_rf & mkdir

I faced this error too and renamed it first as a workaround:
build_dir.directory? &&
build_dir.rename(old_build_dir) &&
FileUtils.rm_rf(old_build_dir)
build_dir.mkdir
(build_dir and old_build_dir are Pathname objects)
Not perfect but better than a random sleep.

Related

Terminal says there is no file

enter image description here
PLEASE HELP! I can't seem to run my file. I've compiled it so it should run shouldn't it?
It also happens to my other files and I can't seem to fix it.
Ok, so a quick point of notice:
It is usually appreciated if you post your code directly in your question and use the code markup for that. It makes it easier to answer your question and prevents the image from turning into a dead-link at one point in time.
Then, regarding your code: it seems your Topic3ex1.c still has some errors, it always helps to fix them first. Maybe the errors are preventing Topic3ex1 (which I believe you have selected as the output file with the -o option? I am not familiar with gcc's syntax.) from being formed. Check if the file exist first by executing ls -a from the command line.
If you get a message saying "No such file or directory" while it is indeed there, this is usually caused by a lack of user rights. Try executing chmod u+x Topic3ex1.c and then run your command again. This will give your current user the right to execute the file (read up on chmod if you do not know it already, you will need it often).
Final question: is Topic3ex1 supposed to be a file or a folder? For if it is a file, then executing it like ./Topic3ex1 will only work if it is a shellscript (in that case, best rename your file to Topic3ex1.sh, its best to always mention the extension of the file). If it is a folder the ./ command won't do anything and the cd Topic3ex1 (cq. change directory-)command you where experiencing with will activate it as your working directory. If it is a file however, then the change directory command will, of course, be useless.

Why does mkdir occasionally give Access Denied?

I have BAT scripts which are nothing tricky and work fine on XP. But on Win 7, about 1 in 5 executions of mkdir following rmdir give a mystery Access Denied. E.g.
S:\TLIB importing\! Curtains\2 To process>rmdir temp3allout /s /q
S:\TLIB importing\! Curtains\2 To process>mkdir temp3allout
Access is denied.
After this, when I try in Explorer, it has no problem making that directory. Running thatBAT again usually succeeds.
Any idea what's going on here?
Win XP disc was a regular 2Gb drive. Win 7 disc is a 2Gb Intel RST RAID1 array with caching and flushing disabled http://i.imgur.com/Ohqkg2t.png .
This happens when the file system hasn't finished deleting the directory yet.
Sometimes this will happen synchronously, i.e., before the rmdir command completes, but sometimes there will be a very short but nonzero delay. (In XP it was always synchronous, IIRC.)
If possible, avoid deleting and immediately recreating directories; if you can't avoid it, you'll need to detect the failure and retry.
You should probably also test and if necessary retry the rmdir; sometimes rmdir runs into the same problem and fails to delete the entire directory tree.
Because that directory or file in that directory is open in some editor,first you have to close that file/directory from editor and then try.
These error usually comes when we create some directory and then delete it,but it is partially deleted and we create new directory with same name.

Errno::ENOENT: Error running rspec test, says file doesn't exist but it does

So I am trying to test for a controller but am running into problems. Part of the code requires a file to be loaded, but I am receiving this error:
Errno::ENOENT:
No such file or directory - ./public/licenses/58efcffc3d8441fcc0627164a8b1350aFCV5Hmashup_lic.xml
However, the file does exit. When I run it in the browser, it works just fine with no problems (but i still want to write a test).
I don't know if this has anything to do with it, but I am using a database when running the test. I know this isn't actually how I should do it, but I don't know any other way.
Any help would be awesome. Thanks.
Most likely because your assumption about where the current working directory is when running the tests is wrong. try putting a puts File.dirname('.') in there to make sure you're running from the directory you think you are.
As a debugging tool:
In your test, just before the statement that checks for the file, try adding
puts "list dir " + `ls ./public/licenses/`
to see if the file is really there.
Could be a permissions problem from the test user id
Could also be that the file isn't actually there...

How can I gain permission to rename a file for my Ruby program?

As per the answer to this question, I am trying to backup a file by renaming it, before I replace it with a new, modified file with the old name.
As per the comments and the documentation here, I am using the following line of code:
File.rename(File.basename(modRaw), File.basename(modRaw)+'.bak')
However, when I do so, I get the following error at runtime:
The program then aborts. (leatherReplacer.rb is the name of my program, and line 88 is the above line of code)
How do I allow my program to rename the files it needs to to run successfully?
Windows has some special rules regarding permissions. The important one at work here, is that the OS prevents moving or renaming a file while the file is open.
Depending on the nature of your code (in size and scope) and the importance of the file you're trying to back up, it may be unfeasible or otherwise not worthwhile to refactor the code in such a way as to make backups possible.
You probably don't want to be calling File.basename in there, that strips off the directory:
Returns the last component of the filename given in *file_name*, which must be formed using forward slashes ("/") regardless of the separator used on the local file system.
So, if modRaw is /where/is/pancakes.house, then you're saying:
File.rename('pancakes.house', 'pancakes.house.bak')
But pancakes.house probably isn't in the script's current directory. Try without the File.basename calls:
File.rename(modRaw, modRaw + '.bak')
If you are owner of that file, use File.chmod to set desired permissions.
I don't know much about ruby, but could you run it under command line/bash with admin privileges, such as "run as administrator" or "su root"?
According to Objectmix and ruby-forum, you should set it to 755 or +x, then perhaps chown to yourself.
try using full file path e.t
File.rename('c:\pancakes.house', 'c:\pancakes.house.bak')
in win7 i encounter same problem

Applescript folder actions support for changed / altered / updated files?

Does the folder action for when a folder item is changed not exist? I want my script to run when and if I update a file. I don't see any reference to it in the documentation. Is there some sort of alternative I am missing because this seams pretty crazy to not have.
on adding folder items to this_folder after receiving added_files
do shell script "anything"
end adding folder items to
on removing folder items from this_folder after losing removed_files
do shell script "anything"
end removing folder items from
-- does not exits?!?
on changing folder items in this_folder after updating changed_files
do shell script "anything"
end changing folder items in
Nope, doesn't exist directly. However, something similar could be accomplished with an idle handler that watches the files in the folder to see if their modification date has changed and perform an action on files where that's true.
There is an alternative to folder actions. You use launchd and setup a watch path. With a watch path, any time something changes in the folder you are watching, your code runs. The biggest difference between folder actions and the launchd action is that with the launchd action you don't know which files changed. You just know something changed. So your code has to figure out what the change actually was, but that shouldn't be too difficult in your case because if you're looking for an updated file you just check the modification date of the files.
You can google for launchd and watch paths if you want to try it.
What about rsync -va '/source/path/' '/destination/path/', using Lingon, have this simple command set as an user Daemon to run, say, every 10 sec?
I found a tricky way to do this with Automator easily and works only in some cases so try it and see if it helps. When you create/modify a folders contents in OSX a hidden OS file called .DS_Store gets written to the folder, its a useless file to a user but it can trigger the folder action. With that said, I use rsync in a Run Shell Script action in my folder action. Once the action is done syncing I then remove the .DS_Store file.
Here is my example:
rsync -r /Users/path/to/source/* /Users/path/to/destination
rm -f /Users/path/to/source/.DS_Store
Then the next time you modify files/folders in that directory, the folder action kicks in and the process would repeat.
I hope this helps...

Resources