2 Prueba de concepto de buscador de diferencias por bloques.
9 // CLIG - Command Line Interpreter Generator:
24 printf("Usage: \n"); usage();
28 hashblock cb_hash
[2][512];
29 int cb_nhash
[2]={0,0};
30 int cb_blocks
[2]={0,0};
31 int cb_deleted
[2]={0,0};
32 int cb_added
[2]={0,0};
34 void calcBytes(int nfile
,int bFrom
,int bTo
, int lFrom
, int lTo
)
36 char *baseFrom
=&basef
[bFrom
];
37 char *baseTo
=&basef
[bTo
];
42 localFrom
=&localf
[lFrom
];
47 localFrom
=&remotef
[lFrom
];
48 localTo
=&remotef
[lTo
];
52 hashblock
*hbchar
=cb_hash
[nfile
]+cb_nhash
[nfile
];
53 int totalsize
=(baseTo
-baseFrom
)+(localTo
-localFrom
);
55 if (debug
) printf("trying %d-%d %d-%d\n",bFrom
,bTo
,lFrom
,lTo
);
57 int nhbchar
= compare2bytevectors(
58 baseFrom
, baseTo
-baseFrom
,
59 localFrom
, localTo
-localFrom
,
61 hbchar
, 512-cb_nhash
[nfile
]);
65 for (j
=0;j
<nhbchar
;j
++)
67 hbchar
[j
].line1
+=bFrom
;
68 hbchar
[j
].line2
+=lFrom
;
69 totalsize1
+=hbchar
[j
].size
;
72 if (debug
) printf("-%d +%d (%d)*\n",
73 baseTo
-baseFrom
-totalsize1
,
74 localTo
-localFrom
-totalsize1
,nhbchar
);
76 cb_nhash
[nfile
]+=nhbchar
;
79 cb_blocks
[nfile
]+=nhbchar
;
80 cb_deleted
[nfile
]+=baseTo
-baseFrom
-totalsize1
;
81 cb_added
[nfile
]+=localTo
-localFrom
-totalsize1
;
86 int doBLRMerge(FILE *out
, char *base
, char *local
, char *remote
)
88 BLRintA split
=splitBLR(base
, local
, remote
);
90 if (debug
) fprintf( stderr
, "Base File: %d blocks found\n",split
.basesz
);
91 if (debug
) fprintf( stderr
, "Local File: %d blocks found\n",split
.localsz
);
92 if (debug
) fprintf( stderr
, "Remote File: %d blocks found\n",split
.remotesz
);
94 BLRintA hash
=hashBLR();
96 hashblock bloque_BR
[64];
97 int nbloquesBR
= compare2hashvectors(
98 hash
.base
, hash
.basesz
,
99 hash
.remote
, hash
.remotesz
,
103 hashblock bloque_BL
[64];
104 int nbloquesBL
= compare2hashvectors(
105 hash
.base
, hash
.basesz
,
106 hash
.local
, hash
.localsz
,
111 for (i
=0;i
<nbloquesBL
+1;i
++)
115 bloque_BL
[i
].line1
=split
.basesz
;
116 bloque_BL
[i
].line2
=split
.localsz
;
121 int b1Start
=bloque_BL
[i
-1].line1
+bloque_BL
[i
-1].size
;
122 int b2Start
=bloque_BL
[i
-1].line2
+bloque_BL
[i
-1].size
;
123 int offset1
=bloque_BL
[i
].line1
-b1Start
;
124 int offset2
=bloque_BL
[i
].line2
-b2Start
;
128 split
.base
[b1Start
+offset1
-1],
129 split
.local
[b2Start
],
130 split
.local
[b2Start
+offset2
-1]);
137 split
.base
[bloque_BL
[i
].line1
],
138 split
.base
[bloque_BL
[i
].line1
+bloque_BL
[i
].size
-1],
140 split
.local
[bloque_BL
[i
].line2
],
141 split
.local
[bloque_BL
[i
].line2
+bloque_BL
[i
].size
-1]
147 if (debug
) printf("Total bytes deleted: %d\n", cb_deleted
[0]);
148 if (debug
) printf("Total bytes added: %d\n", cb_added
[0]);
149 if (debug
) printf("Total blocks used: %d\n", cb_blocks
[0]);
151 if (debug
) printf("\n\n");
154 for (i
=0;i
<nbloquesBR
+1;i
++)
158 bloque_BR
[i
].line1
=split
.basesz
;
159 bloque_BR
[i
].line2
=split
.remotesz
;
164 int b1Start
=bloque_BR
[i
-1].line1
+bloque_BR
[i
-1].size
;
165 int b2Start
=bloque_BR
[i
-1].line2
+bloque_BR
[i
-1].size
;
166 int offset1
=bloque_BR
[i
].line1
-b1Start
;
167 int offset2
=bloque_BR
[i
].line2
-b2Start
;
168 if (debug
) printf("\n Orphan:");
171 split
.base
[b1Start
+offset1
-1],
173 split
.remote
[b2Start
],
174 split
.remote
[b2Start
+offset2
-1]
183 split
.base
[bloque_BR
[i
].line1
],
184 split
.base
[bloque_BR
[i
].line1
+bloque_BR
[i
].size
-1],
186 split
.remote
[bloque_BR
[i
].line2
],
187 split
.remote
[bloque_BR
[i
].line2
+bloque_BR
[i
].size
-1]
193 if (debug
) printf("Total bytes deleted: %d\n", cb_deleted
[1]);
194 if (debug
) printf("Total bytes added: %d\n", cb_added
[1]);
195 if (debug
) printf("Total blocks used: %d\n", cb_blocks
[1]);
197 if (debug
) printf("\n\n");
200 for (j=0;j<cb_nhash[0];j++) // Search blocks in local for that code:
202 int Bfrom0=cb_hash[0][j].line1;
203 int Bto0=cb_hash[0][j].line1+cb_hash[0][j].size-1;
205 printf("%d->%d\n",Bfrom0,Bto0);
209 // Sort remote blocks by remote byte count:
212 int nbloques
=cb_nhash
[1];
213 hashblock auxbloque
[nbloques
];
214 int minline
=0, min_j
=0;
215 for (p
=0;p
<nbloques
;p
++)
218 for (j
=0;j
<nbloques
;j
++)
220 if (cb_hash
[1][j
].line2
<minline
)
222 minline
=cb_hash
[1][j
].line2
;
226 auxbloque
[p
]=cb_hash
[1][min_j
];
227 cb_hash
[1][min_j
].line2
=remotesz
;
229 memcpy(cb_hash
[1],auxbloque
,nbloques
*sizeof(hashblock
));
233 // fwrite(localf,sizeof(char),localsz,out);
234 // fprintf(out,"\n<<<<< LOCAL <<<< \n");
236 for (i
=0;i
<cb_nhash
[1];i
++)
238 int from
=cb_hash
[1][i
].line2
;
239 int to
=cb_hash
[1][i
].line2
+cb_hash
[1][i
].size
-1;
242 // Modified data, only on remote. Copy from remote.
243 // fprintf(out,"\n<<<<< REMOTE <<<< %d bytes\n",from-fcursor);
244 if (debug
) fprintf(out
,"|C>");
245 fwrite(&remotef
[fcursor
],sizeof(char),from
-fcursor
,out
);
246 if (debug
) fprintf(out
,"<C|");
247 // fprintf(out,"\n>>>>> REMOTE >>>>>>>>>>>\n");
253 // Original data: read from base, and translate to local.
254 // fprintf(out,"/*");
255 int Bfrom
=cb_hash
[1][i
].line1
;
256 int Bto
=cb_hash
[1][i
].line1
+cb_hash
[1][i
].size
-1;
258 // printf("**** %d %d->%d\n",i,Bfrom,Bto);
259 for (j
=0;j
<cb_nhash
[0];j
++) // Search blocks in local for that code:
261 int Bfrom0
=cb_hash
[0][j
].line1
;
262 int Bto0
=cb_hash
[0][j
].line1
+cb_hash
[0][j
].size
-1;
264 int f
=(Bfrom0
>Bfrom
)? Bfrom0
:Bfrom
;
265 int t
=(Bto0
<Bto
)? Bto0
:Bto
;
269 if (debug
) fprintf(out
,"|A>");
270 fwrite(&basef
[f
],sizeof(char),t
-f
,out
);
271 if (debug
) fprintf(out
,"<A|");
273 if (t
==Bto0
&& t
<Bto
)
275 int From2
=cb_hash
[0][j
].line2
+cb_hash
[0][j
].size
-1;
276 int To2
=cb_hash
[0][j
+1].line2
;
278 if (debug
) fprintf(out
,"|B>");
279 fwrite(&localf
[From2
],sizeof(char),To2
-From2
,out
);
280 if (debug
) fprintf(out
,"<B|");
286 // fprintf(out,"*/");
289 fwrite(&remotef[fcursor],sizeof(char),to-fcursor,out);
290 fprintf(out,"<C2|");*/
297 // fwrite(ptr,sizeof(char),size,out);
303 int main(int argc
, char **argv
) {
304 // Parse the command line args.
305 Cmdline
*cmd
= parseCmdline(argc
, argv
);
307 if (cmd
->show_helpP
) showUsage();
309 if (cmd
->do_blrmergeP
)
313 if (cmd
->output_fileP
)
315 of1
=fopen(cmd
->output_file
,"w");
319 result
= doBLRMerge( of1
,
322 cmd
->do_blrmerge
[2]);
324 if (cmd
->output_fileP
)
330 if (cmd
->test_pluginP
)
332 printf("\n Intentando cargar el plugin... \n");
333 if (loadsplitplugin())
335 printf("\n - El plugin se cargó correctamente -\n");
339 printf("\n ** ERROR \n");
343 if (cmd
->do_hashcmpP
)
346 int nhashes
=cmd
->do_hashcmpC
-1;
348 unsigned int *int_hash
=malloc((nhashes
)*sizeof(unsigned int));
349 fprintf( stderr
, "nhashes: %d\n",nhashes
);
350 for (i
=1;i
<cmd
->do_hashcmpC
;i
++)
352 int_hash
[i
-1]=ihash(cmd
->do_hashcmp
[i
]);
355 for (i
=0;i
<nhashes
-block_size
+1;i
++)
357 for (b
=0;b
<block_size
;b
++)
359 printf( "%08X", int_hash
[i
+b
]);
367 printf("* Nothing to do.\n");