@@ -663,9 +663,6 @@ public boolean rename(Path src, Path dst) throws IOException {
663663 return innerRename (src , dst );
664664 } catch (AmazonClientException e ) {
665665 throw translateException ("rename(" + src +", " + dst + ")" , src , e );
666- } catch (FileNotFoundException e ) {
667- LOG .error ("rename: src not found {}" , src );
668- return false ;
669666 } catch (RenameFailedException e ) {
670667 LOG .debug (e .getMessage ());
671668 return e .getExitCode ();
@@ -701,6 +698,8 @@ private boolean innerRename(Path src, Path dst) throws IOException,
701698 throw new RenameFailedException (src , dst , "dest is root directory" );
702699 }
703700
701+ // get the source file status; this raises a FNFE if there is no source
702+ // file.
704703 S3AFileStatus srcStatus = getFileStatus (src );
705704
706705 if (srcKey .equals (dstKey )) {
@@ -714,15 +713,31 @@ private boolean innerRename(Path src, Path dst) throws IOException,
714713 S3AFileStatus dstStatus = null ;
715714 try {
716715 dstStatus = getFileStatus (dst );
717-
718- if (srcStatus .isDirectory () && dstStatus .isFile ()) {
719- throw new RenameFailedException (src , dst ,
720- "source is a directory and dest is a file" )
721- .withExitCode (srcStatus .isFile ());
722- }
723- if (dstStatus .isDirectory () && !dstStatus .isEmptyDirectory ()) {
724- return false ;
716+ // if there is no destination entry, an exception is raised.
717+ // hence this code sequence can assume that there is something
718+ // at the end of the path; the only detail being what it is and
719+ // whether or not it can be the destination of the rename.
720+ if (srcStatus .isDirectory ()) {
721+ if (dstStatus .isFile ()) {
722+ throw new RenameFailedException (src , dst ,
723+ "source is a directory and dest is a file" )
724+ .withExitCode (srcStatus .isFile ());
725+ } else if (!dstStatus .isEmptyDirectory ()) {
726+ throw new RenameFailedException (src , dst ,
727+ "Destination is a non-empty directory" )
728+ .withExitCode (false );
729+ }
730+ // at this point the destination is an empty directory
731+ } else {
732+ // source is a file. Look at the destination
733+ if (dstStatus .isFile ()) {
734+ throw new RenameFailedException (src , dst ,
735+ "destination file for rename operations already exists" )
736+ .withExitCode (false );
737+ }
738+ //
725739 }
740+
726741 } catch (FileNotFoundException e ) {
727742 LOG .debug ("rename: destination path {} not found" , dst );
728743 // Parent must exist
@@ -880,6 +895,7 @@ public CopyResult call() throws Exception {
880895 throw new IOException (ex );
881896 }
882897 } else {
898+ //no cause
883899 throw new IOException (e );
884900 }
885901 }
0 commit comments