| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Next » |
|
that's not even a copy routine, it's a rename!
made in platfrom independent java makes it even more a wtf hehe |
|
Reminds me of a saying..."When all you have is a hammer, EVERYTHING looks like a nail"
|
|
Java doesn't actually have an OS-level file copy method that can be called (you have to actually read in the file and write it out to a new file!), so I can understand their twisted logic to some degree. But... a batch file? For the love of god, why?
|
Re: My Kind of copyFile
2007-08-03 09:13
•
by
Look at me! I'm on the internets!
(unregistered)
|
|
Anything wrong with:
process.exec("copy file1 file2"); // windows You could add a System.getProperty("os.name") and make it universal. Of course, I'd probably want to uses an abstract factory to do this -- It's much more enterprisy. |
|
It's not quite rename though is it?
If you try to rename a file as itself, that file will instead be deleted. A poor attempt, to be thorough it should compare the source and dest names, as well as the MD5 sum of the batchfile, C-, must try harder. |
|
Anyone notice that this moves the file, instead o0f copying?
sure, it copies, but then it deletes the source! *sarcastic clap* |
|
The real WTF is that this should be the CodeSOD?
Mike5 |
Re: My Kind of copyFile
2007-08-03 09:21
•
by
Ben
(unregistered)
|
This, of course, is the real WTF! |
Re: My Kind of copyFile
2007-08-03 09:21
•
by
Ben
(unregistered)
|
This, of course, is the real WTF! |
|
I can't believe no one else has mentioned another huge error in this. I guess you people haven't worked with a unix shell enough to know which characters are OK to use in command lines and which need special processing.
This is obviously designed to run on windows, where directories commonly have names like "My Documents," "Program Files" and "Application Data." All of those have a character in them that will make the batch file call fail instantly. |
my eyes! |
So, just copy it back then! |
Re: My Kind of copyFile
2007-08-03 09:24
•
by
Phlo
(unregistered)
|
I wonder if the programmer also burns the original papers after taking a copy of them... oO |
Re: My Kind of copyFile
2007-08-03 09:26
•
by
Greatest CodeSOD ever
(unregistered)
|
|
Yes, this is utterly stupid. First, a batch-file. Second, it's wasting time on copying when it could just use "move" instead. Third, it doesn't check if the copy operation was a success, and just deletes the source file when it's done.
That is, it checks if the _source_ file exists, _AFTER_ it has attempted to copy it. Perfect, beautiful! |
|
it has a tidy API, only a couple of obvious bugs (for example: someone could re-write that batch file to do something different), and it's a great hack!
so what if it is not system independent, but not everyone needs that. sure, it's named wrong: it should be something like 'copyThenDeleteOriginal', but that's not much of a WTF. How this code leaves you speechless is beyond me. |
Re: My Kind of copyFile
2007-08-03 09:29
•
by
anon
(unregistered)
|
huh? |
Plenty of languages don't have built-in routines to do high level operations like copying files, but most have libraries available to do it (such as Perl's File::Copy). The true WTF is that Java doesn't have a standard or sane way of installing extra libraries, so programmers have to reinvent broken wheels like this all the time. |
|
Another cool feature is that it will only "work" one time on any given PC, since it doesn't overwrite the batch file it already exists, so it just keeps running the first batch file ever created by the method. Genius!
|
|
This is a pretty bad implementation of a pretty legit idea. Sometimes you HAVE to use batch files to do those things from java to make the result more predictable and less dependent on particlar jvm problems. In some cases this helps to speed up the file copying procces by 2-5 times or so comparing to common file copying methods from Apache Ant. There is usually more that one command in that batch, though, and there are all kinds of symbol quoting code, as well as checks for the system this is used on.
|
|
I always wanted a back door built into a system that would be hard to spot . The batch file sounds like the perfect place to put in my thanks (delete all files) when I get fired for doing crap like this.
|
|
Now that was a WTF.
The best part was where it deletes the file after it's copied it. The second-best part was that none of the methods are synchronized, creating all kinds of race conditions. |
Re: My Kind of copyFile
2007-08-03 09:44
•
by
alarm
(unregistered)
|
|
Util.copyFile(new File("C:\\Util.class"), new File("C:\\copyFile.bat"));
System.out.println("Problem solved."); |
Re: My Kind of copyFile
2007-08-03 09:46
•
by
Asd
(unregistered)
|
Bwuh? Just drop commons-io.jar into your classpath. Most Java applications use dozens of open source libraries. Java 7 or 8 is supposed to get a new and improved file system. I also noticed their IO util methods don't seem to throw IOException. I wonder do they use RuntimeExceptions (unchecked exceptions), or, more likely, just ignore them. |
I'd say the fact that he fit so many problems into such a short amount of code would be a definite reason for being speechless. I suppose there's worse things he could have done: thrown both functions together, ask for the source and destination in the function rather than handing them in, not actually displayed anything in the catch, not checking if the batch file exists before returning. No matter how bad code gets, there is ALWAYS a worse way to do it (the WTF calculators taught us that) |
Re: My Kind of copyFile
2007-08-03 09:49
•
by
Anon
(unregistered)
|
|
Um, but the batch file does copy %1 %2, so you can keep running the same batch file every time with different arguments, as this one does.
|
|
If I'm not mistaken, an existing batfile is not modified but it is executed nontheless. And a used batchfile is not removed so chances are that one exist after the first "copy".
|
|
I vaguely recall that to execute dos commands from Java you have to run "cmd /c copy" rather than just "copy". Maybe I am misremembering but that could have been the trigger for this sorry batch mess.
|
Re: My Kind of copyFile
2007-08-03 09:52
•
by
rd
(unregistered)
|
Brilliant! |
|
Let's see:
It moves rather than copies. It 'confirms' the move by checking for the existence of the source file, rather than the destination. It'll die horribly if either the source or destination names have spaces in them. It won't work on *nix or, I presume, on the Mac. It won't work if the program doesn't have write and execute access to the root directory--i.e. it requires what are usually administrator rights. It will produce undefined results if two instances are run at once either by separate threads or separate processes. Have I missed anything else? Just what what kind of idiot wrote this? |
|
I'm quite impressed. First it makes a call to a parameterless method which tries to create a batch file in a hard-coded location which might very well not have write permission, opens the file twice (write, then append), doesn't trap any exceptions, doesn't quote the file names, and only does this if the file doesn't already exist (doesn't care if it was modified). Then it launches the batch file as its own process regardless of what might be in it, checks for the existence of the source file after attempting to copy it, deletes the source file even if the source and destination were the same, and essentially eats any exceptions during the "copy" process (but would still throw exceptions encountered during the batch file creation). And on top of this, is named copy when it clearly moves.
The best part though, is that somebody has already written a FileIO class that could probably do this in two lines - we don't know that it has a readFile method, but it definitely has a writeFile. My Java's just a bit rusty but I think I count no less than 10 different errors. It's very Agile though, I'll give them that. Way to think outside the process... er, box. |
Re: My Kind of copyFile
2007-08-03 09:58
•
by
snoofle
(unregistered)
|
|
To add to a prior poster's suggestion:
Think of the possibilities! |
But Java is not a language that normally shies away from providing high level functionality in its standard libraries. |
|
And don't forget that he's assuming that (even in Windows) there *is* a C: drive.
|
Re: My Kind of copyFile
2007-08-03 10:07
•
by
DoubleMalt
(unregistered)
|
|
Moreover the approach won't work because process.exec() does no path resolving (and cannot call built-in commands of the shell). It depends on a existing file with the exact name given. Relative paths are resolved against the current wurking directory.
The smart author of the code snippet aptly avoided this pitfall. ;) |
Re: My Kind of copyFile
2007-08-03 10:09
•
by
DoubleMalt
(unregistered)
|
Forgot to quote. My remark was in reply to this *points to the text above* |
|
I choose not to believe this. It isn't true.
|
Re: My Kind of copyFile
2007-08-03 10:24
•
by
JohnFx
(unregistered)
|
|
There is a million dollar idea in there. A combination photocopier/shredder.
|
Re: My Kind of copyFile
2007-08-03 10:26
•
by
Someone You Know
|
"Brillant!" is the word you're searching for, I'm sure. |
|
The "utility" doesn't check if the batch file has actually been created (the user doesn't have to be authorized, apart from the fact that a C drive is present, as has been noted). It also doesn't clean up its mess, but rather reuses the file! What if someone added a "format c:" to it? Lastly, I don't understand the need to "cd\" if absolute paths to the file are passed. It's mind boggling that such a few lines of code can contain so many problems.
|
Re: My Kind of copyFile
2007-08-03 10:39
•
by
John Doe
(unregistered)
|
I wish there was at least some thinking involved, so this piece of junk would never have been written... |
stick to coding, dude. leave the copy editing to people who know how shit is spelled. |
|
Write once, look stupid everywhere.
|
Re: My Kind of copyFile
2007-08-03 10:57
•
by
Mainc Mailman
(unregistered)
|
You indeed correctly spelled 'shit', but "Someone You Know" also correctly spelled 'Brillant!'. Look it up, dude. |
Re: My Kind of copyFile
2007-08-03 10:58
•
by
Gil
(unregistered)
|
|
Yes, you missed the fact that if anything in the try actually goes wrong, there is no way the calling can know if it was successful short of testing for the presence of the new file.
|
|
Hey now, you've got to give the guy _some_ credit: this would execute faster than Vista's built in copy routine.
|
Re: My Kind of copyFile
2007-08-03 11:02
•
by
dkf
(unregistered)
|
Oh, how have the mighty fallen! If you don't recognize a reference to http://worsethanfailure.com/Articles/The_Brillant_Paula_Bean.aspx straight off, you should either hang your head in shame or go directly to that article and enjoy... |
Re: My Kind of copyFile
2007-08-03 11:02
•
by
snoofle
(unregistered)
|
Good one! |
| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Next » |