微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

perl – -B文件测试的可靠性如何?

当我打开一个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;

解决方法

有关-T和-B的 perlfunc手册页有如下说明:
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中使用它,例如File::MMagic.

原文地址:https://www.jb51.cc/Perl/171354.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