Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 107311 - IStructuredModel#save(IFile) for SaveAs causes a ghost file
Summary: IStructuredModel#save(IFile) for SaveAs causes a ghost file
Status: RESOLVED FIXED
Alias: None
Product: WTP Source Editing
Classification: WebTools
Component: wst.sse (show other bugs)
Version: 0.7   Edit
Hardware: PC Windows XP
: P2 major (vote)
Target Milestone: 2.0 RC1   Edit
Assignee: Nitin Dahyabhai CLA
QA Contact:
URL:
Whiteboard: PMC_approved
Keywords:
Depends on: 118199
Blocks:
  Show dependency tree
 
Reported: 2005-08-18 05:30 EDT by Hirotaka Matsumoto CLA
Modified: 2007-05-24 00:24 EDT (History)
9 users (show)

See Also:
david_williams: pmc_approved+
thatnitind: pmc_approved? (naci.dai)
thatnitind: pmc_approved? (deboer)
thatnitind: pmc_approved? (neil.hauge)
raghunathan.srinivasan: pmc_approved+
for.work.things: review+


Attachments
adds usages of org.eclipse.core.filebuffers.LocationKind to calls to text file buffer manager (3.28 KB, patch)
2007-05-22 18:05 EDT, Nitin Dahyabhai CLA
no flags Details | Diff
updated patch (4.14 KB, patch)
2007-05-23 14:16 EDT, Nitin Dahyabhai CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Hirotaka Matsumoto CLA 2005-08-18 05:30:51 EDT
When the document is Save-As'ed by IStructuredModel#save(IFile),
this file seems to be saved by java.io.File so that the saved
file doesn't appear in the naviagator view. This seems to be
because even if IFile points to the inside of the workspace,
ModelManager uses JavaTextFileBufffer, not a ResourceTextFileBuffer,
and because JavaTextFileBuffer uses java.io.File, not 
org.eclipse.core.internal.resources.File, this problem seems to 
happen.
Comment 1 David Williams CLA 2005-08-19 13:15:31 EDT
I believe that "save" will go away, and we definitly should not be use Java
file.io! 

But, will leave open until we document the right way (or API) to save a model. 

Thanks for reminding me this is critical for you. 
Comment 2 David Williams CLA 2005-09-21 20:37:32 EDT
Changing priority so won't be confused with "P1" meaning "do not declare
milestone". They are just as important and high priority as they were. 
Comment 3 David Williams CLA 2005-09-22 13:32:01 EDT
Will give focus for M9. 
Comment 4 David Williams CLA 2005-11-20 23:48:04 EST
I'm changing milestone to acknowledge I did not get this done for M9, but do still believe its important, and will attempt to find a safe fix to put in before R1.0. 

Thanks very much. 
Comment 5 Hirotaka Matsumoto CLA 2005-11-28 03:14:51 EST
I've opened up TextFileBufferManager#createFileBuffer issue. Please see
https://bugs.eclipse.org/bugs/show_bug.cgi?id=118199
Comment 6 David Williams CLA 2005-12-01 01:45:36 EST
By WTP convention, I'm marking this as P4, indicating we will not fix for Release 1.0, but we do agree its very important. 

Matsumoto-san has indicated to me that they have found a ("hacky") work around for immediate future (basically, I think calling refresh after a save). 

This will be much easier for us to fix if/when bug 118199 is fixed, but even if it is not, we can greatly improve/fix the situration next release. 

Thanks all. 
Comment 7 David Williams CLA 2005-12-01 14:25:23 EST
As discussed on status call, we'll plan on this to be improved for 1.5, and retarget accordingly. 
Comment 8 Hirotaka Matsumoto CLA 2005-12-05 23:58:54 EST
By the following bugzilla :

 https://bugs.eclipse.org/bugs/show_bug.cgi?id=107316
 https://bugs.eclipse.org/bugs/show_bug.cgi?id=107320

the 'full path' like /workspace/WebContent/test.html is always
used as the 'location' for TextBuffers including JavaTextFileBuffer.
The side effect for these changes is that JavaTextFileBuffer still
assumes that the location is the absolute path, like C:/workspace/
WebContent/test.html so that it can create java.io.File class to
access the file. But SSE sets full path, not the absolute path,
for JavaTextFileBuffer, it can't access the right file.

So we will enable another workaround code ( to use
TextFileDocumentProvider to save the document as TextEditor does.
This way is a 'magic' ), but the real solution is not to use
JavaTextFileBuffer to read/write the model from the workspace.


.


