1 23 24 package com.sun.enterprise.config.backup.util; 25 26 import java.io.*; 27 import java.util.*; 28 29 public class FileUtils 30 { 31 private FileUtils() 32 { 33 } 34 35 37 public static boolean safeIsDirectory(File f) 38 { 39 if(f == null || !f.exists() || !f.isDirectory()) 40 return false; 41 42 return true; 43 } 44 45 47 public static boolean safeIsDirectory(String s) 48 { 49 return safeIsDirectory(new File(s)); 50 } 51 52 59 61 74 79 86 87 89 public static String safeGetCanonicalPath(File f) 90 { 91 if(f == null) 92 return null; 93 94 try 95 { 96 return f.getCanonicalPath(); 97 } 98 catch(IOException e) 99 { 100 return f.getAbsolutePath(); 101 } 102 } 103 104 106 public static File safeGetCanonicalFile(File f) 107 { 108 if(f == null) 109 return null; 110 111 try 112 { 113 return f.getCanonicalFile(); 114 } 115 catch(IOException e) 116 { 117 return f.getAbsoluteFile(); 118 } 119 } 120 121 123 public static boolean isZip(String filename) 124 { 125 return hasExtensionIgnoreCase(filename, ".zip"); 126 } 127 128 130 public static boolean isZip(File f) 131 { 132 return hasExtensionIgnoreCase(f, ".zip"); 133 } 134 135 137 public static void whack(File parent) 138 { 139 if(safeIsDirectory(parent)) 140 { 141 File[] kids = parent.listFiles(); 142 143 for(int i = 0; i < kids.length; i++) 144 { 145 File f = kids[i]; 146 147 if(f.isDirectory()) 148 whack(f); 149 150 if(!f.delete()) 151 { 152 f.deleteOnExit(); 153 } 154 } 155 } 156 157 parent.delete(); 158 } 159 160 162 163 public static boolean protect(File f) 164 { 165 if(!f.exists()) 166 return true; 167 168 if(OS.isUNIX()) 169 return protectUNIX(f); 170 else 171 return protectWindows(f); 172 } 173 174 176 177 public static boolean makeExecutable(File f) 178 { 179 if(!OS.isUNIX()) 180 return true; 182 if(!f.exists()) 183 return true; 185 if(!f.isDirectory()) 186 return makeExecutable(new File[] { f} ); 187 188 190 return makeExecutable(f.listFiles()); 191 } 192 200 public static void copyTree(File din, File dout) throws IOException 201 { 202 if(!safeIsDirectory(din)) 203 throw new IllegalArgumentException ("Source isn't a directory"); 204 205 dout.mkdirs(); 206 207 if(!safeIsDirectory(dout)) 208 throw new IllegalArgumentException ("Can't create destination directory"); 209 210 FileListerRelative flr = new FileListerRelative(din); 211 String [] files = flr.getFiles(); 212 213 for(int i = 0; i < files.length; i++) 214 { 215 File fin = new File(din, files[i]); 216 File fout = new File(dout, files[i]); 217 218 copy(fin, fout); 219 } 220 } 221 222 226 private static boolean hasExtension(String filename, String ext) 227 { 228 if(filename == null || filename.length() <= 0) 229 return false; 230 231 return filename.endsWith(ext); 232 } 233 234 236 private static boolean hasExtension(File f, String ext) 237 { 238 if(f == null || !f.exists()) 239 return false; 240 241 return f.getName().endsWith(ext); 242 } 243 244 246 private static boolean hasExtensionIgnoreCase(String filename, String ext) 247 { 248 if(filename == null || filename.length() <= 0) 249 return false; 250 251 return filename.toLowerCase().endsWith(ext.toLowerCase()); 252 } 253 254 256 private static boolean hasExtensionIgnoreCase(File f, String ext) 257 { 258 if(f == null || !f.exists()) 259 return false; 260 261 return f.getName().toLowerCase().endsWith(ext.toLowerCase()); 262 } 263 264 273 private static void copy(InputStream inStream, OutputStream outStream) throws IOException 274 { 275 copyWithoutClose(inStream, outStream); 276 277 inStream.close(); 279 outStream.close(); 280 } 281 282 291 private static void copyWithoutClose(InputStream inStream, OutputStream outStream) throws IOException 292 { 293 BufferedInputStream bis = new BufferedInputStream(inStream, BUFFER_SIZE); 294 BufferedOutputStream bos = new BufferedOutputStream(outStream, BUFFER_SIZE); 295 byte[] buf = new byte[BUFFER_SIZE]; 296 297 int len = 0; 298 while (len != -1) 299 { 300 try 301 { 302 len = bis.read(buf, 0, buf.length); 303 } 304 catch (EOFException eof) 305 { 306 break; 307 } 308 309 if (len != -1) 310 { 311 bos.write(buf, 0, len); 312 } 313 } 314 bos.flush(); 315 } 316 323 private static void copy(String from, String to) throws IOException 324 { 325 if(from == null || to == null) 327 throw new IllegalArgumentException ("null or empty filename argument"); 328 329 File fin = new File(from); 330 File fout = new File(to); 331 332 copy(fin, fout); 333 } 334 342 private static void copy(File fin, File fout) throws IOException 343 { 344 if(safeIsDirectory(fin)) 345 { 346 copyTree(fin, fout); 347 return; 348 } 349 350 if(!fin.exists()) 351 throw new IllegalArgumentException ("File source doesn't exist"); 352 353 if(!safeIsDirectory(fout.getParentFile())) 354 fout.getParentFile().mkdirs(); 355 356 copy(new FileInputStream(fin), new FileOutputStream(fout)); 357 } 358 359 361 private static boolean protectUNIX(File f) 362 { 363 if(f == null) 364 return false; 365 366 try 367 { 368 List<String > cmds = new ArrayList<String >(); 369 File[] files = null; 370 ProcessBuilder pb = null; 371 Process p = null; 372 boolean ret = false; 373 374 if(f.isDirectory()) 375 { 376 cmds.add("chmod"); 379 cmds.add("0700"); 380 cmds.add(safeGetCanonicalPath(f)); 381 pb = new ProcessBuilder (cmds); 382 p = pb.start(); 383 ret = p.waitFor() == 0 ? true : false; 384 385 files = f.listFiles(); 386 387 if(files == null || files.length < 1) 388 return ret; 389 } 390 else 391 { 392 ret = true; 393 files = new File[] { f }; 394 } 395 cmds.clear(); 396 cmds.add("chmod"); 397 cmds.add("0600"); 398 399 for(File file : files) 400 cmds.add(safeGetCanonicalPath(file)); 401 402 pb = new ProcessBuilder (cmds); 403 p = pb.start(); 404 405 return ret && (p.waitFor() == 0 ? true : false); 407 } 408 catch(Exception e) 409 { 410 return false; 411 } 412 } 413 414 416 private static boolean protectWindows(File f) 417 { 418 422 String fname = f.getAbsolutePath(); 423 String uname = System.getProperty("user.name"); 424 425 try 426 { 427 ProcessBuilder pb = new ProcessBuilder ("cacls", fname, "/G", uname + ":F"); 428 Process p = pb.start(); 429 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); 430 writer.write('Y'); 431 writer.newLine(); 432 writer.flush(); 433 return p.waitFor() == 0 ? true : false; 434 } 435 catch(Exception e) 436 { 437 return false; 438 } 439 } 440 441 443 private static boolean makeExecutable(File[] files) 444 { 445 449 453 456 457 465 if(files == null || files.length <= 0) 466 return true; 467 468 List<String > cmds = new ArrayList<String >(); 469 470 cmds.add("chmod"); 471 cmds.add("0744"); 472 473 for(File f : files) 474 cmds.add(safeGetCanonicalPath(f)); 475 476 try 477 { 478 ProcessBuilder pb = new ProcessBuilder (cmds); 479 Process p = pb.start(); 480 return p.waitFor() == 0 ? true : false; 481 } 482 catch(Exception e) 483 { 484 return false; 485 } 486 } 487 488 490 private static final int BUFFER_SIZE = 0x10000; private final static char[] ILLEGAL_FILENAME_CHARS = 492 {'/', '\\', ':', '*', '?', '"', '<', '>', '|' }; 493 private final static String ILLEGAL_FILENAME_STRING = "\\/:*?\"<>|"; 494 private final static char REPLACEMENT_CHAR = '_'; 495 private final static char BLANK = ' '; 496 private final static char DOT = '.'; 497 private static String TMPFILENAME = "scratch"; 498 } 499 | Popular Tags |