<commit_before>// Copyright 2009 <NAME>.
// This source comes from the XtreemFS project. It is licensed under the GPLv2 (see COPYING for terms and conditions).

#include "xtreemfs/file.h"
#include "xtreemfs/mrc_proxy.h"
#include "xtreemfs/volume.h"
using namespace xtreemfs;

#include "yieldfs.h"

#include <errno.h>

#ifdef _WIN32
#include <windows.h>
#pragma warning( push )
#pragma warning( disable: 4100 )
#else
#include <errno.h>
#endif


#define FILE_OPERATION_BEGIN \
  try \
  {

#define FILE_OPERATION_END \
  } \
  catch ( ProxyExceptionResponse& proxy_exception_response ) \
  { \
    YIELD::platform::Exception::set_errno \
    ( \
      proxy_exception_response.get_platform_error_code() \
    ); \
    return false; \
  } \
  catch ( std::exception& ) \
  { \
    YIELD::platform::Exception::set_errno( EIO ); \
    return false; \
  }


class File::ReadBuffer 
  : public yidl::runtime::FixedBuffer
{
public:
  ReadBuffer( void* buf, size_t len )
    : FixedBuffer( len )
  {
    iov.iov_base = buf;
  }
};

class File::ReadResponse 
  : public org::xtreemfs::interfaces::OSDInterface::readResponse
{
public:
  ReadResponse( yidl::runtime::auto_Buffer buffer )
    : org::xtreemfs::interfaces::OSDInterface::readResponse
        ( org::xtreemfs::interfaces::ObjectData( 0, false, 0, buffer ) )
  { }
};

class File::ReadRequest 
  : public org::xtreemfs::interfaces::OSDInterface::readRequest
{
public:
  ReadRequest
  ( 
    const org::xtreemfs::interfaces::FileCredentials& file_credentials, 
    const std::string& file_id, 
    uint64_t object_number, 
    uint64_t object_version, 
    uint32_t offset, 
    uint32_t length, 
    yidl::runtime::auto_Buffer buffer 
  ) 
    : org::xtreemfs::interfaces::OSDInterface::readRequest
      ( 
        file_credentials, 
        file_id, 
        object_number, 
        object_version, 
        offset, 
        length 
       ),
    buffer( buffer )
  { }

  YIELD::concurrency::auto_Response createResponse() 
  { 
    return new ReadResponse( buffer ); 
  }

private:
  yidl::runtime::auto_Buffer buffer;
};


class File::WriteBuffer 
  : public yidl::runtime::FixedBuffer
{
public:
  WriteBuffer( const void* buf, size_t len )
    : FixedBuffer( len )
  {
    iov.iov_base = const_cast<void*>( buf );
    iov.iov_len = len;
  }
};


class File::XCapTimer 
  : public YIELD::platform::TimerQueue::Timer
{
public:
  XCapTimer( auto_File file, const YIELD::platform::Time& timeout )
    : YIELD::platform::TimerQueue::Timer( timeout ),
      file( file )
  { }

  XCapTimer& operator=( XCapTimer& )
  {
    return *this;
  }

  // YIELD::platform::TimerQueue::Timer
  void fire()
  {
    if ( !file->closed ) // See note in File::File re: the rationale for this
    {
      try
      {
        org::xtreemfs::interfaces::XCap renewed_xcap;

        file->parent_volume->get_log()->
          getStream( YIELD::platform::Log::LOG_INFO ) << 
          "xtreemfs::File: renewing XCap for file " << 
          file->file_credentials.get_xcap().get_file_id() << ".";

        file->parent_volume->get_mrc_proxy()->xtreemfs_renew_capability
        ( 
          file->file_credentials.get_xcap(),
          renewed_xcap      
        );

        file->parent_volume->get_log()->
          getStream( YIELD::platform::Log::LOG_INFO ) << 
          "xtreemfs::File: successfully renewed XCap for file " <<
          file->file_credentials.get_xcap().get_file_id() << ".";

        file->file_credentials.set_xcap( renewed_xcap );
      }
      catch ( std::exception& exc )
      {
        file->parent_volume->get_log()->
          getStream( YIELD::platform::Log::LOG_ERR ) << 
          "xtreemfs::File: caught exception trying to renew XCap for file " <<
          file->file_credentials.get_xcap().get_file_id() << 
          ": " << exc.what() << ".";
      }
    }
  }

private:
  auto_File file;
};


