My own personal WTF


  • Considered Harmful

    I work with a buggy development environment that frequently manages to lock files and not be able to unlock them.  Further attempts to compile are met with Access Denied errors.  Typically the only way to get a compilation to work when this occurs is to close the environment, stop the service it depends on, delete all cached copies of the compiled binaries, restart the service, and restart the environment.  It's a 5 to 10 minute process I have to repeat a dozen times every day.  That's not the WTF.

    Since it's something I do so frequently, I wrote a batch file to automate it.  I keep it in my %PATH% so I can run it from anywhere.  It's been working correctly for a year and half now, every day.  Today I ran into a subtle bug that caused it to delete itself and all my other batch files.  Since it's gone now, I'll attempt to recreate the basics of it here.  See if you can spot the bug.

    @echo off
    pskill devenv
    iisreset /stop
    pushd
    cd %temp%
    attrib *.* -r -a -s -h /s
    del *.* /s /f /q
    rem --snip-- a bunch of other stuff
    popd
    iisreset /start

    Do you see it?

    The cd, change directory, command changes the working path of the specified volume letter; only this time when I ran it I was currently in the D: volume.  So it kept executing in the current directory, a nice del *.*.  Fun.  I suppose I'm lucky that it was only my batch files folder that was deleted.

    Of course, the real WTF is that this script is even necessary.  Bonus points if you recognize the specific bug I'm working around.



  • @joe.edwards@imaginuity.com said:

    The cd, change directory,
    command changes the working path of the specified volume letter; only
    this time when I ran it I was currently in the D: volume.  So it
    kept executing in the current directory, a nice del .
    Fun.  I suppose I'm lucky that it was only my batch files folder
    that was deleted.

      Ow, you have my
    sympathy!  Auto-mass-delete scripts are scary... I often write
    mine so they don't do the delete, they just generate a script file that
    explicitly deletes all the needed files, then I eyeball it to be sure
    it'll DTRT before kicking it off manually from the command line.

     
    BTW, you may not have heard of the "/D" flag that 'cd' accepts in
    cmd.exe.  It was never in the '9x series command.com, but it's
    been in the NT/2k/Xp series since at least NT4.  It's the solution
    to your problem.... just sorry that it's a little too late. I guess one
    cwd per drive could be considered a WTF in itself.


  • Considered Harmful

    @DaveK said:

    @joe.edwards@imaginuity.com said:

    The cd, change directory, command changes the working path of the specified volume letter; only this time when I ran it I was currently in the D: volume.  So it kept executing in the current directory, a nice del ..  Fun.  I suppose I'm lucky that it was only my batch files folder that was deleted.

      Ow, you have my sympathy!  Auto-mass-delete scripts are scary... I often write mine so they don't do the delete, they just generate a script file that explicitly deletes all the needed files, then I eyeball it to be sure it'll DTRT before kicking it off manually from the command line.

      BTW, you may not have heard of the "/D" flag that 'cd' accepts in cmd.exe.  It was never in the '9x series command.com, but it's been in the NT/2k/Xp series since at least NT4.  It's the solution to your problem.... just sorry that it's a little too late. I guess one cwd per drive could be considered a WTF in itself.

    Every mistake I make is a mistake I won't make again.  I like to think of it as, "I learned something new today."  I can recreate most of my batches from memory.  Thanks for the tip about /D, though the one about generating explicit delete statements may be even better.



  • At a guess the problem would be the fact the IIS will require you restart the machine if you replace or attempt to remove any COM objects that hook into IIS.

    Additionally the bastard thing locks files that will complicate things.



  • I feel your pain.  I managed to write a Perl script that deleted the directory it was in.  Luckily I had another file open so I only got an error log with 90,000 lines of "Can't delete file".



  • @evilkarl said:

    At a guess the problem would be the fact the IIS will require you restart the machine if you replace or attempt to remove any COM objects that hook into IIS.

    Additionally the bastard thing locks files that will complicate things.

    what is that flag again?

    oh.. i remember....

     fopen(  file , FILE_MY_PRECIOUSSS );

    :P 



  • It's things like this that remind me why the unix shell is the worst way to manage automation tasks, except for all the other ways.



  • Misread that.  And /how/ short is the time limit for deleting your own posts?  Less than about five seconds as far as I can see.

     



  • I remember having a similar problem to that when I was playing with .dll injection while debugging the host application. Even if I removed the .dll file from the process, the debugger (windbg, I believe) kept a handle to it.

    I guess I was smarter/dumber back then, since it didn't occur to me to automate the clean-up :) 



  • @asuffield said:

    It's things like this that remind me why the unix shell is the worst way to manage automation tasks, except for all the other ways.

    Other than the fact that this is the windows cmd.exe shell.

     

    Also, when I have a script that deletes lots of files, I usually make it so that it backs them up first into a directory... that way if I run it I can always get all the files back (as long as I don't run the thing twice before I notice I guess).  If you do this technique you can also make it generate a script that will move all the files back to their original locations if you are expecting to mess up a lot!
     



  • @tster said:

    @asuffield said:

    It's things like this that remind me why the unix shell is the worst way to manage automation tasks, except for all the other ways.

    Other than the fact that this is the windows cmd.exe shell.

    No, that was part of my point. Read it more carefully. 




  • I work with a buggy development environment

    I was wondering what environment you were talking about, and being slowly lulled into your story, watching, waiting for the punchline, biting my nails, fidgetting on the edge of my seat.  I read through your code line by line, to see if I was clever enough to spot the subtlety of the WTF.

     Then I found it:

    iisreset /start

    You started IIS instead of Apache...
     


Log in to reply