Program: 106026
Project: 106026
Buggy source:
/** 
 * This function copies a physical disk from Secondary Storage to Primary Storage or from Primary to Primary Storage The first time a template is deployed in Primary Storage it will be copied from Secondary to Primary. If it has been created on Primary Storage, it will be copied on the Primary Storage
 */
@Override public KVMPhysicalDisk createDiskFromTemplate(KVMPhysicalDisk template,String name,PhysicalDiskFormat format,long size,KVMStoragePool destPool,int timeout){
  String newUuid=name;
  KVMStoragePool srcPool=template.getPool();
  KVMPhysicalDisk disk=null;
  try {
    if (destPool.getType() != StoragePoolType.RBD) {
      disk=destPool.createPhysicalDisk(newUuid,format,template.getVirtualSize());
      if (template.getFormat() == PhysicalDiskFormat.TAR) {
        Script.runSimpleBashScript("String_Node_Str" + template.getPath() + "String_Node_Str"+ disk.getPath(),timeout);
      }
 else       if (template.getFormat() == PhysicalDiskFormat.DIR) {
        Script.runSimpleBashScript("String_Node_Str" + disk.getPath());
        Script.runSimpleBashScript("String_Node_Str" + disk.getPath());
        Script.runSimpleBashScript("String_Node_Str" + template.getPath() + "String_Node_Str"+ disk.getPath(),timeout);
      }
 else       if (format == PhysicalDiskFormat.QCOW2) {
        QemuImgFile backingFile=new QemuImgFile(template.getPath(),template.getFormat());
        QemuImgFile destFile=new QemuImgFile(disk.getPath());
        QemuImg qemu=new QemuImg(timeout);
        qemu.create(destFile,backingFile);
      }
 else       if (format == PhysicalDiskFormat.RAW) {
        QemuImgFile sourceFile=new QemuImgFile(template.getPath(),template.getFormat());
        QemuImgFile destFile=new QemuImgFile(disk.getPath(),PhysicalDiskFormat.RAW);
        QemuImg qemu=new QemuImg(timeout);
        qemu.convert(sourceFile,destFile);
      }
    }
 else {
      format=PhysicalDiskFormat.RAW;
      disk=new KVMPhysicalDisk(destPool.getSourceDir() + "String_Node_Str" + newUuid,newUuid,destPool);
      disk.setFormat(format);
      disk.setSize(template.getVirtualSize());
      disk.setVirtualSize(disk.getSize());
      QemuImg qemu=new QemuImg(timeout);
      QemuImgFile srcFile;
      QemuImgFile destFile=new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(destPool.getSourceHost(),destPool.getSourcePort(),destPool.getAuthUserName(),destPool.getAuthSecret(),disk.getPath()));
      destFile.setFormat(format);
      if (srcPool.getType() != StoragePoolType.RBD) {
        srcFile=new QemuImgFile(template.getPath(),template.getFormat());
        qemu.convert(srcFile,destFile);
      }
 else {
        try {
          if ((srcPool.getSourceHost().equals(destPool.getSourceHost())) && (srcPool.getSourceDir().equals(destPool.getSourceDir()))) {
            s_logger.debug("String_Node_Str");
            Rados r=new Rados(srcPool.getAuthUserName());
            r.confSet("String_Node_Str",srcPool.getSourceHost() + "String_Node_Str" + srcPool.getSourcePort());
            r.confSet("String_Node_Str",srcPool.getAuthSecret());
            r.connect();
            s_logger.debug("String_Node_Str" + r.confGet("String_Node_Str"));
            IoCTX io=r.ioCtxCreate(srcPool.getSourceDir());
            Rbd rbd=new Rbd(io);
            RbdImage srcImage=rbd.open(template.getName());
            if (srcImage.isOldFormat()) {
              s_logger.debug("String_Node_Str" + srcPool.getSourceDir() + "String_Node_Str"+ template.getName()+ "String_Node_Str"+ template.getVirtualSize()+ "String_Node_Str");
              rbd.create(disk.getName(),template.getVirtualSize(),this.rbdFeatures,this.rbdOrder);
              RbdImage destImage=rbd.open(disk.getName());
              s_logger.debug("String_Node_Str" + srcImage.getName() + "String_Node_Str"+ destImage.getName()+ "String_Node_Str"+ srcPool.getSourceDir());
              rbd.copy(srcImage,destImage);
              s_logger.debug("String_Node_Str" + srcImage.getName() + "String_Node_Str"+ destImage.getName()+ "String_Node_Str"+ srcPool.getSourceDir());
              rbd.close(destImage);
            }
 else {
              s_logger.debug("String_Node_Str" + srcPool.getSourceDir() + "String_Node_Str"+ template.getName()+ "String_Node_Str"+ this.rbdTemplateSnapName);
              rbd.clone(template.getName(),this.rbdTemplateSnapName,io,disk.getName(),this.rbdFeatures,this.rbdOrder);
              s_logger.debug("String_Node_Str" + template.getName() + "String_Node_Str"+ this.rbdTemplateSnapName+ "String_Node_Str"+ disk.getName());
            }
            rbd.close(srcImage);
            r.ioCtxDestroy(io);
          }
 else {
            s_logger.debug("String_Node_Str");
            Rados rSrc=new Rados(srcPool.getAuthUserName());
            rSrc.confSet("String_Node_Str",srcPool.getSourceHost() + "String_Node_Str" + srcPool.getSourcePort());
            rSrc.confSet("String_Node_Str",srcPool.getAuthSecret());
            rSrc.connect();
            s_logger.debug("String_Node_Str" + rSrc.confGet("String_Node_Str"));
            Rados rDest=new Rados(destPool.getAuthUserName());
            rDest.confSet("String_Node_Str",destPool.getSourceHost() + "String_Node_Str" + destPool.getSourcePort());
            rDest.confSet("String_Node_Str",destPool.getAuthSecret());
            rDest.connect();
            s_logger.debug("String_Node_Str" + rDest.confGet("String_Node_Str"));
            IoCTX sIO=rSrc.ioCtxCreate(srcPool.getSourceDir());
            Rbd sRbd=new Rbd(sIO);
            IoCTX dIO=rDest.ioCtxCreate(destPool.getSourceDir());
            Rbd dRbd=new Rbd(dIO);
            s_logger.debug("String_Node_Str" + disk.getName() + "String_Node_Str"+ rDest.confGet("String_Node_Str")+ "String_Node_Str"+ destPool.getSourceDir());
            dRbd.create(disk.getName(),template.getVirtualSize(),this.rbdFeatures,this.rbdOrder);
            RbdImage srcImage=sRbd.open(template.getName());
            RbdImage destImage=dRbd.open(disk.getName());
            s_logger.debug("String_Node_Str" + template.getName() + "String_Node_Str"+ rSrc.confGet("String_Node_Str")+ "String_Node_Str"+ disk.getName()+ "String_Node_Str"+ rDest.confGet("String_Node_Str"));
            sRbd.copy(srcImage,destImage);
            sRbd.close(srcImage);
            dRbd.close(destImage);
            rSrc.ioCtxDestroy(sIO);
            rDest.ioCtxDestroy(dIO);
          }
        }
 catch (        RadosException e) {
          s_logger.error("String_Node_Str" + e.getMessage());
          disk=null;
        }
catch (        RbdException e) {
          s_logger.error("String_Node_Str" + e.getMessage());
          disk=null;
        }
      }
    }
  }
 catch (  QemuImgException e) {
    s_logger.error("String_Node_Str" + disk.getPath() + "String_Node_Str"+ e.getMessage());
  }
  if (disk == null) {
    throw new CloudRuntimeException("String_Node_Str" + disk.getPath() + "String_Node_Str"+ template.getName());
  }
  return disk;
}

