void Space::Release(Space* pSpace, FileHandle& pHandler)
{
  if (NULL == pSpace)
    return;

  switch(pSpace->type()) {
    case ALLOCATED_ARRAY:
      free(pSpace->memory());
      break;
    case MMAPED:
      if (!pHandler.munmap(pSpace->memory(), pSpace->size()))
        error(diag::err_cannot_munmap_file) << pHandler.path();
      break;
    default: // external and unallocated memory buffers
      break;
  } // end of switch
}
void Space::Sync(Space* pSpace, FileHandle& pHandler)
{
  if (NULL == pSpace || !pHandler.isWritable())
    return;

  switch(pSpace->type()) {
    case Space::ALLOCATED_ARRAY: {
      if (!pHandler.write(pSpace->memory(),
                          pSpace->start(),
                          pSpace->size())) {
        error(diag::err_cannot_write_file) << pHandler.path()
                                           << pSpace->start()
                                           << pSpace->size();
      }
      return;
    }
    case Space::MMAPED:
    default: {
      // system will eventually write bakc the memory after
      // calling ::munmap
      return;
    }
  } // end of switch
}
Ejemplo n.º 3
0
void FileHandle::copyTo (FileHandle& dest) { 
    ifstream_ptr input;
    ofstream_ptr output;
    try {
        input = read();
        output = dest.write(false);
        char buffer[4096];
        while (true) {
            input->read(buffer, 4096);
            output->write(buffer, input->gcount());
            if(input->eof() || input->peek() == EOF) break;
        }
    } catch(std::runtime_error ex) {
        if(input->is_open()) input->close();
        if(output->is_open()) output->close();
        throw std::runtime_error("Error copying source file: " + path() + " (" + typetoString() + ")\n" + "To destination: " + dest.path() + " (" + dest.typetoString() + ")");
    }
   if(input->is_open()) input->close();
   if(output->is_open()) output->close();
}