File::File
( 
  auto_Volume parent_volume, 
  const YIELD::platform::Path& path, 
  const org::xtreemfs::interfaces::FileCredentials& file_credentials 
)
: parent_volume( parent_volume ), 
  path( path ), 
  file_credentials( file_credentials )
{
  closed = false;

  selected_file_replica = 0;

  if ( file_credentials.get_xcap().get_expire_timeout_s() > 10 )
  {
    // Do not keep a reference to the xcap timer, since that would create
    // circular references with this object
    // Instead the timer will always fire, check whether the File is closed,
    // and take the last reference to the File with it
    // That means that the File will not be deleted until the xcap expires!
    // -> it's important to explicitly close() instead of relying on the destructor
    // close(). (The FUSE interface explicitly close()s on release()).
    YIELD::platform::TimerQueue::getDefaultTimerQueue().addTimer
    ( 
      new XCapTimer
      (
        incRef(), 
        ( file_credentials.get_xcap().get_expire_timeout_s() - 10 )* NS_IN_S 
      )
    );  
  }
  else 
    parent_volume->get_log()->getStream( YIELD::platform::Log::LOG_ERR ) <<
      "xtreemfs::File: received xcap that expires in less than 10 seconds, " <<
      "will not try to renew.";
}

File::~File()
{
  close();
}

