Bug 9351 - Copying a compilation unit onto itself destroys compilation unit
Summary: Copying a compilation unit onto itself destroys compilation unit
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows 2000
: P1 major (vote)
Target Milestone: 2.0 M3   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 8873
  Show dependency tree
 
Reported: 2002-02-09 10:50 EST by Kai-Uwe Maetzel CLA
Modified: 2002-02-19 07:50 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kai-Uwe Maetzel CLA 2002-02-09 10:50:36 EST
If you have a working copy of a compilation unit and you call
unit.copy(fragment, null, fileName, true, monitor);
whereas fragment is the working copy's package and fileName is the resource 
name of the working copy's original, the copy fails and destroys the original 
causing data loss. See bug #8873 for a scenario.

The operation should either detect this and abort the operation or handle it 
properly but not destroy data.

Suspect offending code in 
CopyResourceElementsOperation.processCompilationUnitResource:

...
if (destFile.exists()) {
  if (fForce) {
    // we can remove it
    deleteResource(destFile, false);
  } else {
    // abort
    throw new JavaModelException(new JavaModelStatus(...));
  }
}
if (this.isMove()) {
  sourceResource.move(destFile.getFullPath(), fForce, true, 
getSubProgressMonitor(1));
} else {
  sourceResource.copy(destFile.getFullPath(), fForce, getSubProgressMonitor(1));
}
Comment 1 Philipe Mulet CLA 2002-02-11 09:35:24 EST
Problem identified. When resolving single names, we did set the depth 
incorrectly (during each iteration of the loop through enclosing scopes) 
instead of when returning the found file only.

Fixed, regression test added (will go into next integration build).
Comment 2 Philipe Mulet CLA 2002-02-11 09:36:17 EST
Oops, comment went to wrong bug report, ignore my previous post.
Comment 3 Olivier Thomann CLA 2002-02-11 12:59:42 EST
You localized the error. But what do we want to do?
Failed with a NAME_COLISION. What I don't like 
is that the UI is checking if the file already exists and the JavaModelOperation does the same 
thing. If the user said that he wants the file to be overriden, we should save the new contents.
We 
can silently handle this case by saving the new contents.
Then I don't understand what this code 
is doing in CompilationUnitEditor.performSaveAs:

/* 
 * 1GF5YOX: ITPJUI:ALL - Save of 
delete file claims it's still there
 * Changed false to true.
 
*/
getDocumentProvider().saveDocument(monitor, newInput, 
getDocumentProvider().getDocument(getEditorInput()), true);

I think this is the 
responsability of the java model operation to update the contents of the file if necessary. Any 
comment?
Comment 4 Olivier Thomann CLA 2002-02-11 17:08:45 EST
I changed the code to simply set the contents of the existing file to the new contents in case you do a 
saveas on the same file. There is no mode added deltas propagated after this operation. If you are 
not in a mode you want to override the existing file, I throw a NAME_COLISION error. Is this 
behavior acceptable for you?
If yes, I will release it for the next build (tomorrow's build if 
you reply quickly).
Comment 5 Olivier Thomann CLA 2002-02-15 10:10:04 EST
Fixed and released in HEAD. Hopefully this will be the behavior you expected.