Fixed version (ground truth):
/** 
 * This function copies a physical disk from Secondary Storage to Primary Storage or from Primary to Primary Storage The first time a template is deployed in Primary Storage it will be copied from Secondary to Primary. If it has been created on Primary Storage, it will be copied on the Primary Storage
 */
@Override public KVMPhysicalDisk createDiskFromTemplate(KVMPhysicalDisk template,String name,PhysicalDiskFormat format,long size,KVMStoragePool destPool,int timeout){
  String newUuid=name;
  KVMStoragePool srcPool=template.getPool();
  KVMPhysicalDisk disk=null;
  try {
    if (destPool.getType() != StoragePoolType.RBD) {
      disk=destPool.createPhysicalDisk(newUuid,format,template.getVirtualSize());
      if (template.getFormat() == PhysicalDiskFormat.TAR) {
        Script.runSimpleBashScript("String_Node_Str" + template.getPath() + "String_Node_Str"+ disk.getPath(),timeout);
      }
 else       if (template.getFormat() == PhysicalDiskFormat.DIR) {
        Script.runSimpleBashScript("String_Node_Str" + disk.getPath());
        Script.runSimpleBashScript("String_Node_Str" + disk.getPath());
        Script.runSimpleBashScript("String_Node_Str" + template.getPath() + "String_Node_Str"+ disk.getPath(),timeout);
      }
 else       if (format == PhysicalDiskFormat.QCOW2) {
        QemuImgFile backingFile=new QemuImgFile(template.getPath(),template.getFormat());
        QemuImgFile destFile=new QemuImgFile(disk.getPath());
        QemuImg qemu=new QemuImg(timeout);
        qemu.create(destFile,backingFile);
      }
 else       if (format == PhysicalDiskFormat.RAW) {
        QemuImgFile sourceFile=new QemuImgFile(template.getPath(),template.getFormat());
        QemuImgFile destFile=new QemuImgFile(disk.getPath(),PhysicalDiskFormat.RAW);
        QemuImg qemu=new QemuImg(timeout);
        qemu.convert(sourceFile,destFile);
      }
    }
 else {
      format=PhysicalDiskFormat.RAW;
      disk=new KVMPhysicalDisk(destPool.getSourceDir() + "String_Node_Str" + newUuid,newUuid,destPool);
      disk.setFormat(format);
      disk.setSize(template.getVirtualSize());
      disk.setVirtualSize(disk.getSize());
      QemuImg qemu=new QemuImg(timeout);
      QemuImgFile srcFile;
      QemuImgFile destFile=new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(destPool.getSourceHost(),destPool.getSourcePort(),destPool.getAuthUserName(),destPool.getAuthSecret(),disk.getPath()));
      destFile.setFormat(format);
      if (srcPool.getType() != StoragePoolType.RBD) {
        srcFile=new QemuImgFile(template.getPath(),template.getFormat());
        qemu.convert(srcFile,destFile);
      }
 else {
        try {
          if ((srcPool.getSourceHost().equals(destPool.getSourceHost())) && (srcPool.getSourceDir().equals(destPool.getSourceDir()))) {
            s_logger.debug("String_Node_Str");
            Rados r=new Rados(srcPool.getAuthUserName());
            r.confSet("String_Node_Str",srcPool.getSourceHost() + "String_Node_Str" + srcPool.getSourcePort());
            r.confSet("String_Node_Str",srcPool.getAuthSecret());
            r.connect();
            s_logger.debug("String_Node_Str" + r.confGet("String_Node_Str"));
            IoCTX io=r.ioCtxCreate(srcPool.getSourceDir());
            Rbd rbd=new Rbd(io);
            RbdImage srcImage=rbd.open(template.getName());
            if (srcImage.isOldFormat()) {
              s_logger.debug("String_Node_Str" + srcPool.getSourceDir() + "String_Node_Str"+ template.getName()+ "String_Node_Str"+ template.getVirtualSize()+ "String_Node_Str");
              rbd.create(disk.getName(),template.getVirtualSize(),rbdFeatures,rbdOrder);
              RbdImage destImage=rbd.open(disk.getName());
              s_logger.debug("String_Node_Str" + srcImage.getName() + "String_Node_Str"+ destImage.getName()+ "String_Node_Str"+ srcPool.getSourceDir());
              rbd.copy(srcImage,destImage);
              s_logger.debug("String_Node_Str" + srcImage.getName() + "String_Node_Str"+ destImage.getName()+ "String_Node_Str"+ srcPool.getSourceDir());
              rbd.close(destImage);
            }
 else {
              s_logger.debug("String_Node_Str" + srcPool.getSourceDir() + "String_Node_Str"+ template.getName()+ "String_Node_Str"+ rbdTemplateSnapName);
              rbd.clone(template.getName(),rbdTemplateSnapName,io,disk.getName(),rbdFeatures,rbdOrder);
              s_logger.debug("String_Node_Str" + template.getName() + "String_Node_Str"+ rbdTemplateSnapName+ "String_Node_Str"+ disk.getName());
            }
            rbd.close(srcImage);
            r.ioCtxDestroy(io);
          }
 else {
            s_logger.debug("String_Node_Str");
            Rados rSrc=new Rados(srcPool.getAuthUserName());
            rSrc.confSet("String_Node_Str",srcPool.getSourceHost() + "String_Node_Str" + srcPool.getSourcePort());
            rSrc.confSet("String_Node_Str",srcPool.getAuthSecret());
            rSrc.connect();
            s_logger.debug("String_Node_Str" + rSrc.confGet("String_Node_Str"));
            Rados rDest=new Rados(destPool.getAuthUserName());
            rDest.confSet("String_Node_Str",destPool.getSourceHost() + "String_Node_Str" + destPool.getSourcePort());
            rDest.confSet("String_Node_Str",destPool.getAuthSecret());
            rDest.connect();
            s_logger.debug("String_Node_Str" + rDest.confGet("String_Node_Str"));
            IoCTX sIO=rSrc.ioCtxCreate(srcPool.getSourceDir());
            Rbd sRbd=new Rbd(sIO);
            IoCTX dIO=rDest.ioCtxCreate(destPool.getSourceDir());
            Rbd dRbd=new Rbd(dIO);
            s_logger.debug("String_Node_Str" + disk.getName() + "String_Node_Str"+ rDest.confGet("String_Node_Str")+ "String_Node_Str"+ destPool.getSourceDir());
            dRbd.create(disk.getName(),template.getVirtualSize(),rbdFeatures,rbdOrder);
            RbdImage srcImage=sRbd.open(template.getName());
            RbdImage destImage=dRbd.open(disk.getName());
            s_logger.debug("String_Node_Str" + template.getName() + "String_Node_Str"+ rSrc.confGet("String_Node_Str")+ "String_Node_Str"+ disk.getName()+ "String_Node_Str"+ rDest.confGet("String_Node_Str"));
            sRbd.copy(srcImage,destImage);
            sRbd.close(srcImage);
            dRbd.close(destImage);
            rSrc.ioCtxDestroy(sIO);
            rDest.ioCtxDestroy(dIO);
          }
        }
 catch (        RadosException e) {
          s_logger.error("String_Node_Str" + e.getMessage());
          disk=null;
        }
catch (        RbdException e) {
          s_logger.error("String_Node_Str" + e.getMessage());
          disk=null;
        }
      }
    }
  }
 catch (  QemuImgException e) {
    s_logger.error("String_Node_Str" + disk.getPath() + "String_Node_Str"+ e.getMessage());
  }
  if (disk == null) {
    throw new CloudRuntimeException("String_Node_Str" + template.getName());
  }
  return disk;
}