csegmentation-faultrsynclibrsync

Segmentation fault - while using librsync library


I have two base file filea.txt and fileb.txt. Trying to create signature, then delta using signature and fileb.txt, then patch filea.txt with that delta. Resulting file's contents should be same as fileb.txt

First part creates signature file from filea.txt:

#include <stdlib.h>
#include <stdio.h>
#include "librsync.h"

int main(){
    FILE *fpa;
    fpa = fopen("filea.txt","r");

    FILE *fps;
    fps = fopen("sig.sig","w+");


    rs_result res = rs_sig_file(fpa, fps, 1,2,NULL);

    fclose(fpa);
    fclose(fps);

    printf("Result code: %d\n", res);

    return 0;
}

Then the second one is supposed to use that signature with fileb.txt file to create delta file

#include <stdlib.h>
#include <stdio.h>
#include "librsync.h"


int main(){
    FILE *fpb;
    fpb = fopen("fileb.txt", "r");

    FILE *fpd;
    fpd = fopen("delta.delt","w+");

    FILE *sigFile;
    sigFile = fopen("sig.sig","r");

    rs_signature_t *signature;

    rs_loadsig_file(sigFile, &signature, NULL);

    rs_result res = rs_delta_file(signature, fpb, fpd, NULL);

    printf("Result: %d", res);

    fclose(fpb); fclose(fpd); fclose(sigFile);

    return 0;
}

I'm compiling both of them like this gcc -o delta create_delta.c -Wall -g -lrsync

But the second part gives Segmentation fault error.

Maybe because of this, third executable produces empty file:

int main() {
    FILE *fpd;
    fpd = fopen("delta.delt", "r");

    FILE *fpa;
    fpa = fopen("filea.txt", "r");

    FILE *fpn;
    fpn = fopen("file_new.txt", "w+");

    rs_patch_file(fpa, fpd, fpn, NULL);

    fclose(fpd); fclose(fpa); fclose(fpn);

    return 0;
}

UPDATE Tried checking file pointers for null after fopen didn't catch any errors.


Solution

  • After looking at https://rproxy.samba.org/doxygen/librsync/rsync_h.html i figured you have to use

    rs_build_hash_table(signature)

    before calling

    rs_delta_file(signature, fpb, fpd, NULL);

    Works fine for me then.