Comment 9 David Williams CLA 2005-12-07 01:35:01 EST
no change in importance, just trying to "line up" with right WTP conventions on srubbing bugs. Since I'm not * positive * this will be in first 1.5 milestone, will leave blank for now. 

Comment 10 David Williams CLA 2005-12-08 09:10:10 EST
no change in importance, just trying to "line up" with right WTP conventions on
srubbing bugs.
Comment 11 Arthur Ryman CLA 2006-01-05 14:22:54 EST
David, please followup and determine if this is still required for 1.0.1. Thx.
Comment 12 David Williams CLA 2006-01-09 00:05:55 EST
As described in the history and related bugs, this problem is suitable for 1.5, not 1.0.1, and I simply forgot to re-assign the target. 





Comment 13 David Williams CLA 2006-01-09 00:22:57 EST
Also, Matsumoto-san, since you've found a work around, and 
"Critical" means "rashes, loss of data, severe memory leak", 
I think this might better be categorized as 'major'. If you 
disagree, please explain here and we'll assign it back. 

Comment 14 Jeffrey Liu CLA 2006-03-10 11:16:02 EST
03/09 status meeting - No progress this week.
Comment 15 Jeffrey Liu CLA 2006-04-21 17:49:48 EDT
Per the 04/20 status call, we are removing this from the hot list. Scott, if you feel this is still a hot bug for you, please explain why this is an hot issue. Thanks.
Comment 16 David Williams CLA 2006-06-01 12:36:18 EDT
since you've worked around this, I'd prefer not to bother this relase, but will look at next release when we made a formatl API. 

Sound acceptable? 

Comment 17 Mari Kuroki CLA 2006-06-05 13:14:18 EDT
As Matsumoto-san mentioned before, we have the workaround code, however, it can not completely avoid this problem. Our workaround code works fine if a file does not exist. But once the file is saved, after that, JavaTextFileBuffer is used to save it until the model is released. So the file will be in the out-of-sync state. And we have not found the workaround about this issue yet.
Comment 18 David Williams CLA 2006-06-05 13:20:23 EDT
Thanks for the info. I'll still (try) and take a look for this release, to 
see if a safe fix is possible. 
Comment 19 David Williams CLA 2006-08-17 14:39:00 EDT
This used to be a hot bug, but was deferred due to time. 
I'm putting back on the hotbug list, and we will investigate. 
(but, still not positive it can be fixed, but, we will try). 

Comment 20 David Williams CLA 2006-08-27 06:36:19 EDT
I believe one way to look at this bug is that there is nothing in SSE for us to do. 
We are simply waiting for bug 118199 to be fixed? 

Or, did you have another fix in mind for us? I ask because comment #17 mentions JavaTextFileBuffer but we do not use that class in SSE. Am I missing the point? 



Comment 21 David Williams CLA 2006-08-29 01:50:19 EDT
I'm marking as dependent on 118119, since I don't think there's much we in SSE can do about this, except to provide our own implementation of a TextFileBufferManager which we would not want to do in a maintenance release, even if we wanted to have our own implemenation (which, we'd prefer not to). 


Also, I apologize if this should be obvious from your description, but, how can we reproduce this in WTP by itself? We dont' seem to see a "ghost file" after creating a new file. 

Thanks. 
Comment 22 Susumu Fukuda CLA 2006-08-30 12:14:19 EDT
Here are the updates. I couldn't reproduce this on SaveAs scenario within WTP.
Now we can see the problem on using SSE API. I thought that following sequence reproduced the
problem where 'file' is unexisting IFile:

 IStructuredModel model = StructuredModelManager.getModelManager().getNewModelForEdit(file, false);
 model.getStructuredDocument().set("content");
 model.save();

But there is another problem (I think it is already reported, but I cannot find it now..) that this model#save()
creates a file to wrong location (when the 'file' is yet to exist). And it prevents us from reproducing
the 'ghost'.

So the following is the alternative snippet to reproduce the 'out-of-synch' situation.
On running the code, the IResourceChangeListener#resourceChanged() should be called and ".resourceChanged()"
should be printed out. (Please make sure that auto-building is turned off.)

 final IResourceChangeListener rc = new IResourceChangeListener() {
  public void resourceChanged(IResourceChangeEvent event) {
   IResourceDelta d = event.getDelta().findMember(file.getFullPath());
   if (d != null) {
    System.out.println(".resourceChanged()");
   }
  }
 };
 try {
  IStructuredModel model = StructuredModelManager.getModelManager().getNewModelForEdit(file, false);
  model.getStructuredDocument().set("content");
  file.create(new ByteArrayInputStream(new byte[0]), true, new NullProgressMonitor());
     // workaround for the file to be saved in the workspace
  try { Thread.sleep(1000); } catch (Exception e) {}
  Platform.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null);

  ResourcesPlugin.getWorkspace().addResourceChangeListener(rc);
  model.save();
 } catch (Exception e) {
 } finally {
  // ResourcesPlugin.getWorkspace().removeResourceChangeListener(rc);
  // leak this for testing
 }

