どのようにしたがってファイルリストからタールアーカイブとディレクトリ?

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.rmREADMEの名前を変更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::TarFile::Find モジュールすることができます。基本的な例を以下に示します。それだけで画に関する情報はファイル、タールのディレクトリにあるファイルです。

ついては現時点では明確でないからお問い比較を行いたいファイルです。が必要な場合を比較し、実際のコンテンツの get_content() 方法 Archive::Tar::File が必要となります。場合により簡単な比較は適切(たとえば、名前、サイズ、および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/$$

アーカイブにあるすべてのファイルは、また、フォルダ内に存在するかどうかを確認しHERESに例ます。

# $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の/シェルプログラミングに新しいですので、これを行うには良い方法はおそらくあります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top