filebrowser/helpertools/snapshot_compare_dirs.pl
branchRCL_3
changeset 22 fad26422216a
parent 0 d6fe6244b863
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:  
       
    15 #
       
    16 
       
    17 use strict;
       
    18 
       
    19 use Getopt::Std;
       
    20 use Digest::MD5 qw( md5 );
       
    21 use File::Basename;
       
    22 use File::Copy;
       
    23 use File::Find;
       
    24 use File::Path;
       
    25 
       
    26 my @changed_files=();
       
    27 my $dir_1_length;
       
    28 my $dir_2_length;
       
    29 my $directory1;
       
    30 my $directory2;
       
    31 my %list1=();
       
    32 my %list2=();
       
    33 my %opts=();
       
    34 my $switch= 0;
       
    35 my $result_dir;
       
    36 
       
    37 ###################################################
       
    38 ##  usage()                                      ##
       
    39 ##  Prints the usage                             ##
       
    40 ###################################################
       
    41 sub usage
       
    42 {
       
    43   print "\nUsage\n\tsnapshot_compare_dirs.pl -1 <original dir> -2 <updated dir>  -r <result dir>\n";
       
    44   print "\nWhere\n\t-1  Original directory\n\t-2  Updated directory\n\t-r  Result directory\n";
       
    45   exit;
       
    46 }
       
    47 
       
    48 ###################################################
       
    49 ##  get_param()                                  ##
       
    50 ##  Gets command line parameters                 ##
       
    51 ###################################################
       
    52 sub get_param
       
    53 {
       
    54   getopts('h1:2:r:', \%opts);
       
    55 
       
    56   if ( $opts{'h'} ) {
       
    57     usage();
       
    58   }
       
    59 
       
    60   if ( $opts{'1'} ) {
       
    61     $directory1 = $opts{'1'};
       
    62     if ( ! -d $directory1 ) {
       
    63       print "\nError:\t Directory \"$directory1\" doesn't exist\n";
       
    64       exit;
       
    65     }
       
    66   } else {
       
    67     usage();
       
    68   }
       
    69 
       
    70   if ( $opts{'2'} ) {
       
    71     $directory2 = $opts{'2'};
       
    72     if ( ! -d $directory2 ) {
       
    73       print "\nError:\t Directory \"$directory2\" doesn't exist\n";
       
    74       exit;
       
    75     }
       
    76   } else {
       
    77     usage();
       
    78   }
       
    79 
       
    80   if ( $opts{'r'} ) {
       
    81     $result_dir = $opts{'r'};
       
    82     if ( ! -d $result_dir ) {
       
    83         mkdir $result_dir;
       
    84     }
       
    85   } else {
       
    86     usage();
       
    87   }
       
    88 
       
    89 }
       
    90 
       
    91 ###################################################
       
    92 ##  calculate_hash()  Reads file content and     ##
       
    93 ##                    calculates MD5 hash        ##
       
    94 ###################################################
       
    95 sub calculate_hash
       
    96 {
       
    97   my ( $file_name ) = @_;
       
    98 
       
    99   open( FILE, $file_name ) or die "Error: Cannot open $file_name\n";
       
   100   my @file_stat = stat FILE;
       
   101   binmode( FILE );
       
   102 
       
   103   my ( $buffer, $hash_value );
       
   104   read( FILE, $buffer, $file_stat[7] );
       
   105 
       
   106   close FILE;
       
   107 
       
   108   $hash_value = md5( $buffer );
       
   109 
       
   110   return $hash_value;
       
   111 }
       
   112 
       
   113 ###################################################
       
   114 ##  copy_file()                                  ##
       
   115 ##  Copies files with same directory structure   ##
       
   116 ###################################################
       
   117 sub copy_file
       
   118 {
       
   119     my ( $file1, $file2 ) = @_;
       
   120     my $dir_name = dirname( $file2 );
       
   121 
       
   122     mkpath( $dir_name, 0, 0777 );
       
   123 
       
   124     copy( $file1, $file2 );
       
   125 }
       
   126 
       
   127 ###################################################
       
   128 ##  process_files()                              ##
       
   129 ##  Indexes files                                ##
       
   130 ###################################################
       
   131 sub process_files
       
   132 {
       
   133   my $file = $File::Find::name;
       
   134 
       
   135   if ( -f $file ) {
       
   136     if ($switch eq 0 ) {
       
   137       $file = substr( $file, $dir_1_length );
       
   138       $list1{$file} = 1;;
       
   139     } else {
       
   140       $file = substr( $file, $dir_2_length );
       
   141       $list2{$file} = 1;
       
   142     }
       
   143   }
       
   144 }
       
   145 
       
   146 ###################################################
       
   147 ##  main()                                       ##
       
   148 ##  Main function                                ##
       
   149 ###################################################
       
   150 sub main
       
   151 {
       
   152   get_param();
       
   153 
       
   154   $dir_1_length = length( $directory1 );
       
   155   $dir_2_length = length( $directory2 );
       
   156 
       
   157   print "Info:\tProcessing files. This may take some time\n";
       
   158 
       
   159   $switch = 0;
       
   160   find( \&process_files, $directory1 );
       
   161 
       
   162   $switch = 1;
       
   163   find( \&process_files, $directory2 );
       
   164 
       
   165   print "Info:\tFiles only exist in \"$directory2\" :\n\n";
       
   166   my $counter = 0;
       
   167   my $file;
       
   168   my $key;
       
   169   my $hash1;
       
   170   my $hash2;
       
   171 
       
   172   foreach $key ( sort keys %list2 ){
       
   173     if ( $list1{$key} ne 1 ) {
       
   174        $key =~ s/\//\\/g;
       
   175        print "\t$key\n";
       
   176        copy_file( "$directory2$key", "$result_dir$key" );
       
   177        $counter++;
       
   178     } else {
       
   179        $hash1 = calculate_hash("$directory1$key");
       
   180        $hash2 = calculate_hash("$directory2$key");
       
   181        if ($hash1 ne $hash2) {
       
   182          push @changed_files, $key;
       
   183        }
       
   184     }
       
   185   }
       
   186 
       
   187   print "\nInfo:\tTotal $counter new file(s)\n";
       
   188 
       
   189   print "\nInfo:\tList of the changed file\n";
       
   190 
       
   191   $counter = 0;
       
   192 
       
   193   foreach $file ( @changed_files ){
       
   194       $counter++;
       
   195       print "\t$file\n";
       
   196   }
       
   197 
       
   198   print "\nInfo:\tTotal $counter file(s) changed\n";
       
   199   print "Info:\tNote: Only new files are copied to $result_dir\n";
       
   200 }
       
   201 
       
   202 main();