Program: 106149
Project: 106149
Buggy source:
@Override public Answer backupSnapshot(CopyCommand cmd){
  DataTO srcData=cmd.getSrcTO();
  DataTO destData=cmd.getDestTO();
  SnapshotObjectTO snapshot=(SnapshotObjectTO)srcData;
  PrimaryDataStoreTO primaryStore=(PrimaryDataStoreTO)snapshot.getDataStore();
  SnapshotObjectTO destSnapshot=(SnapshotObjectTO)destData;
  DataStoreTO imageStore=destData.getDataStore();
  if (!(imageStore instanceof NfsTO)) {
    return backupSnapshotForObjectStore(cmd);
  }
  NfsTO nfsImageStore=(NfsTO)imageStore;
  String secondaryStoragePoolUrl=nfsImageStore.getUrl();
  int index=snapshot.getPath().lastIndexOf("String_Node_Str");
  String snapshotName=snapshot.getPath().substring(index + 1);
  String volumePath=snapshot.getVolume().getPath();
  String snapshotDestPath=null;
  String snapshotRelPath=null;
  String vmName=snapshot.getVmName();
  KVMStoragePool secondaryStoragePool=null;
  try {
    Connect conn=LibvirtConnection.getConnectionByVmName(vmName);
    secondaryStoragePool=storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
    String ssPmountPath=secondaryStoragePool.getLocalPath();
    snapshotRelPath=destSnapshot.getPath();
    snapshotDestPath=ssPmountPath + File.separator + snapshotRelPath;
    KVMPhysicalDisk snapshotDisk=storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(),primaryStore.getUuid(),volumePath);
    KVMStoragePool primaryPool=snapshotDisk.getPool();
    if (primaryPool.getType() == StoragePoolType.RBD) {
      try {
        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.connect();
        s_logger.debug("String_Node_Str" + r.confGet("String_Node_Str"));
        IoCTX io=r.ioCtxCreate(primaryPool.getSourceDir());
        Rbd rbd=new Rbd(io);
        RbdImage image=rbd.open(snapshotDisk.getName(),snapshotName);
        long startTime=System.currentTimeMillis() / 1000;
        File snapDir=new File(snapshotDestPath);
        s_logger.debug("String_Node_Str" + snapDir.getAbsolutePath() + "String_Node_Str");
        FileUtils.forceMkdir(snapDir);
        File snapFile=new File(snapshotDestPath + "String_Node_Str" + snapshotName);
        s_logger.debug("String_Node_Str" + snapFile.getAbsolutePath());
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(snapFile));
        int chunkSize=4194304;
        long offset=0;
        while (true) {
          byte[] buf=new byte[chunkSize];
          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"+ snapFile.getAbsolutePath()+ "String_Node_Str"+ offset);
        bos.close();
        s_logger.debug("String_Node_Str" + snapshotName + "String_Node_Str"+ snapshotDisk.getName());
        image.snapRemove(snapshotName);
        r.ioCtxDestroy(io);
      }
 catch (      RadosException e) {
        s_logger.error("String_Node_Str" + e.getMessage());
        return new CopyCmdAnswer(e.toString());
      }
catch (      RbdException e) {
        s_logger.error("String_Node_Str" + snapshotDisk.getName() + "String_Node_Str"+ e.getMessage());
        return new CopyCmdAnswer(e.toString());
      }
catch (      FileNotFoundException e) {
        s_logger.error("String_Node_Str" + snapshotDestPath + "String_Node_Str"+ e.getMessage());
        return new CopyCmdAnswer(e.toString());
      }
catch (      IOException e) {
        s_logger.debug("String_Node_Str" + snapshotDestPath);
        return new CopyCmdAnswer(e.toString());
      }
    }
 else {
      Script command=new Script(_manageSnapshotPath,cmd.getWait() * 1000,s_logger);
      command.add("String_Node_Str",snapshotDisk.getPath());
      command.add("String_Node_Str",snapshotName);
      command.add("String_Node_Str",snapshotDestPath);
      command.add("String_Node_Str",snapshotName);
      String result=command.execute();
      if (result != null) {
        s_logger.debug("String_Node_Str" + result);
        return new CopyCmdAnswer(result);
      }
    }
    DomainInfo.DomainState state=null;
    Domain vm=null;
    if (vmName != null) {
      try {
        vm=this.resource.getDomain(conn,vmName);
        state=vm.getInfo().state;
      }
 catch (      LibvirtException e) {
        s_logger.trace("String_Node_Str",e);
      }
    }
    KVMStoragePool primaryStorage=storagePoolMgr.getStoragePool(primaryStore.getPoolType(),primaryStore.getUuid());
    if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
      DomainSnapshot snap=vm.snapshotLookupByName(snapshotName);
      snap.delete(0);
      vm=this.resource.getDomain(conn,vmName);
      state=vm.getInfo().state;
      if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
        vm.resume();
      }
    }
 else {
      if (primaryPool.getType() != StoragePoolType.RBD) {
        Script command=new Script(_manageSnapshotPath,_cmdsTimeout,s_logger);
        command.add("String_Node_Str",snapshotDisk.getPath());
        command.add("String_Node_Str",snapshotName);
        String result=command.execute();
        if (result != null) {
          s_logger.debug("String_Node_Str" + result);
          return new CopyCmdAnswer("String_Node_Str" + result);
        }
      }
    }
    SnapshotObjectTO newSnapshot=new SnapshotObjectTO();
    newSnapshot.setPath(snapshotRelPath + File.separator + snapshotName);
    return new CopyCmdAnswer(newSnapshot);
  }
 catch (  LibvirtException e) {
    s_logger.debug("String_Node_Str" + e.toString());
    return new CopyCmdAnswer(e.toString());
  }
