Program: 105222
Project: 105222
Buggy source:
@Override public Answer execute(final BackupSnapshotCommand command,final LibvirtComputingResource libvirtComputingResource){
  final Long dcId=command.getDataCenterId();
  final Long accountId=command.getAccountId();
  final Long volumeId=command.getVolumeId();
  final String secondaryStoragePoolUrl=command.getSecondaryStorageUrl();
  final String snapshotName=command.getSnapshotName();
  String snapshotDestPath=null;
  String snapshotRelPath=null;
  final String vmName=command.getVmName();
  KVMStoragePool secondaryStoragePool=null;
  final KVMStoragePoolManager storagePoolMgr=libvirtComputingResource.getStoragePoolMgr();
  try {
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper=libvirtComputingResource.getLibvirtUtilitiesHelper();
    final Connect conn=libvirtUtilitiesHelper.getConnectionByVmName(vmName);
    secondaryStoragePool=storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
    final String ssPmountPath=secondaryStoragePool.getLocalPath();
    snapshotRelPath=File.separator + "String_Node_Str" + File.separator+ dcId+ File.separator+ accountId+ File.separator+ volumeId;
    snapshotDestPath=ssPmountPath + File.separator + "String_Node_Str"+ File.separator+ dcId+ File.separator+ accountId+ File.separator+ volumeId;
    final KVMStoragePool primaryPool=storagePoolMgr.getStoragePool(command.getPool().getType(),command.getPrimaryStoragePoolNameLabel());
    final KVMPhysicalDisk snapshotDisk=primaryPool.getPhysicalDisk(command.getVolumePath());
    final String manageSnapshotPath=libvirtComputingResource.manageSnapshotPath();
    final int cmdsTimeout=libvirtComputingResource.getCmdsTimeout();
    if (primaryPool.getType() == StoragePoolType.RBD) {
      try {
        final Rados r=new Rados(primaryPool.getAuthUserName());
        r.confSet("String_Node_Str",primaryPool.getSourceHost() + "String_Node_Str" + primaryPool.getSourcePort());
        r.confSet("String_Node_Str",primaryPool.getAuthSecret());
        r.confSet("String_Node_Str","String_Node_Str");
        r.connect();
        s_logger.debug("String_Node_Str" + r.confGet("String_Node_Str"));
        final IoCTX io=r.ioCtxCreate(primaryPool.getSourceDir());
        final Rbd rbd=new Rbd(io);
        final RbdImage image=rbd.open(snapshotDisk.getName(),snapshotName);
        final File fh=new File(snapshotDestPath);
        try (BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(fh))){
          final int chunkSize=4194304;
          long offset=0;
          s_logger.debug("String_Node_Str" + snapshotName + "String_Node_Str"+ snapshotDestPath);
          while (true) {
            final byte[] buf=new byte[chunkSize];
            final int bytes=image.read(offset,buf,chunkSize);
            if (bytes <= 0) {
              break;
            }
            bos.write(buf,0,bytes);
            offset+=bytes;
          }
          s_logger.debug("String_Node_Str" + snapshotName + "String_Node_Str"+ snapshotDestPath+ "String_Node_Str"+ offset);
        }
 catch (        final IOException ex) {
          s_logger.error("String_Node_Str" + ex.getMessage());
        }
        r.ioCtxDestroy(io);
      }
 catch (      final RadosException e) {
        s_logger.error("String_Node_Str" + e.getMessage());
        return new BackupSnapshotAnswer(command,false,e.toString(),null,true);
      }
catch (      final RbdException e) {
        s_logger.error("String_Node_Str" + snapshotDisk.getName() + "String_Node_Str"+ e.getMessage());
        return new BackupSnapshotAnswer(command,false,e.toString(),null,true);
      }
    }
 else {
      final Script scriptCommand=new Script(manageSnapshotPath,cmdsTimeout,s_logger);
      scriptCommand.add("String_Node_Str",snapshotDisk.getPath());
      scriptCommand.add("String_Node_Str",snapshotName);
      scriptCommand.add("String_Node_Str",snapshotDestPath);
      scriptCommand.add("String_Node_Str",snapshotName);
      final String result=scriptCommand.execute();
      if (result != null) {
        s_logger.debug("String_Node_Str" + result);
        return new BackupSnapshotAnswer(command,false,result,null,true);
      }
    }
    DomainState state=null;
    Domain vm=null;
    if (vmName != null) {
      try {
        vm=libvirtComputingResource.getDomain(conn,command.getVmName());
        state=vm.getInfo().state;
      }
 catch (      final LibvirtException e) {
        s_logger.trace("String_Node_Str",e);
      }
    }
    final KVMStoragePool primaryStorage=storagePoolMgr.getStoragePool(command.getPool().getType(),command.getPool().getUuid());
    if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
      final MessageFormat snapshotXML=new MessageFormat("String_Node_Str" + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str");
      final String vmUuid=vm.getUUIDString();
      final Object[] args=new Object[]{snapshotName,vmUuid};
      final String snapshot=snapshotXML.format(args);
      s_logger.debug(snapshot);
      final DomainSnapshot snap=vm.snapshotLookupByName(snapshotName);
      snap.delete(0);
      vm=libvirtComputingResource.getDomain(conn,command.getVmName());
      state=vm.getInfo().state;
      if (state == DomainState.VIR_DOMAIN_PAUSED) {
        vm.resume();
      }
    }
 else {
      final Script scriptCommand=new Script(manageSnapshotPath,cmdsTimeout,s_logger);
      scriptCommand.add("String_Node_Str",snapshotDisk.getPath());
      scriptCommand.add("String_Node_Str",snapshotName);
      final String result=scriptCommand.execute();
      if (result != null) {
        s_logger.debug("String_Node_Str" + result);
        return new BackupSnapshotAnswer(command,false,"String_Node_Str" + result,null,true);
      }
    }
  }
 catch (  final LibvirtException e) {
    return new BackupSnapshotAnswer(command,false,e.toString(),null,true);
  }
