-rw-r--r-- | rsync/qrsync.cpp | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/rsync/qrsync.cpp b/rsync/qrsync.cpp new file mode 100644 index 0000000..fe5f1bc --- a/dev/null +++ b/rsync/qrsync.cpp | |||
@@ -0,0 +1,110 @@ | |||
1 | #include "qrsync.h" | ||
2 | #include <stdio.h> | ||
3 | extern "C" { | ||
4 | #include "rsync.h" | ||
5 | } | ||
6 | #include <stdlib.h> | ||
7 | |||
8 | #include <qfile.h> | ||
9 | |||
10 | static const char *rdiffNewFile = "/tmp/rdiff/result"; | ||
11 | static size_t block_len = RS_DEFAULT_BLOCK_LEN; | ||
12 | static size_t strong_len = RS_DEFAULT_STRONG_LEN; | ||
13 | |||
14 | |||
15 | void QRsync::generateSignature( QString baseFile, QString sigFile ) | ||
16 | { | ||
17 | |||
18 | if ( QFile::exists( baseFile ) ) { | ||
19 | FILE *basis_file, *sig_file; | ||
20 | rs_result result; | ||
21 | |||
22 | basis_file = fopen( baseFile.latin1(), "rb" ); | ||
23 | sig_file = fopen( sigFile.latin1(), "wb" ); | ||
24 | |||
25 | result = rs_sig_file(basis_file, sig_file, block_len, strong_len, 0); | ||
26 | |||
27 | fclose( basis_file ); | ||
28 | fclose( sig_file ); | ||
29 | if (result != RS_DONE) | ||
30 | printf("error in rdiffGenSig: %d", result ); | ||
31 | } | ||
32 | } | ||
33 | |||
34 | |||
35 | void QRsync::generateDiff( QString baseFile, QString sigFile, QString deltaFile ) | ||
36 | { | ||
37 | if ( QFile::exists( baseFile ) && QFile::exists( sigFile ) ) { | ||
38 | FILE *sig_file, *new_file, *delta_file; | ||
39 | rs_result result; | ||
40 | rs_signature_t *sumset; | ||
41 | |||
42 | sig_file = fopen(sigFile.latin1(), "rb"); | ||
43 | new_file = fopen(baseFile.latin1(), "rb"); | ||
44 | delta_file = fopen(deltaFile.latin1(), "wb"); | ||
45 | |||
46 | result = rs_loadsig_file(sig_file, &sumset, 0); | ||
47 | if (result != RS_DONE) { | ||
48 | qDebug( "rdiffGenDiff: loading of sig file failed, error=%d", result ); | ||
49 | } else { | ||
50 | result = rs_build_hash_table(sumset); | ||
51 | if ( result != RS_DONE) { | ||
52 | qDebug( "rdiffGenDiff: building of hash table failed, error=%d", result ); | ||
53 | } else { | ||
54 | result = rs_delta_file(sumset, new_file, delta_file, 0); | ||
55 | if ( result != RS_DONE) { | ||
56 | qDebug( "rdiffGenDiff: writing of diff file failed, error=%d", result ); | ||
57 | } | ||
58 | } | ||
59 | } | ||
60 | |||
61 | if ( sumset ) | ||
62 | rs_free_sumset( sumset ); | ||
63 | fclose( new_file ); | ||
64 | fclose( delta_file ); | ||
65 | fclose( sig_file ); | ||
66 | |||
67 | } | ||
68 | } | ||
69 | |||
70 | void QRsync::applyDiff( QString baseFile, QString deltaFile ) | ||
71 | { | ||
72 | if ( QFile::exists( baseFile ) && QFile::exists( deltaFile ) ) { | ||
73 | FILE *basis_file, *delta_file, *new_file; | ||
74 | rs_result result; | ||
75 | |||
76 | basis_file = fopen(baseFile.latin1(), "rb"); | ||
77 | delta_file = fopen(deltaFile.latin1(), "rb"); | ||
78 | #ifdef Q_WS_WIN | ||
79 | new_file = fopen( (baseFile + ".new").latin1(), "wb" ); | ||
80 | #else | ||
81 | new_file = fopen(rdiffNewFile, "wb"); | ||
82 | #endif | ||
83 | |||
84 | result = rs_patch_file(basis_file, delta_file, new_file, 0); | ||
85 | |||
86 | fclose( basis_file ); | ||
87 | fclose( delta_file ); | ||
88 | fclose( new_file ); | ||
89 | |||
90 | if (result != RS_DONE) { | ||
91 | qDebug( "rdiffApplyDiff failed with result %d", result ); | ||
92 | return; | ||
93 | } | ||
94 | |||
95 | |||
96 | #ifdef Q_WS_WIN | ||
97 | QDir dir; | ||
98 | QFile backup = baseFile + "~"; | ||
99 | dir.rename( baseFile, backup ); | ||
100 | dir.rename( (baseFile + ".new"), baseFile ); | ||
101 | dir.remove( backup ); | ||
102 | #else | ||
103 | QString cmd = "mv "; | ||
104 | cmd += rdiffNewFile; | ||
105 | cmd += " " + baseFile; | ||
106 | system( cmd.latin1() ); | ||
107 | #endif | ||
108 | } | ||
109 | |||
110 | } | ||