Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.8 KiB

  1. @echo off
  2. for %%i in (%0.cmd) do perl -w -x %%~dp$PATH:i%0.cmd %*
  3. goto :eof
  4. #!perl
  5. # line 6
  6. #
  7. # FileName: sdrestore.cmd
  8. #
  9. # Usage = sdrestore [-v] <backupdir>
  10. #
  11. # This script does the converse of sdbackup by copying the saved files
  12. # from <backupdir> into the client's SD enlistments. All the files in
  13. # <backupdir> will be checked out on the local machine and the backups
  14. # will be restored.
  15. #
  16. # Currently this script bails out if any of the files in <backupdir> are
  17. # either already checked out or a different version than was backed up.
  18. # Clever diffing and merging could be done here instead.
  19. #
  20. #
  21. use Getopt::Std;
  22. use File::Path;
  23. use File::Spec;
  24. use File::Copy;
  25. #
  26. # initialize globals
  27. #
  28. $opt_v = 0;
  29. %src_revision_list = ();
  30. %dest_revision_list = ();
  31. %src_filelist = ();
  32. %dest_filelist = ();
  33. $Usage = "
  34. USAGE: $0 [-v] backupdir
  35. -v Verbose output
  36. backupdir Supplies directory where files were backed up to with sdbackup
  37. ";
  38. getopts('v') or die $Usage;
  39. #
  40. # Check for valid backup directory and log file
  41. #
  42. $backupdir = $ARGV[0] or die $Usage;
  43. -d $backupdir or die "Backup directory $backupdir does not exist\n";
  44. -e "$backupdir\\backup.log" or die "backup logfile $backupdir\\backup.log does not exist\n";
  45. open(LOGFILE, "$backupdir\\backup.log") or die "couldn't open $backupdir\\backuplog: $!\n";
  46. @backupfiles = <LOGFILE>;
  47. foreach $file (@backupfiles) {
  48. $opt_v and print $file;
  49. chomp $file;
  50. ($depotfilename, $revision, $srcfile) = split(/\s/,$file);
  51. $depotfilename = lc($depotfilename);
  52. $revision =~ tr/#//d; # get rid of #
  53. $src_revision_list{$depotfilename} = $revision;
  54. $src_filelist{$depotfilename} = File::Spec->catfile($backupdir,$srcfile);
  55. }
  56. #
  57. # now we have a list of depot names. Get the client name and the current
  58. # version by calling sdx have
  59. #
  60. $filelist = join(' ',keys(%src_filelist));
  61. foreach $line (`sdx have $filelist`) {
  62. #
  63. # throw away blank lines, depot specifiers, "not found" lines,
  64. # and lines that start with "-"
  65. #
  66. $line =~ /^[\t\s]*$|^-------|file\(s\) not on client|^-/ and next;
  67. chomp $line;
  68. #
  69. # parse resulting output, it looks like:
  70. # <depotfilename>#<version> - <clientfilename>
  71. $opt_v and print ("line = $line\n");
  72. ($depotfilename, $revision, $clientfilename) = split(/#| - /,$line);
  73. $depotfilename = lc($depotfilename);
  74. $opt_v and print ("\tfilename: $depotfilename\n\trevision: $revision\n\tclientfilename: $clientfilename\n\n");
  75. $dest_revision_list{$depotfilename} = $revision;
  76. $dest_filelist{$depotfilename} = $clientfilename;
  77. #
  78. # Now we have a list of the local filenames and versions.
  79. # If a file has the same version in both places and is read-only,
  80. # put it on the checkout list. Otherwise, it needs to be merged
  81. # and is put on the merge list.
  82. #
  83. if (($revision == $src_revision_list{$depotfilename}) &&
  84. (!(-w $clientfilename)))
  85. {
  86. $opt_v and print("Putting $depotfilename on the checkout list\n");
  87. push @checkoutlist, $depotfilename;
  88. } else {
  89. $opt_v and print "Putting $depotfilename on the merge list\n";
  90. $opt_v and print "\tsrcversion $src_revision_list{$depotfilename} destversion $revision\n";
  91. push @mergelist, $depotfilename;
  92. }
  93. }
  94. $opt_v and print "to checkout:\n\t" . join("\n\t", @checkoutlist);
  95. $opt_v and print "\nto merge:\n\t" . join("\n\t", @mergelist) . "\n";
  96. if (@mergelist) {
  97. print "The following files are either checked out or different versions:\n";
  98. foreach $file (@mergelist) {
  99. print "$src_filelist{$file}#$src_revision_list{$file} -> $dest_filelist{$file}#$dest_revision_list{$file}\n";
  100. }
  101. print "sdrestore cannot continue.\n";
  102. exit;
  103. }