Changeset 27424


Ignore:
Timestamp:
2013-05-24T09:40:07+12:00 (11 years ago)
Author:
jmt12
Message:

Adding canRead() and isAbsolute() functions, and some more debugging while tracking down issue with files of 4000-8000 bytes failing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs2-extensions/parallel-building/trunk/src/perllib/FileUtils/HDThriftFS.pm

    r27386 r27424  
    6969my $host = "localhost";
    7070my $port = 58660;
     71my $debug = 0;
    7172my $debug_encoding = 0;
    7273
     
    108109    if ($@)
    109110    {
    110       &_printError('Unable to connect: ' . $@->{message}, 1);
     111      &FileUtils::printError('Unable to connect: ' . $@->{message}, 1);
    111112    }
    112113  }
     
    135136## _generateHDFSPath()
    136137
     138
     139## @function _printDebug()
     140#
     141sub _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()
    137151
    138152################################################################################
     
    141155
    142156
     157## @function canRead()
     158#
     159sub canRead
     160{
     161  my $path = shift(@_);
     162  return &checkPermission($path, 'r', @_);
     163}
     164## canRead()
     165
     166
     167## @function checkPermission()
     168#
     169sub 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
    143251## @function closeFileHandle()
    144252#
     
    146254{
    147255  my $fh_ref = shift(@_);
     256  &_printDebug('');
    148257  close($$fh_ref);
    149258  untie($$fh_ref);
     
    238347
    239348
     349## @function isFilenameAbsolute()
     350#
     351sub isFilenameAbsolute
     352{
     353  # File paths against HDFS must be.
     354  return 1;
     355}
     356# isFilenameAbsolute()
     357
     358
    240359## @function makeDirectory()
    241360#
     
    279398{
    280399  my ($raw_path, $mode, $fh_ref) = @_;
     400  &_printDebug('path: ' . $raw_path . ', mode: ' . $mode . ', fh_ref');
    281401  # ensure we have a connection to the thrift server
    282402  &_establishClient();
Note: See TracChangeset for help on using the changeset viewer.