当前位置: 首页>>代码示例>>C++>>正文


C++ FileNode::writeHandler方法代码示例

本文整理汇总了C++中FileNode::writeHandler方法的典型用法代码示例。如果您正苦于以下问题:C++ FileNode::writeHandler方法的具体用法?C++ FileNode::writeHandler怎么用?C++ FileNode::writeHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在FileNode的用法示例。


在下文中一共展示了FileNode::writeHandler方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: get

bool GlusterBackend::get(const SyncEvent* _getEvent) {
  if(_getEvent == nullptr || _getEvent->fullPathBuffer.empty())
    return false;

  //Try to find file
  /**
   * RETURN VALUES
   * NULL   : Failure. @errno will be set with the type of failure.
   * Others : Pointer to the opened glfs_fd_t.
   */
  glfs_fd_t *fd = glfs_open((glfs_t*)fs, _getEvent->fullPathBuffer.c_str(), O_RDONLY);
  if(!fd){
    LOG(ERROR)<<"Error while openeing a handle to:"<<_getEvent->fullPathBuffer;
    return false;
  }

  FileNode* fileNode = FileSystem::getInstance().findAndOpenNode(_getEvent->fullPathBuffer);
  //If File exist then we won't download it!
  if(fileNode!=nullptr){
    LOG(DEBUG)<<"File "<<fileNode->getFullPath()<<" already exist! no need to download.";
    //Close it! so it can be removed if needed
    uint64_t inodeNum = FileSystem::getInstance().assignINodeNum((intptr_t)fileNode);
    fileNode->close(inodeNum);
    glfs_close(fd);
    return false;
  }

  //Now create a file in FS
  //handle directories
  //FileSystem::getInstance().createHierarchy(_getEvent->fullPathBuffer,false);
  //FileNode *newFile = FileSystem::getInstance().mkFile(_getEvent->fullPathBuffer,false,true);//open
  string name = FileSystem::getInstance().getFileNameFromPath(_getEvent->fullPathBuffer);
  FileNode* newFile = new FileNode(name,_getEvent->fullPathBuffer, false,false);
  if(newFile == nullptr){
    LOG(ERROR)<<"Failed to create a newNode:"<<_getEvent->fullPathBuffer;
    glfs_close(fd);
    return false;
  }

  //and write the content
  uint64_t bufSize = 64ll*1024ll*1024ll;
  char *buff = new char[bufSize];//64MB buffer
  size_t offset = 0;
  int64_t read = 0;
  do {

    //No need to download it anymore.
    if(newFile->mustBeDeleted()){
      delete newFile;
      glfs_close(fd);
      return true;
    }

    read = glfs_pread(fd,buff,bufSize,offset,0);

    if(read < 0){
      LOG(ERROR)<<"Error while reading: "<<_getEvent->fullPathBuffer;
      glfs_close(fd);
      return false;
    }

    if(read>0){
      FileNode* afterMove = nullptr;
      long retCode = newFile->writeHandler(buff,offset,read,afterMove,true);

      while(retCode == -1)//-1 means moving
        retCode = newFile->writeHandler(buff,offset,read,afterMove,true);

      if(afterMove){
        newFile = afterMove;
        FileSystem::getInstance().replaceAllInodesByNewNode((intptr_t)newFile,(intptr_t)afterMove);
      }
      //Check space availability
      if(retCode < 0) {
        LOG(ERROR)<<"Error in writing file:"<<newFile->getFullPath()<<", probably no diskspace, Code:"<<retCode;
        delete newFile;
        glfs_close(fd);
        delete []buff;
        return false;
      }
    }

    offset += read;
  }while(read);

  newFile->setNeedSync(false);//We have just created this file so it's upload flag false
  glfs_close(fd);
  delete []buff;
  //Add it to File system
  if(FileSystem::getInstance().createHierarchy(_getEvent->fullPathBuffer,false)==nullptr){
    LOG(ERROR)<<"Error in creating hierarchy for newly downloaded file:"<<newFile->getFullPath();
    delete newFile;
    return false;
  }
  if(!FileSystem::getInstance().addFile(newFile)){
    LOG(ERROR)<<"Error in adding newly downloaded file:"<<newFile->getFullPath();
    delete newFile;
    return false;
  }
  //Gone well
//.........这里部分代码省略.........
开发者ID:40a,项目名称:BFS,代码行数:101,代码来源:GlusterBackend.cpp


注:本文中的FileNode::writeHandler方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。