faubackup2/src/copier distributor.cpp,1.15,1.16 distributor.hpp,1.10,1.11

Erik Merkel <[email protected]> Fri, 04 Nov 2005 18:28:55 +0000
Newsgroups gmane.comp.sysutils.backup.faubackup.cvs
Message-ID <[email protected]>
Update of /cvsroot/faubackup/faubackup2/src/copier
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13436

Modified Files:
	distributor.cpp distributor.hpp 
Log Message:
adding comments + cleanup


Index: distributor.cpp
===================================================================
RCS file: /cvsroot/faubackup/faubackup2/src/copier/distributor.cpp,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** distributor.cpp	22 Sep 2005 16:43:24 -0000	1.15
--- distributor.cpp	4 Nov 2005 18:28:53 -0000	1.16
***************
*** 13,23 ****
--- 13,28 ----
  extern int do_nothing;
  
+ /* sends command to Filesender if file has been changed on client. 
+    Returns true if a transfer is scheduled - false if the file does exist
+    on server and transfer mode is keep existing file only */
  bool Distributor::file_changed(Fileinfo &file)
  {
  	if(file.getBackupMode() == KeepExisting) {
+ 		/* just link file - do not transfer */
  		file_unchanged(file);
  		return false;
  	}
  	if(do_nothing) {
+ 		/* only show what to do */
  		output.information(F_("transfer file:\t%s")
  				   % file.getFilename(), DEBUG_OUTPUT);
***************
*** 26,29 ****
--- 31,36 ----
  	struct stat st;
  	getStat(old_dir + file.getFilename(), st);
+ 	
+ 	/* only rsync if both file sizes are greater then MIN_RSYNC_SIZE */
  	if((st.st_size > MIN_RSYNC_SIZE) &&
  	    (file.getFileSize() > MIN_RSYNC_SIZE) &&
***************
*** 33,38 ****
--- 40,47 ----
  		network->sendByte(RdiffRegFile);
  		network->sendString(file.getFilename());
+ 		/* send rsync checksum from file on server */
  		checkSum(old_dir + file.getFilename(), st.st_size, *network);
  	} else {
+ 		/* do normal copy */
  		output.information(F_("copy file:\t%s %d")
  				   % file.getFilename() % rsync, DEBUG_OUTPUT);
***************
*** 43,46 ****
--- 52,56 ----
  	return true;
  }
+ 
  void Distributor::file_unchanged(Fileinfo &file)
  {
***************
*** 55,58 ****
--- 65,69 ----
  
  }
+ 
  bool Distributor::new_file(Fileinfo &file)
  {
***************
*** 72,77 ****
  	network->sendString(file.getFilename());
  	return true;
- 
  }
  void Distributor::no_reg_file(Fileinfo &file)
  {
--- 83,88 ----
  	network->sendString(file.getFilename());
  	return true;
  }
+ 
  void Distributor::no_reg_file(Fileinfo &file)
  {
***************
*** 81,86 ****
  		return;
  	}
! 	if(file.getBackupMode() == KeepExisting) {
! 		if( existFile(old_dir + file.getFilename()) ) {
  			if(file.getFileType() == SLink)
  				network->sendByte(SymlinkFile);
--- 92,98 ----
  		return;
  	}
! 	/* only transfer if file exists on server when KeepExisting is set */
! 	if(!((file.getBackupMode() == KeepExisting) 
! 		&& !existFile(old_dir + file.getFilename()))) {
  			if(file.getFileType() == SLink)
  				network->sendByte(SymlinkFile);
***************
*** 88,102 ****
  				network->sendByte(CopySpecial);
  			network->sendString(file.getFilename());
- 		}
- 	} else {
- 		if(file.getFileType() == SLink)
- 			network->sendByte(SymlinkFile);
- 		else
- 			network->sendByte(CopySpecial);
- 		network->sendString(file.getFilename());
  	}
! }
! 
  
  void Distributor::register_old_hardlink_file(Fileinfo &info)
  {
--- 100,107 ----
  				network->sendByte(CopySpecial);
  			network->sendString(file.getFilename());
  	}
! } 
  