But it doesn't. When you type 'F5' to refresh resources on Navigator view,
the ".resourceChanged()" comes.

---

Anyway, I cannot see why you use IFile#getFullPath() instead of IFile#getLocation() in 
FileBufferModelManager#getFile(IFile) and in FileBufferModelManager#calcurateId(IFile).
I seem it's the key to this. Could I have some more detailed info?
Comment 23 David Williams CLA 2006-08-30 13:12:49 EDT
I'm re-opening, since some information was supplied, though, I haven't digested it all yet. 

But, to answer the last question, I believe we changed to getFullPath at your request :) thought I can't find that bug at the moment. If we don't use getFullPath, then there's some ambiguity due to bug 118199 and files are saved/created in the "wrong" place, and, if I recall, there's some danger of creating two models for what is essentially the same resource. 

Comment 24 David Williams CLA 2006-08-30 13:28:53 EDT
Ah, I found the related bugs: bug 107316 and bug 107320. 
These were "fixed" by the fullPath change. 
Comment 25 Susumu Fukuda CLA 2006-08-30 22:46:03 EDT
Thank you for the answer, David.
I see why getFullPath() :) and it is a sort of bug 118199.
Comment 26 David Williams CLA 2006-08-31 00:14:27 EDT
Ok, I'll take it off the hotlist to acknowledge we can not fix it in this maintenance release, but, it bugs me too, so will leave open. 

Mabye we need to look closer at defining our own TextFileBufferManager 
(there one other thing is does we don't like ... notifies and gives the documnet to others when the document is created before the requester gets it! and, they also do an "automatic refresh" which can cause an auto-build to start, and, hence is not job safe :( 

As a work around for your case, is your code in a place you could programatically call "refresh" after the save? 

Comment 27 David Williams CLA 2007-04-11 23:35:49 EDT
We should still investigate this for a fix in WTP 2.0, as I think the base platform as improved their API's to make this fix possible on an Eclipse 3.3 base. 

Comment 28 Susumu Fukuda CLA 2007-05-13 23:17:44 EDT
Now some base platform APIs are extended for this purpose and some APIs are marked as deprecated, that are still used in FileBufferModelManager.

I guess this could be handled by fixing the use of deprecated APIs in FileBufferModelManager. Could you please take a look? Thank you,

Comment 29 Nitin Dahyabhai CLA 2007-05-22 18:05:19 EDT
Created attachment 68226 [details]
adds usages of org.eclipse.core.filebuffers.LocationKind to calls to text file buffer manager

Attaching a simple patch to make use of the connect/get methods that take a location kind and to supply them.  The callers of connect/get already know whether they're operating on java.ui.Files or IFiles so the determination is simple, there.  The internal info object has been altered to remember the location kind when those calls are made so it can also be used to correctly disconnect the buffer.
Comment 30 Nitin Dahyabhai CLA 2007-05-22 18:06:28 EDT
CCing Amy for a quick sanity check of the patch
Comment 31 Amy Wu CLA 2007-05-23 03:08:49 EDT
code looks good, some additional suggestions:
-since you're cleaning up deprecated calls, it looks like in detectContentType()
FileBuffers.getSystemFileAtLocation(location);
should be
FileBuffers.getFileStoreAtLocation(location);

-In getModel(IFile), there is a check for info != null before its fields are set.  The same should probably also be done in getModel(File)

-make sure you update the copyright
Comment 32 Nitin Dahyabhai CLA 2007-05-23 14:16:52 EDT
Created attachment 68403 [details]
updated patch

(In reply to comment #31)
Most of this sounds good, but getting rid of the deprecated FileBuffers.getSystemFileAtLocation() call the right way would mean adding a requirement on org.eclipse.core.filesystem and making use of the IFileStore APIs, a change I feel is better left for 3.0.  The other deprecated cleanups are specifically to fix the issue in this bug.
Comment 33 Amy Wu CLA 2007-05-23 15:16:09 EDT
okay, updated patch looks good
Comment 34 Nitin Dahyabhai CLA 2007-05-24 00:24:25 EDT
Patch released for RC1.