| 1 | # | ||||||||||||||||||||
| 2 | # Copyright (c) 2003,2004,2005 Alexander Taler (dissent@0--0.org) | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # All rights reserved. This program is free software; you can redistribute it | ||||||||||||||||||||
| 5 | # and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | package VCS::LibCVS::Datum::FileContents; | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | use IO::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | VCS::LibCVS::Datum::FileContents - File contents for use in CVS | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | $fc = VCS::LibCVS::Datum::FileContents->new("/cvs/dir/new.c"); | ||||||||||||||||||||
| 22 | $rq = VCS::LibCVS::Client::Request::Modified->new(["new.c",$mode,$fc]); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | Datum::FileContents presents the contents of a file in a way which is suitable | ||||||||||||||||||||
| 27 | for the various purposes of LibCVS. Important aspects are the transmission of | ||||||||||||||||||||
| 28 | the length of the file before the contents of the file itself, and the | ||||||||||||||||||||
| 29 | conversion of line terminators. | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | A Datum::FileContents does not consist of a group of lines, so it is handled | ||||||||||||||||||||
| 32 | differently from the others and it overrides most of the class routines. | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =head1 SUPERCLASS | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | VCS::LibCVS::Datum | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | ############################################################################### | ||||||||||||||||||||
| 41 | # Class constants | ||||||||||||||||||||
| 42 | ############################################################################### | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 1 | ▶ | use constant REVISION => '$Header: /cvsroot/libcvs-perl/libcvs-perl/VCS/LibCVS/Datum/FileContents.pm,v 1.11 2005/10/10 12:52:12 dissent Exp $ '; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 1 | ▶ | use vars ('@ISA'); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 47 | 1 | @ISA = ("VCS::LibCVS::Datum"); | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | ############################################################################### | ||||||||||||||||||||
| 50 | # Private variables | ||||||||||||||||||||
| 51 | ############################################################################### | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | # $self->{Length} The length of the contents | ||||||||||||||||||||
| 54 | # $self->{Contents} The contents of the file, right now just a big scalar | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | ############################################################################### | ||||||||||||||||||||
| 57 | # Class routines | ||||||||||||||||||||
| 58 | ############################################################################### | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =head1 CLASS ROUTINES | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =head2 B<new()> | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | $arg = Datum::FileContents->new($data) | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | =over 4 | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =item return type: Datum::FileContents | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =item argument 1 type: ... | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =over 2 | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =item E<32>E<32>option 1: IO::Handle | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | An IO::Handle object from which the file data will be read. The file data | ||||||||||||||||||||
| 77 | must arrive over the IO::Handle in the same manner as the CVS protocol, ie. a | ||||||||||||||||||||
| 78 | file length in bytes followed by newline followed by the file transmission. | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =item E<32>E<32>option 2: scalar | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | The name of a file in the local file system. This file will not be read until | ||||||||||||||||||||
| 83 | the Datum is accessed, so if it changes results may be unpredictable. | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =item E<32>E<32>option 3: Datum::FileContents | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | If the argument is already a Datum::FileContents object, it will be | ||||||||||||||||||||
| 88 | returned. It will I<not> be copied. | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =item E<32>E<32>option 4: \%hash ref | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | If the hash ref contains a parameter named 'scalar', it will be used as the | ||||||||||||||||||||
| 93 | entire contents of the file. | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =back | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =back | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | Construct a new Datum::FileContents. The IO::Handle option is generally used | ||||||||||||||||||||
| 100 | when reading responses from the server, the others when constructing requests. | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 105 | 0 | my ($class, $arg_data) = @_; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | # if the data is already of the right type, just return it | ||||||||||||||||||||
| 108 | 0 | ▶ | return $arg_data if (UNIVERSAL::isa($arg_data, $class)); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | my $that = bless {}, $class; | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 0 | ▶ | if (!ref($arg_data)) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 113 | 0 | my $ioh = IO::File->new($arg_data, "r"); | |||||||||||||||||||
| 114 | 0 | $that->{Length} = (stat($arg_data))[7]; | |||||||||||||||||||
| 115 | 0 | $that->_read_from_ioh($ioh); | |||||||||||||||||||
| 116 | } elsif (UNIVERSAL::isa($arg_data, "IO::Handle")) { | ||||||||||||||||||||
| 117 | 0 | $that->{Length} = $arg_data->getline(); | |||||||||||||||||||
| 118 | 0 | chomp $that->{Length}; | |||||||||||||||||||
| 119 | 0 | $that->_read_from_ioh($arg_data); | |||||||||||||||||||
| 120 | ▶ | } elsif ((ref($arg_data) eq "HASH") && (my $scalar = $arg_data->{'scalar'})) { | |||||||||||||||||||
|
Condition0%0/3
ref $arg_data eq "HASH" and my $scalar = $arg_data->{"scalar"}
MC/DC0%0/2
ref $arg_data eq "HASH" and my $scalar = $arg_data->{"scalar"}
ref $arg_data eq "HASH"
my $scalar = $arg_data->{"scalar"}
Truth table0%0/3
A and B
A= ref $arg_data eq "HASH"B= my $scalar = $arg_data->{"scalar"}
| |||||||||||||||||||||
| 121 | 0 | $that->{Length} = length $scalar; | |||||||||||||||||||
| 122 | 0 | $that->{Contents} = $scalar; | |||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||
| 124 | 0 | confess "Wrong type for data of Arg:" . ref($arg_data); | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | return $that; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | ############################################################################### | ||||||||||||||||||||
| 131 | # Instance routines | ||||||||||||||||||||
| 132 | ############################################################################### | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =head1 INSTANCE ROUTINES | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =head2 B<as_string()> | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | $datum->as_string() | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =over 4 | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =item return type: scalar string | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =back | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | Returns the file contents as a scalar, which should not be modified. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | ▶ | sub as_string { | ||||||||||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 151 | 0 | my $self = shift; | |||||||||||||||||||
| 152 | 0 | return $self->{Contents}; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =head2 B<as_protocol_string()> | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | $file_contents = $datum->as_protocol_string() | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =over 4 | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =item return type: string scalar | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =back | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | Returns the Datum::FileContents as a string suitable for being sent to the | ||||||||||||||||||||
| 166 | server: filelength, newline, filecontents. | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 0 | ▶ | sub as_protocol_string { | ||||||||||||||||||
|
Subroutine
as_protocol_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 171 | 0 | my $self = shift; | |||||||||||||||||||
| 172 | 0 | return $self->{Length} . "\n" . $self->{Contents}; | |||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | ############################################################################### | ||||||||||||||||||||
| 176 | # Private routines | ||||||||||||||||||||
| 177 | ############################################################################### | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | # read data in from an io handle | ||||||||||||||||||||
| 180 | # it expects to find the number of bytes to read in $self->{Length} | ||||||||||||||||||||
| 181 | # the data is put in $self->{Contents} | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | # This doesn't handle network stuff very well. read isn't properly documented, | ||||||||||||||||||||
| 184 | # but I assume that it's going to block on network stuff, and I'll have to muck | ||||||||||||||||||||
| 185 | # about with select and I don't want to do that. | ||||||||||||||||||||
| 186 | 0 | ▶ | sub _read_from_ioh { | ||||||||||||||||||
|
Subroutine
_read_from_ioh: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 187 | 0 | my ($self, $ioh) = @_; | |||||||||||||||||||
| 188 | 0 | my $num_bytes_read = $ioh->read($self->{Contents}, $self->{Length}); | |||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 0 | ▶ | confess "Error on read." if (!defined ($num_bytes_read)); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 191 | 0 | ▶ | confess "Wrong number of bytes" if ($num_bytes_read != $self->{Length}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =head1 SEE ALSO | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | VCS::LibCVS::Datum | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =cut | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 1 | 1; | |||||||||||||||||||