My favorites
▼
|
Sign in
syncr
Simple program to synchronise directories fast
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
de.xam.syncr
/
src
/
main
/
java
/
de
/
xam
/
syncr
/
Syncr.java
‹r4
r5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package de.xam.syncr;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author voelkel
*
* TODO preserve flags
*
* FIXME preserve timestamps
*
* TODO propagate deletion of files, too
*
* TODO write info file to targetRoot about sync date + job
*
* TODO add timeouts when reading a file takes too long
*
* FIXME uses too much memory sometimes - probably when copying really
* large files
*
*/
public class Syncr {
private static final long KB = 1024;
private static final long MB = 1024 * KB;
@SuppressWarnings("unused")
private static final long GB = 1024 * MB;
private static Logger log = LoggerFactory.getLogger(Syncr.class);
public static Set<String> skipFilenames = new HashSet<String>();
// FIXME
public static Set<String> skipDirnames = new HashSet<String>();
public static void main(String[] args) {
// example
// sync("C:/", "D:/_syncr");
}
/** FIXME 5 x 18 hours */
// TODO find out how change dates are stored in zip files - maybe a metadata
// standard
public static long skipAge = 5 * 18 * 60 * 60 * 1000;
public static long sleepMsAfterFileCopy = 0;// 100;
public static long sleepMsAfterFileRead = 0;// 10;
private static long dirProcessed = 0;
private static long kb_written = 0;
private static long kb_read = 0;
private static long start = 0;
private static long lastStats = 0;
/**
* Makes sure that all files residing at sourceDir are also present at
* targetDir. If files in sourceDir have a more recent changeDate than the
* one in targetDir, the file is copied again.
*
* @param sourceDirName
* @param targetDirName
* @param archiveDeleted if true, files deleted on 'source' are moved to a
* special folder __deleted. So in effect, they are not really
* deleted.
* @param compressSomeFolders if true, eclipse projects end up in a zip
* file. This results in much faster writes, since writing a
* single zip file is faster than writing 1000 of small files.
* @throws IOException
*/
public static void sync(String sourceDirName, String targetDirName, boolean archiveDeleted,
boolean compressSomeFolders) throws IOException {
skipFilenames.addAll(Arrays.asList(new String[] { "hiberfil.sys", "pagefile.sys",
"Thumbs.db", "autorun.inf", "UsrClass.dat", "UsrClass.dat.LOG", "ntuser.dat.LOG",
"ntuser.dat", "parent.lock" /* firefox */}));
skipDirnames.addAll(Arrays.asList(new String[] { "Temp", "RECYCLER",
"System Volume Information", "Google Desktop" }));
start = System.currentTimeMillis();
System.out.println("Start " + new Date());
System.out.println("Config: sleepMsAfterFileCopy=" + sleepMsAfterFileCopy
+ " sleepMsAfterFileRead=" + sleepMsAfterFileRead);
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
File source = new File(sourceDirName);
if(!source.exists()) {
log.error("Source dir " + source.getAbsolutePath() + " does not exist");
}
File target = new File(targetDirName);
if(!target.exists()) {
target.mkdirs();
log.info("Target dir " + target.getAbsolutePath() + " has been created");
}
syncDirectory(source, source, target, archiveDeleted, compressSomeFolders);
System.out.println("Done at " + new Date());
printStats();
}
private static long runningSeconds() {
long stop = System.currentTimeMillis();
long seconds = ((stop - start) / 1000);
return seconds;
}
private static void printStats() {
long seconds = runningSeconds() + 1;
log.info("Speed: " + (kb_read / seconds) + " KB/s, " + (dirProcessed / seconds)
+ " directory/s. " + seconds + "s. " + filesCopied + " files. In "
+ (kb_read / 1024) + " MB. Out " + (kb_written / 1024) + " MB.");
}
private static void printStats(File dir) {
printStats();
log.info("Processed " + dirProcessed + " directories. Syncing " + dir.getAbsolutePath());
}
/**
* @param sourceRoot
* @param sourceDir
* @param targetRoot
* @param archiveDeleted TODO
* @param compressSomeFolders TODO
* @return true if any write operations happened in this directory or
* subdirectories thereof
* @throws IOException
*/
public static boolean syncDirectory(File sourceRoot, File sourceDir, File targetRoot,
boolean archiveDeleted, boolean compressSomeFolders) throws IOException {
assert sourceDir != null;
assert sourceDir.isDirectory() : "sourcedir " + sourceDir.getAbsolutePath()
+ " is not a directoy";
dirProcessed++;
if(isExclude(sourceDir)) {
log.debug("Excluded: " + sourceDir.getAbsolutePath());
return false;
}
if(compressSomeFolders && shouldZipDirOnSyncDrive(sourceDir)) {
// create target zip file
File targetZipFile = new File(targetRoot, getLocalName(sourceRoot, sourceDir) + ".zip");
log.debug("Syncing to zip file " + targetZipFile.getAbsolutePath());
syncDirectoryToZipFile(sourceRoot, sourceDir, targetRoot, sourceDir, targetZipFile);
return true;
}
/** normal copy */
log.trace("Syncing " + sourceDir.getAbsolutePath());
// print stats every 30 seconds
long runtime = runningSeconds();
if(runtime - lastStats >= 30) {
lastStats = runtime;
printStats(sourceDir);
}
// if(dirProcessed % 100 == 0) {
// printStats(sourceDir);
// }
// TODO speed up with a local database of synced files
File targetDir = targetDir(sourceRoot, sourceDir, targetRoot);
// backup empty directories
targetDir.mkdirs();
File[] sourceFiles = sourceDir.listFiles();
boolean targetDirChanged = false;
if(sourceFiles == null) {
log.warn("Could not process (or even list) files in " + sourceDir.getAbsolutePath());
} else {
Set<String> syncedSourceFileNames = new HashSet<String>();
for(File sourceFile : sourceFiles) {
syncedSourceFileNames.add(sourceFile.getName());
if(!sourceFile.exists()) {
log.warn("While Syncr is running, this file has been removed: "
+ sourceFile.getAbsolutePath());
} else if(sourceFile.isFile()) {
File targetFile = targetFile(sourceRoot, sourceFile, targetRoot);
if(shouldCopy(sourceFile, targetFile)) {
copy(sourceFile, targetFile);
targetDirChanged = true;
try {
if(sleepMsAfterFileCopy > 0)
Thread.sleep(sleepMsAfterFileCopy);
} catch(InterruptedException e) {
}
}
try {
if(sleepMsAfterFileRead > 0)
Thread.sleep(sleepMsAfterFileRead);
} catch(InterruptedException e) {
}
} else if(sourceFile.isDirectory()) {
targetDirChanged = syncDirectory(sourceRoot, sourceFile, targetRoot,
archiveDeleted, compressSomeFolders);
} else {
log.warn("Cannot copy " + sourceFile.getAbsolutePath()
+ " - it is neither a file, nor a directory");
}
}
if(archiveDeleted) {
// move files in target that are no longer in sourceDir
File targetFiles[] = targetDir.listFiles();
if(targetFiles != null) {
File backupRoot = new File(targetRoot, "__deleted");
boolean localBackupRootCreated = false;
for(File targetFile : targetFiles) {
if(!localBackupRootCreated) {
backupRoot.mkdirs();
localBackupRootCreated = true;
}
if(!syncedSourceFileNames.contains(targetFile.getName())) {
File backupFile = targetFile(targetRoot, targetFile, backupRoot);
backupFile.getParentFile().mkdirs();
targetFile.renameTo(backupFile);
}
}
}
}
if(targetDirChanged) {
targetDir.setLastModified(
/**
* fix for: 'java.lang.IllegalArgumentException: Negative time
* at java.io.File.setLastModified(File.java:1222)'
*/
Math.max(sourceDir.lastModified(), 0));
}
}
return targetDirChanged;
}
/**
* Archive all files in sourceDir to the zipArchive targetZipFile
*
* @param sourceRoot
* @param sourceDir
* @param targetRoot
* @param sourceZippedDirectory TODO
* @param targetZipFile
* @throws IOException
* @throws IOException
* @throws ZipException
*/
private static void syncDirectoryToZipFile(File sourceRoot, File sourceDir, File targetRoot,
File sourceZippedDirectory, File targetZipFile) throws IOException {
targetZipFile.getParentFile().mkdirs();
// avoid creating the same zip file if we interrupted a backup session
if(targetZipFile.exists()) {
if(targetZipFile.lastModified() + skipAge >= System.currentTimeMillis()) {
log.debug("Skipping zip file creation for source dir "
+ sourceDir.getAbsolutePath());
return;
}
}
log.info("Zipping " + sourceDir.getAbsolutePath());
File targetZipFileTemp = new File(targetZipFile.getParentFile(), targetZipFile.getName()
+ ".temp");
try {
targetZipFileTemp.createNewFile();
FileOutputStream fos;
try {
fos = new FileOutputStream(targetZipFileTemp);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.setLevel(ZipOutputStream.DEFLATED);
syncDirectoryToZipOutputStream(sourceRoot, sourceZippedDirectory, sourceDir, zos);
try {
zos.close();
} catch(IOException e) {
log.error("", e);
}
try {
fos.close();
} catch(IOException e) {
log.error("", e);
}
// rename
targetZipFileTemp.renameTo(targetZipFile);
} catch(FileNotFoundException e) {
log.error("", e);
assert false;
}
} catch(IOException e) {
throw new RuntimeException("Could not create " + targetZipFileTemp.getAbsolutePath(), e);
} finally {
// delete temp file
targetZipFileTemp.deleteOnExit();
}
}
private static void syncDirectoryToZipOutputStream(File sourceRoot, File sourceZippedDirectory,
File sourceDir, ZipOutputStream zos) throws IOException {
if(isExclude(sourceDir)) {
log.debug("Excluded: " + sourceDir.getAbsolutePath());
return;
}
for(File sourceFile : sourceDir.listFiles()) {
if(sourceFile.isFile()) {
if(shouldArchive(sourceFile)) {
String zipEntryName = targetNameInZipFile(
sourceZippedDirectory.getParentFile(), sourceFile);
ZipEntry zipEntry = new ZipEntry(zipEntryName);
/* modification time */
zipEntry.setTime(sourceFile.lastModified());
zipEntry.setSize(sourceFile.length());
zipEntry.setComment("");
zos.putNextEntry(zipEntry);
copyFileToStream(sourceFile, zos);
zos.closeEntry();
// maybe sleep here
try {
Thread.sleep(sleepMsAfterFileRead);
} catch(InterruptedException e) {
}
}
} else {
// recurse
syncDirectoryToZipOutputStream(sourceRoot, sourceZippedDirectory, sourceFile, zos);
}
}
}
/** for Zip streams */
private static void copyFileToStream(File file, OutputStream os) throws IOException {
FileChannel inChannel = new FileInputStream(file).getChannel();
WritableByteChannel outChannel = Channels.newChannel(os);
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch(IOException e) {
throw e;
} finally {
if(inChannel != null)
inChannel.close();
// do not close out channel
}
kb_read += file.length() / 1024;
filesCopied++;
}
private static boolean shouldArchive(File sourceFile) {
assert sourceFile.isFile();
return !isSkipFileName(sourceFile.getName());
}
private static boolean isSkipFileName(String name) {
return skipFilenames.contains(name);
}
private static boolean isSkipDirName(String name) {
return skipDirnames.contains(name);
}
// FIXME kill
static long LARGE = 1 * 1024 * 1024 * 1024;
private static boolean shouldCopy(File sourceFile, File targetFile) {
return shouldArchive(sourceFile)
&& (!targetFile.exists() || targetFile.length() != sourceFile.length() || targetFile
.lastModified() < sourceFile.lastModified())
&& (sourceFile.length() <= LARGE);
}
private static long filesCopied = 0;
/** reuse buffer to allocate that memory only once */
private static byte[] buffer;
/**
* Create targetFile parent directories and copy sourceFile into targetFile
*
* @param sourceFile
* @param targetFile
* @throws IOException
*/
public static void copy(File sourceFile, File targetFile) {
// handle copying of very large files
long length = sourceFile.length();
if(length > 128 * MB) {
Progress p = new Progress();
p.start();
log.info("Copy large file with " + (length / MB) + " MB: "
+ sourceFile.getAbsolutePath());
FileInputStream fin;
try {
fin = new FileInputStream(sourceFile);
FileOutputStream fout = new FileOutputStream(targetFile);
if(buffer == null) {
buffer = new byte[(int)(8 * MB)];
}
long count = 0;
int n = 0;
try {
while(-1 != (n = fin.read(buffer))) {
p.tick();
if(p.intervalElapsed(20)) {
log.info("Writing block of " + n / MB + " MB, "
+ ((100 * count) / length) + "% done. Speed: "
+ ((count / MB) / (p.tick() / 1000d)) + " MB/s");
}
fout.write(buffer, 0, n);
count += n;
}
} catch(IOException e) {
log.warn("Could not copy large file " + sourceFile.getAbsolutePath()
+ " due to ", e);
targetFile.deleteOnExit();
}
fout.flush();
fout.close();
return;
} catch(FileNotFoundException e) {
log.warn("Cannot copy large file " + sourceFile.getAbsolutePath() + " due to ", e);
} catch(IOException e) {
log.warn("Cannot copy large file " + sourceFile.getAbsolutePath() + " due to ", e);
}
}
log.trace("Copy small file with just " + sourceFile.length() + " bytes");
try {
targetFile.getParentFile().mkdirs();
FileChannel inChannel = new FileInputStream(sourceFile).getChannel();
FileChannel outChannel = new FileOutputStream(targetFile).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch(IOException e) {
log.warn(e.getMessage(), e);
throw e;
} finally {
if(inChannel != null)
inChannel.close();
if(outChannel != null)
outChannel.close();
}
// preserve file date
targetFile.setLastModified(sourceFile.lastModified());
filesCopied++;
kb_read += sourceFile.length() / 1024;
kb_written += sourceFile.length() / 1024;
} catch(FileNotFoundException e) {
log.warn("Cannot copy " + sourceFile.getAbsolutePath() + " due to ", e);
} catch(IOException e) {
log.warn("Cannot copy " + sourceFile.getAbsolutePath() + " due to ", e);
}
}
/**
* @param sourceRoot
* @param sourceFile can also be a directory
* @param targetRoot
* @return a File representing the source File but with a modified path so
* that it is in the targetRoot
*/
private static File targetFile(File sourceRoot, File sourceFile, File targetRoot) {
return new File(targetRoot, getLocalName(sourceRoot, sourceFile));
}
private static File targetDir(File sourceRoot, File sourceDir, File targetRoot) {
return new File(targetRoot, getLocalName(sourceRoot, sourceDir) + "/");
}
/**
* @param rootDir
* @param file
* @return the local name of 'file' in 'rootDir', with a leading '/' or '\'
*/
public static String getLocalName(File rootDir, File file) {
return file.getAbsolutePath().substring(rootDir.getAbsolutePath().length());
}
/**
* @param zipFileRoot
* @param sourceFile
* @param targetRoot
* @return a File representing the source File but with a modified path so
* that it is in the targetRoot
*/
private static String targetNameInZipFile(File zipFileRoot, File sourceFile) {
return getLocalName(zipFileRoot, sourceFile).substring(1);
}
/**
* @param dir
* @return true if this directory should result in a zip archive on the sync
* drive
*/
public static boolean shouldZipDirOnSyncDrive(File dir) {
assert dir.isDirectory();
File[] fileList = dir.listFiles();
return isSoftwareProjectRootDir(fileList) | isEclipseWorkspaceMetadata(dir)
| containsZipMaxSyncFile(fileList);
}
private static boolean containsZipMaxSyncFile(File[] fileList) {
if(fileList == null) {
return false;
}
for(File f : fileList) {
String name = f.getName();
if(name.equals("zip.maxsync")) {
return true;
}
}
return false;
}
private static boolean isEclipseWorkspaceMetadata(File dir) {
return dir.getName().equals(".metadata");
}
/**
* @param dir
* @return true if directory should not be synced
*/
public static boolean isExclude(File dir) {
return isMavenTargetDir(dir) | isSkipDirName(dir.getName());
}
private static boolean isMavenTargetDir(File dir) {
return dir.getParentFile() != null
&& isSoftwareProjectRootDir(dir.getParentFile().listFiles())
&& dir.getName().equals("target");
}
/**
* Detect Eclipse projects
*
* @param dir
* @return
*/
public static boolean isSoftwareProjectRootDir(File[] fileList) {
if(fileList == null) {
return false;
}
for(File f : fileList) {
String name = f.getName();
if(name.equals(".classpath") || name.equals(".project")) {
return true;
}
}
return false;
}
public static void copyFilesLargerThan(File sourceRoot, File targetRoot, final long minSize) {
for(File f : sourceRoot.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.length() >= minSize;
}
})) {
File targetFile = targetFile(sourceRoot, f, targetRoot);
if(shouldCopy(f, targetFile)) {
copy(f, targetFile);
}
}
// recurse
for(File dir : sourceRoot.listFiles(new FileFilter() {
@Override
public boolean accept(File dir) {
return dir.isDirectory();
}
})) {
copyFilesLargerThan(dir, targetDir(sourceRoot, dir, targetRoot), minSize);
}
}
}
Show details
Hide details
Change log
r5
by max.at.xam.de on Feb 11, 2012
Diff
Improve copying large files
Go to:
.../java/de/xam/syncr/Progress.java
...ain/java/de/xam/syncr/Syncr.java
Project members,
sign in
to write a code review
Older revisions
r4
by max.at.xam.de on Oct 13, 2010
Diff
[No log message]
r3
by max.at.xam.de on Jun 28, 2010
Diff
Public release
All revisions of this file
File info
Size: 18980 bytes, 605 lines
View raw file
Powered by
Google Project Hosting