catch (  CloudRuntimeException e) {
    s_logger.debug("String_Node_Str" + e.toString());
    return new CopyCmdAnswer(e.toString());
  }
 finally {
    if (secondaryStoragePool != null) {
      secondaryStoragePool.delete();
    }
  }
}

Fixed version (ground truth):
@Override public Answer backupSnapshot(CopyCommand cmd){
  DataTO srcData=cmd.getSrcTO();
  DataTO destData=cmd.getDestTO();
  SnapshotObjectTO snapshot=(SnapshotObjectTO)srcData;
  PrimaryDataStoreTO primaryStore=(PrimaryDataStoreTO)snapshot.getDataStore();
  SnapshotObjectTO destSnapshot=(SnapshotObjectTO)destData;
  DataStoreTO imageStore=destData.getDataStore();
  if (!(imageStore instanceof NfsTO)) {
    return backupSnapshotForObjectStore(cmd);
  }
  NfsTO nfsImageStore=(NfsTO)imageStore;
  String secondaryStoragePoolUrl=nfsImageStore.getUrl();
  int index=snapshot.getPath().lastIndexOf("String_Node_Str");
  String snapshotName=snapshot.getPath().substring(index + 1);
  String volumePath=snapshot.getVolume().getPath();
  String snapshotDestPath=null;
  String snapshotRelPath=null;
  String vmName=snapshot.getVmName();
  KVMStoragePool secondaryStoragePool=null;
  try {
    Connect conn=LibvirtConnection.getConnectionByVmName(vmName);
    secondaryStoragePool=storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
    String ssPmountPath=secondaryStoragePool.getLocalPath();
    snapshotRelPath=destSnapshot.getPath();
    snapshotDestPath=ssPmountPath + File.separator + snapshotRelPath;
    KVMPhysicalDisk snapshotDisk=storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(),primaryStore.getUuid(),volumePath);
    KVMStoragePool primaryPool=snapshotDisk.getPool();
    if (primaryPool.getType() == StoragePoolType.RBD) {
      try {
        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.connect();
        s_logger.debug("String_Node_Str" + r.confGet("String_Node_Str"));
        IoCTX io=r.ioCtxCreate(primaryPool.getSourceDir());
        Rbd rbd=new Rbd(io);
        RbdImage image=rbd.open(snapshotDisk.getName(),snapshotName);
        long startTime=System.currentTimeMillis() / 1000;
        File snapDir=new File(snapshotDestPath);
        s_logger.debug("String_Node_Str" + snapDir.getAbsolutePath() + "String_Node_Str");
        FileUtils.forceMkdir(snapDir);
        File snapFile=new File(snapshotDestPath + "String_Node_Str" + snapshotName);
        s_logger.debug("String_Node_Str" + snapFile.getAbsolutePath());
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(snapFile));
        int chunkSize=4194304;
        long offset=0;
        while (true) {
          byte[] buf=new byte[chunkSize];
          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"+ snapFile.getAbsolutePath()+ "String_Node_Str"+ offset);
        bos.close();
        s_logger.debug("String_Node_Str" + snapshotName + "String_Node_Str"+ snapshotDisk.getName());
        image.snapRemove(snapshotName);
        r.ioCtxDestroy(io);
      }
 catch (      RadosException e) {
        s_logger.error("String_Node_Str" + e.getMessage());
        return new CopyCmdAnswer(e.toString());
      }
catch (      RbdException e) {
        s_logger.error("String_Node_Str" + snapshotDisk.getName() + "String_Node_Str"+ e.getMessage());
        return new CopyCmdAnswer(e.toString());
      }
catch (      FileNotFoundException e) {
        s_logger.error("String_Node_Str" + snapshotDestPath + "String_Node_Str"+ e.getMessage());
        return new CopyCmdAnswer(e.toString());
      }
catch (      IOException e) {
        s_logger.debug("String_Node_Str" + snapshotDestPath);
        return new CopyCmdAnswer(e.toString());
      }
    }
 else {
      Script command=new Script(_manageSnapshotPath,cmd.getWaitInMillSeconds(),s_logger);
      command.add("String_Node_Str",snapshotDisk.getPath());
      command.add("String_Node_Str",snapshotName);
      command.add("String_Node_Str",snapshotDestPath);
      command.add("String_Node_Str",snapshotName);
      String result=command.execute();
      if (result != null) {
        s_logger.debug("String_Node_Str" + result);
        return new CopyCmdAnswer(result);
      }
    }
    DomainInfo.DomainState state=null;
    Domain vm=null;
    if (vmName != null) {
      try {
        vm=this.resource.getDomain(conn,vmName);
        state=vm.getInfo().state;
      }
 catch (      LibvirtException e) {
        s_logger.trace("String_Node_Str",e);
      }
    }
    KVMStoragePool primaryStorage=storagePoolMgr.getStoragePool(primaryStore.getPoolType(),primaryStore.getUuid());
    if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
      DomainSnapshot snap=vm.snapshotLookupByName(snapshotName);
      snap.delete(0);
      vm=this.resource.getDomain(conn,vmName);
      state=vm.getInfo().state;
      if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
        vm.resume();
      }
    }
 else {
      if (primaryPool.getType() != StoragePoolType.RBD) {
        Script command=new Script(_manageSnapshotPath,_cmdsTimeout,s_logger);
        command.add("String_Node_Str",snapshotDisk.getPath());
        command.add("String_Node_Str",snapshotName);
        String result=command.execute();
        if (result != null) {
          s_logger.debug("String_Node_Str" + result);
          return new CopyCmdAnswer("String_Node_Str" + result);
        }
      }
    }
    SnapshotObjectTO newSnapshot=new SnapshotObjectTO();
    newSnapshot.setPath(snapshotRelPath + File.separator + snapshotName);
    return new CopyCmdAnswer(newSnapshot);
  }
 catch (  LibvirtException e) {
    s_logger.debug("String_Node_Str" + e.toString());
    return new CopyCmdAnswer(e.toString());
  }
catch (  CloudRuntimeException e) {
    s_logger.debug("String_Node_Str" + e.toString());
    return new CopyCmdAnswer(e.toString());
  }
 finally {
    if (secondaryStoragePool != null) {
      secondaryStoragePool.delete();
    }
  }
}