Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl B文件测试的可靠性如何?_Perl_Find_Binaryfiles - Fatal编程技术网

Perl B文件测试的可靠性如何?

Perl B文件测试的可靠性如何?,perl,find,binaryfiles,Perl,Find,Binaryfiles,当我打开一个SQLite数据库文件时,文件的开头有很多可读文本-由于-B文件测试,SQLite文件被错误过滤掉的可能性有多大 #!/usr/bin/env perl use warnings; use strict; use 5.10.1; use File::Find; my $dir = shift; my $databases; find( { wanted => sub { my $file = $File::Find::name;

当我打开一个SQLite数据库文件时,文件的开头有很多可读文本-由于
-B
文件测试,SQLite文件被错误过滤掉的可能性有多大

#!/usr/bin/env perl
use warnings;
use strict;
use 5.10.1;
use File::Find;

my $dir = shift;
my $databases;

find( {
    wanted     => sub {
        my $file = $File::Find::name;
        return if not -B $file;
        return if not -s $file;
        return if not -r $file;
        say $file;
        open my $fh, '<', $file or die "$file: $!";
        my $firstline = readline( $fh ) // '';
        close $fh or die $!;
        push @$databases, $file if $firstline =~ /\ASQLite\sformat/;
    },
    no_chdir   => 1,
},
$dir );

say scalar @$databases;
#/usr/bin/env perl
使用警告;
严格使用;
使用5.10.1;
使用File::Find;
我的$dir=shift;
我的$数据库;
查找({
通缉=>sub{
我的$file=$file::Find::name;
如果不是,则返回-B$文件;
如果不是,则返回-s$文件;
如果不是,则返回-r$文件;
比如说$file;
打开我的$fh,手册页有以下关于
-T
-B
的说明:

The -T and -B switches work as follows. The first block or so of the file is
examined for odd characters such as strange control codes or characters with
the high bit set. If too many strange characters (>30%) are found, it's a -B
file; otherwise it's a -T file. Also, any file containing a zero byte in the
first block is considered a binary file. 
当然,您现在可以对许多sqlite文件进行统计分析,分析它们的“第一个块左右”中的“奇数字符”,计算它们出现的概率,这将让您了解
-B
对sqlite文件失败的可能性有多大

然而,你也可以走简单的路线。它会失败吗?是的,这是一个启发性的方法。在这方面很糟糕。所以不要使用它


Unix上的文件类型识别通常是通过评估文件的内容来完成的。是的,有些人已经为您完成了所有的工作:它被称为
libmagic
(生成
文件
命令行工具的东西)。您可以从Perl中使用它,例如..

好吧,从技术上讲,所有文件都是字节的集合,因此是二进制的。除此之外,没有公认的二进制定义,因此不可能评估
-B
的可靠性,除非您愿意提出一个定义来评估它。

我不认为
-T的细节de>和
-B
是文档化的,我想这意味着它们可能会在未来的Perl版本中发生变化。然后,这又会使它们变得毫无用处……对这个问题的一个好答案是描述启发式的细节。我一直在想,它们是在perlfunc中记录的,所有的文件操作符都是文档化的不过,这一点并不明确。