PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 8ms 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
  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
 23package org.gjt.sp.jedit.bufferio;
 24
 25//{{{ Imports
 26import java.io.*;
 27import java.util.zip.*;
 28
 29import org.gjt.sp.jedit.io.*;
 30import org.gjt.sp.jedit.*;
 31import org.gjt.sp.util.*;
 32import java.nio.charset.UnsupportedCharsetException;
 33//}}}
 34
 35/**
 36 * A buffer save request.
 37 * @author Slava Pestov
 38 * @version $Id: BufferSaveRequest.java 19409 2011-02-28 14:58:27Z kpouer $
 39 */
 40public class BufferSaveRequest extends BufferIORequest
 41{
 42	//{{{ BufferSaveRequest constructor
 43	/**
 44	 * Creates a new buffer I/O request.
 45	 * @param view The view
 46	 * @param buffer The buffer
 47	 * @param session The VFS session
 48	 * @param vfs The VFS
 49	 * @param path The path
 50	 */
 51	public BufferSaveRequest(View view, Buffer buffer,
 52		Object session, VFS vfs, String path)
 53	{
 54		super(view,buffer,session,vfs,path);
 55	} //}}}
 56
 57	//{{{ run() method
 58	public void run()
 59	{
 60		/* if the VFS supports renaming files, we first
 61		 * save to #<filename>#save#, then rename that
 62		 * to <filename>, so that if the save fails,
 63		 * data will not be lost.
 64		 *
 65		 * as of 4.1pre7 we now call vfs.getTwoStageSaveName()
 66		 * instead of constructing the path directly
 67		 * since some VFS's might not allow # in filenames.
 68		 */
 69
 70		boolean vfsRenameCap = (vfs.getCapabilities() &
 71			VFS.RENAME_CAP) != 0;
 72
 73		boolean wantTwoStage = wantTwoStageSave(buffer);
 74		boolean twoStageSave = vfsRenameCap && wantTwoStage;
 75
 76		try
 77		{
 78			String[] args = { vfs.getFileName(path) };
 79			setStatus(jEdit.getProperty("vfs.status.save",args));
 80
 81			// the entire save operation can be aborted...
 82			setAbortable(true);
 83
 84			path = vfs._canonPath(session,path,view);
 85			if(!MiscUtilities.isURL(path))
 86				path = MiscUtilities.resolveSymlinks(path);
 87
 88			String savePath;
 89			if(twoStageSave)
 90			{
 91				savePath = vfs.getTwoStageSaveName(path);
 92				if (savePath == null)
 93				{
 94					throw new IOException(
 95						"Can't get a temporary path for two-stage save: "
 96						+ path);
 97				}
 98			}
 99			else
100			{
101				makeBackup();
102				savePath = path;
103			}
104
105			OutputStream out = vfs._createOutputStream(session,savePath,view);
106			if(out == null)
107			{
108				buffer.setBooleanProperty(ERROR_OCCURRED,true);
109				return;
110			}
111			try
112			{
113				// this must be after the stream is created or
114				// we deadlock with SSHTools.
115				buffer.readLock();
116				try
117				{
118					// Can't use buffer.getName() here because
119					// it is not changed until the save is
120					// complete
121					if(path.endsWith(".gz"))
122						buffer.setBooleanProperty(Buffer.GZIPPED,true);
123					else if (buffer.getName().endsWith(".gz"))
124					{
125						// The path do not ends with gz.
126						// The buffer name was .gz.
127						// So it means it's blabla.txt.gz -> blabla.txt, I remove
128						// the gz property
129						buffer.setBooleanProperty(Buffer.GZIPPED, false);
130					}
131
132					if(buffer.getBooleanProperty(Buffer.GZIPPED))
133						out = new GZIPOutputStream(out);
134
135					write(buffer,out);
136				}
137				finally
138				{
139					buffer.readUnlock();
140				}
141			}
142			finally
143			{
144				IOUtilities.closeQuietly(out);
145			}
146
147			if(twoStageSave)
148			{
149				makeBackup();
150				if(!vfs._rename(session,savePath,path,view))
151					throw new IOException("Rename failed: " + savePath);
152			}
153
154			if(!twoStageSave)
155				VFSManager.sendVFSUpdate(vfs,path,true);
156		}
157		catch (FileNotFoundException e)
158		{
159			Log.log(Log.ERROR,this,"Unable to save buffer " + e);
160			String[] pp = { e.getMessage() };
161			VFSManager.error(view,path,"ioerror.write-error",pp);
162
163			buffer.setBooleanProperty(ERROR_OCCURRED,true);
164		}
165		catch(UnsupportedCharsetException e)
166		{
167			Log.log(Log.ERROR, this, e, e);
168			String[] pp = { e.getCharsetName() };
169			VFSManager.error(view,path,"ioerror.unsupported-encoding-error",pp);
170
171			buffer.setBooleanProperty(ERROR_OCCURRED,true);
172		}
173		catch(Exception e)
174		{
175			Log.log(Log.ERROR,this,e);
176			String[] pp = { e.toString() };
177			VFSManager.error(view,path,"ioerror.write-error",pp);
178
179			buffer.setBooleanProperty(ERROR_OCCURRED,true);
180		}
181		catch(WorkThread.Abort a)
182		{
183			buffer.setBooleanProperty(ERROR_OCCURRED,true);
184		}
185		finally
186		{
187			try
188			{
189				vfs._saveComplete(session,buffer,path,view);
190				if( twoStageSave )
191				{
192					vfs._finishTwoStageSave(session,buffer,path,view);
193				}
194				// clean up left-over markers file
195				if(!jEdit.getBooleanProperty("persistentMarkers"))
196					vfs._delete(session,Buffer.getMarkersPath(vfs, path),view);
197				vfs._endVFSSession(session,view);
198			}
199			catch(Exception e)
200			{
201				Log.log(Log.ERROR,this,e);
202				String[] pp = { e.toString() };
203				VFSManager.error(view,path,"ioerror.write-error",pp);
204
205				buffer.setBooleanProperty(ERROR_OCCURRED,true);
206			}
207			catch(WorkThread.Abort a)
208			{
209				buffer.setBooleanProperty(ERROR_OCCURRED,true);
210			}
211		}
212	} //}}}
213
214	//{{{ Private members
215
216	//{{{ makeBackup() method
217	/**
218	 * Make the backup.
219	 */
220	private void makeBackup() throws IOException
221	{
222		// Only backup once per session
223		if(buffer.getProperty(Buffer.BACKED_UP) == null
224			|| jEdit.getBooleanProperty("backupEverySave"))
225		{
226			if (jEdit.getIntegerProperty("backups",1) > 0)
227				vfs._backup(session,path,view);
228			buffer.setBooleanProperty(Buffer.BACKED_UP, true);
229		}
230	} //}}}
231
232	//{{{ wantTwoStageSave() method
233	private static boolean wantTwoStageSave(Buffer buffer)
234	{
235		return !buffer.getBooleanProperty("forbidTwoStageSave") &&
236			(buffer.getBooleanProperty("overwriteReadonly") ||
237			jEdit.getBooleanProperty("twoStageSave"));
238	}//}}}
239
240	//}}}
241}