| 1 | package File::Atomism::utils; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | File::Atomism::utils - Misc file handling stuff | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | Utilities for manipulating and creating filenames. | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | A collection of standard perl functions and utilities for messing | ||||||||||||||||||||
| 14 | with filenames. These are generally useful for manipulating files | ||||||||||||||||||||
| 15 | in an 'atomised' directory, see L<File::Atomism>. | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 3 | ▶ | use File::stat; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 3 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 3 | ▶ | use Exporter; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | 3 | ▶ | use File::Path qw /mkpath/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 3 | ▶ | use Sys::Hostname qw /hostname/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 3 | ▶ | use Time::HiRes qw /gettimeofday/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 3 | ▶ | use Cwd qw /abs_path/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 3 | ▶ | our $CONFDIR = $ENV{ATOMISM_CONF} || $ENV{HOME} ."/.atomism"; | ||||||||||||||||||
|
Condition50%1/2
$ENV{'ATOMISM_CONF'} || $ENV{'HOME'} . '/.atomism'
MC/DC0%0/1
$ENV{'ATOMISM_CONF'} || $ENV{'HOME'} . '/.atomism'
$ENV{'ATOMISM_CONF'}
Truth table50%1/2
A
A= $ENV{'ATOMISM_CONF'}
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 3 | ▶ | use vars qw /@ISA @EXPORT_OK/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 3 | @ISA = qw /Exporter/; | |||||||||||||||||||
| 34 | 3 | @EXPORT_OK = qw /Hostname Pid Inode Unixdate Dir File Extension TempFilename PermFilename Journal Undo Redo/; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =pod | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =head1 USAGE | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | Access some values useful for constructing temporary filenames: | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | my $hostname = Hostname(); | ||||||||||||||||||||
| 43 | my $pid = Pid(); | ||||||||||||||||||||
| 44 | my $unixdate = Unixdate(); | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | sub Hostname | ||||||||||||||||||||
| 49 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
Hostname: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 50 | 1 | ▶ | eval {hostname} || 'localhost.localdomain'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | sub Pid | ||||||||||||||||||||
| 54 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
Pid: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 55 | 1 | $$; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | sub Unixdate | ||||||||||||||||||||
| 59 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
Unixdate: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 60 | 1 | time; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =pod | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | Retrieve the inode of a file like so: | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | my $inode = Inode ('/path/to/my-house.jpg'); | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | sub Inode | ||||||||||||||||||||
| 72 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
Inode: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 73 | 1 | my $filename = shift; | |||||||||||||||||||
| 74 | 1 | ▶ | my $stat = stat ($filename) || return 0; | ||||||||||||||||||
|
Condition50%1/2
&File::Atomism::utils::stat($filename) || (return 0)
MC/DC0%0/1
&File::Atomism::utils::stat($filename) || (return 0)
&File::Atomism::utils::stat($filename)
Truth table50%1/2
A
A= &File::Atomism::utils::stat($filename)
| |||||||||||||||||||||
| 75 | 1 | $stat->ino; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =pod | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | Access the directory and filename given a path: | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | Dir ('/path/to/my-house.jpg'); | ||||||||||||||||||||
| 83 | File ('/path/to/my-house.jpg'); | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | .returns '/path/to/' and 'my-house.jpg' respectively. | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | sub Dir | ||||||||||||||||||||
| 90 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
Dir: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 91 | 1 | my $path = shift; | |||||||||||||||||||
| 92 | 1 | my ($volume, $dir, $file) = File::Spec->splitpath ($path); | |||||||||||||||||||
| 93 | 1 | return $dir; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | sub File | ||||||||||||||||||||
| 97 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
File: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 98 | 1 | my $path = shift; | |||||||||||||||||||
| 99 | 1 | my ($volume, $dir, $file) = File::Spec->splitpath ($path); | |||||||||||||||||||
| 100 | 1 | return $file; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =pod | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | Retrieve the file extension (.doc, .txt, .jpg) of a file like so: | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | my $extension = Extension ('my-house.jpg'); | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | sub Extension | ||||||||||||||||||||
| 112 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
Extension: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 113 | 1 | my $filename = shift; | |||||||||||||||||||
| 114 | 1 | ▶ | return 0 unless ($filename =~ /[^.]\.[a-z0-9_]+$/); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 1 | $filename =~ s/.*\.//; | |||||||||||||||||||
| 116 | 1 | $filename =~ s/[^a-z0-9_]//gi; | |||||||||||||||||||
| 117 | 1 | return $filename; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =pod | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | Ask for a temporary filename like so: | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | my $tempname = TempFilename ('/path/to/'); | ||||||||||||||||||||
| 125 | or | ||||||||||||||||||||
| 126 | my $tempname = TempFilename ('/path/to/myfile.yml'); | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | A unique filepath based on the directory, hostname, current PID and | ||||||||||||||||||||
| 129 | extension (if supplied, otherwise .tmp will be appended) will be | ||||||||||||||||||||
| 130 | returned: | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | /path/to/.foo.example.com-666.tmp | ||||||||||||||||||||
| 133 | /path/to/.foo.example.com-666.yml | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | sub TempFilename | ||||||||||||||||||||
| 138 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
TempFilename: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 139 | 0 | my $path = shift; | |||||||||||||||||||
| 140 | 0 | ▶ | my $ext = Extension ($path) || 'tmp'; | ||||||||||||||||||
|
Condition0%0/2
Extension($path) || 'tmp'
MC/DC0%0/1
Extension($path) || 'tmp'
Extension($path)
Truth table0%0/2
A
A= Extension($path)
| |||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 0 | Dir ($path) .".". Hostname ."-". Pid .".". $ext; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =pod | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | Ask for a permanent filename like so: | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | my $filename = PermFilename ('/path/to/.foo.example.com-666.yml'); | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | A unique filepath based on the hostname, current PID, inode of the | ||||||||||||||||||||
| 152 | temporary file, date and file extension will be supplied: | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | /path/to/foo.example.com-666-1234-1095026759.yml | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | Alternatively you can specify the final extension as a second | ||||||||||||||||||||
| 157 | parameter: | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | my $filename = PermFilename ('/path/to/.foo.example.com-666.tmp', 'yml'); | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | sub PermFilename | ||||||||||||||||||||
| 164 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
PermFilename: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 165 | 0 | my $path = shift; | |||||||||||||||||||
| 166 | 0 | ▶ | my $ext = shift || Extension ($path); | ||||||||||||||||||
|
Condition0%0/3
shift() || Extension($path)
MC/DC0%0/2
shift() || Extension($path)
shift()
Extension($path)
Truth table0%0/3
A || B
A= shift()B= Extension($path)
| |||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 0 | Dir ($path) . Hostname ."-". Pid ."-". Inode ($path) ."-". Unixdate .".". $ext; | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =pod | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Write to the undo buffer like so: | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | Journal ([['persistent-file1.yml', '.temp-file1.yml'], [ ... ] ]); | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =cut | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | sub Journal | ||||||||||||||||||||
| 180 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
Journal: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 181 | 0 | my $list = shift; | |||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | # figure out where to put the journal from the first pair of files | ||||||||||||||||||||
| 184 | 0 | ▶ | my $dir = Dir (abs_path ($list->[0]->[0])) || Dir (abs_path ($list->[0]->[1])); | ||||||||||||||||||
|
Condition0%0/3
Dir(abs_path($list->[0][0])) || Dir(abs_path($list->[0][1]))
MC/DC0%0/2
Dir(abs_path($list->[0][0])) || Dir(abs_path($list->[0][1]))
Dir(abs_path($list->[0][0]))
Dir(abs_path($list->[0][1]))
Truth table0%0/3
A || B
A= Dir(abs_path($list->[0][0]))B= Dir(abs_path($list->[0][1]))
| |||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 0 | my $undodir = $CONFDIR ."/UNDO". $dir; | |||||||||||||||||||
| 187 | 0 | my $redodir = $CONFDIR ."/REDO". $dir; | |||||||||||||||||||
| 188 | 0 | mkpath ([$undodir, $redodir]); | |||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 0 | my $journal = $undodir . join (".", gettimeofday) .".patch"; | |||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 0 | for my $item (@{$list}) | |||||||||||||||||||
| 193 | { | ||||||||||||||||||||
| 194 | 0 | ▶ | my $old = abs_path ($item->[0]) || '/dev/null'; | ||||||||||||||||||
|
Condition0%0/2
abs_path($item->[0]) || '/dev/null'
MC/DC0%0/1
abs_path($item->[0]) || '/dev/null'
abs_path($item->[0])
Truth table0%0/2
A
A= abs_path($item->[0])
| |||||||||||||||||||||
| 195 | 0 | ▶ | my $new = abs_path ($item->[1]) || '/dev/null'; | ||||||||||||||||||
|
Condition0%0/2
abs_path($item->[1]) || '/dev/null'
MC/DC0%0/1
abs_path($item->[1]) || '/dev/null'
abs_path($item->[1])
Truth table0%0/2
A
A= abs_path($item->[1])
| |||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 0 | `diff -u $old $new >> $journal;`; | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | 0 | system 'sync'; | |||||||||||||||||||
| 200 | } | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =pod | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | Undo the most recent change to the journal by supplying a directory | ||||||||||||||||||||
| 205 | path to the Undo() method: | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | Undo ('/path/to'); | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =cut | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | sub Undo | ||||||||||||||||||||
| 212 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
Undo: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 213 | 0 | my $dir = abs_path (shift); | |||||||||||||||||||
| 214 | 0 | my $undodir = $CONFDIR ."/UNDO". $dir; | |||||||||||||||||||
| 215 | 0 | my $redodir = $CONFDIR ."/REDO". $dir; | |||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | 0 | ▶ | opendir (DIR, $undodir) or warn "$!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 218 | 0 | my @files = sort (readdir (DIR)); | |||||||||||||||||||
| 219 | 0 | @files = grep !/^\./, @files; | |||||||||||||||||||
| 220 | 0 | ▶ | my $file = pop (@files) || return; | ||||||||||||||||||
|
Condition0%0/2
pop @files || (return)
MC/DC0%0/1
pop @files || (return)
pop @files
Truth table0%0/2
A
A= pop @files
| |||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 0 | my $undo = $undodir ."/". $file; | |||||||||||||||||||
| 223 | 0 | my $redo = $redodir ."/". $file; | |||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | 0 | `cd $dir; patch -p0 --batch --no-backup < $undo; cd -`; | |||||||||||||||||||
| 226 | 0 | rename $undo, $redo; | |||||||||||||||||||
| 227 | 0 | system 'sync'; | |||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | 0 | closedir (DIR); | |||||||||||||||||||
| 230 | } | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | =pod | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | Undo the most recent undo() with the Redo() method. Usage is the | ||||||||||||||||||||
| 235 | same as for Undo(): | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | Redo ('/path/to'); | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =cut | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | sub Redo | ||||||||||||||||||||
| 242 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
Redo: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 243 | 0 | my $dir = abs_path (shift); | |||||||||||||||||||
| 244 | 0 | my $undodir = $CONFDIR ."/UNDO". $dir; | |||||||||||||||||||
| 245 | 0 | my $redodir = $CONFDIR ."/REDO". $dir; | |||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | 0 | ▶ | opendir (DIR, $redodir) or warn "$!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 248 | 0 | my @files = sort (readdir (DIR)); | |||||||||||||||||||
| 249 | 0 | @files = grep !/^\./, @files; | |||||||||||||||||||
| 250 | 0 | ▶ | my $file = shift (@files) || return; | ||||||||||||||||||
|
Condition0%0/2
shift @files || (return)
MC/DC0%0/1
shift @files || (return)
shift @files
Truth table0%0/2
A
A= shift @files
| |||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | 0 | my $undo = $undodir ."/". $file; | |||||||||||||||||||
| 253 | 0 | my $redo = $redodir ."/". $file; | |||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | 0 | `cd $dir; patch -p0 --force --no-backup < $redo; cd -`; | |||||||||||||||||||
| 256 | 0 | rename $redo, $undo; | |||||||||||||||||||
| 257 | 0 | system 'sync'; | |||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | 0 | closedir (DIR); | |||||||||||||||||||
| 260 | } | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | 3 | 1; | |||||||||||||||||||