Как я могу сравнить список файлов из архива tar и каталога?

StackOverflow https://stackoverflow.com/questions/1271879

  •  13-09-2019
  •  | 
  •  

Вопрос

Я все еще изучаю Perl.Кто-нибудь, пожалуйста, может предложить мне Perl-код для сравнения файлов.tar.gz и путь к каталогу.

Допустим, у меня есть tar.gz резервная копия следующего пути к каталогу, который я сделал несколько дней назад.

a/file1
a/file2
a/file3
a/b/file4
a/b/file5
a/c/file5
a/b/d/file and so on..

Теперь я хочу сравнить файлы и каталоги по этому пути с файлом резервной копии tar.gz.

Пожалуйста, предложите Perl-код для этого.

Это было полезно?

Решение

Это может быть хорошей отправной точкой для создания хорошей программы на Perl.Тем не менее, он делает то, о чем задавался вопрос.

Он был просто взломан и игнорирует большинство лучших практик Perl.

perl test.pl full                            \
     Downloads/update-dnsomatic-0.1.2.tar.gz \
     Downloads/                              \
     update-dnsomatic-0.1.2
#! /usr/bin/env perl
use strict;
use 5.010;
use warnings;
use autodie;

use Archive::Tar;
use File::Spec::Functions qw'catfile catdir';

my($action,$file,$directory,$special_dir) = @ARGV;

if( @ARGV == 1 ){
  $file = *STDOUT{IO};
}
if( @ARGV == 3 ){
  $special_dir = '';
}

sub has_file(_);
sub same_size($$);
sub find_missing(\%$);

given( lc $action ){

  # only compare names
  when( @{[qw'simple name names']} ){
    my @list = Archive::Tar->list_archive($file);

    say qq'missing file: "$_"' for grep{ ! has_file } @list;
  }

  # compare names, sizes, contents
  when( @{[qw'full aggressive']} ){
    my $next = Archive::Tar->iter($file);
    my( %visited );

    while( my $file = $next->() ){
      next unless $file->is_file;
      my $name = $file->name;
      $visited{$name} = 1;

      unless( has_file($name) ){
        say qq'missing file: "$name"' ;
        next;
      }

      unless( same_size( $name, $file->size ) ){
        say qq'different size: "$name"';
        next;
      }

      next unless $file->size;

      unless( same_checksum( $name, $file->get_content ) ){
        say qq'different checksums: "$name"';
        next;
      }
    }

    say qq'file not in archive: "$_"' for find_missing %visited, $special_dir;
  }

}

sub has_file(_){
  my($file) = @_;
  if( -e catfile $directory, $file ){
    return 1;
  }
  return;
}

sub same_size($$){
  my($file,$size) = @_;
  if( -s catfile($directory,$file) == $size ){
    return $size || '0 but true';
  }
  return; # empty list/undefined
}

sub same_checksum{
  my($file,$contents) = @_;
  require Digest::SHA1;

  my($outside,$inside);

  my $sha1 = Digest::SHA1->new;
  {
    open my $io, '<', catfile $directory, $file;
    $sha1->addfile($io);
    close $io;
    $outside = $sha1->digest;
  }

  $sha1->add($contents);
  $inside = $sha1->digest;


  return 1 if $inside eq $outside;
  return;
}

sub find_missing(\%$){
  my($found,$current_dir) = @_;

  my(@dirs,@files);

  {
    my $open_dir = catdir($directory,$current_dir);
    opendir my($h), $open_dir;

    while( my $elem = readdir $h ){
      next if $elem =~ /^[.]{1,2}[\\\/]?$/;

      my $path = catfile $current_dir, $elem;
      my $open_path = catfile $open_dir, $elem;

      given($open_path){
        when( -d ){
          push @dirs, $path;
        }
        when( -f ){
          push @files, $path, unless $found->{$path};
        }
        default{
          die qq'not a file or a directory: "$path"';
        }
      }
    }
  }

  for my $path ( @dirs ){
    push @files, find_missing %$found, $path;
  }

  return @files;
}

После переименования config к config.rm, добавив дополнительный символ к README, изменение символа в install.sh, и добавление файла .test.Вот что выдало:

missing file: "update-dnsomatic-0.1.2/config"
different size: "update-dnsomatic-0.1.2/README"
different checksums: "update-dnsomatic-0.1.2/install.sh"
file not in archive: "update-dnsomatic-0.1.2/config.rm"
file not in archive: "update-dnsomatic-0.1.2/.test"

Другие советы

Видеть Архив::Тар.

Тот самый Archive::Tar и File::Find модули будут полезны.Простой пример показан ниже.Он просто печатает информацию о файлах в tar и файлах в дереве каталогов.

Из вашего вопроса было неясно, как вы хотите сравнить файлы.Если вам нужно сравнить фактический контент, то get_content() метод в Archive::Tar::File скорее всего, это будет необходимо.Если достаточно более простого сравнения (например, name, size и mtime), вам не понадобится намного больше методов, используемых в примере ниже.

#!/usr/bin/perl
use strict;
use warnings;

# A utility function to display our results.
sub Print_file_info {
    print map("$_\n", @_), "\n";
}

# Print some basic information about files in a tar.
use Archive::Tar qw();
my $tar_file = 'some_tar_file.tar.gz';
my $tar = Archive::Tar->new($tar_file);
for my $ft ( $tar->get_files ){
    # The variable $ft is an Archive::Tar::File object.
    Print_file_info(
        $ft->name,
        $ft->is_file ? 'file' : 'other',
        $ft->size,
        $ft->mtime,
    );
}

# Print some basic information about files in a directory tree.
use File::Find;
my $dir_name = 'some_directory';
my @files;
find(sub {push @files, $File::Find::name}, $dir_name);
Print_file_info(
    $_,
    -f $_ ? 'file' : 'other',
    -s,
    (stat)[9],
) for @files;

На самом деле Perl для этого слишком излишен.Сценарий оболочки подойдет.Однако шаги, которые вам необходимо предпринять:

  • Извлеките tar куда-нибудь во временную папку.
  • diff -uR две папки и перенаправить вывод куда-нибудь (или, возможно, по каналу в less как уместно)
  • Очистите временную папку.

И вы сделали.Не должно быть больше 5-6 строк.Что-то быстрое и непроверенное:

#!/bin/sh
mkdir $TEMP/$$
tar -xz -f ../backups/backup.tgz $TEMP/$$
diff -uR $TEMP/$$ ./ | less
rm -rf $TEMP/$$

Вот пример, который проверяет, существует ли каждый файл, находящийся в архиве, в папке.

# $1 is the file to test
# $2 is the base folder
for file in $( tar --list -f $1 | perl -pe'chomp;$_=qq["'$2'$_" ]' )
do
  # work around bash deficiency
  if [[ -e "$( perl -eprint$file )" ]]
    then
      echo "   $file"
    else
      echo "no $file"
  fi
done

Вот как я это проверял:

Я удалил/переименовал config, затем выполнил следующее:

bash test Downloads/update-dnsomatic-0.1.2.tar.gz Downloads/

Что дало результат:

   "Downloads/update-dnsomatic-0.1.2/"
no "Downloads/update-dnsomatic-0.1.2/config"
   "Downloads/update-dnsomatic-0.1.2/update-dnsomatic"
   "Downloads/update-dnsomatic-0.1.2/README"
   "Downloads/update-dnsomatic-0.1.2/install.sh"

Я новичок в программировании на bash/shell, поэтому, вероятно, есть лучший способ сделать это.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top