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( ExecutorService executor, Path path, Map<Path, FileAttributes> map, 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( Path dir, BasicFileAttributes attrs ) throws IOException {
55  		if( dir != null ) {
56  			if( dir.endsWith( "$RECYCLE.BIN" ) ||
57  					dir.endsWith( "Papierkorb" ) ||
58  					attrs.isSymbolicLink() ||
59  					attrs.isOther() ) {
60  //					Debug.PRINT_DEBUG("Skip_preVisitDirectory -> " + dir);
61  				return FileVisitResult.SKIP_SUBTREE;
62  			}
63  		}
64  		return FileVisitResult.CONTINUE;
65  	}
66  
67  	@Override
68  	public FileVisitResult visitFile( Path file, BasicFileAttributes attrs ) throws IOException {
69  		if( attrs.isSymbolicLink() || !attrs.isRegularFile() ) {
70  			Debug.printDebug( "[FileVisit] Skip_VisitFile -> " + file );
71  			return FileVisitResult.CONTINUE;
72  		}
73  		executor.execute( new FileScan( file, path, map, deepScan, attrs ) );
74  		return FileVisitResult.CONTINUE;
75  	}
76  
77  	@Override
78  	public FileVisitResult visitFileFailed( final Path file, final IOException exc ) throws IOException {
79  		Debug.printDebug( "[FileVisit] VisitFileFailed -> " + file );
80  		return FileVisitResult.CONTINUE;
81  	}
82  
83  	@Override
84  	public FileVisitResult postVisitDirectory( Path dir, IOException exc ) throws IOException {
85  		return FileVisitResult.CONTINUE;
86  	}
87  
88  }