View Javadoc
1   package de.spiritscorp.datasync.model;
2   
3   /*-
4    * 		Data Sync
5    *
6    * 		Copyright ©   2022    The Spirit
7    * 		@email                        thespirit@spiritscorp.network
8    *
9    * 		This program is free software; you can redistribute it and/or modify
10   * 		it under the terms of the GNU General Public License as published by
11   * 		the Free Software Foundation; either version 3 of the License, or
12   * 		(at your option) any later version.
13   *
14   * 		This program is distributed in the hope that it will be useful,
15   * 		but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * 		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17   * 		See the GNU General Public License for more details.
18   *
19   * 		You should have received a copy of the GNU General Public License
20   * 		along with this program. If not, see <http://www.gnu.org/licenses/>.
21   */
22  
23  import java.io.IOException;
24  import java.nio.file.FileVisitResult;
25  import java.nio.file.FileVisitor;
26  import java.nio.file.Path;
27  import java.nio.file.attribute.BasicFileAttributes;
28  import java.util.Map;
29  import java.util.concurrent.ExecutorService;
30  
31  import de.spiritscorp.datasync.ScanType;
32  import de.spiritscorp.datasync.io.Debug;
33  
34  class FileVisit implements FileVisitor<Path> {
35  	private final ExecutorService executor;
36  	private final Path path;
37  	private final Map<Path, FileAttributes> map;
38  	private final ScanType deepScan;
39  
40  	/**
41  	 * @param executor
42  	 * @param path
43  	 * @param map
44  	 * @param deepScan
45  	 */
46  	FileVisit( final ExecutorService executor, final Path path, final Map<Path, FileAttributes> map, final ScanType deepScan ) {
47  		this.executor = executor;
48  		this.path = path;
49  		this.map = map;
50  		this.deepScan = deepScan;
51  	}
52  
53  	@Override
54  	public FileVisitResult preVisitDirectory( final Path dir, final BasicFileAttributes attrs ) throws IOException {
55  		if( dir != null ) {
56  			if( dir.endsWith( "$RECYCLE.BIN" ) ||
57  					dir.endsWith( "Papierkorb" ) ||
58  					attrs.isSymbolicLink() ||
59  					attrs.isOther() ) { return FileVisitResult.SKIP_SUBTREE; }
60  		}
61  		return FileVisitResult.CONTINUE;
62  	}
63  
64  	@Override
65  	public FileVisitResult visitFile( final Path file, final BasicFileAttributes attrs ) throws IOException {
66  		if( attrs.isSymbolicLink() || !attrs.isRegularFile() ) {
67  			Debug.printDebug( "[File Visit] Skip_VisitFile -> " + file );
68  			return FileVisitResult.CONTINUE;
69  		}
70  		executor.execute( new FileScan( file.normalize(), path.normalize(), map, deepScan, attrs ) );
71  		return FileVisitResult.CONTINUE;
72  	}
73  
74  	@Override
75  	public FileVisitResult visitFileFailed( final Path file, final IOException exc ) throws IOException {
76  		Debug.printDebug( "[File Visit] VisitFileFailed -> " + file );
77  		return FileVisitResult.CONTINUE;
78  	}
79  
80  	@Override
81  	public FileVisitResult postVisitDirectory( final Path dir, final IOException exc ) throws IOException {
82  		return FileVisitResult.CONTINUE;
83  	}
84  
85  }