bool File::close()
{
  if ( !closed )
  {
    try<commit_msg>client: xcap renewal: Linux fixes<commit_after>// Copyright 2009 <NAME>.
// This source comes from the XtreemFS project. It is licensed under the GPLv2 (see COPYING for terms and conditions).

#include "xtreemfs/file.h"
#include "xtreemfs/mrc_proxy.h"
#include "xtreemfs/volume.h"
using namespace xtreemfs;

#include "yieldfs.h"

#include <errno.h>

#ifdef _WIN32
#include <windows.h>
#pragma warning( push )
#pragma warning( disable: 4100 )
#else
#include <errno.h>
#endif


#define FILE_OPERATION_BEGIN \
  try \
  {

#define FILE_OPERATION_END \
  } \
  catch ( ProxyExceptionResponse& proxy_exception_response ) \
  { \
    YIELD::platform::Exception::set_errno \
    ( \
      proxy_exception_response.get_platform_error_code() \
    ); \
    return false; \
  } \
  catch ( std::exception& ) \
  { \
    YIELD::platform::Exception::set_errno( EIO ); \
    return false; \
  }


class File::ReadBuffer 
  : public yidl::runtime::FixedBuffer
{
public:
  ReadBuffer( void* buf, size_t len )
    : FixedBuffer( len )
  {
    iov.iov_base = buf;
  }
};

class File::ReadResponse 
  : public org::xtreemfs::interfaces::OSDInterface::readResponse
{
public:
  ReadResponse( yidl::runtime::auto_Buffer buffer )
    : org::xtreemfs::interfaces::OSDInterface::readResponse
        ( org::xtreemfs::interfaces::ObjectData( 0, false, 0, buffer ) )
  { }
};

class File::ReadRequest 
  : public org::xtreemfs::interfaces::OSDInterface::readRequest
{
public:
  ReadRequest
  ( 
    const org::xtreemfs::interfaces::FileCredentials& file_credentials, 
    const std::string& file_id, 
    uint64_t object_number, 
    uint64_t object_version, 
    uint32_t offset, 
    uint32_t length, 
    yidl::runtime::auto_Buffer buffer 
  ) 
    : org::xtreemfs::interfaces::OSDInterface::readRequest
      ( 
        file_credentials, 
        file_id, 
        object_number, 
        object_version, 
        offset, 
        length 
       ),
    buffer( buffer )
  { }

  YIELD::concurrency::auto_Response createResponse() 
  { 
    return new ReadResponse( buffer ); 
  }

private:
  yidl::runtime::auto_Buffer buffer;
};


class File::WriteBuffer 
  : public yidl::runtime::FixedBuffer
{
public:
  WriteBuffer( const void* buf, size_t len )
    : FixedBuffer( len )
  {
    iov.iov_base = const_cast<void*>( buf );
    iov.iov_len = len;
  }
};


class File::XCapTimer 
  : public YIELD::platform::TimerQueue::Timer
{
public:
  XCapTimer( auto_File file, const YIELD::platform::Time& timeout )
    : YIELD::platform::TimerQueue::Timer( timeout ),
      file( file )
  { }

  XCapTimer& operator=( XCapTimer& )
  {
    return *this;
  }

  // YIELD::platform::TimerQueue::Timer
  void fire()
  {
    if ( !file->closed ) // See note in File::File re: the rationale for this
    {
      try
      {
        org::xtreemfs::interfaces::XCap renewed_xcap;

        file->parent_volume->get_log()->
          getStream( YIELD::platform::Log::LOG_INFO ) << 
          "xtreemfs::File: renewing XCap for file " << 
          file->file_credentials.get_xcap().get_file_id() << ".";

        file->parent_volume->get_mrc_proxy()->xtreemfs_renew_capability
        ( 
          file->file_credentials.get_xcap(),
          renewed_xcap      
        );

        file->parent_volume->get_log()->
          getStream( YIELD::platform::Log::LOG_INFO ) << 
          "xtreemfs::File: successfully renewed XCap for file " <<
          file->file_credentials.get_xcap().get_file_id() << ".";

        file->file_credentials.set_xcap( renewed_xcap );

        if ( renewed_xcap.get_expire_timeout_s() > 10 )
        {
          // Add another timer for the renewed xcap
          // Don't use periods here on the pessimistic assumption that
          // most xcaps will never be renewed
          YIELD::platform::TimerQueue::getDefaultTimerQueue().addTimer
          (
            new XCapTimer
            (
              file,
              ( renewed_xcap.get_expire_timeout_s() - 10 ) * NS_IN_S
            )
          );
        }
        else 
          file->parent_volume->get_log()->getStream( YIELD::platform::Log::LOG_ERR ) <<
            "xtreemfs::File: received xcap for file " << renewed_xcap.get_file_id() <<
            "that expires in less than 10 seconds, will not try to renew.";
      }
      catch ( std::exception& exc )
      {
        file->parent_volume->get_log()->
          getStream( YIELD::platform::Log::LOG_ERR ) << 
          "xtreemfs::File: caught exception trying to renew XCap for file " <<
          file->file_credentials.get_xcap().get_file_id() << 
          ": " << exc.what() << ".";
      }
    }
  }

private:
  auto_File file;
};


File::File
( 
  auto_Volume parent_volume, 
  const YIELD::platform::Path& path, 
  const org::xtreemfs::interfaces::FileCredentials& file_credentials 
)
: parent_volume( parent_volume ), 
  path( path ), 
  file_credentials( file_credentials )
{
  closed = false;

  selected_file_replica = 0;

  if ( file_credentials.get_xcap().get_expire_timeout_s() > 10 )
  {
    // Do not keep a reference to the xcap timer, since that would create
    // circular references with this object
    // Instead the timer will always fire, check whether the File is closed,
    // and take the last reference to the File with it
    // That means that the File will not be deleted until the xcap expires!
    // -> it's important to explicitly close() instead of relying on the destructor
    // close(). (The FUSE interface explicitly close()s on release()).
    YIELD::platform::TimerQueue::getDefaultTimerQueue().addTimer
    ( 
      new XCapTimer
      (
        incRef(), 
        ( file_credentials.get_xcap().get_expire_timeout_s() - 10 ) * NS_IN_S 
      )
    );  
  }
  else 
    parent_volume->get_log()->getStream( YIELD::platform::Log::LOG_ERR ) <<
      "xtreemfs::File: received xcap that expires in less than 10 seconds, " <<
      "will not try to renew.";
}

File::~File()
{
  close();
}

bool File::close()
{
  if ( !closed )
  {
    try
{'fraction_non_alphanumeric': 0.12646035851244092, 'fraction_numerical': 0.005529296352448051, 'mean_word_length': 2.8469982847341337, 'pattern_counts': {'":': 2, '<': 71, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6579285', 'n_tokens_mistral': 3817, 'n_tokens_neox': 3638, 'n_words': 780}