1 18 19 import jcifs.smb.SmbFile; 20 import jcifs.util.*; 21 import java.util.LinkedList ; 22 import java.util.ListIterator ; 23 import java.net.MalformedURLException ; 24 import java.io.IOException ; 25 26 public class T2Crawler { 27 28 class Semaphore { 29 private int value = 0; 30 31 Semaphore() {value = 0;} 32 Semaphore(int initial) {value = initial;} 33 34 public synchronized void P() { 35 value--; 36 if (value < 0) { 37 try { 38 wait(); 39 } catch (InterruptedException e) { 40 e.printStackTrace(); 41 } 42 } 43 } 44 45 public synchronized void V() { 46 value++; 47 notify(); 48 } 49 } 50 51 class CrawlerThread extends Thread { 52 LinkedList list; 53 Semaphore sem; 54 SmbFile dir; 55 int depth; 56 57 58 CrawlerThread( SmbFile dir, Semaphore sem, int depth ) { 59 this.dir = dir; 60 list = new LinkedList (); 61 list.add( dir ); 62 this.sem = sem; 63 this.depth = depth; 64 } 65 66 public void run() { 67 SmbFile d; 68 SmbFile l[]; 69 70 while( list.isEmpty() == false ) { 71 int i; 72 d = (SmbFile)list.remove( 0 ); 73 try { 74 l = d.listFiles(); 75 76 80 depth--; 81 for( i = 0; i < l.length; i++ ) { 82 System.out.println( l[i] ); 83 if( depth > 0 && l[i].isDirectory() ) { 85 list.add( l[i] ); 86 } 87 } 88 } catch( Exception e ) { 89 System.out.println( d ); 90 e.printStackTrace(); 91 } 92 } 93 sem.V(); 94 } 95 } 96 97 T2Crawler( String dir, int numThreads, int depth ) throws Exception { 98 SmbFile top = new SmbFile( dir ); 99 Semaphore sem = new Semaphore( numThreads ); 100 SmbFile[] l = null; 101 int i = 0; 102 103 try { 104 l = top.listFiles(); 105 depth--; 106 for( i = 0; i < l.length; i++ ) { 107 try { 108 System.out.println( l[i] ); 109 if( !l[i].isDirectory() || l[i].isHidden() ) { 110 continue; 111 } 112 if( depth > 0 ) { 113 sem.P(); 114 (new CrawlerThread( l[i], sem, depth )).start(); 115 } 116 } catch( Exception e ) { 117 e.printStackTrace(); 118 } 119 } 120 for( i = 0; i < l.length; i++ ) { 121 try { 122 l[i].canRead(); 123 } catch(Exception ex) { 124 System.err.println( l[i] ); 125 ex.printStackTrace(); 126 } 127 } 128 } catch( Exception ex ) { 129 System.err.println( l[i] ); 130 ex.printStackTrace(); 131 } 132 } 133 public static void main(String [] argv) throws Exception { 134 if( argv.length < 3) { 135 System.out.println( "$ java -Djcifs.properties=miallen.prp T2Crawler <dir> <num threads> <depth>"); 136 System.exit(1); 137 } 138 new T2Crawler( argv[0], Integer.parseInt( argv[1] ), Integer.parseInt( argv[2] )); 139 } 140 } 141 | Popular Tags |