+ /* register files that exists unchanged on server */
  void Distributor::register_old_hardlink_file(Fileinfo &info)
  {
***************
*** 104,107 ****
--- 109,113 ----
  }
  
+ /* register files that does not exists or are different on server */
  void Distributor::register_hardlink_file(ino_t inodeNo, Fileinfo &info)
  {
***************
*** 109,118 ****
  }
  
  void Distributor::preservedHardlink(ino_t inodeNo, dev_t deviceNo, string destination)
  {
  	multimap<ino_t , Fileinfo>::iterator i, j, todelete;
! 	pair<	multimap<ino_t ,  Fileinfo>::iterator,
! 	multimap<ino_t , Fileinfo>::iterator
! 	> result;
  	result = HardlinkMap.equal_range(inodeNo);
  	i = result.first;
--- 115,126 ----
  }
  
+ /* Searches for all entrys with inodeNo and deviceNo in map for changed or non 
+    existing files on server. All matches are linked against destination as a
+    hardlink. These entrys will be removed from the map afterwards */
  void Distributor::preservedHardlink(ino_t inodeNo, dev_t deviceNo, string destination)
  {
  	multimap<ino_t , Fileinfo>::iterator i, j, todelete;
! 	pair<multimap<ino_t, Fileinfo>::iterator,
! 	multimap<ino_t , Fileinfo>::iterator> result;
  	result = HardlinkMap.equal_range(inodeNo);
  	i = result.first;
***************
*** 133,142 ****
  }
  
  void Distributor::unresolvedHardlinks()
  {
  	multimap<ino_t , Fileinfo>::iterator i, j, begin, todelete;
  	pair<	multimap<ino_t ,  Fileinfo>::iterator,
! 	multimap<ino_t , Fileinfo>::iterator
! 	> result;
  	while(HardlinkMap.size()) {
  		begin = HardlinkMap.begin();
--- 141,154 ----
  }
  
+ /* Takes the first entry of the map with the new or changed files an search for
+    mor entrys with the same InodeNo. 
+    The result is a set of entrys with the same InodeNo. The first entry will be
+    transfered and all remaining entrys will be linked angainst the first entry.
+    */
  void Distributor::unresolvedHardlinks()
  {
  	multimap<ino_t , Fileinfo>::iterator i, j, begin, todelete;
  	pair<	multimap<ino_t ,  Fileinfo>::iterator,
! 	multimap<ino_t , Fileinfo>::iterator> result;
  	while(HardlinkMap.size()) {
  		begin = HardlinkMap.begin();
***************
*** 145,155 ****
  		string destination = i->second.getFilename();
  		dev_t deviceNo = i->second.getDeviceNo();
! 		if(i->second.getExisting()) {
! 			if(!file_changed(i->second)) //keep existing
  			{
  				HardlinkMap.erase(i);
  				continue;
  			}
! 		} else {
  			if(!new_file(i->second)) //keep existing and file does not exist
  			{
--- 157,179 ----
  		string destination = i->second.getFilename();
  		dev_t deviceNo = i->second.getDeviceNo();
! 		if(i->second.getExisting()) {// file exists on server
! 			
! 			/* try to transfer file - will return false - if fails.
! 			   This means the file will not be transfered because
! 			   the mode is set to KeepExisting and a link to an old
! 			   version will be created - we need an actual one! */
! 			if(!file_changed(i->second)) 
  			{
+ 				/* we cannot link other files to this one
+ 				   because this version is not backed up */
  				HardlinkMap.erase(i);
  				continue;
  			}
! 		} else {// files does not exist on server
! 			
! 			/* try to transfer file - will return false - if fails.
! 			   This means the file will not be transfered because
! 			   the mode is set to KeepExisting and there are no
! 			   old version on server */
  			if(!new_file(i->second)) //keep existing and file does not exist
  			{
***************
*** 158,161 ****
--- 182,186 ----
  			}
  		}
+ 		/* takes the rest of the matching set */
  		j = result.second;
  		if(i == j) { //just one element
***************
*** 165,171 ****
  		todelete = i;
  		i++;
  		HardlinkMap.erase(todelete);
! 		if(i != HardlinkMap.end())
  			do {
  				if(deviceNo == i->second.getDeviceNo()) //make sure file is one the same dev
  				{
--- 190,199 ----
  		todelete = i;
  		i++;
+ 		/* erase the first entry - we have already transfered them */
  		HardlinkMap.erase(todelete);
! 		if(i != HardlinkMap.end()) //Map end
  			do {
+ 				/* iterate throug all remaing matching files and
+ 				   send them as hardlinks to the first entry */
  				if(deviceNo == i->second.getDeviceNo()) //make sure file is one the same dev
  				{
***************
*** 182,189 ****
  }
  
  void Distributor::sendHardlinks()
  {
! 	while(OldHardlinkMap.size()) {
  		file_unchanged(OldHardlinkMap.front());
  		preservedHardlink(OldHardlinkMap.front().getInodeNo(),
  				  OldHardlinkMap.front().getDeviceNo(),
--- 210,226 ----
  }
  
+ /* Sends all remaining hardlinks to FileSender.
+    Takes all files from the list with the unchanged hardlinks and compares them
+    with the entrys from the list with the new (or changed) entrys. All new
+    entrys that has the same InodeNo will be linked as a hardlink to the old
+    entry. */
  void Distributor::sendHardlinks()
  {
! 	while(OldHardlinkMap.size()) { 
! 		/* stores the unchanged file */
  		file_unchanged(OldHardlinkMap.front());
+ 		
+ 		/* search for more files with the same InodeNo on Client
+ 		   and send these as links to the unchanged file */
  		preservedHardlink(OldHardlinkMap.front().getInodeNo(),
  				  OldHardlinkMap.front().getDeviceNo(),
***************
*** 191,194 ****
--- 228,232 ----
  		OldHardlinkMap.pop_front();
  	}
+ 	/* process the leftovers */
  	unresolvedHardlinks();
  }
***************
*** 206,220 ****
  {}
  
- 
  void Distributor::distribute()
  {
! 	unsigned int size;
! 
  	while(1) {
  		char *data = (char *) network->readData(size);
  		Fileinfo fileinfo(data, size);
  		output.information(F_("received Fileinfo: %s type %d")
  				   % fileinfo.getFilename()
  				   % fileinfo.getFileType(), DEBUG_OUTPUT);
  		if(fileinfo.getFileType() == EXIT) {
  			sendHardlinks();
--- 244,260 ----
  {}
  
  void Distributor::distribute()
  {
! 	
  	while(1) {
+ 		unsigned int size;
  		char *data = (char *) network->readData(size);
+ 		/* create FileInfo of file from client */
  		Fileinfo fileinfo(data, size);
  		output.information(F_("received Fileinfo: %s type %d")
  				   % fileinfo.getFilename()
  				   % fileinfo.getFileType(), DEBUG_OUTPUT);
+ 		
+ 		/* Last FileInfo from InformationSender received */
  		if(fileinfo.getFileType() == EXIT) {
  			sendHardlinks();
***************
*** 226,267 ****
  		output.information(F_("test for: %s %s")
  				   % old_dir % fileinfo.getFilename(), DEBUG_OUTPUT);
  		if(fileinfo.getFileType() != Reg) {
  			output.information(F_("no reg file: %s")
  					   % fileinfo.getFilename(), DEBUG_OUTPUT);
  			no_reg_file(fileinfo);
! 		} else
  			if( existFile(old_dir + fileinfo.getFilename()) ) {
  				output.information(F_("file exist: %s %s")
  						   % old_dir % fileinfo.getFilename(),
  						   DEBUG_OUTPUT);
! 
  				Fileinfo oldfile(fileinfo.getFilename(), old_dir +
  						 fileinfo.getFilename());
  				if(oldfile == fileinfo) {
  					output.information("file exist unchanged: " +
  							   fileinfo.getFilename(),
  							   DEBUG_OUTPUT);
! 					if(fileinfo.getInodeNo())
  						register_old_hardlink_file(fileinfo);
  					else
! 						file_unchanged(fileinfo);
! 				} else {
  					fileinfo.setExisting(true);
  					output.information("file exist changed: " +
  							   fileinfo.getFilename(),
  							   DEBUG_OUTPUT);
  					if(fileinfo.getInodeNo())
  						register_hardlink_file(fileinfo.getInodeNo(), fileinfo);
  					else
  						file_changed(fileinfo);
  				}
! 			} else {
  				fileinfo.setExisting(false);
  				if(fileinfo.getInodeNo()) {
  					register_hardlink_file(fileinfo.getInodeNo(), fileinfo);
  				} else
  					new_file(fileinfo);
! 			}
! 	}
  }
  
--- 266,327 ----
  		output.information(F_("test for: %s %s")
  				   % old_dir % fileinfo.getFilename(), DEBUG_OUTPUT);
+ 		/* non-regular files need special treatment */
  		if(fileinfo.getFileType() != Reg) {
  			output.information(F_("no reg file: %s")
  					   % fileinfo.getFilename(), DEBUG_OUTPUT);
+ 					   
+ 			/* send transfer command to FileSender */
  			no_reg_file(fileinfo);
! 		} else //regular files
  			if( existFile(old_dir + fileinfo.getFilename()) ) {
  				output.information(F_("file exist: %s %s")
  						   % old_dir % fileinfo.getFilename(),
  						   DEBUG_OUTPUT);
! 						   
! 				/* create FileInfo of file on server */
  				Fileinfo oldfile(fileinfo.getFilename(), old_dir +
  						 fileinfo.getFilename());
+ 				
+ 				/* compare local and remote file */
  				if(oldfile == fileinfo) {
  					output.information("file exist unchanged: " +
  							   fileinfo.getFilename(),
  							   DEBUG_OUTPUT);
! 					if(fileinfo.getInodeNo()) 
! 						/* file has a link counter > 1 on client; 
! 						   we wait for more files with this inodeNo */
  						register_old_hardlink_file(fileinfo);
  					else
! 					/* no links on this file - we can continue with transfer */
! 					file_unchanged(fileinfo);
! 				} else { //files are different
! 					/* we need this information in unresolvedHardlinks() */
  					fileinfo.setExisting(true);
  					output.information("file exist changed: " +
  							   fileinfo.getFilename(),
  							   DEBUG_OUTPUT);
+ 							   
+ 					/* file has a link counter > 1 on client; 
+ 					   we wait for more files with this inodeNo */
  					if(fileinfo.getInodeNo())
  						register_hardlink_file(fileinfo.getInodeNo(), fileinfo);
  					else
+ 						/* no links on this file - we can continue with transfer */
  						file_changed(fileinfo);
  				}
! 			} else { //regular file does not exist on server
! 				/* we need this information in unresolvedHardlinks() */
  				fileinfo.setExisting(false);
+ 				
  				if(fileinfo.getInodeNo()) {
+ 					/* file has a link counter > 1 on client; 
+ 					we don't have this file on server but there may
+ 					be more files with this inodeNo */
  					register_hardlink_file(fileinfo.getInodeNo(), fileinfo);
  				} else
+ 					/* no links on this file - we can continue with transfer */
  					new_file(fileinfo);
! 			} //reg file exits
! 	} //while 1
  }
  

Index: distributor.hpp
===================================================================
RCS file: /cvsroot/faubackup/faubackup2/src/copier/distributor.hpp,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** distributor.hpp	20 Sep 2005 23:31:12 -0000	1.10
--- distributor.hpp	4 Nov 2005 18:28:53 -0000	1.11
***************
*** 28,33 ****
  {
  private:
- 
- 
  	NetCommunication *network;
  	string old_dir, destination;
--- 28,31 ----
***************
*** 41,45 ****
  	void unresolvedHardlinks();
  
- 
  	bool file_changed(Fileinfo &file);
  	void file_unchanged(Fileinfo &file);
--- 39,42 ----



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php