priya
priya

Reputation: 33

c code to find differences using tar

I need to write a C program for finding the differences between two folders, folder1 and folder2.tar and update the contents of folder2.tar with folder1. So I have written using tar --diff utility command like below:

#include <unistd.h>
#include <stdio.h>
main()
{
system("tar df folder2.tar folder1 > file.patch");
system("tar uf folder2 folder1"); //Assume folder2 is the extracted version of folder2.tar
}

Here I have few questions:

  1. Firstly, I am not able to get the differences in contents of folders into the separate file. I am getting only the modified filename in the folder and time modified
  2. Secondly, as will be many files in the folders, and after modifying a random file in folder1, how do we specify the path to update only that specific file in folder2.tar?
  3. How do we write the code for the above scenario?

Any idea !!!!! Please help me if you have so ! Thanks

Upvotes: 0

Views: 292

Answers (1)

alexis
alexis

Reputation: 50210

Try this:

tar xf folder2.tar
diff -rq folder1 folder2

You can then get the modified filenames from the output of diff and pass them to tar -u. Feel free to wrap it in system() so you can call it a C program, but I'd advise you to make it a bash script: Smaller, faster and you don't need to recompile when you make changes.

Upvotes: 1

Related Questions