- Timestamp:
- 2013-05-24T09:40:07+12:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gs2-extensions/parallel-building/trunk/src/perllib/FileUtils/HDThriftFS.pm
r27386 r27424 69 69 my $host = "localhost"; 70 70 my $port = 58660; 71 my $debug = 0; 71 72 my $debug_encoding = 0; 72 73 … … 108 109 if ($@) 109 110 { 110 & _printError('Unable to connect: ' . $@->{message}, 1);111 &FileUtils::printError('Unable to connect: ' . $@->{message}, 1); 111 112 } 112 113 } … … 135 136 ## _generateHDFSPath() 136 137 138 139 ## @function _printDebug() 140 # 141 sub _printDebug 142 { 143 my ($msg) = @_; 144 if ($debug) 145 { 146 my ($package, $filename, $line, $function) = caller(1); 147 print STDERR '[DEBUG] ' . $function . ': ' . $msg . "\n"; 148 } 149 } 150 ## _printDebug() 137 151 138 152 ################################################################################ … … 141 155 142 156 157 ## @function canRead() 158 # 159 sub canRead 160 { 161 my $path = shift(@_); 162 return &checkPermission($path, 'r', @_); 163 } 164 ## canRead() 165 166 167 ## @function checkPermission() 168 # 169 sub checkPermission 170 { 171 my ($path, $mode, $username, $usergroup) = @_; 172 my $offsets = {'r' => 0, 'w' => 1, 'x' => 2}; 173 # - ensure we have a connection to the thrift server 174 &_establishClient(); 175 # - convert the path into a proper thrift path object 176 $path = &_generateHDFSPath($path); 177 # - determine the user (defaults to current user) 178 if (!defined $username) 179 { 180 if ($ENV{'GSDLOS'} =~ /^windows$/i) 181 { 182 require Win32; 183 $username = Win32::LoginName(); 184 } 185 else 186 { 187 $username = getlogin || getpwuid($<); 188 } 189 } 190 # - determine the group 191 my $usergroups = {}; 192 if (defined $usergroup) 193 { 194 $usergroups = (ref $usergroup eq "HASH") ? $usergroup : {$usergroup => 1}; 195 } 196 else 197 { 198 if ($ENV{'GSDLOS'} =~ /^windows$/i) 199 { 200 # dunno 201 } 202 else 203 { 204 my $raw_groups = `groups`; 205 foreach my $group ( split(/\s/, $raw_groups)) 206 { 207 $usergroups->{$group} = 1; 208 } 209 } 210 } 211 # Retrieve details from the file 212 my $file_stat = $thrift_client->stat($path); 213 my $owner = $file_stat->{'owner'}; 214 my $group = $file_stat->{'group'}; 215 my $permissions = $file_stat->{'permission'}; 216 # Begin the cascade of tests to determine if the identified user belonging to 217 # the identified group can perform 'mode' access to the file. 218 my $has_permission = 0; 219 # - start with [u]ser permission 220 if (defined $owner && $username eq $owner) 221 { 222 my $target_char = substr($permissions, $offsets->{$mode}, 1); 223 if ($mode eq $target_char) 224 { 225 $has_permission = 1; 226 } 227 } 228 # - failing that, try [g]roup level permissions 229 if (!$has_permission && defined $group && defined $usergroups->{$group}) 230 { 231 my $target_char = substr($permissions, 3 + $offsets->{$mode}, 1); 232 if ($mode eq $target_char) 233 { 234 $has_permission = 1; 235 } 236 } 237 # - and finally try [o]ther level permission 238 if (!$has_permission) 239 { 240 my $target_char = substr($permissions, 6 + $offsets->{$mode}, 1); 241 if ($mode eq $target_char) 242 { 243 $has_permission = 1; 244 } 245 } 246 return $has_permission; 247 } 248 ## checkPermission 249 250 143 251 ## @function closeFileHandle() 144 252 # … … 146 254 { 147 255 my $fh_ref = shift(@_); 256 &_printDebug(''); 148 257 close($$fh_ref); 149 258 untie($$fh_ref); … … 238 347 239 348 349 ## @function isFilenameAbsolute() 350 # 351 sub isFilenameAbsolute 352 { 353 # File paths against HDFS must be. 354 return 1; 355 } 356 # isFilenameAbsolute() 357 358 240 359 ## @function makeDirectory() 241 360 # … … 279 398 { 280 399 my ($raw_path, $mode, $fh_ref) = @_; 400 &_printDebug('path: ' . $raw_path . ', mode: ' . $mode . ', fh_ref'); 281 401 # ensure we have a connection to the thrift server 282 402 &_establishClient();
Note:
See TracChangeset
for help on using the changeset viewer.