Program: 106159
Project: 106159
Buggy source:
/** 
 * This copies a volume from Primary Storage to Secondary Storage In theory it could also do it the other way around, but the current implementation in ManagementServerImpl shows that the destPool is always a Secondary Storage Pool
 */
@Override public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk,String name,KVMStoragePool destPool){
  KVMStoragePool srcPool=disk.getPool();
  PhysicalDiskFormat sourceFormat=disk.getFormat();
  String sourcePath=disk.getPath();
  KVMPhysicalDisk newDisk;
  if (destPool.getType() != StoragePoolType.RBD) {
    if (disk.getFormat() == PhysicalDiskFormat.TAR) {
      newDisk=destPool.createPhysicalDisk(name,PhysicalDiskFormat.DIR,disk.getVirtualSize());
    }
 else {
      if (srcPool.getType() != StoragePoolType.RBD) {
        newDisk=destPool.createPhysicalDisk(name,disk.getVirtualSize());
      }
 else {
        newDisk=destPool.createPhysicalDisk(name,sourceFormat,disk.getVirtualSize());
      }
    }
  }
 else {
    newDisk=new KVMPhysicalDisk(destPool.getSourceDir() + "String_Node_Str" + name,name,destPool);
    newDisk.setFormat(PhysicalDiskFormat.RAW);
    newDisk.setSize(disk.getVirtualSize());
    newDisk.setVirtualSize(disk.getSize());
  }
  String destPath=newDisk.getPath();
  PhysicalDiskFormat destFormat=newDisk.getFormat();
  QemuImg qemu=new QemuImg();
  QemuImgFile srcFile=null;
  QemuImgFile destFile=null;
  if ((srcPool.getType() != StoragePoolType.RBD) && (destPool.getType() != StoragePoolType.RBD)) {
    if (sourceFormat == PhysicalDiskFormat.TAR) {
      Script.runSimpleBashScript("String_Node_Str" + sourcePath + "String_Node_Str"+ destPath);
    }
 else     if (sourceFormat == PhysicalDiskFormat.DIR) {
      Script.runSimpleBashScript("String_Node_Str" + destPath);
      Script.runSimpleBashScript("String_Node_Str" + destPath);
      Script.runSimpleBashScript("String_Node_Str" + sourcePath + "String_Node_Str"+ destPath);
    }
 else {
      srcFile=new QemuImgFile(sourcePath,sourceFormat);
      try {
        Map<String,String> info=qemu.info(srcFile);
        String backingFile=info.get(new String("String_Node_Str"));
        if (sourceFormat.equals(destFormat) && backingFile == null) {
          Script.runSimpleBashScript("String_Node_Str" + sourcePath + "String_Node_Str"+ destPath);
        }
 else {
          destFile=new QemuImgFile(destPath,destFormat);
          try {
            qemu.convert(srcFile,destFile);
          }
 catch (          QemuImgException e) {
            s_logger.error("String_Node_Str" + srcFile.getFileName() + "String_Node_Str"+ destFile.getFileName()+ "String_Node_Str"+ e.getMessage());
            newDisk=null;
          }
        }
      }
 catch (      QemuImgException e) {
        s_logger.error("String_Node_Str" + srcFile.getFileName() + "String_Node_Str"+ e.getMessage());
        newDisk=null;
      }
    }
  }
 else   if ((srcPool.getType() != StoragePoolType.RBD) && (destPool.getType() == StoragePoolType.RBD)) {
    s_logger.debug("String_Node_Str");
    String sourceFile;
    boolean useTmpFile=false;
    try {
      if (sourceFormat != destFormat) {
        srcFile=new QemuImgFile(sourcePath,sourceFormat);
        destFile=new QemuImgFile("String_Node_Str" + name);
        s_logger.debug("String_Node_Str" + srcFile.getFileName() + "String_Node_Str"+ destFile.getFileName()+ "String_Node_Str");
        qemu.convert(srcFile,destFile);
        sourceFile=destFile.getFileName();
        useTmpFile=true;
      }
 else {
        sourceFile=sourcePath;
      }
      Rados r=new Rados(destPool.getAuthUserName());
      r.confSet("String_Node_Str",destPool.getSourceHost() + "String_Node_Str" + destPool.getSourcePort());
      r.confSet("String_Node_Str",destPool.getAuthSecret());
      r.connect();
      s_logger.debug("String_Node_Str" + r.confGet("String_Node_Str"));
      IoCTX io=r.ioCtxCreate(destPool.getSourceDir());
      Rbd rbd=new Rbd(io);
      s_logger.debug("String_Node_Str" + name + "String_Node_Str"+ destPool.getSourceDir()+ "String_Node_Str");
      rbd.create(name,disk.getVirtualSize(),this.rbdFeatures,this.rbdOrder);
      RbdImage image=rbd.open(name);
      File fh=new File(sourceFile);
      BufferedInputStream bis=new BufferedInputStream(new FileInputStream(fh));
      int chunkSize=4194304;
      long offset=0;
      s_logger.debug("String_Node_Str" + sourceFile + "String_Node_Str"+ fh.length()+ "String_Node_Str"+ name+ "String_Node_Str"+ chunkSize+ "String_Node_Str");
      while (true) {
        byte[] buf=new byte[chunkSize];
        int bytes=bis.read(buf);
        if (bytes <= 0) {
          break;
        }
        image.write(buf,offset,bytes);
        offset+=bytes;
      }
      s_logger.debug("String_Node_Str" + sourceFile + "String_Node_Str"+ name+ "String_Node_Str"+ offset);
      bis.close();
      if (useTmpFile) {
        s_logger.debug("String_Node_Str" + sourceFile);
        fh.delete();
      }
      s_logger.debug("String_Node_Str" + this.rbdTemplateSnapName + "String_Node_Str"+ name);
      image.snapCreate(this.rbdTemplateSnapName);
      s_logger.debug("String_Node_Str" + this.rbdTemplateSnapName + "String_Node_Str"+ name);
      image.snapProtect(this.rbdTemplateSnapName);
      rbd.close(image);
      r.ioCtxDestroy(io);
    }
 catch (    QemuImgException e) {
      s_logger.error("String_Node_Str" + srcFile.getFileName() + "String_Node_Str"+ destFile.getFileName()+ "String_Node_Str"+ e.getMessage());
      newDisk=null;
    }
catch (    RadosException e) {
      s_logger.error("String_Node_Str" + e.getReturnValue() + "String_Node_Str"+ e.getMessage());
      newDisk=null;
    }
catch (    RbdException e) {
      s_logger.error("String_Node_Str" + e.getReturnValue() + "String_Node_Str"+ e.getMessage());
      newDisk=null;
    }
catch (    IOException e) {
      s_logger.error("String_Node_Str" + e.getMessage());
      newDisk=null;
    }
  }
 else {
    srcFile=new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(srcPool.getSourceHost(),srcPool.getSourcePort(),srcPool.getAuthUserName(),srcPool.getAuthSecret(),sourcePath));
    srcFile.setFormat(sourceFormat);
    destFile=new QemuImgFile(destPath);
    destFile.setFormat(destFormat);
    try {
      qemu.convert(srcFile,destFile);
    }
 catch (    QemuImgException e) {
      s_logger.error("String_Node_Str" + srcFile.getFileName() + "String_Node_Str"+ destFile.getFileName()+ "String_Node_Str"+ e.getMessage());
      newDisk=null;
    }
  }
  if (newDisk == null) {
    throw new CloudRuntimeException("String_Node_Str" + disk.getPath() + "String_Node_Str"+ name);
  }
  return newDisk;
}

