File Coverage

lib/SMB/v2/Command/Create.pm
Criterion Covered Total %
statement 15 56 26.7
branch 0 10 0.0
condition 0 9 0.0
subroutine 5 10 50.0
pod 0 5 0.0
total 20 90 22.2


line stmt bran cond sub pod time code
1             # SMB Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org
2             #
3             # This program is free software: you can redistribute it and/or modify
4             # it under the terms of the GNU General Public License as published by
5             # the Free Software Foundation, either version 3 of the License, or
6             # (at your option) any later version.
7             #
8             # This program is distributed in the hope that it will be useful,
9             # but WITHOUT ANY WARRANTY; without even the implied warranty of
10             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11             # GNU General Public License for more details.
12             #
13             # You should have received a copy of the GNU General Public License
14             # along with this program. If not, see .
15              
16             package SMB::v2::Command::Create;
17              
18 1     1   5 use strict;
  1         2  
  1         60  
19 1     1   6 use warnings;
  1         2  
  1         28  
20              
21 1     1   6 use parent 'SMB::v2::Command';
  1         2  
  1         6  
22              
23 1     1   547 use SMB::OpenFile;
  1         4  
  1         45  
24              
25             use constant {
26 1         905 OPTIONS_DIRECTORY_FILE => 0x00000001,
27             OPTIONS_WRITE_THROUGH => 0x00000002,
28             OPTIONS_SEQUENTIAL_ONLY => 0x00000004,
29             OPTIONS_NO_INTERMEDIATE_BUFFERING => 0x00000008,
30             OPTIONS_SYNCHRONOUS_IO_ALERT => 0x00000010,
31             OPTIONS_SYNCRHONOUS_IO_NONALERT => 0x00000020,
32             OPTIONS_NON_DIRECTORY_FILE => 0x00000040,
33             OPTIONS_COMPLETE_IF_OPLOCKED => 0x00000100,
34             OPTIONS_NO_EA_KNOWLEDGE => 0x00000200,
35             OPTIONS_RANDOM_ACCESS => 0x00000800,
36             OPTIONS_DELETE_ON_CLOSE => 0x00001000,
37             OPTIONS_OPEN_BY_FILE_ID => 0x00002000,
38             OPTIONS_OPEN_FOR_BACKUP_INTENT => 0x00004000,
39             OPTIONS_NO_COMPRESSION => 0x00008000,
40             OPTIONS_RESERVE_OPFILTER => 0x00100000,
41             OPTIONS_OPEN_REPARSE_POINT => 0x00200000,
42             OPTIONS_OPEN_NO_RECALL => 0x00400000,
43             OPTIONS_OPEN_FOR_FREE_SPACE_QUERY => 0x00800000,
44 1     1   6 };
  1         2  
45              
46             sub init ($) {
47 0     0 0   $_[0]->set(
48             security_flags => 0,
49             oplock => 0,
50             impersonation => 2,
51             create_flags => 0,
52             access_mask => 0x81,
53             file_attributes => 0,
54             share_access => 3,
55             disposition => 1,
56             options => 0,
57             file_name => '',
58              
59             flags => 0,
60             fid => undef,
61             openfile => undef,
62             )
63             }
64              
65             sub parse ($$) {
66 0     0 0   my $self = shift;
67 0           my $parser = shift;
68              
69 0 0         if ($self->is_response) {
70 0           my $file = SMB::File->new(name => $self->file_name);
71              
72 0           $self->oplock($parser->uint8);
73 0           $self->flags($parser->uint8);
74 0           my $action = $parser->uint32;
75              
76 0           my @file_params = (
77             $parser->uint64, # creation_time
78             $parser->uint64, # last_access_time
79             $parser->uint64, # last_write_time
80             $parser->uint64, # change_time
81             $parser->uint64, # allocation_size
82             $parser->uint64, # end_of_file
83             $parser->uint32, # attributes
84             );
85 0           $file->update(@file_params);
86              
87 0           $parser->uint32; # reserved
88 0           $self->fid($parser->fid2);
89 0           $self->openfile(SMB::OpenFile->new($file, 0, $action));
90             } else {
91 0           $self->security_flags($parser->uint8);
92 0           $self->oplock($parser->uint8);
93 0           $self->impersonation($parser->uint32);
94 0           $self->create_flags($parser->uint64);
95 0           $parser->uint64; # reserved
96 0           $self->access_mask($parser->uint32);
97 0           $self->file_attributes($parser->uint32);
98 0           $self->share_access($parser->uint32);
99 0           $self->disposition($parser->uint32);
100 0           $self->options($parser->uint32);
101 0           my $name_offset = $parser->uint16;
102 0           my $name_len = $parser->uint16;
103 0           $parser->uint32(0); # contexts
104 0           $parser->uint32(0);
105 0           $self->file_name($parser->utf16($name_len));
106             }
107              
108 0           return $self;
109             }
110              
111             sub pack ($$) {
112 0     0 0   my $self = shift;
113 0           my $packer = shift;
114              
115 0 0         if ($self->is_response) {
116 0           my $openfile = $self->openfile;
117 0   0       my $file = $openfile && $openfile->file;
118              
119 0 0 0       return $self->abort_pack($packer, SMB::STATUS_NO_SUCH_FILE)
      0        
120             unless $file && $file->exists && $self->fid;
121              
122 0           $packer
123             ->uint8($self->oplock)
124             ->uint8($self->flags)
125             ->uint32($openfile->action)
126             ->uint64($file->creation_time)
127             ->uint64($file->last_access_time)
128             ->uint64($file->last_write_time)
129             ->uint64($file->change_time)
130             ->uint64($file->allocation_size)
131             ->uint64($file->end_of_file)
132             ->uint32($file->attributes)
133             ->uint32(0) # reserved
134             ->fid2($self->fid)
135             ->uint32(0) # contexts
136             ->uint32(0)
137             ;
138             } else {
139 0           $packer
140             ->uint8($self->security_flags)
141             ->uint8($self->oplock)
142             ->uint32($self->impersonation)
143             ->uint64($self->create_flags)
144             ->uint64(0) # reserved
145             ->uint32($self->access_mask)
146             ->uint32($self->file_attributes)
147             ->uint32($self->share_access)
148             ->uint32($self->disposition)
149             ->uint32($self->options)
150             ->uint16($packer->diff('smb-header') + 12)
151             ->uint16(length($self->file_name) * 2)
152             ->uint32(0) # contexts
153             ->uint32(0)
154             ->utf16($self->file_name)
155             ->uint8(0)
156             ;
157             }
158             }
159              
160             sub requested_directory ($) {
161 0     0 0   my $self = shift;
162              
163 0 0         return $self->options & OPTIONS_DIRECTORY_FILE ? 1 : 0;
164             }
165              
166             sub requested_non_directory ($) {
167 0     0 0   my $self = shift;
168              
169 0 0         return $self->options & OPTIONS_NON_DIRECTORY_FILE ? 1 : 0;
170             }
171              
172             1;