Comment On My Kind of copyFile

After running this site for a little more than three years, it’s rare that I’ll come across a snippet of code that leaves me speechless. Today’s snippet comes from a large Java application that Byron recently started working on and is … well, I’ll just let the code do the talking… [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: My Kind of copyFile

2007-08-03 09:02 • by Tim (unregistered)
wow...

Re: My Kind of copyFile

2007-08-03 09:03 • by Dube (unregistered)
that's not even a copy routine, it's a rename!

made in platfrom independent java makes it even more a wtf hehe

Re: My Kind of copyFile

2007-08-03 09:11 • by Joe (unregistered)
Reminds me of a saying..."When all you have is a hammer, EVERYTHING looks like a nail"

Re: My Kind of copyFile

2007-08-03 09:12 • by Volmarias
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.

Re: My Kind of copyFile

2007-08-03 09:14 • by RaCeR (unregistered)
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.

Re: My Kind of copyFile

2007-08-03 09:16 • by DOA
That hurt my brain

Re: My Kind of copyFile

2007-08-03 09:16 • by Jim (unregistered)
Anyone notice that this moves the file, instead o0f copying?

sure, it copies, but then it deletes the source!

*sarcastic clap*

Re: My Kind of copyFile

2007-08-03 09:17 • by Mike5 (unregistered)
The real WTF is that this should be the CodeSOD?

Mike5

Re: My Kind of copyFile

2007-08-03 09:21 • by Ben (unregistered)
148031 in reply to 148025
Volmarias:
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!)


This, of course, is the real WTF!

Re: My Kind of copyFile

2007-08-03 09:21 • by Ben (unregistered)
148032 in reply to 148025
Volmarias:
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!)


This, of course, is the real WTF!

Re: My Kind of copyFile

2007-08-03 09:22 • by ailivac
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.

Re: My Kind of copyFile

2007-08-03 09:23 • by GuntherVB
148034 in reply to 148026
Look at me! I'm on the internets!:
Anything wrong with:

process.exec("copy file1 file2"); // windows

You could add a System.getProperty("os.name") and make it universal.


my eyes!

Re: My Kind of copyFile

2007-08-03 09:23 • by bkendig
148035 in reply to 148029
Jim:
sure, it copies, but then it deletes the source!


So, just copy it back then!

Re: My Kind of copyFile

2007-08-03 09:24 • by Phlo (unregistered)
148037 in reply to 148021
if (sourceFile.exists()){
sourceFile.delete();
}
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!

The REAL WTF is

2007-08-03 09:27 • by Nojazz Song Hurts (unregistered)
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)
148041 in reply to 148029
Jim:
Anyone notice that this moves the file, instead o0f copying?

sure, it copies, but then it deletes the source!

*sarcastic clap*


huh?

Re: My Kind of copyFile

2007-08-03 09:29 • by ailivac
148042 in reply to 148032
Ben:
Volmarias:
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!)


This, of course, is the real WTF!


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.

Re: My Kind of copyFile

2007-08-03 09:30 • by Alex (unregistered)
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!

Re: My Kind of copyFile

2007-08-03 09:33 • by Kittown (unregistered)
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.

Re: My Kind of copyFile

2007-08-03 09:39 • by DOOM (unregistered)
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.

Re: My Kind of copyFile

2007-08-03 09:39 • by jaspax
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)
148050 in reply to 148045
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)
148051 in reply to 148042
ailivac:

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.

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.

Re: My Kind of copyFile

2007-08-03 09:47 • by T $
148052 in reply to 148039
Nojazz Song Hurts:
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.


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)
148054 in reply to 148043
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.

Re: My Kind of copyFile

2007-08-03 09:50 • by ikke (unregistered)
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".

Re: My Kind of copyFile

2007-08-03 09:51 • by Asd (unregistered)
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)
148057 in reply to 148035
bkendig:
Jim:
sure, it copies, but then it deletes the source!


So, just copy it back then!



Brilliant!

Re: My Kind of copyFile

2007-08-03 09:53 • by Packrat (unregistered)
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?

Re: My Kind of copyFile

2007-08-03 09:53 • by Aaron
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)
148061 in reply to 148021
To add to a prior poster's suggestion:


public void copyFile(File sourceFile, File destFile) {
File batchFile = createBatchFile();
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec(batchFile.getAbsolutePath()
+ " " + sourceFile.getAbsolutePath()
+ " " + destFile.getAbsolutePath());
p.waitFor();
if (sourceFile.exists()){
sourceFile.delete();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}

public File createBatchFile(){
File batchFile = new File("C:\\copyFile.bat");
if (!batchFile.exists()){
utils.FileIO.writeFile(
batchFile.getAbsolutePath(),
"cd\\ \n");
utils.FileIO.appendFile(
batchFile.getAbsolutePath(),
"iexplore http://www.worsethanfailure.com \n");
}
return batchFile;
}


Think of the possibilities!

Re: My Kind of copyFile

2007-08-03 09:59 • by SuperousOxide
148062 in reply to 148042
ailivac:

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).


But Java is not a language that normally shies away from providing high level functionality in its standard libraries.

Re: My Kind of copyFile

2007-08-03 10:07 • by n9ds
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)
148064 in reply to 148034
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 zolf (unregistered)
WTF

Re: My Kind of copyFile

2007-08-03 10:09 • by DoubleMalt (unregistered)
148066 in reply to 148034
GuntherVB:
Look at me! I'm on the internets!:
Anything wrong with:

process.exec("copy file1 file2"); // windows

You could add a System.getProperty("os.name") and make it universal.


my eyes!
Forgot to quote. My remark was in reply to this *points to the text above*

Re: My Kind of copyFile

2007-08-03 10:10 • by troels (unregistered)
I choose not to believe this. It isn't true.

Re: My Kind of copyFile

2007-08-03 10:24 • by JohnFx (unregistered)
148068 in reply to 148037
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
148069 in reply to 148057
rd:
bkendig:
Jim:
sure, it copies, but then it deletes the source!


So, just copy it back then!



Brilliant!



"Brillant!" is the word you're searching for, I'm sure.

Re: My Kind of copyFile

2007-08-03 10:35 • by John Doe (unregistered)
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)
148073 in reply to 148059
Aaron:
Way to think outside the process... er, box.

I wish there was at least some thinking involved, so this piece of junk would never have been written...

Re: My Kind of copyFile

2007-08-03 10:46 • by tobjar (unregistered)
"Brillant!" is the word you're searching for, I'm sure.


stick to coding, dude. leave the copy editing to people who know how shit is spelled.

Re: My Kind of copyFile

2007-08-03 10:57 • by JBQ (unregistered)
Write once, look stupid everywhere.

Re: My Kind of copyFile

2007-08-03 10:57 • by Mainc Mailman (unregistered)
148078 in reply to 148074
tobjar:
"Brillant!" is the word you're searching for, I'm sure.


stick to coding, dude. leave the copy editing to people who know how shit is spelled.


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)
148079 in reply to 148058
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.

Re: My Kind of copyFile

2007-08-03 10:59 • by Ant (unregistered)
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)
148081 in reply to 148074
tobjar:
"Brillant!" is the word you're searching for, I'm sure.
stick to coding, dude. leave the copy editing to people who know how shit is spelled.
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)
148082 in reply to 148078
Mainc Mailman:
tobjar:
"Brillant!" is the word you're searching for, I'm sure.


stick to coding, dude. leave the copy editing to people who know how shit is spelled.


You indeed correctly spelled 'shit', but "Someone You Know" also correctly spelled 'Brillant!'.

Look it up, dude.

Good one!
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment