1 24 25 package org.gjt.sp.jedit.io; 26 27 import javax.swing.filechooser.FileSystemView ; 29 import javax.swing.*; 30 import java.awt.Component ; 31 import java.io.*; 32 import java.text.*; 33 import java.util.Date ; 34 import org.gjt.sp.jedit.*; 35 import org.gjt.sp.util.Log; 36 38 43 public class FileVFS extends VFS 44 { 45 public static final String PERMISSIONS_PROPERTY = "FileVFS__perms"; 46 47 public FileVFS() 49 { 50 super("file",READ_CAP | WRITE_CAP | DELETE_CAP 51 | RENAME_CAP | MKDIR_CAP | LOW_LATENCY_CAP 52 | ((OperatingSystem.isCaseInsensitiveFS()) 53 ? CASE_INSENSITIVE_CAP : 0), 54 new String [] { EA_TYPE, EA_SIZE, EA_STATUS, 55 EA_MODIFIED }); 56 } 58 public String getParentOfPath(String path) 60 { 61 if(OperatingSystem.isDOSDerived()) 62 { 63 if(path.length() == 2 && path.charAt(1) == ':') 64 return FileRootsVFS.PROTOCOL + ':'; 65 else if(path.length() == 3 && path.endsWith(":\\")) 66 return FileRootsVFS.PROTOCOL + ':'; 67 else if(path.startsWith("\\\\") && path.indexOf('\\',2) == -1) 68 return path; 69 } 70 71 return super.getParentOfPath(path); 72 } 74 public String constructPath(String parent, String path) 76 { 77 if(parent.endsWith(File.separator) 78 || parent.endsWith("/")) 79 return parent + path; 80 else 81 return parent + File.separator + path; 82 } 84 public char getFileSeparator() 86 { 87 return File.separatorChar; 88 } 90 100 public String getTwoStageSaveName(String path) 101 { 102 File parent = new File(getParentOfPath(path)); 103 return (parent.canWrite()) 104 ? super.getTwoStageSaveName(path) 105 : null; 106 } 108 public boolean save(View view, Buffer buffer, String path) 110 { 111 if(OperatingSystem.isUnix()) 112 { 113 int permissions = getPermissions(buffer.getPath()); 114 Log.log(Log.DEBUG,this,buffer.getPath() + " has permissions 0" 115 + Integer.toString(permissions,8)); 116 buffer.setIntegerProperty(PERMISSIONS_PROPERTY,permissions); 117 } 118 119 return super.save(view,buffer,path); 120 } 122 public boolean insert(View view, Buffer buffer, String path) 124 { 125 File file = new File(path); 126 127 if(!file.exists()) 129 return false; 130 131 if(file.isDirectory()) 132 { 133 VFSManager.error(view,file.getPath(), 134 "ioerror.open-directory",null); 135 return false; 136 } 137 138 if(!file.canRead()) 139 { 140 VFSManager.error(view,file.getPath(), 141 "ioerror.no-read",null); 142 return false; 143 } 145 return super.insert(view,buffer,path); 146 } 148 154 public boolean recursiveDelete(File path) { 155 if( path.exists() ) { 156 File[] files = path.listFiles(); 157 for(int i=0; i<files.length; i++) { 158 if(files[i].isDirectory()) { 159 recursiveDelete(files[i]); 160 } 161 else { 162 files[i].delete(); 163 } 164 } 165 } 166 return path.delete(); 167 } 169 179 public String _canonPath(Object session, String path, Component comp) 180 throws IOException 181 { 182 return MiscUtilities.canonPath(path); 183 } 185 public static class LocalFile extends VFSFile 187 { 188 private File file; 189 190 public static DateFormat DATE_FORMAT 192 = DateFormat.getInstance(); 193 194 197 public long modified; 198 199 public LocalFile(File file) 200 { 201 this.file = file; 202 203 205 setName(file.getName()); 206 String path = file.getPath(); 207 setPath(path); 208 setDeletePath(path); 209 setHidden(file.isHidden()); 210 setType(file.isDirectory() 211 ? VFSFile.DIRECTORY 212 : VFSFile.FILE); 213 } 214 215 public String getExtendedAttribute(String name) 216 { 217 if(name.equals(EA_MODIFIED)) 218 return DATE_FORMAT.format(new Date (modified)); 219 else 220 return super.getExtendedAttribute(name); 221 } 222 223 protected void fetchAttrs() 224 { 225 if(fetchedAttrs()) 226 return; 227 228 super.fetchAttrs(); 229 230 setSymlinkPath(MiscUtilities.resolveSymlinks( 231 file.getPath())); 232 setReadable(file.canRead()); 233 setWriteable(file.canWrite()); 234 setLength(file.length()); 235 setModified(file.lastModified()); 236 } 237 238 246 public Icon getIcon(boolean expanded, boolean openBuffer) 247 { 248 if (icon == null) 249 { 250 if (fsView == null) 251 fsView = FileSystemView.getFileSystemView(); 252 253 icon = fsView.getSystemIcon(file); 254 } 255 return icon; 256 } 257 258 public String getSymlinkPath() 259 { 260 fetchAttrs(); 261 return super.getSymlinkPath(); 262 } 263 264 public long getLength() 265 { 266 fetchAttrs(); 267 return super.getLength(); 268 } 269 270 public boolean isReadable() 271 { 272 fetchAttrs(); 273 return super.isReadable(); 274 } 275 276 public boolean isWriteable() 277 { 278 fetchAttrs(); 279 return super.isWriteable(); 280 } 281 282 public long getModified() 283 { 284 fetchAttrs(); 285 return modified; 286 } 287 288 public void setModified(long modified) 289 { 290 this.modified = modified; 291 } 292 293 private transient FileSystemView fsView; 294 private transient Icon icon; 295 } 297 public VFSFile[] _listFiles(Object session, String path, 299 Component comp) 300 { 301 310 if(OperatingSystem.isWindows()) 311 { 312 if(path.length() == 2 && path.charAt(1) == ':') 313 path = path.concat(File.separator); 314 } 316 File directory = new File(path); 317 File[] list = null; 318 if(directory.exists()) 319 list = fsView.getFiles(directory,false); 320 321 if(list == null) 322 { 323 VFSManager.error(comp,path,"ioerror.directory-error-nomsg",null); 324 return null; 325 } 326 327 VFSFile[] list2 = new VFSFile[list.length]; 328 for(int i = 0; i < list.length; i++) 329 list2[i] = new LocalFile(list[i]); 330 331 return list2; 332 } 334 public VFSFile _getFile(Object session, String path, 336 Component comp) 337 { 338 if(path.equals("/") && OperatingSystem.isUnix()) 339 { 340 return new VFS.DirectoryEntry(path,path,path, 341 VFSFile.DIRECTORY,0L,false); 342 } 343 344 File file = new File(path); 345 if(!file.exists()) 346 return null; 347 348 return new LocalFile(file); 349 } 351 public boolean _delete(Object session, String path, Component comp) 353 { 354 File file = new File(path); 355 String canonPath; 358 try 359 { 360 canonPath = file.getCanonicalPath(); 361 } 362 catch(IOException io) 363 { 364 canonPath = path; 365 } 366 boolean retVal; 368 if (!file.isDirectory()) { 369 retVal = file.delete(); 370 } 371 else 372 { 373 retVal = recursiveDelete(file); 374 } 375 if(retVal) 376 VFSManager.sendVFSUpdate(this,canonPath,true); 377 return retVal; 378 } 380 public boolean _rename(Object session, String from, String to, 382 Component comp) 383 { 384 File _to = new File(to); 385 386 String toCanonPath; 387 try 388 { 389 toCanonPath = _to.getCanonicalPath(); 390 } 391 catch(IOException io) 392 { 393 toCanonPath = to; 394 } 395 396 File parent = new File(_to.getParent()); 399 if(parent.exists()) 400 { 401 if(!parent.isDirectory()) 402 return false; 403 } 404 else 405 { 406 parent.mkdirs(); 407 if(!parent.exists()) 408 return false; 409 } 410 411 File _from = new File(from); 412 413 String fromCanonPath; 414 try 415 { 416 fromCanonPath = _from.getCanonicalPath(); 417 } 418 catch(IOException io) 419 { 420 fromCanonPath = from; 421 } 422 423 if(!fromCanonPath.equalsIgnoreCase(toCanonPath)) 425 _to.delete(); 426 427 boolean retVal = _from.renameTo(_to); 428 VFSManager.sendVFSUpdate(this,fromCanonPath,true); 429 VFSManager.sendVFSUpdate(this,toCanonPath,true); 430 return retVal; 431 } 433 public boolean _mkdir(Object session, String directory, Component comp) 435 { 436 String parent = getParentOfPath(directory); 437 if(!new File(parent).exists()) 438 { 439 if(!_mkdir(session,parent,comp)) 440 return false; 441 } 442 443 File file = new File(directory); 444 445 boolean retVal = file.mkdir(); 446 String canonPath; 447 try 448 { 449 canonPath = file.getCanonicalPath(); 450 } 451 catch(IOException io) 452 { 453 canonPath = directory; 454 } 455 VFSManager.sendVFSUpdate(this,canonPath,true); 456 return retVal; 457 } 459 public void _backup(Object session, String path, Component comp) 461 throws IOException 462 { 463 int backups = jEdit.getIntegerProperty("backups",1); 465 466 if(backups == 0) 467 return; 468 469 String backupPrefix = jEdit.getProperty("backup.prefix"); 470 String backupSuffix = jEdit.getProperty("backup.suffix"); 471 472 String backupDirectory = jEdit.getProperty("backup.directory"); 473 474 int backupTimeDistance = jEdit.getIntegerProperty("backup.minTime",0); 475 File file = new File(path); 476 477 if (!file.exists()) 478 return; 479 480 if(backupDirectory == null || backupDirectory.length() == 0) 483 backupDirectory = file.getParent(); 484 else 485 { 486 backupDirectory = MiscUtilities.constructPath( 487 System.getProperty("user.home"),backupDirectory); 488 489 backupDirectory = MiscUtilities.concatPath( 492 backupDirectory,file.getParent()); 493 494 File dir = new File(backupDirectory); 495 496 if (!dir.exists()) 497 dir.mkdirs(); 498 } 499 500 MiscUtilities.saveBackup(file,backups,backupPrefix, 501 backupSuffix,backupDirectory,backupTimeDistance); 502 } 504 public InputStream _createInputStream(Object session, String path, 506 boolean ignoreErrors, Component comp) throws IOException 507 { 508 try 509 { 510 return new FileInputStream(path); 511 } 512 catch(IOException io) 513 { 514 if(ignoreErrors) 515 return null; 516 else 517 throw io; 518 } 519 } 521 public OutputStream _createOutputStream(Object session, String path, 523 Component comp) throws IOException 524 { 525 return new FileOutputStream(path); 526 } 528 public void _saveComplete(Object session, Buffer buffer, String path, 530 Component comp) 531 { 532 int permissions = buffer.getIntegerProperty(PERMISSIONS_PROPERTY,0); 533 setPermissions(path,permissions); 534 } 536 538 539 540 541 547 public static int getPermissions(String path) 548 { 549 int permissions = 0; 550 551 if(jEdit.getBooleanProperty("chmodDisabled")) 552 return permissions; 553 554 if(OperatingSystem.isUnix()) 555 { 556 String [] cmdarray = { "ls", "-ld", path }; 557 558 try 559 { 560 Process process = Runtime.getRuntime().exec(cmdarray); 561 562 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 563 564 String output = reader.readLine(); 565 566 if(output != null) 567 { 568 String s = output.substring(1, 10); 569 570 permissions = MiscUtilities 571 .parsePermissions(s); 572 } 573 } 574 575 catch (Throwable t) 579 { 580 } 581 } 582 583 return permissions; 584 } 586 592 public static void setPermissions(String path, int permissions) 593 { 594 if(jEdit.getBooleanProperty("chmodDisabled")) 595 return; 596 597 if(permissions != 0) 598 { 599 if(OperatingSystem.isUnix()) 600 { 601 String [] cmdarray = { "chmod", Integer.toString(permissions, 8), path }; 602 603 try 604 { 605 Process process = Runtime.getRuntime().exec(cmdarray); 606 process.getInputStream().close(); 607 process.getOutputStream().close(); 608 process.getErrorStream().close(); 609 615 } 616 617 catch (Throwable t) 621 { 622 } 623 } 624 } 625 } 627 629 private static FileSystemView fsView = FileSystemView.getFileSystemView(); 631 } 633 | Popular Tags |