File Coverage

blib/lib/File/Overwrite.pm
Criterion Covered Total %
statement 25 25 100.0
branch 3 6 50.0
condition 1 2 50.0
subroutine 7 7 100.0
pod 2 2 100.0
total 38 42 90.4


line stmt bran cond sub pod time code
1 1     1   765 use strict;
  1         2  
  1         26  
2 1     1   4 use warnings;
  1         1  
  1         36  
3              
4             package File::Overwrite;
5              
6 1     1   4 use vars qw($VERSION @ISA @EXPORT_OK);
  1         4  
  1         340  
7              
8             require Exporter;
9              
10             @ISA = qw(Exporter);
11             @EXPORT_OK = qw(overwrite overwrite_and_unlink);
12             $VERSION = '1.2';
13              
14             =head1 NAME
15              
16             File::Overwrite - overwrite the contents of a file and optionally unlink it
17              
18             =head1 SYNOPSIS
19              
20             use File::Overwrite qw(overwrite);
21            
22             # haha, now no-one will know I stole it
23             overwrite('sekrit_formular.txt');
24             unlink('sekrit_formular.txt');
25              
26             =head1 DESCRIPTION
27              
28             This module provides a few simple functions for overwriting data files. This
29             will protect against the simplest forms of file recovery.
30              
31             =head1 SECURITY
32              
33             This module makes all kinds of assumptions about your system - how the disks
34             work, how the filesystem works, and so on. Even if it does overwrite the
35             actual disk blocks containing the original data, this will not necessarily
36             protect you against someone with sufficient equipment and/or determination. If
37             you want to stop forensic recovery of the data, don't put it on a computer in
38             the first place. If you have already put it on a computer, I recommend
39             melting all your disks.
40              
41             =cut
42              
43             =head1 FUNCTIONS
44              
45             All of the following functions can be exported if you wish. However, none are
46             exported by default. All take a filename as their only parameter (any subsequent
47             params are ignored) and die if that file can't be fiddled with. In case of failure,
48             the file may be left fractionally fiddled.
49              
50             =over 4
51              
52             =item overwrite
53              
54             Overwrites the file.
55              
56             =cut
57              
58             sub overwrite {
59 2     2 1 8372 my $file = shift();
60 2         8 _overwrite(with => 'X', file => $file);
61             }
62              
63             =item overwrite_and_unlink
64              
65             Overwrites and unlinks the file.
66              
67             =cut
68              
69             sub overwrite_and_unlink {
70 1     1 1 206 my $file = shift();
71 1         5 overwrite($file);
72 1         4 _unlink($file);
73             }
74              
75             # =item overwrite_and_delete
76             #
77             # Overwrites the file and then tries to find and unlink all links to it
78             #
79             # =cut
80             #
81             # sub overwrite_and_delete {
82             # my $file = shift();
83             # overwrite($file);
84             # foreach my $link (_find_links($file)) { _unlink($link); }
85             # }
86              
87             sub _overwrite {
88 2     2   8 my %params = @_;
89 2         5 my $file = $params{file};
90 2         4 my $with = $params{with};
91 2         22 my $bytes = -s $file;
92              
93 2 50       75 open(my $fh, '+<', $file) || die("Couldn't open $file: $!");
94 2         11 seek($fh, 0, 0);
95 2         7 for(; $bytes; $bytes--) {
96 18   50     49 print $fh $with || die("Couldn't overwrite $file: $!");
97             }
98 2 50       44 close($fh) || die("Couldn't close $file: $!");
99             }
100              
101             sub _unlink {
102 1     1   2 my $file = shift;
103 1 50       103 die("Couldn't unlink $file") unless(unlink($file) == 1);
104             }
105              
106             =back
107              
108             =head1 BUGS
109              
110             None known. Please report any that you find using L.
111              
112             You should be aware, however, that the tests are only rudimentary. There
113             is no portable way of determining whether a file's data really is overwritten
114             so I don't try very hard.
115              
116             On Win32 you can't delete files that are open. This is a bug in the
117             operating system, not in this module.
118              
119             =head1 FEEDBACK
120              
121             I like to know who's using my code. All comments, including constructive
122             criticism, are welcome. Please email me.
123              
124             =head1 THANKS TO
125              
126             Daniel Muey, for reporting some bugs and misfeatures,
127             see L.
128              
129             =head1 SEE ALSO
130              
131             L
132              
133             =head1 AUTHOR, COPYRIGHT and LICENCE
134              
135             Copyright 2008 David Cantrell Edavid@cantrell.org.ukE
136              
137             This module was written in response to a post by 'fluffyvoidwarrior'
138             on perlmonks.
139              
140             This software is free-as-in-speech software, and may be used,
141             distributed, and modified under the terms of either the GNU
142             General Public Licence version 2 or the Artistic Licence. It's
143             up to you which one you use. The full text of the licences can
144             be found in the files GPL2.txt and ARTISTIC.txt, respectively.
145              
146             =head1 CONSPIRACY
147              
148             This module is also free-as-in-mason software.
149              
150             =cut
151              
152             1;