RSync - Merging Files
Jakob Jenkov |
Once the differences between the new and the old files are detected, the old file needs to be merged with the changes from the new file, to create a copy of the new version of the file.
As you know, the computer containing the old version of the file, receives an instruction stream on how to construct the new version of the file. The instruction stream contains references to blocks in the old file that has not changed in the new version, as well as the sequences of data that are new.
To merge the old file with the new data sequences, the computer holding the old file iterates the instruction stream. When a block reference is encountered in the instruction stream, the corresponding block of data is copied from the old file to the new file. When a sequence of data is encountered in the instruction stream, that data is copied directly to the new file. This process continues until the end of the instruction stream. The new version of the file is then complete.
Here is a simple pseudo code outline of the process:
while( stream.hasMoreData() ) { if( stream.isBlockReference() ) { copyOldBlock(newFileOutputStream, stream.getBlockReference()); } else if( stream.isDataSequence() ) { copyDataSequence(newFileOutputStream, stream); } stream.moveToNext(); }
Tweet | |
Jakob Jenkov |