commit | author | age
|
e369b9
|
1 |
/* |
c45b52
|
2 |
fm - File management class library |
e369b9
|
3 |
Copyright (C) 2024 Ulrich Hilger |
U |
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.fm; |
|
19 |
|
|
20 |
import java.io.File; |
|
21 |
import java.io.IOException; |
|
22 |
import java.nio.file.FileVisitResult; |
|
23 |
import java.nio.file.FileVisitor; |
|
24 |
import java.nio.file.Files; |
|
25 |
import java.nio.file.Path; |
|
26 |
import java.nio.file.attribute.BasicFileAttributes; |
|
27 |
|
|
28 |
/** |
|
29 |
* Ein FileVisitor zum Verschieben oder Kopieren ganzer Ordnerstrukturen mit |
|
30 |
* Hilfe der Methode Files.walkFileTree von java.nio. |
|
31 |
* |
|
32 |
* @author Ulrich Hilger |
|
33 |
* @version 1, 14. Mai 2021 |
|
34 |
*/ |
c45b52
|
35 |
public class CopyMoveVisitor extends FileHelper implements FileVisitor { |
e369b9
|
36 |
|
U |
37 |
private Path targetDir; |
|
38 |
private int operation; |
|
39 |
|
973951
|
40 |
/** |
U |
41 |
* Den Zielordner fuer Kopier- oder Verschiebeoperationen angeben |
|
42 |
* |
|
43 |
* @param targetDir der Zielordner |
|
44 |
*/ |
e369b9
|
45 |
public void setTargetDir(Path targetDir) { |
U |
46 |
this.targetDir = targetDir; |
|
47 |
} |
|
48 |
|
|
49 |
/** |
973951
|
50 |
* Die gewuenschte Dateioperation angeben, |
U |
51 |
* OP_COPY, OP_MOVE oder OP_DELETE |
e369b9
|
52 |
* |
973951
|
53 |
* @param op die Dateioperation |
e369b9
|
54 |
*/ |
U |
55 |
public void setOperation(int op) { |
|
56 |
this.operation = op; |
|
57 |
} |
|
58 |
|
973951
|
59 |
/** |
U |
60 |
* Dafuer sorgen, dass beim Kopieren oder Verschieben kein am Ziel bereits existierender |
|
61 |
* Ordner ueberschrieben wird, indem am Ziel fuer einen bereits existierenden Ordner ein |
|
62 |
* anderer Name mit laufender Nummer erzeugt wird. |
|
63 |
* |
|
64 |
* @param dir Zielordner |
|
65 |
* @param attrs die gewuenschten Attribute |
|
66 |
* @return gibt stets FileVisitResult.CONTINUE zurueck |
|
67 |
* @throws IOException wenn etwas schief geht |
|
68 |
*/ |
e369b9
|
69 |
@Override |
U |
70 |
public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException { |
973951
|
71 |
if (operation != Eraser.OP_DELETE) { |
e369b9
|
72 |
if (dir instanceof Path) { |
U |
73 |
Path sourceDir = (Path) dir; |
|
74 |
File destFile = targetDir.resolve(sourceDir.getFileName()).toFile(); |
|
75 |
//logger.fine("sourceDir: " + sourceDir + ", destFile: " + destFile); |
|
76 |
if (destFile.exists()) { |
|
77 |
File newDir = getNewFileName(destFile); |
|
78 |
destFile.renameTo(newDir); |
|
79 |
} |
|
80 |
destFile.mkdir(); |
|
81 |
this.targetDir = destFile.toPath(); |
|
82 |
//logger.fine("targetDir now: " + targetDir.toString()); |
|
83 |
} |
|
84 |
} |
|
85 |
return FileVisitResult.CONTINUE; |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
|
89 |
public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException { |
973951
|
90 |
if(operation != Eraser.OP_DELETE) { |
e369b9
|
91 |
if (file instanceof Path) { |
U |
92 |
Path source = (Path) file; |
|
93 |
File destFile = targetDir.resolve(source.getFileName()).toFile(); |
|
94 |
if (destFile.exists()) { |
|
95 |
destFile = getNewFileName(destFile); |
|
96 |
} |
973951
|
97 |
if (operation == Mover.OP_MOVE) { |
e369b9
|
98 |
//logger.fine("move source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); |
U |
99 |
Files.move(source, destFile.toPath()); |
973951
|
100 |
} else if (operation == Mover.OP_COPY) { |
e369b9
|
101 |
//logger.fine("copy source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); |
U |
102 |
Files.copy(source, destFile.toPath()); |
|
103 |
} |
|
104 |
} |
|
105 |
} else { |
|
106 |
Files.delete((Path) file); |
|
107 |
} |
|
108 |
return FileVisitResult.CONTINUE; |
|
109 |
} |
|
110 |
|
|
111 |
@Override |
|
112 |
public FileVisitResult visitFileFailed(Object file, IOException exc) throws IOException { |
|
113 |
return FileVisitResult.CONTINUE; |
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException { |
973951
|
118 |
if (operation != Eraser.OP_DELETE) { |
e369b9
|
119 |
if (dir instanceof Path) { |
U |
120 |
Path finishedDir = (Path) dir; |
|
121 |
targetDir = targetDir.getParent(); |
973951
|
122 |
if(operation == Mover.OP_MOVE) { |
e369b9
|
123 |
//logger.fine("delete " + finishedDir.toString()); |
U |
124 |
Files.delete(finishedDir); |
|
125 |
} |
|
126 |
} |
|
127 |
//logger.fine("targetDir now: " + targetDir.toString()); |
|
128 |
} else { |
|
129 |
Files.delete((Path) dir); |
|
130 |
} |
|
131 |
return FileVisitResult.CONTINUE; |
|
132 |
} |
|
133 |
|
|
134 |
} |