3

I am using rsync command-line tool to transfer files from a source folder to a destination folder as follows:

rsync -r  /source_folder /destination_folder

How can this be done programmatically in c++? I have looked into librync, which is supposed to do this. But i think it is not very well documented and dosent have examples to show how to sync two folders. Also I am looking for a way to do this preferably without using system()/popen().

Is there any other way to do this in c++? Thanks in advance!

vacky
  • 277
  • 1
  • 4
  • 16
  • Why using `system` doesn't work for you? – Maxim Egorushkin Oct 02 '18 at 15:11
  • 1
    Rsync is infamous for being badly documented as a library. So you either have to go through the code on your own or simply use system()/popen(). Note that rsync will more than likely completely dominate any overhead due to suprocess spawn. – freakish Oct 02 '18 at 15:12
  • Using system does work. But I am interested to do this in a proper way using a c++ library. Wont that be better than using system()? – vacky Oct 02 '18 at 15:17
  • Define "better". – freakish Oct 02 '18 at 15:19
  • 2
    Using library you will have to write more code with more risk of introducing programming errors, especially that as you mention it is not well documented. Whereas using `system` may be simpler and more robust. – Maxim Egorushkin Oct 02 '18 at 15:21
  • 2
    @vacky The process of "spawning a subprocess" costs some resources like cpu time and memory. The process of "using rsync" costs some resources as well. You always have to pay the other cost. But if you utilize librsync inside your code you don't have to pay the "spawning" cost. But this cost is more than likely insignificant compared to rsync's cost. – freakish Oct 02 '18 at 15:22
  • @freakish From what Iv read about system on different forums and posts, Iv think that using system should be avoided. May be I am wrong about it. Please let me know about it – vacky Oct 02 '18 at 15:23
  • 3
    @vacky system and popen calls depend on the underlying OS. There's nothing wrong in using them. It's just that it might not be portable. On the other hand it is unlikely that you will encounter a platform with broken system/popen but with working rsync. – freakish Oct 02 '18 at 15:27
  • 3
    `rsync` does not provide a library unfortunately (`librsync` is not quite the same, and infact notes on the GitHub page that it does not implement the rsync wire protocol). There may be another 3rd party implementation, otherwise shelling out is the most practical solution, unless you want to start a project to take the relevant code out of the CLI tool as a library. If you want portability rsync is a bit of a pain... It also means if you wanted a GUI or such showing status info that is a lot harder than it would be with a proper library... – Fire Lancer Oct 02 '18 at 15:30
  • 1
    If you do not need some of the advanced features of rsync, SSH client libraries are readily available with SFTP support, including being fairly easy to get running on MSVC/Windows if portability on the client side is desired. – Fire Lancer Oct 02 '18 at 15:33
  • @FireLancer Thanks for your input. Yes I agree librsync does not implement rsync wire protocol. But it can do the folder synchronization which is what i am interested in. – vacky Oct 02 '18 at 15:34

0 Answers0