11package org .tron .plugins ;
22
33import com .typesafe .config .Config ;
4+ import com .typesafe .config .ConfigException ;
45import com .typesafe .config .ConfigFactory ;
56import java .io .File ;
67import java .io .IOException ;
78import java .nio .file .Files ;
9+ import java .nio .file .InvalidPathException ;
10+ import java .nio .file .LinkOption ;
811import java .nio .file .Path ;
912import java .nio .file .Paths ;
10- import java .nio .file .StandardCopyOption ;
11- import java .util .Arrays ;
13+ import java .nio .file .attribute . BasicFileAttributes ;
14+ import java .util .ArrayList ;
1215import java .util .HashSet ;
1316import java .util .List ;
14- import java .util .Objects ;
1517import java .util .Set ;
1618import java .util .concurrent .Callable ;
19+ import java .util .concurrent .atomic .AtomicBoolean ;
1720import java .util .stream .Collectors ;
21+ import java .util .stream .Stream ;
1822import lombok .extern .slf4j .Slf4j ;
1923import me .tongfei .progressbar .ProgressBar ;
2024import org .tron .plugins .utils .FileUtils ;
2327
2428@ Slf4j (topic = "move" )
2529@ Command (name = "mv" , aliases = "move" ,
30+ header = Db .STOP_NODE_HEADER ,
2631 description = "Move db to pre-set new path . For example HDD,reduce storage expenses." )
2732public class DbMove implements Callable <Integer > {
2833
@@ -65,9 +70,6 @@ public Integer call() throws Exception {
6570 printNotExist ();
6671 return 0 ;
6772 }
68- String dbPath = config .hasPath (DB_DIRECTORY_CONFIG_KEY )
69- ? config .getString (DB_DIRECTORY_CONFIG_KEY ) : DEFAULT_DB_DIRECTORY ;
70-
7173 dbs = dbs .stream ()
7274 .filter (c -> c .hasPath (NAME_CONFIG_KEY ) && c .hasPath (PATH_CONFIG_KEY ))
7375 .collect (Collectors .toList ());
@@ -76,83 +78,203 @@ public Integer call() throws Exception {
7678 printNotExist ();
7779 return 0 ;
7880 }
79- List <Property > toBeMove = dbs .stream ()
80- .map (c -> {
81- try {
82- return new Property (c .getString (NAME_CONFIG_KEY ),
83- Paths .get (database .toString (), dbPath , c .getString (NAME_CONFIG_KEY )),
84- Paths .get (c .getString (PATH_CONFIG_KEY ), dbPath , c .getString (NAME_CONFIG_KEY )));
85- } catch (IOException e ) {
86- spec .commandLine ().getErr ().println (e );
87- }
88- return null ;
89- }).filter (Objects ::nonNull )
90- .filter (p -> !p .destination .equals (p .original )).collect (Collectors .toList ());
91-
92- if (toBeMove .isEmpty ()) {
93- printNotExist ();
94- return 0 ;
81+ List <Property > toBeMove = new ArrayList <>();
82+ try {
83+ String dbPath = config .hasPath (DB_DIRECTORY_CONFIG_KEY )
84+ ? config .getString (DB_DIRECTORY_CONFIG_KEY ) : DEFAULT_DB_DIRECTORY ;
85+ for (Config c : dbs ) {
86+ String name = c .getString (NAME_CONFIG_KEY );
87+ toBeMove .add (new Property (name ,
88+ Paths .get (database .toString (), dbPath , name ),
89+ Paths .get (c .getString (PATH_CONFIG_KEY ), dbPath , name )));
90+ }
91+ } catch (IOException | ConfigException | InvalidPathException e ) {
92+ spec .commandLine ().getErr ().println (e );
93+ return 2 ;
94+ }
95+ if (hasOverlappingPaths (toBeMove )) {
96+ return 2 ;
97+ }
98+ if (!copyAll (toBeMove )) {
99+ cleanupDestinations (toBeMove );
100+ return 1 ;
95101 }
96- toBeMove = toBeMove .stream ()
97- .filter (property -> {
98- if (property .destination .toFile ().exists ()) {
99- spec .commandLine ().getOut ().println (String .format ("%s already exist,skip." ,
100- property .destination ));
101- return false ;
102- } else {
103- return true ;
104- }
105- }).collect (Collectors .toList ());
106102
107- if (toBeMove .isEmpty ()) {
108- printNotExist ();
109- return 0 ;
103+ boolean allMoved = ProgressBar .wrap (toBeMove .stream (), "link task" )
104+ .map (this ::replaceSourceWithLink ).reduce (Boolean .TRUE , Boolean ::logicalAnd );
105+ if (!allMoved ) {
106+ return 1 ;
110107 }
111- ProgressBar .wrap (toBeMove .stream (), "mv task" ).forEach (this ::run );
112108 spec .commandLine ().getOut ().println ("move db done." );
113-
114109 } else {
115110 printNotExist ();
116111 return 0 ;
117112 }
118113 return 0 ;
119114 }
120115
121- private void run (Property p ) {
122- if (p .destination .toFile ().mkdirs ()) {
123- ProgressBar .wrap (Arrays .stream (Objects .requireNonNull (p .original .toFile ().listFiles ()))
124- .filter (File ::isFile ).map (File ::getName ).parallel (), p .name ).forEach (file -> {
125- Path original = Paths .get (p .original .toString (), file );
126- Path destination = Paths .get (p .destination .toString (), file );
127- try {
128- Files .copy (original , destination ,
129- StandardCopyOption .REPLACE_EXISTING );
130- } catch (IOException e ) {
131- spec .commandLine ().getErr ().println (e );
132- }
133- });
116+ private boolean copyAll (List <Property > properties ) {
117+ try (ProgressBar bar = new ProgressBar ("copy task" , properties .size ())) {
118+ for (Property p : properties ) {
119+ if (!copy (p )) {
120+ return false ;
121+ }
122+ bar .step ();
123+ }
124+ return true ;
125+ }
126+ }
127+
128+ private boolean copy (Property p ) {
129+ AtomicBoolean hasError = new AtomicBoolean (false );
130+ try (Stream <Path > files = Files .walk (p .original )) {
131+ // Collect the tree before copying anything: a traversal failure must
132+ // surface while no copy task is in flight, otherwise a task started
133+ // before the failure could recreate the destination after the rollback
134+ // has already deleted it, and the retry would then be rejected.
135+ List <Path > sources = files .collect (Collectors .toList ());
136+ Files .createDirectories (p .destination .getParent ());
137+ Files .createDirectory (p .destination );
138+ p .created = true ;
139+ ProgressBar .wrap (sources .parallelStream (), p .name ).forEach (source -> {
140+ if (hasError .get ()) {
141+ return ;
142+ }
143+ try {
144+ copyEntry (p , source );
145+ } catch (IOException | RuntimeException e ) {
146+ hasError .set (true );
147+ spec .commandLine ().getErr ().println (e );
148+ }
149+ });
150+ } catch (IOException | RuntimeException e ) {
151+ hasError .set (true );
152+ spec .commandLine ().getErr ().println (e );
153+ }
154+
155+ if (hasError .get ()) {
156+ spec .commandLine ().getErr ().println (String .format (
157+ "%s copy to %s failed, source kept." ,
158+ p .original , p .destination ));
159+ return false ;
160+ }
161+ return true ;
162+ }
163+
164+ private void copyEntry (Property p , Path source ) throws IOException {
165+ BasicFileAttributes attributes = Files .readAttributes (
166+ source , BasicFileAttributes .class , LinkOption .NOFOLLOW_LINKS );
167+ Path destination = p .destination .resolve (p .original .relativize (source ));
168+ if (attributes .isDirectory ()) {
169+ Files .createDirectories (destination );
170+ } else if (attributes .isRegularFile ()) {
171+ Files .createDirectories (destination .getParent ());
172+ Files .copy (source , destination );
173+ } else {
174+ throw new IOException (String .format (
175+ "%s is neither a regular file nor a directory, can not be moved." , source ));
176+ }
177+ }
178+
179+ private boolean replaceSourceWithLink (Property p ) {
180+ try {
181+ if (!FileUtils .deleteDir (p .original .toFile ())) {
182+ spec .commandLine ().getErr ().println (String .format (
183+ "%s delete failed and may be incomplete; the only complete copy is at %s, keep it." ,
184+ p .original , p .destination ));
185+ printRecoveryHint (p );
186+ return false ;
187+ }
188+ Files .createSymbolicLink (p .original , p .destination );
189+ return true ;
190+ } catch (IOException | RuntimeException x ) {
191+ spec .commandLine ().getErr ().println (x );
192+ spec .commandLine ().getErr ().println (String .format (
193+ "%s move failed; the complete copy is at %s, keep it." ,
194+ p .original , p .destination ));
195+ printRecoveryHint (p );
196+ return false ;
197+ }
198+ }
199+
200+ private void printRecoveryHint (Property p ) {
201+ spec .commandLine ().getErr ().println (String .format (
202+ "To recover manually: remove %s if present, then create a symbolic link at %s"
203+ + " pointing to %s." ,
204+ p .original , p .original , p .destination ));
205+ }
206+
207+ private void cleanupDestinations (List <Property > properties ) {
208+ boolean allCleaned = properties .stream ().map (property -> {
209+ File destination = property .destination .toFile ();
210+ if (Files .notExists (destination .toPath (), LinkOption .NOFOLLOW_LINKS )) {
211+ return true ;
212+ }
213+ if (!property .created ) {
214+ spec .commandLine ().getErr ().println (String .format (
215+ "%s was not created by this run and was kept; move it aside before retrying." ,
216+ property .destination ));
217+ return false ;
218+ }
134219 try {
135- if (FileUtils .deleteDir (p . original . toFile () )) {
136- Files . createSymbolicLink ( p . original , p . destination ) ;
220+ if (FileUtils .deleteDir (destination )) {
221+ return true ;
137222 }
138- } catch (IOException | UnsupportedOperationException x ) {
139- spec .commandLine ().getErr ().println (x );
223+ } catch (RuntimeException e ) {
224+ spec .commandLine ().getErr ().println (e );
140225 }
226+ spec .commandLine ().getErr ().println (String .format (
227+ "%s cleanup failed; remove the leftover copy before retrying." ,
228+ property .destination ));
229+ return false ;
230+ }).reduce (Boolean .TRUE , Boolean ::logicalAnd );
231+
232+ if (allCleaned ) {
233+ spec .commandLine ().getErr ().println (
234+ "move db failed; all source databases were kept, please retry." );
141235 } else {
142- spec .commandLine ().getErr ().println (String .format ("%s create failed." , p .destination ));
236+ spec .commandLine ().getErr ().println (
237+ "move db failed; all source databases were kept, but some destinations remain." );
143238 }
144239 }
145240
146241 private void printNotExist () {
147242 spec .commandLine ().getErr ().println (NOT_FIND );
148243 }
149244
245+ private boolean hasOverlappingPaths (List <Property > properties ) {
246+ for (Property outer : properties ) {
247+ for (Property inner : properties ) {
248+ if (isInside ("destination" , inner .destination , "original" , outer .original )) {
249+ return true ;
250+ }
251+ if (inner == outer ) {
252+ continue ;
253+ }
254+ if (isInside ("original" , inner .original , "original" , outer .original )
255+ || isInside ("destination" , inner .destination , "destination" , outer .destination )) {
256+ return true ;
257+ }
258+ }
259+ }
260+ return false ;
261+ }
262+
263+ private boolean isInside (String kind , Path path , String rootKind , Path root ) {
264+ if (!path .startsWith (root )) {
265+ return false ;
266+ }
267+ spec .commandLine ().getErr ().println (String .format (
268+ "%s [%s] can not be inside %s [%s], please check!" , kind , path , rootKind , root ));
269+ return true ;
270+ }
150271
151272 static class Property {
152273
153274 private final String name ;
154275 private final Path original ;
155276 final Path destination ;
277+ private boolean created ;
156278
157279 public Property (String name , Path original , Path destination ) throws IOException {
158280 this .name = name ;
@@ -167,7 +289,7 @@ public Property(String name, Path original, Path destination) throws IOException
167289 throw new IOException (original + " is symbolicLink!" );
168290 }
169291 this .destination = destination .toFile ().getCanonicalFile ().toPath ();
170- if (this .destination . toFile (). exists ( )) {
292+ if (! Files . notExists ( this .destination , LinkOption . NOFOLLOW_LINKS )) {
171293 throw new IOException (this .destination + " already exist!" );
172294 }
173295 if (this .destination .equals (this .original )) {
@@ -195,9 +317,6 @@ public Config convert(String value) throws Exception {
195317 if (dbs .isEmpty ()) {
196318 throw notFind ;
197319 }
198- String dbPath = config .hasPath (DB_DIRECTORY_CONFIG_KEY )
199- ? config .getString (DB_DIRECTORY_CONFIG_KEY ) : DEFAULT_DB_DIRECTORY ;
200-
201320 dbs = dbs .stream ()
202321 .filter (c -> c .hasPath (NAME_CONFIG_KEY ) && c .hasPath (PATH_CONFIG_KEY ))
203322 .collect (Collectors .toList ());
@@ -207,13 +326,10 @@ public Config convert(String value) throws Exception {
207326 }
208327 Set <String > toBeMove = new HashSet <>();
209328 for (Config c : dbs ) {
210- if (!toBeMove .add (new Property (c .getString (NAME_CONFIG_KEY ),
211- Paths .get (database .toString (), dbPath , c .getString (NAME_CONFIG_KEY )),
212- Paths .get (c .getString (PATH_CONFIG_KEY ), dbPath ,
213- c .getString (NAME_CONFIG_KEY ))).name )) {
329+ String name = c .getString (NAME_CONFIG_KEY );
330+ if (!toBeMove .add (name )) {
214331 throw new IllegalArgumentException (
215- "DB config has duplicate key:[" + c .getString (NAME_CONFIG_KEY )
216- + "],please check! " );
332+ "DB config has duplicate key:[" + name + "],please check! " );
217333 }
218334 }
219335 } else {
0 commit comments