PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bufferio/BufferSaveRequest.java

#
Java | 241 lines | 151 code | 24 blank | 66 comment | 21 complexity | 07b10a52c1f9c319df1685d67d226130 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * BufferSaveRequest.java - I/O request
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2005 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.bufferio;
  23. //{{{ Imports
  24. import java.io.*;
  25. import java.util.zip.*;
  26. import org.gjt.sp.jedit.io.*;
  27. import org.gjt.sp.jedit.*;
  28. import org.gjt.sp.util.*;
  29. import java.nio.charset.UnsupportedCharsetException;
  30. //}}}
  31. /**
  32. * A buffer save request.
  33. * @author Slava Pestov
  34. * @version $Id: BufferSaveRequest.java 19409 2011-02-28 14:58:27Z kpouer $
  35. */
  36. public class BufferSaveRequest extends BufferIORequest
  37. {
  38. //{{{ BufferSaveRequest constructor
  39. /**
  40. * Creates a new buffer I/O request.
  41. * @param view The view
  42. * @param buffer The buffer
  43. * @param session The VFS session
  44. * @param vfs The VFS
  45. * @param path The path
  46. */
  47. public BufferSaveRequest(View view, Buffer buffer,
  48. Object session, VFS vfs, String path)
  49. {
  50. super(view,buffer,session,vfs,path);
  51. } //}}}
  52. //{{{ run() method
  53. public void run()
  54. {
  55. /* if the VFS supports renaming files, we first
  56. * save to #<filename>#save#, then rename that
  57. * to <filename>, so that if the save fails,
  58. * data will not be lost.
  59. *
  60. * as of 4.1pre7 we now call vfs.getTwoStageSaveName()
  61. * instead of constructing the path directly
  62. * since some VFS's might not allow # in filenames.
  63. */
  64. boolean vfsRenameCap = (vfs.getCapabilities() &
  65. VFS.RENAME_CAP) != 0;
  66. boolean wantTwoStage = wantTwoStageSave(buffer);
  67. boolean twoStageSave = vfsRenameCap && wantTwoStage;
  68. try
  69. {
  70. String[] args = { vfs.getFileName(path) };
  71. setStatus(jEdit.getProperty("vfs.status.save",args));
  72. // the entire save operation can be aborted...
  73. setAbortable(true);
  74. path = vfs._canonPath(session,path,view);
  75. if(!MiscUtilities.isURL(path))
  76. path = MiscUtilities.resolveSymlinks(path);
  77. String savePath;
  78. if(twoStageSave)
  79. {
  80. savePath = vfs.getTwoStageSaveName(path);
  81. if (savePath == null)
  82. {
  83. throw new IOException(
  84. "Can't get a temporary path for two-stage save: "
  85. + path);
  86. }
  87. }
  88. else
  89. {
  90. makeBackup();
  91. savePath = path;
  92. }
  93. OutputStream out = vfs._createOutputStream(session,savePath,view);
  94. if(out == null)
  95. {
  96. buffer.setBooleanProperty(ERROR_OCCURRED,true);
  97. return;
  98. }
  99. try
  100. {
  101. // this must be after the stream is created or
  102. // we deadlock with SSHTools.
  103. buffer.readLock();
  104. try
  105. {
  106. // Can't use buffer.getName() here because
  107. // it is not changed until the save is
  108. // complete
  109. if(path.endsWith(".gz"))
  110. buffer.setBooleanProperty(Buffer.GZIPPED,true);
  111. else if (buffer.getName().endsWith(".gz"))
  112. {
  113. // The path do not ends with gz.
  114. // The buffer name was .gz.
  115. // So it means it's blabla.txt.gz -> blabla.txt, I remove
  116. // the gz property
  117. buffer.setBooleanProperty(Buffer.GZIPPED, false);
  118. }
  119. if(buffer.getBooleanProperty(Buffer.GZIPPED))
  120. out = new GZIPOutputStream(out);
  121. write(buffer,out);
  122. }
  123. finally
  124. {
  125. buffer.readUnlock();
  126. }
  127. }
  128. finally
  129. {
  130. IOUtilities.closeQuietly(out);
  131. }
  132. if(twoStageSave)
  133. {
  134. makeBackup();
  135. if(!vfs._rename(session,savePath,path,view))
  136. throw new IOException("Rename failed: " + savePath);
  137. }
  138. if(!twoStageSave)
  139. VFSManager.sendVFSUpdate(vfs,path,true);
  140. }
  141. catch (FileNotFoundException e)
  142. {
  143. Log.log(Log.ERROR,this,"Unable to save buffer " + e);
  144. String[] pp = { e.getMessage() };
  145. VFSManager.error(view,path,"ioerror.write-error",pp);
  146. buffer.setBooleanProperty(ERROR_OCCURRED,true);
  147. }
  148. catch(UnsupportedCharsetException e)
  149. {
  150. Log.log(Log.ERROR, this, e, e);
  151. String[] pp = { e.getCharsetName() };
  152. VFSManager.error(view,path,"ioerror.unsupported-encoding-error",pp);
  153. buffer.setBooleanProperty(ERROR_OCCURRED,true);
  154. }
  155. catch(Exception e)
  156. {
  157. Log.log(Log.ERROR,this,e);
  158. String[] pp = { e.toString() };
  159. VFSManager.error(view,path,"ioerror.write-error",pp);
  160. buffer.setBooleanProperty(ERROR_OCCURRED,true);
  161. }
  162. catch(WorkThread.Abort a)
  163. {
  164. buffer.setBooleanProperty(ERROR_OCCURRED,true);
  165. }
  166. finally
  167. {
  168. try
  169. {
  170. vfs._saveComplete(session,buffer,path,view);
  171. if( twoStageSave )
  172. {
  173. vfs._finishTwoStageSave(session,buffer,path,view);
  174. }
  175. // clean up left-over markers file
  176. if(!jEdit.getBooleanProperty("persistentMarkers"))
  177. vfs._delete(session,Buffer.getMarkersPath(vfs, path),view);
  178. vfs._endVFSSession(session,view);
  179. }
  180. catch(Exception e)
  181. {
  182. Log.log(Log.ERROR,this,e);
  183. String[] pp = { e.toString() };
  184. VFSManager.error(view,path,"ioerror.write-error",pp);
  185. buffer.setBooleanProperty(ERROR_OCCURRED,true);
  186. }
  187. catch(WorkThread.Abort a)
  188. {
  189. buffer.setBooleanProperty(ERROR_OCCURRED,true);
  190. }
  191. }
  192. } //}}}
  193. //{{{ Private members
  194. //{{{ makeBackup() method
  195. /**
  196. * Make the backup.
  197. */
  198. private void makeBackup() throws IOException
  199. {
  200. // Only backup once per session
  201. if(buffer.getProperty(Buffer.BACKED_UP) == null
  202. || jEdit.getBooleanProperty("backupEverySave"))
  203. {
  204. if (jEdit.getIntegerProperty("backups",1) > 0)
  205. vfs._backup(session,path,view);
  206. buffer.setBooleanProperty(Buffer.BACKED_UP, true);
  207. }
  208. } //}}}
  209. //{{{ wantTwoStageSave() method
  210. private static boolean wantTwoStageSave(Buffer buffer)
  211. {
  212. return !buffer.getBooleanProperty("forbidTwoStageSave") &&
  213. (buffer.getBooleanProperty("overwriteReadonly") ||
  214. jEdit.getBooleanProperty("twoStageSave"));
  215. }//}}}
  216. //}}}
  217. }