Skip to content

Commit

Permalink
v 1.1 making the logging more rebust. support verbose vs none verbose…
Browse files Browse the repository at this point in the history
… on the fly using the Show Details checkbox .

fixed bug in the console version so it parse the command line argumens correctly.
set a threadhold to the text area output log size so it does not become not responsive on huge directory copying
  • Loading branch information
x8699876 committed Jun 29, 2018
1 parent 3a778bc commit 577dffa
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 137 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target
.idea/workspace.xml
dist/Fastcopy(1.0.4).app/
dist/tmp_make_exe/
dist/Fastcopy(1.1).app/
4 changes: 2 additions & 2 deletions .idea/ant.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 4 additions & 14 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions FastCopy.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand All @@ -9,7 +9,7 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="Oracle JDK 1.8" jdkType="JavaSDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.github.fracpete:vfsjfilechooser2:0.2.7" level="project" />
<orderEntry type="library" name="Maven: junit:junit:3.8.2" level="project" />
Expand Down
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

<property name="AppVersion" value="1.0.4"/>
<property name="AppVersion" value="1.1"/>
<property name="repository.dir" value="~/repository"/>

<!-- set the operating system test properties -->
Expand Down
Binary file modified dist/fastcopy-console.jar
Binary file not shown.
Binary file modified dist/fastcopy-ui.exe
Binary file not shown.
Binary file modified dist/fastcopy-ui.jar
Binary file not shown.
Binary file modified dist/fc.exe
Binary file not shown.
4 changes: 1 addition & 3 deletions src/main/java/org/mhisoft/fc/CopyFileThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ public class CopyFileThread implements Runnable {
static final int TRIGGER_MULTI_THREAD_THRESHHOLD = 20;

private String dir;
private boolean verbose;
private FileCopyStatistics statistics;
private UI rdProUI;

File sourceFile;
File targetFile;


public CopyFileThread(UI rdProUI, File sourceFile, File targetFile, boolean verbose, FileCopyStatistics frs) {
public CopyFileThread(UI rdProUI, File sourceFile, File targetFile, FileCopyStatistics frs) {
this.sourceFile = sourceFile;
this.targetFile = targetFile;
this.verbose = verbose;
this.statistics = frs;
this.rdProUI = rdProUI;
}
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/org/mhisoft/fc/FastCopy.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
public class FastCopy {
public static boolean debug = Boolean.getBoolean("debug");
public static final int DEFAULT_THREAD_NUM =5;
public static final int DEFAULT_THREAD_NUM = 5;

FileCopyStatistics frs = new FileCopyStatistics();
Workers workerPool;
Expand All @@ -54,7 +54,7 @@ public FileCopyStatistics getStatistics() {
return frs;
}

public static boolean stopThreads=false;
public static boolean stopThreads = false;
private boolean running;

public static boolean isStopThreads() {
Expand Down Expand Up @@ -87,15 +87,15 @@ public void run(RunTimeProperties props) {
FileWalker fw = new FileWalker(rdProUI, workerPool, props, frs);
long t1 = System.currentTimeMillis();

running= true;
running = true;

String[] files = props.sourceDir.split(";");
fw.walk( files, props.getDestDir());
fw.walk(files, props.getDestDir());

workerPool.shutDownandWaitForAllThreadsToComplete();

//reset the flags
running= false;
running = false;
stopThreads = false;

rdProUI.println("");
Expand All @@ -109,11 +109,14 @@ public void run(RunTimeProperties props) {
public static void main(String[] args) {
FastCopy fastCopy = new FastCopy(new ConsoleRdProUIImpl());
RunTimeProperties props = fastCopy.getRdProUI().parseCommandLineArguments(args);
if (!props.isSuccess()) {
System.exit(-1);
}

if (props.isDebug())
fastCopy.getRdProUI().dumpArguments(args, props);

if (props.getSourceDir()!=null) {
if (props.getSourceDir() != null) {
Path path = Paths.get(props.getSourceDir());
if (Files.notExists(path)) {
fastCopy.getRdProUI().printError("The source dir does not exist:" + props.getSourceDir());
Expand All @@ -123,19 +126,16 @@ public static void main(String[] args) {

boolean b = fastCopy.getRdProUI().isAnswerY(
"Start to copy everything under \"" + props.getSourceDir() + "\"" +
"to \"" + props.getDestDir() + "\"" +
" to \"" + props.getDestDir() + "\"" +
" (y/n/q or h for help)?");


if (!b)
System.exit(-2);
}


if (props.isSuccess()) {
fastCopy.getRdProUI().print("working.");
fastCopy.run(props);
}
fastCopy.getRdProUI().print("working.");
fastCopy.run(props);
}

}
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/org/mhisoft/fc/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
*/
public class FileUtils {



public static void createDir(final File theDir, final UI ui, final FileCopyStatistics frs) {
// if the directory does not exist, create it
if (!theDir.exists()) {
Expand All @@ -55,7 +53,8 @@ public static void createDir(final File theDir, final UI ui, final FileCopyStati
ui.println(String.format("[error] Failed to create directory: %s", theDir.getName()));
}
if (result) {
ui.println(String.format("Directory created: %s", theDir.getName()));
if (RunTimeProperties.instance.isVerbose())
ui.println(String.format("Directory created: %s", theDir.getName()));
frs.setDirCount(frs.getDirCount() + 1);
}
}
Expand All @@ -76,7 +75,7 @@ private static void copyFileUsingFileChannels(File source, File dest)
}
}

private static final int BUFFER = 4096*16;
private static final int BUFFER = 4096 * 16;
static final DecimalFormat df = new DecimalFormat("#,###.##");

public static void showPercent(final UI rdProUI, double digital) {
Expand All @@ -99,21 +98,22 @@ public static void nioBufferCopy(final File source, final File target, FileCopyS
out = new FileOutputStream(target).getChannel();
totalFileSize = in.size();
//double size2InKB = size / 1024 ;
rdProUI.print(String.format("\nCopying file %s-->%s, size:%s KBytes", source.getAbsolutePath(),
target.getAbsolutePath(), df.format(totalFileSize/1024)));
if (RunTimeProperties.instance.isVerbose())
rdProUI.print(String.format("\n\tCopying file %s-->%s, size:%s KBytes", source.getAbsolutePath(),
target.getAbsolutePath(), df.format(totalFileSize / 1024)));

ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER);
int readSize = in.read(buffer);
long totalRead = 0;
int progress = 0;

long startTime, endTime ;
long startTime, endTime;

while (readSize != -1) {

if (FastCopy.isStopThreads()) {
rdProUI.println("[warn]Cancelled by user. Stoping copying.");
rdProUI.println("\t" + Thread.currentThread().getName() + "is stopped.");
rdProUI.println("[warn]Cancelled by user. Stoping copying.", true);
rdProUI.println("\t" + Thread.currentThread().getName() + "is stopped.", true);
return;
}
startTime = System.currentTimeMillis();
Expand All @@ -137,7 +137,7 @@ public static void nioBufferCopy(final File source, final File target, FileCopyS
statistics.addToTotalFileSizeAndTime(totalFileSize, readSize / 1024, (endTime - startTime));
}

statistics.setFilesCount(statistics.getFilesCount()+1);
statistics.setFilesCount(statistics.getFilesCount() + 1);


} catch (IOException e) {
Expand All @@ -146,7 +146,7 @@ public static void nioBufferCopy(final File source, final File target, FileCopyS
close(in);
close(out);
try {
boolean b = target.setLastModified( source.lastModified());
boolean b = target.setLastModified(source.lastModified());
//rdProUI.println("modify file date to: " + b + "," + new Timestamp(target.lastModified()));
} catch (Exception e) {
e.printStackTrace();
Expand Down
45 changes: 28 additions & 17 deletions src/main/java/org/mhisoft/fc/FileWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
*/
public class FileWalker {

RunTimeProperties props;
//Integer threads;
boolean lastAnsweredDeleteAll = false;
boolean initialConfirmation = false;
Expand All @@ -46,16 +45,16 @@ public FileWalker(UI rdProUI,
, FileCopyStatistics frs
) {
this.workerPool = workerPool;
this.props = props;
this.rdProUI = rdProUI;
this.statistics = frs;
rdProUI.reset();
}


public void walk(final String[] sourceFileDirs, final String destDir) {
public void walk(final String[] sourceFileDirs, String destDir) {

FileUtils.createDir(new File(destDir), rdProUI, statistics);
rdProUI.println("Copying files under directory " + destDir);

for (String source : sourceFileDirs) {

Expand All @@ -69,17 +68,24 @@ public void walk(final String[] sourceFileDirs, final String destDir) {
String sTarget = destDir + File.separator + fSource.getName();
File targetFile = new File(sTarget);
if (overrideTargetFile(fSource, targetFile)) {
CopyFileThread t = new CopyFileThread(rdProUI, fSource, targetFile, props.verbose, statistics);
CopyFileThread t = new CopyFileThread(rdProUI, fSource, targetFile, statistics);
workerPool.addTask(t);
} else
} else {
if (RunTimeProperties.instance.isVerbose())
rdProUI.println(String.format("\tFile %s exists on the target dir. Skip. ", sTarget));
}
} else if (fSource.isDirectory()) {
// get the last dir of the source and make it under dest
//ext /Users/me/doc --> /Users/me/target make /Users/me/target/doc
String _targetDir =destDir;
if (props.isCreateTheSameSourceFolderUnderTarget()) {
_targetDir=destDir+File.separator+fSource.getName() ;
FileUtils.createDir(new File(_targetDir), rdProUI, statistics);
if (RunTimeProperties.instance.isCreateTheSameSourceFolderUnderTarget()) {
//String sourceDirName =

_targetDir=destDir+File.separator + fSource.getName() ;
if (!new File(_targetDir).exists())
FileUtils.createDir(new File(_targetDir), rdProUI, statistics);
destDir = _targetDir;
RunTimeProperties.instance.setDestDir(destDir);
}
walkSubDir(fSource, destDir);
}
Expand All @@ -90,9 +96,9 @@ public void walk(final String[] sourceFileDirs, final String destDir) {

private boolean overrideTargetFile(final File srcFile, final File targetFile) {
if (targetFile.exists()) { //target file exists
if (props.overwrite) {
if (RunTimeProperties.instance.overwrite) {
return true;
} else if (props.isOverwriteIfNewerOrDifferent()) {
} else if (RunTimeProperties.instance.isOverwriteIfNewerOrDifferent()) {
if (srcFile.lastModified() > targetFile.lastModified()
|| (srcFile.length() != targetFile.length())
)
Expand All @@ -113,8 +119,10 @@ private boolean overrideTargetFile(final File srcFile, final File targetFile) {

public void walkSubDir(final File rootDir, final String destRootDir) {



if (FastCopy.isStopThreads()) {
rdProUI.println("[warn]Cancelled by user. stop walk. ");
rdProUI.println("[warn]Cancelled by user. stop walk. ", true);
return;
}

Expand All @@ -140,22 +148,24 @@ public boolean accept(File dir, String name) {
return;


rdProUI.println("Copying files under directory " + destRootDir);

for (File childFile : list) {

if (FastCopy.isStopThreads()) {
rdProUI.println("[warn]Cancelled by user. stop walk. ");
rdProUI.println("[warn]Cancelled by user. stop walk. ", true);
return;
}

if (childFile.isDirectory()) {
//keep walking down

if (!props.flatCopy) {
if (!RunTimeProperties.instance.flatCopy) {
//create the mirror child dir
targetDir = destRootDir + File.separator + childFile.getName();
FileUtils.createDir(new File(targetDir), rdProUI, statistics);
} else {
targetDir = props.getDestDir();
targetDir = RunTimeProperties.instance.getDestDir();
}

walkSubDir(childFile, targetDir);
Expand All @@ -164,19 +174,20 @@ public boolean accept(File dir, String name) {
else {


if (!props.flatCopy) {
if (!RunTimeProperties.instance.flatCopy) {
targetDir = destRootDir ;
} else {
targetDir = props.getDestDir();
targetDir = RunTimeProperties.instance.getDestDir();
}


String newDestFile = targetDir + File.separator + childFile.getName();
File targetFile = new File(newDestFile);
if (overrideTargetFile(childFile, targetFile)) {
CopyFileThread t = new CopyFileThread(rdProUI, childFile, targetFile, props.verbose, statistics);
CopyFileThread t = new CopyFileThread(rdProUI, childFile, targetFile, statistics);
workerPool.addTask(t);
} else {
if (RunTimeProperties.instance.isVerbose())
rdProUI.println(String.format("\tFile %s exists on the target dir. Skip based on the input. ", newDestFile));
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/mhisoft/fc/RunTimeProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
* Run time properties
*/
public class RunTimeProperties {

public static RunTimeProperties instance = new RunTimeProperties();

private RunTimeProperties() {
//
}

String sourceDir = null;
String destDir = null;
boolean success;
Expand Down
Loading

0 comments on commit 577dffa

Please sign in to comment.