Fixed version (ground truth):
/** 
 * This copies a volume from Primary Storage to Secondary Storage In theory it could also do it the other way around, but the current implementation in ManagementServerImpl shows that the destPool is always a Secondary Storage Pool
 */
@Override public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk,String name,KVMStoragePool destPool,int timeout){
  KVMStoragePool srcPool=disk.getPool();
  PhysicalDiskFormat sourceFormat=disk.getFormat();
  String sourcePath=disk.getPath();
  KVMPhysicalDisk newDisk;
  if (destPool.getType() != StoragePoolType.RBD) {
    if (disk.getFormat() == PhysicalDiskFormat.TAR) {
      newDisk=destPool.createPhysicalDisk(name,PhysicalDiskFormat.DIR,disk.getVirtualSize());
    }
 else {
      if (srcPool.getType() != StoragePoolType.RBD) {
        newDisk=destPool.createPhysicalDisk(name,disk.getVirtualSize());
      }
 else {
        newDisk=destPool.createPhysicalDisk(name,sourceFormat,disk.getVirtualSize());
      }
    }
  }
 else {
    newDisk=new KVMPhysicalDisk(destPool.getSourceDir() + "String_Node_Str" + name,name,destPool);
    newDisk.setFormat(PhysicalDiskFormat.RAW);
    newDisk.setSize(disk.getVirtualSize());
    newDisk.setVirtualSize(disk.getSize());
  }
  String destPath=newDisk.getPath();
  PhysicalDiskFormat destFormat=newDisk.getFormat();
  QemuImg qemu=new QemuImg(timeout);
  QemuImgFile srcFile=null;
  QemuImgFile destFile=null;
  if ((srcPool.getType() != StoragePoolType.RBD) && (destPool.getType() != StoragePoolType.RBD)) {
    if (sourceFormat == PhysicalDiskFormat.TAR) {
      Script.runSimpleBashScript("String_Node_Str" + sourcePath + "String_Node_Str"+ destPath,timeout);
    }
 else     if (sourceFormat == PhysicalDiskFormat.DIR) {
      Script.runSimpleBashScript("String_Node_Str" + destPath);
      Script.runSimpleBashScript("String_Node_Str" + destPath);
      Script.runSimpleBashScript("String_Node_Str" + sourcePath + "String_Node_Str"+ destPath,timeout);
    }
 else {
      srcFile=new QemuImgFile(sourcePath,sourceFormat);
      try {
        Map<String,String> info=qemu.info(srcFile);
        String backingFile=info.get(new String("String_Node_Str"));
        if (sourceFormat.equals(destFormat) && backingFile == null) {
          String result=Script.runSimpleBashScript("String_Node_Str" + sourcePath + "String_Node_Str"+ destPath,timeout);
          if (result != null) {
            throw new CloudRuntimeException("String_Node_Str" + result);
          }
        }
 else {
          destFile=new QemuImgFile(destPath,destFormat);
          try {
            qemu.convert(srcFile,destFile);
          }
 catch (          QemuImgException e) {
            s_logger.error("String_Node_Str" + srcFile.getFileName() + "String_Node_Str"+ destFile.getFileName()+ "String_Node_Str"+ e.getMessage());
            newDisk=null;
          }
        }
      }
 catch (      QemuImgException e) {
        s_logger.error("String_Node_Str" + srcFile.getFileName() + "String_Node_Str"+ e.getMessage());
        newDisk=null;
      }
    }
  }
 else   if ((srcPool.getType() != StoragePoolType.RBD) && (destPool.getType() == StoragePoolType.RBD)) {
    s_logger.debug("String_Node_Str");
    String sourceFile;
    boolean useTmpFile=false;
    try {
      if (sourceFormat != destFormat) {
        srcFile=new QemuImgFile(sourcePath,sourceFormat);
        destFile=new QemuImgFile("String_Node_Str" + name);
        s_logger.debug("String_Node_Str" + srcFile.getFileName() + "String_Node_Str"+ destFile.getFileName()+ "String_Node_Str");
        qemu.convert(srcFile,destFile);
        sourceFile=destFile.getFileName();
        useTmpFile=true;
      }
 else {
        sourceFile=sourcePath;
      }
      Rados r=new Rados(destPool.getAuthUserName());
      r.confSet("String_Node_Str",destPool.getSourceHost() + "String_Node_Str" + destPool.getSourcePort());
      r.confSet("String_Node_Str",destPool.getAuthSecret());
      r.connect();
      s_logger.debug("String_Node_Str" + r.confGet("String_Node_Str"));
      IoCTX io=r.ioCtxCreate(destPool.getSourceDir());
      Rbd rbd=new Rbd(io);
      s_logger.debug("String_Node_Str" + name + "String_Node_Str"+ destPool.getSourceDir()+ "String_Node_Str");
      rbd.create(name,disk.getVirtualSize(),this.rbdFeatures,this.rbdOrder);
      RbdImage image=rbd.open(name);
      File fh=new File(sourceFile);
      BufferedInputStream bis=new BufferedInputStream(new FileInputStream(fh));
      int chunkSize=4194304;
      long offset=0;
      s_logger.debug("String_Node_Str" + sourceFile + "String_Node_Str"+ fh.length()+ "String_Node_Str"+ name+ "String_Node_Str"+ chunkSize+ "String_Node_Str");
      while (true) {
        byte[] buf=new byte[chunkSize];
        int bytes=bis.read(buf);
        if (bytes <= 0) {
          break;
        }
        image.write(buf,offset,bytes);
        offset+=bytes;
      }
      s_logger.debug("String_Node_Str" + sourceFile + "String_Node_Str"+ name+ "String_Node_Str"+ offset);
      bis.close();
      if (useTmpFile) {
        s_logger.debug("String_Node_Str" + sourceFile);
        fh.delete();
      }
      s_logger.debug("String_Node_Str" + this.rbdTemplateSnapName + "String_Node_Str"+ name);
      image.snapCreate(this.rbdTemplateSnapName);
      s_logger.debug("String_Node_Str" + this.rbdTemplateSnapName + "String_Node_Str"+ name);
      image.snapProtect(this.rbdTemplateSnapName);
      rbd.close(image);
      r.ioCtxDestroy(io);
    }
 catch (    QemuImgException e) {
      s_logger.error("String_Node_Str" + srcFile.getFileName() + "String_Node_Str"+ destFile.getFileName()+ "String_Node_Str"+ e.getMessage());
      newDisk=null;
    }
catch (    RadosException e) {
      s_logger.error("String_Node_Str" + e.getReturnValue() + "String_Node_Str"+ e.getMessage());
      newDisk=null;
    }
catch (    RbdException e) {
      s_logger.error("String_Node_Str" + e.getReturnValue() + "String_Node_Str"+ e.getMessage());
      newDisk=null;
    }
catch (    IOException e) {
      s_logger.error("String_Node_Str" + e.getMessage());
      newDisk=null;
    }
  }
 else {
    srcFile=new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(srcPool.getSourceHost(),srcPool.getSourcePort(),srcPool.getAuthUserName(),srcPool.getAuthSecret(),sourcePath));
    srcFile.setFormat(sourceFormat);
    destFile=new QemuImgFile(destPath);
    destFile.setFormat(destFormat);
    try {
      qemu.convert(srcFile,destFile);
    }
 catch (    QemuImgException e) {
      s_logger.error("String_Node_Str" + srcFile.getFileName() + "String_Node_Str"+ destFile.getFileName()+ "String_Node_Str"+ e.getMessage());
      newDisk=null;
    }
  }
  if (newDisk == null) {
    throw new CloudRuntimeException("String_Node_Str" + disk.getPath() + "String_Node_Str"+ name);
  }
  return newDisk;
}