| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # file::write Brik | ||
| 5 | # | ||
| 6 | package Metabrik::File::Write; | ||
| 7 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 5 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 5 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | output => [ qw(file) ], | ||
| 20 | append => [ qw(0|1) ], | ||
| 21 | overwrite => [ qw(0|1) ], | ||
| 22 | encoding => [ qw(utf8|ascii) ], | ||
| 23 | fd => [ qw(file_descriptor) ], | ||
| 24 | unbuffered => [ qw(0|1) ], | ||
| 25 | use_locking => [ qw(0|1) ], | ||
| 26 | }, | ||
| 27 | attributes_default => { | ||
| 28 | append => 1, | ||
| 29 | overwrite => 0, | ||
| 30 | unbuffered => 0, | ||
| 31 | use_locking => 0, | ||
| 32 | }, | ||
| 33 | commands => { | ||
| 34 | open => [ qw(file|OPTIONAL) ], | ||
| 35 | lock => [ ], | ||
| 36 | unlock => [ ], | ||
| 37 | write => [ qw($data|$data_ref|$data_list) ], | ||
| 38 | close => [ ], | ||
| 39 | }, | ||
| 40 | require_modules => { | ||
| 41 | Fcntl => [ qw(:flock) ], | ||
| 42 | }, | ||
| 43 | }; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub brik_use_properties { | ||
| 47 | my $self = shift; | ||
| 48 | |||
| 49 | # encoding: see `perldoc Encode::Supported' for other types | ||
| 50 | return { | ||
| 51 | attributes_default => { | ||
| 52 | encoding => defined($self->global) && $self->global->encoding || 'utf8', | ||
| 53 | }, | ||
| 54 | }; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub open { | ||
| 58 | my $self = shift; | ||
| 59 | my ($output) = @_; | ||
| 60 | |||
| 61 | $output ||= $self->output; | ||
| 62 | $self->brik_help_run_undef_arg('open', $output) or return; | ||
| 63 | |||
| 64 | my $encoding = $self->encoding; | ||
| 65 | if ($encoding eq 'ascii') { | ||
| 66 | $encoding = ''; | ||
| 67 | } | ||
| 68 | |||
| 69 | my $out; | ||
| 70 | if ($self->append) { | ||
| 71 | my $r = open($out, ">>$encoding", $output); | ||
| 72 | if (! defined($r)) { | ||
| 73 | return $self->log->error("open: open: append file [$output]: $!"); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | elsif (! $self->append && $self->overwrite) { | ||
| 77 | my $r = open($out, ">$encoding", $output); | ||
| 78 | if (! defined($r)) { | ||
| 79 | return $self->log->error("open: open: write file [$output]: $!"); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | elsif (! $self->append && ! $self->overwrite && -f $output) { | ||
| 83 | $self->log->info("open: we will not overwrite an existing file. See:"); | ||
| 84 | return $self->log->error($self->brik_help_set('overwrite')); | ||
| 85 | } | ||
| 86 | |||
| 87 | my $previous_default = select(STDOUT); | ||
| 88 | select($out); | ||
| 89 | $| = $self->unbuffered ? 1 : 0; | ||
| 90 | select($previous_default); | ||
| 91 | |||
| 92 | $self->log->debug("open: fd [$out]"); | ||
| 93 | |||
| 94 | return $self->fd($out); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub close { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | if (defined($self->fd)) { | ||
| 101 | close($self->fd); | ||
| 102 | $self->fd(undef); | ||
| 103 | } | ||
| 104 | |||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub lock { | ||
| 109 | my $self = shift; | ||
| 110 | |||
| 111 | my $fd = $self->fd or return 1; | ||
| 112 | |||
| 113 | my $r = flock($fd, Fcntl::LOCK_EX()); | ||
| 114 | if (! defined($r)) { | ||
| 115 | return $self->log->error("lock: flock: locking failed: $!"); | ||
| 116 | } | ||
| 117 | |||
| 118 | $r = seek($fd, 0, Fcntl::SEEK_END()); | ||
| 119 | if (! defined($r)) { | ||
| 120 | return $self->log->error("lock: seek: seeking failed: $!"); | ||
| 121 | } | ||
| 122 | |||
| 123 | $self->log->debug("lock: locking fd"); | ||
| 124 | |||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub unlock { | ||
| 129 | my $self = shift; | ||
| 130 | |||
| 131 | my $fd = $self->fd or return 1; | ||
| 132 | |||
| 133 | my $r = flock($fd, Fcntl::LOCK_UN()); | ||
| 134 | if (! defined($r)) { | ||
| 135 | return $self->log->error("lock: flock: unlocking failed: $!"); | ||
| 136 | } | ||
| 137 | |||
| 138 | $self->log->debug("unlock: unlocking fd"); | ||
| 139 | |||
| 140 | return 1; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub write { | ||
| 144 | my $self = shift; | ||
| 145 | my ($data) = @_; | ||
| 146 | |||
| 147 | my $fd = $self->fd; | ||
| 148 | $self->brik_help_run_undef_arg('open', $fd) or return; | ||
| 149 | $self->brik_help_run_undef_arg('write', $data) or return; | ||
| 150 | |||
| 151 | if ($self->use_locking) { | ||
| 152 | $self->lock or return; | ||
| 153 | } | ||
| 154 | |||
| 155 | $self->log->debug("write: data[$data]"); | ||
| 156 | |||
| 157 | if (ref($data) eq 'ARRAY') { | ||
| 158 | for my $this (@$data) { | ||
| 159 | print $fd $this."\n"; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | else { | ||
| 163 | ref($data) eq 'SCALAR' ? print $fd $$data : print $fd $data; | ||
| 164 | } | ||
| 165 | |||
| 166 | if ($self->use_locking) { | ||
| 167 | $self->unlock or return; | ||
| 168 | } | ||
| 169 | |||
| 170 | return $data; | ||
| 171 | } | ||
| 172 | |||
| 173 | 1; | ||
| 174 | |||
| 175 | __END__ | ||