1 22 23 package org.gjt.sp.jedit.bufferio; 24 25 import java.io.*; 27 import java.util.zip.*; 28 29 import org.gjt.sp.jedit.io.*; 30 import org.gjt.sp.jedit.*; 31 import org.gjt.sp.util.*; 32 34 39 public class BufferSaveRequest extends BufferIORequest 40 { 41 50 public BufferSaveRequest(View view, Buffer buffer, 51 Object session, VFS vfs, String path) 52 { 53 super(view,buffer,session,vfs,path); 54 } 56 public void run() 58 { 59 68 69 boolean vfsRenameCap = (vfs.getCapabilities() & 70 VFS.RENAME_CAP) != 0; 71 72 boolean wantTwoStage = wantTwoStageSave(buffer); 73 boolean twoStageSave = vfsRenameCap && wantTwoStage; 74 75 try 76 { 77 String [] args = { vfs.getFileName(path) }; 78 setStatus(jEdit.getProperty("vfs.status.save",args)); 79 80 setAbortable(true); 82 83 path = vfs._canonPath(session,path,view); 84 if(!MiscUtilities.isURL(path)) 85 path = MiscUtilities.resolveSymlinks(path); 86 87 String savePath; 88 if(twoStageSave) 89 { 90 savePath = vfs.getTwoStageSaveName(path); 91 if (savePath == null) 92 { 93 throw new IOException( 94 "Can't get a temporary path for two-stage save: " 95 + path); 96 } 97 } 98 else 99 { 100 makeBackup(); 101 savePath = path; 102 } 103 104 OutputStream out = vfs._createOutputStream(session,savePath,view); 105 if(out == null) 106 { 107 buffer.setBooleanProperty(ERROR_OCCURRED,true); 108 return; 109 } 110 try 111 { 112 buffer.readLock(); 115 try 116 { 117 if(path.endsWith(".gz")) 121 buffer.setBooleanProperty(Buffer.GZIPPED,true); 122 else if (buffer.getName().endsWith(".gz")) 123 { 124 buffer.setBooleanProperty(Buffer.GZIPPED, false); 129 } 130 131 if(buffer.getBooleanProperty(Buffer.GZIPPED)) 132 out = new GZIPOutputStream(out); 133 134 write(buffer,out); 135 136 if(twoStageSave) 137 { 138 makeBackup(); 139 if(!vfs._rename(session,savePath,path,view)) 140 throw new IOException("Rename failed: " + savePath); 141 } 142 143 if(!twoStageSave) 144 VFSManager.sendVFSUpdate(vfs,path,true); 145 } 146 finally 147 { 148 buffer.readUnlock(); 149 } 150 } 151 finally 152 { 153 IOUtilities.closeQuietly(out); 154 } 155 } 156 catch(IOException io) 157 { 158 Log.log(Log.ERROR,this,io); 159 String [] pp = { io.toString() }; 160 VFSManager.error(view,path,"ioerror.write-error",pp); 161 162 buffer.setBooleanProperty(ERROR_OCCURRED,true); 163 } 164 catch(WorkThread.Abort a) 165 { 166 buffer.setBooleanProperty(ERROR_OCCURRED,true); 167 } 168 finally 169 { 170 try 171 { 172 vfs._saveComplete(session,buffer,path,view); 173 if( twoStageSave ) 174 { 175 vfs._finishTwoStageSave(session,buffer,path,view); 176 } 177 if(!jEdit.getBooleanProperty("persistentMarkers")) 179 vfs._delete(session,buffer.getMarkersPath(vfs),view); 180 vfs._endVFSSession(session,view); 181 } 182 catch(IOException io) 183 { 184 Log.log(Log.ERROR,this,io); 185 String [] pp = { io.toString() }; 186 VFSManager.error(view,path,"ioerror.write-error",pp); 187 188 buffer.setBooleanProperty(ERROR_OCCURRED,true); 189 } 190 catch(WorkThread.Abort a) 191 { 192 buffer.setBooleanProperty(ERROR_OCCURRED,true); 193 } 194 } 195 } 197 201 private void makeBackup() throws IOException 202 { 203 if(buffer.getProperty(Buffer.BACKED_UP) == null 205 || jEdit.getBooleanProperty("backupEverySave")) 206 { 207 vfs._backup(session,path,view); 208 buffer.setBooleanProperty(Buffer.BACKED_UP,true); 209 } 210 } 212 public static boolean wantTwoStageSave(Buffer buffer) 214 { 215 return !buffer.getBooleanProperty("forbidTwoStageSave") && 216 (buffer.getBooleanProperty("overwriteReadonly") || 217 jEdit.getBooleanProperty("twoStageSave")); 218 }} 220 | Popular Tags |