litesoft
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.util; import org.litesoft.commonfoundation.base.*; import org.litesoft.commonfoundation.exceptions.*; import org.litesoft.commonfoundation.typeutils.*; import java.io.*; import java.util.*; @SuppressWarnings("UnusedDeclaration") public class Directories { public static String currentWorking() { return System.getProperty( "user.dir" ); } public static String temp() { return System.getProperty( "java.io.tmpdir" ); } public static File findFromCurrent( String pSubPath ) { return find( currentWorking(), pSubPath ); } public static File find( String pStartingPath, String pSubPath ) { Confirm.isNotNull( "StartingPath", pStartingPath ); pSubPath = Confirm.significant( "SubPath", pSubPath ); File target = locate( pStartingPath, pSubPath, Paths.justTheLastName( pSubPath ) + ".path" ); if ( target != null ) { return target; } throw new IllegalStateException( "Unable to locate Directory: " + pSubPath ); } private static File locate( String pStartingPath, String pSubPath, String pRedirectorName ) { File base = new File( pStartingPath ); do { File target = new File( base, pSubPath ); if ( target.exists() ) { return target; } if ( pRedirectorName != null ) { target = new File( base, pRedirectorName ); if ( target.isFile() ) { for ( String line : FileUtils.loadTextFile( target ) ) { if ( Currently.isNotNullOrEmpty( line ) ) { if ( null != (target = locate( line, pSubPath, null )) ) { return target; } } } } } base = base.getParentFile(); } while ( base != null ); return null; } public static File assertDirectory( File pExpectedDir ) throws FileSystemException { Confirm.isNotNull( "ExpectedDir", pExpectedDir ); if ( !pExpectedDir.isDirectory() ) { throw new FileSystemException( "Not a Directory: " + pExpectedDir.getAbsolutePath() ); } return pExpectedDir; } public static void checkDirectoryable( File pExpectedDir ) throws FileSystemException { Confirm.isNotNull( "ExpectedDir", pExpectedDir ); if ( !pExpectedDir.isDirectory() && pExpectedDir.exists() ) { throw new FileSystemException( "Exists, but is Not a Directory: " + pExpectedDir.getAbsolutePath() ); } } public static File insureParent( File pExpectedFile ) throws FileSystemException { Confirm.isNotNull( "ExpectedFile", pExpectedFile ); File zParentFile = pExpectedFile.getParentFile(); if ( zParentFile != null ) { insure( zParentFile ); } return pExpectedFile; } public static File insure( File pExpectedDir ) throws FileSystemException { Confirm.isNotNull( "ExpectedDir", pExpectedDir ); if ( !pExpectedDir.isDirectory() ) { if ( pExpectedDir.exists() ) { throw new FileSystemException( "Exists, but is Not a Directory: " + pExpectedDir.getAbsolutePath() ); } if ( !pExpectedDir.mkdirs() || !pExpectedDir.isDirectory() ) { throw new FileSystemException( "Unable to create Directory: " + pExpectedDir.getAbsolutePath() ); } } return pExpectedDir; } public static void moveAllFiles( String pFromPath, String pToPath ) { File zFrom = assertDirectory( new File( pFromPath ) ); File zTo = insure( new File( pToPath ) ); File[] zFiles = zFrom.listFiles(); if ( zFiles != null ) { for ( File zFile : zFiles ) { String zName = zFile.getName(); moveFile( zFile, new File( zTo, zName ) ); } } } public static void moveFile( File pSourceFile, File pTargetFile ) throws FileSystemException { Confirm.isNotNull( "SourceFile", pSourceFile ); Confirm.isNotNull( "TargetFile", pTargetFile ); Throwable zProblem = null; try { if ( pSourceFile.exists() ) { insureParent( pTargetFile ); } if ( pSourceFile.renameTo( pTargetFile ) ) { if ( pTargetFile.exists() ) { return; } } } catch ( RuntimeException e ) { zProblem = e; } throw new FileSystemException( "Failed to Move (rename): " + pSourceFile + " to " + pTargetFile, zProblem ); } public static void deleteIfExists( File pDirectory ) throws FileSystemException { Confirm.isNotNull( "Directory", pDirectory ); if ( pDirectory.isDirectory() ) { LowLevelDeleteAllEntries( pDirectory ); if ( !pDirectory.delete() || pDirectory.exists() ) { throw new FileSystemException( "Unable to delete: " + pDirectory.getAbsolutePath() ); } } } @SuppressWarnings({"UnusedDeclaration"}) public static void deleteAllEntries( File pDirectory ) throws FileSystemException { Confirm.isNotNull( "Directory", pDirectory ); if ( pDirectory.isDirectory() ) { LowLevelDeleteAllEntries( pDirectory ); } } private static void LowLevelDeleteAllEntries( File pDirectory ) throws FileSystemException { String[] files = pDirectory.list(); for ( String file : files ) { File entry = new File( pDirectory, file ); if ( entry.isDirectory() ) { deleteIfExists( entry ); } else { FileUtils.deleteIfExists( entry ); } } } /** * @param pExpectedDirectory e.g. C:\work\OBEDS\test\odlfExpected * @param pActualDirectory e.g. C:\work\OBEDS\test\odlf * * @return error if not null or empty */ @SuppressWarnings({"UnusedDeclaration"}) public static String compare( String pExpectedDirectory, String pActualDirectory, String... pSkipEntries ) { File expected = new File( pExpectedDirectory ); File actual = new File( pActualDirectory ); if ( !expected.isDirectory() ) { return "'" + expected.getAbsolutePath() + "' Not a Directory"; } if ( !actual.isDirectory() ) { return "'" + actual.getAbsolutePath() + "' Not a Directory"; } AllButFilter filter = new AllButFilter( pSkipEntries ); String[] entriesExpected = expected.list( filter ); String[] entriesActual = actual.list( filter ); Arrays.sort( entriesExpected ); Arrays.sort( entriesActual ); if ( entriesActual.length == 0 ) { return (entriesExpected.length == 0) ? null : // No Error "Missing: " + addEntries( entriesExpected, 0 ).substring( 1 ); } if ( entriesExpected.length == 0 ) { return "Unexpected: " + addEntries( entriesActual, 0 ).substring( 1 ); } List<FileMatcher> matchers = new ArrayList<FileMatcher>(); StringBuilder unexpected = new StringBuilder(); StringBuilder missing = new StringBuilder(); int atActual = 0; int atExpected = 0; while ( (atActual < entriesActual.length) && (atExpected < entriesExpected.length) ) { int cmp = entriesActual[atActual].compareTo( entriesExpected[atExpected] ); if ( cmp == 0 ) { // Equal matchers.add( new FileMatcher( pExpectedDirectory, pActualDirectory, entriesExpected[atExpected++] ) ); atActual++; } else if ( cmp > 0 ) { // Actual > Expected add( missing, entriesExpected[atExpected++] ); } else { // Actual < Expected add( unexpected, entriesActual[atActual++] ); } } while ( atActual < entriesActual.length ) { add( unexpected, entriesActual[atActual++] ); } while ( atExpected < entriesExpected.length ) { add( missing, entriesExpected[atExpected++] ); } StringBuilder issues = new StringBuilder(); if ( unexpected.length() != 0 ) { issues.append( "<br>" ).append( "Unexpected: " ).append( unexpected.toString().substring( 1 ) ); } if ( missing.length() != 0 ) { issues.append( "<br>" ).append( "Missing: " ).append( missing.toString().substring( 1 ) ); } if ( issues.length() != 0 ) { return issues.toString(); } for ( FileMatcher matcher : matchers ) { matcher.checkLengths( issues ); } if ( issues.length() != 0 ) { return issues.toString(); } for ( FileMatcher matcher : matchers ) { matcher.checkContents( issues ); } if ( issues.length() != 0 ) { return issues.toString(); } return null; // No error } private static String addEntries( String[] pEntries, int pAt ) { StringBuilder sb = new StringBuilder(); while ( pAt < pEntries.length ) { add( sb, pEntries[pAt++] ); } return sb.toString(); } private static void add( StringBuilder pSb, String pEntry ) { pSb.append( ", " ).append( pEntry ); } public static class DirsOnlyFilter implements FilenameFilter { public static final FilenameFilter INSTANCE = new DirsOnlyFilter(); @Override public boolean accept( File dir, String name ) { return new File( dir, name ).isDirectory(); } } public static class DirsOnlyFileFilter implements FileFilter { public static final FileFilter INSTANCE = new DirsOnlyFileFilter(); @Override public boolean accept( File pathname ) { return pathname.isDirectory(); } } public static class AllButFilter implements FilenameFilter { private String[] mButs; public AllButFilter( String... pButs ) { mButs = pButs; } @Override public boolean accept( File dir, String name ) { for ( String but : mButs ) { if ( name.equals( but ) ) { return false; } } return true; } } public static class FileMatcher { private File mExpected, mActual; private String mName; public FileMatcher( String pExpectedDirectory, String pActualDirectory, String pName ) { mName = pName; mExpected = new File( pExpectedDirectory, pName ); mActual = new File( pActualDirectory, pName ); } public void checkLengths( StringBuilder pIssues ) { long expected = mExpected.length(); long actual = mActual.length(); if ( actual != expected ) { pIssues.append( "<br>Files '" ).append( mName ).append( "' Lengths MisMatch " ).append( "Actual(" ).append( actual ).append( ") != Expected(" ) .append( expected ).append( ")" ); } } public void checkContents( StringBuilder pIssues ) { StreamBlockReader eSBR = new StreamBlockReader(); StreamBlockReader aSBR = new StreamBlockReader(); try { eSBR.initialize( new FileInputStream( mExpected ) ); aSBR.initialize( new FileInputStream( mActual ) ); eSBR.readNextBlock(); aSBR.readNextBlock(); do { if ( !eSBR.blocksEqual( aSBR ) ) { pIssues.append( "<br>Files '" ).append( mName ).append( "' Contents Mismatch" ); return; } } while ( 0 != (eSBR.readNextBlock() + aSBR.readNextBlock()) ); } catch ( IOException e ) { pIssues.append( "<br>Files '" ).append( mName ).append( "' IOException: " ).append( e.getMessage() ); } finally { eSBR.dispose(); aSBR.dispose(); } } } public static class StreamBlockReader { private InputStream mInputStream = null; private byte[] mBuf = new byte[16 * 1024]; private int mCount = 0; public void initialize( InputStream pInputStream ) { mInputStream = pInputStream; mCount = 0; } public int readNextBlock() throws IOException { mCount = 0; int bytes; while ( 0 < (bytes = mInputStream.read( mBuf, mCount, mBuf.length - mCount )) ) { mCount += bytes; if ( mCount == mBuf.length ) { break; } } return mCount; } public boolean blocksEqual( StreamBlockReader pThem ) { if ( this.mCount != pThem.mCount ) { return false; } for ( int i = 0; i < mCount; i++ ) { if ( this.mBuf[i] != pThem.mBuf[i] ) { return false; } } return true; } public void dispose() { if ( mInputStream != null ) { Utils.dispose( mInputStream ); mInputStream = null; } } } public static void main( String[] args ) throws Exception { DirectoriesDepthFirst.Visitor zVisitor = new MyVisitor( FileUtils.createFileNameFilter( Strings.parseChar( args[0], '*' ) ) ); new DirectoriesDepthFirst( zVisitor ).process( new File( currentWorking() ) ); } private static class MyVisitor implements DirectoriesDepthFirst.Visitor { private FilenameFilter mFilter; public MyVisitor( FilenameFilter pFilter ) { mFilter = pFilter; } @Override public void process( File pDirectory ) throws IOException { File[] zFiles = pDirectory.listFiles( mFilter ); if ( Currently.isNotNullOrEmpty( zFiles ) ) { System.out.println( pDirectory ); for ( File zFile : zFiles ) { boolean zDeleted = zFile.delete(); System.out.println( " " + (zDeleted ? " " : "!") + " " + zFile.getName() ); } String[] zFileNames = pDirectory.list(); if ( Currently.isNullOrEmpty( zFileNames ) ) { boolean zDeleted = pDirectory.delete(); System.out.println( " " + (zDeleted ? "x!" : "!x") ); } } } } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/util/Directories.java
Revision | Author | Commited | Message |
---|---|---|---|
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
917 Diff | GeorgeS | Sun 08 Dec, 2013 20:49:56 +0000 | 1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model! |
878 Diff | GeorgeS | Mon 10 Dec, 2012 03:45:05 +0000 | |
823 Diff | GeorgeS | Sun 19 Aug, 2012 16:10:13 +0000 | |
821 Diff | GeorgeS | Sun 19 Aug, 2012 00:08:41 +0000 | |
809 Diff | GeorgeS | Thu 16 Aug, 2012 04:10:46 +0000 | |
804 | GeorgeS | Wed 15 Aug, 2012 12:48:51 +0000 |