catch (  final CloudRuntimeException e) {
    return new BackupSnapshotAnswer(command,false,e.toString(),null,true);
  }
 finally {
    if (secondaryStoragePool != null) {
      storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(),secondaryStoragePool.getUuid());
    }
  }
  return new BackupSnapshotAnswer(command,true,null,snapshotRelPath + File.separator + snapshotName,true);
}

Fixed version (ground truth):
@Override public Answer execute(final BackupSnapshotCommand command,final LibvirtComputingResource libvirtComputingResource){
  final Long dcId=command.getDataCenterId();
  final Long accountId=command.getAccountId();
  final Long volumeId=command.getVolumeId();
  final String secondaryStoragePoolUrl=command.getSecondaryStorageUrl();
  final String snapshotName=command.getSnapshotName();
  String snapshotDestPath=null;
  String snapshotRelPath=null;
  final String vmName=command.getVmName();
  KVMStoragePool secondaryStoragePool=null;
  final KVMStoragePoolManager storagePoolMgr=libvirtComputingResource.getStoragePoolMgr();
  try {
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper=libvirtComputingResource.getLibvirtUtilitiesHelper();
    final Connect conn=libvirtUtilitiesHelper.getConnectionByVmName(vmName);
    secondaryStoragePool=storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
    final String ssPmountPath=secondaryStoragePool.getLocalPath();
    snapshotRelPath=File.separator + "String_Node_Str" + File.separator+ dcId+ File.separator+ accountId+ File.separator+ volumeId;
    snapshotDestPath=ssPmountPath + File.separator + "String_Node_Str"+ File.separator+ dcId+ File.separator+ accountId+ File.separator+ volumeId;
    final KVMStoragePool primaryPool=storagePoolMgr.getStoragePool(command.getPool().getType(),command.getPrimaryStoragePoolNameLabel());
    final KVMPhysicalDisk snapshotDisk=primaryPool.getPhysicalDisk(command.getVolumePath());
    final String manageSnapshotPath=libvirtComputingResource.manageSnapshotPath();
    final int cmdsTimeout=libvirtComputingResource.getCmdsTimeout();
    if (primaryPool.getType() == StoragePoolType.RBD) {
      try {
        final Rados r=new Rados(primaryPool.getAuthUserName());
        r.confSet("String_Node_Str",primaryPool.getSourceHost() + "String_Node_Str" + primaryPool.getSourcePort());
        r.confSet("String_Node_Str",primaryPool.getAuthSecret());
        r.confSet("String_Node_Str","String_Node_Str");
        r.connect();
        s_logger.debug("String_Node_Str" + r.confGet("String_Node_Str"));
        final IoCTX io=r.ioCtxCreate(primaryPool.getSourceDir());
        final Rbd rbd=new Rbd(io);
        final RbdImage image=rbd.open(snapshotDisk.getName(),snapshotName);
        final File fh=new File(snapshotDestPath);
        try (BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(fh))){
          final int chunkSize=4194304;
          long offset=0;
          s_logger.debug("String_Node_Str" + snapshotName + "String_Node_Str"+ snapshotDestPath);
          while (true) {
            final byte[] buf=new byte[chunkSize];
            final int bytes=image.read(offset,buf,chunkSize);
            if (bytes <= 0) {
              break;
            }
            bos.write(buf,0,bytes);
            offset+=bytes;
          }
          s_logger.debug("String_Node_Str" + snapshotName + "String_Node_Str"+ snapshotDestPath+ "String_Node_Str"+ offset);
        }
 catch (        final IOException ex) {
          s_logger.error("String_Node_Str" + ex.getMessage());
        }
        r.ioCtxDestroy(io);
      }
 catch (      final RadosException e) {
        s_logger.error("String_Node_Str" + e.getMessage());
        return new BackupSnapshotAnswer(command,false,e.toString(),null,true);
      }
catch (      final RbdException e) {
        s_logger.error("String_Node_Str" + snapshotDisk.getName() + "String_Node_Str"+ e.getMessage());
        return new BackupSnapshotAnswer(command,false,e.toString(),null,true);
      }
    }
 else {
      final Script scriptCommand=new Script(manageSnapshotPath,cmdsTimeout,s_logger);
      scriptCommand.add("String_Node_Str",snapshotDisk.getPath());
      scriptCommand.add("String_Node_Str",snapshotName);
      scriptCommand.add("String_Node_Str",snapshotDestPath);
      scriptCommand.add("String_Node_Str",snapshotName);
      final String result=scriptCommand.execute();
      if (result != null) {
        s_logger.debug("String_Node_Str" + result);
        return new BackupSnapshotAnswer(command,false,result,null,true);
      }
    }
    DomainState state=null;
    Domain vm=null;
    if (vmName != null) {
      try {
        vm=libvirtComputingResource.getDomain(conn,command.getVmName());
        state=vm.getInfo().state;
      }
 catch (      final LibvirtException e) {
        s_logger.trace("String_Node_Str",e);
      }
    }
    final KVMStoragePool primaryStorage=storagePoolMgr.getStoragePool(command.getPool().getType(),command.getPool().getUuid());
    if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
      final MessageFormat snapshotXML=new MessageFormat("String_Node_Str" + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str");
      final String vmUuid=vm.getUUIDString();
      final Object[] args=new Object[]{snapshotName,vmUuid};
      final String snapshot=snapshotXML.format(args);
      s_logger.debug(snapshot);
      final DomainSnapshot snap=vm.snapshotLookupByName(snapshotName);
      if (snap != null) {
        snap.delete(0);
      }
 else {
        throw new CloudRuntimeException("String_Node_Str" + snapshotName);
      }
      vm=libvirtComputingResource.getDomain(conn,command.getVmName());
      state=vm.getInfo().state;
      if (state == DomainState.VIR_DOMAIN_PAUSED) {
        vm.resume();
      }
    }
 else {
      final Script scriptCommand=new Script(manageSnapshotPath,cmdsTimeout,s_logger);
      scriptCommand.add("String_Node_Str",snapshotDisk.getPath());
      scriptCommand.add("String_Node_Str",snapshotName);
      final String result=scriptCommand.execute();
      if (result != null) {
        s_logger.debug("String_Node_Str" + result);
        return new BackupSnapshotAnswer(command,false,"String_Node_Str" + result,null,true);
      }
    }
  }
 catch (  final LibvirtException e) {
    return new BackupSnapshotAnswer(command,false,e.toString(),null,true);
  }
catch (  final CloudRuntimeException e) {
    return new BackupSnapshotAnswer(command,false,e.toString(),null,true);
  }
 finally {
    if (secondaryStoragePool != null) {
      storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(),secondaryStoragePool.getUuid());
    }
  }
  return new BackupSnapshotAnswer(command,true,null,snapshotRelPath + File.separator + snapshotName,true);
}