/** * get the capacity and size of the disk. * * @param vafile disk object * @return input disk object with capacity and size */ public VADisk getDiskCapacity(VAFile vafile) { Runtime rt = Runtime.getRuntime(); Process proc; long capacity = 0; long size = 0; // get the disk capacity and size try { String command = VAMUtil.getDiskInfoCommand(vafile.getSavePath()); String[] shell = new String[] {"/bin/sh", "-c", command}; proc = rt.exec(shell); InputStream is = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { if (line.startsWith("virtual size")) { Pattern pattern = Pattern.compile("(\\d+) bytes"); Matcher matcher = pattern.matcher(line); if (matcher.find()) { capacity = Long.parseLong(matcher.group(1)); } } } String path = vafile.getSavePath(); File file = new File(path); if (file.exists()) { size = file.length(); } } catch (IOException e) { VAMUtil.errorLog(e.getMessage()); } VADisk vadisk = new VADisk(vafile); vadisk.setCapacity(capacity); vadisk.setSize(size); return vadisk; }
/** * create file object. * * @param id file id * @param location file location, if location = null, generate location * @param fileType file type * @param format file format * @param size file size * @return file object * @throws Exception */ public VAFile createFile(String id, String location, String fileType, String format, long size) throws Exception { String guid = VAMUtil.genGuid(); if (location == null) { location = "file." + guid; } // create a new file object VAFile vafile = new VAFile(guid, fileType, format, location, id, size, VAMConstants.STATE_UNDEFINE); if (fileType == null) { throw new Exception("The file type must not be null"); } if (format == null) { throw new Exception("The file format must not be null"); } // check file type and file format if (fileType.equals(VAMConstants.VAF_FILE_TYPE_DISC)) { if (!format.equals(VAMConstants.VAF_FORMAT_ISO)) { throw new Exception("The disc format must be " + VAMConstants.VAF_FORMAT_ISO); } } else if (fileType.equals(VAMConstants.VAF_FILE_TYPE_DISK)) { if (!format.equals(VAMConstants.VAF_FORMAT_RAW) && !format.equals(VAMConstants.VAF_FORMAT_QCOW) && !format.equals(VAMConstants.VAF_FORMAT_QCOW_2) && !format.equals(VAMConstants.VAF_FORMAT_VMDK)) { throw new Exception( "The disk format must be " + VAMConstants.VAF_FORMAT_RAW + ", " + VAMConstants.VAF_FORMAT_QCOW + ", " + VAMConstants.VAF_FORMAT_QCOW_2 + ", " + VAMConstants.VAF_FORMAT_VMDK); } } else if (fileType.equals(VAMConstants.VAF_FILE_TYPE_CONFIG)) { if (!format.equals(VAMConstants.VAF_FORMAT_TEXT)) { throw new Exception("The config format must be " + VAMConstants.VAF_FORMAT_TEXT); } } else { fileType = VAMConstants.VAF_FILE_TYPE_OTHER; format = VAMConstants.VAF_FORMAT_VMDK; } vafile.setFileType(fileType); vafile.setFormat(format); vafile.setParent(VAMConstants.NULL); vafile.setRef(0); return vafile; }
/** * convert disk format. * * @param vafile file object * @param srcPath the source disk path * @param format disk format * @param deleteSrcFile whether delete the source disk after converting * @return a new file object * @throws Exception */ public VAFile convertDiskFormat( VAFile vafile, String srcPath, String format, boolean deleteSrcFile) throws Exception { // check disk checkDiskType(vafile); // check state of the file if (vafile.getState() != VAMConstants.STATE_READY) { throw new Exception("The file's state is not correct"); } // save old information String oldLocation = vafile.getLocation(); String oldFormat = vafile.getFormat(); // String oldParent = vafile.getParent(); int oldState = vafile.getState(); // set new information String newLocation = "file." + VAMUtil.genGuid(); vafile.setSrcPath(srcPath); vafile.setLocation(newLocation); vafile.setFormat(format); // vafile.setParent(VAMConstants.NULL); vafile.setDeletefileAfterOperation(deleteSrcFile); // get file data access object VAFileDao fileDao = DaoFactory.getVAFileDao(); fileDao.update(vafile); try { // convert disk format FileOperation.getInstance() .convertDiskFormat( VAMConfig.getNfsHost(), VAMConfig.getNfsUser(), vafile, srcPath, vafile.getSavePath(), format); } catch (Exception e) { // restore the old information vafile.setLocation(oldLocation); vafile.setFormat(oldFormat); vafile.setState(oldState); // vafile.setParent(oldParent); DaoFactory.getVAFileDao().update(vafile); throw new Exception(e); } return vafile; }
/** * clear incorrect file. * * @param clearFlag the clear flag, which supports VAMConstants.FILE_CLEAR_FLAG_ERROR, * VAMConstants.FILE_CLEAR_FLAG_NOT_READY, VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_DATA, * VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_FILE, VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_PARENT, * it can use or operation, * @throws Exception */ public void clearFile(int clearFlag) throws Exception { VAFileDao fileDao = DaoFactory.getVAFileDao(); List<VAFile> fileList = getAllFile(); // clear the file whose data can't be found in the database if ((clearFlag & VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_DATA) != 0) { Map<String, String> pathMap = new HashMap<String, String>(); for (int i = 0; i < fileList.size(); i++) { VAFile va = (VAFile) fileList.get(i); pathMap.put(va.getSavePath(), "ok"); } File fileDir = new File(VAMConfig.getFileDirLocation()); if (fileDir.exists() && fileDir.isDirectory()) { checkFile(fileDir, pathMap); } } // clear the file whose physical file can't be found if ((clearFlag & VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_FILE) != 0) { File fileDir = new File(VAMConfig.getFileDirLocation()); if (fileDir.exists() && fileDir.isDirectory()) { for (int i = 0; i < fileList.size(); i++) { VAFile va = (VAFile) fileList.get(i); File file = new File(va.getSavePath()); if (!file.exists()) { fileDao.remove(va.getGuid()); } } } } // clear the file whose state is error if ((clearFlag & VAMConstants.FILE_CLEAR_FLAG_ERROR) != 0) { List<VAObject> errorFiles = fileDao.getErrorFiles(); for (int i = 0; i < errorFiles.size(); i++) { try { removeFile(null, null, errorFiles.get(i).getGuid()); } catch (Exception e) { e.printStackTrace(); } } } // clear the file whose state is not ready if ((clearFlag & VAMConstants.FILE_CLEAR_FLAG_NOT_READY) != 0) { List<VAObject> notReadyFiles = fileDao.getNotReadyFiles(); for (int i = 0; i < notReadyFiles.size(); i++) { VAFile va = new VAFile(notReadyFiles.get(i)); if (VAMUtil.getTimestamp() - va.getTimestamp() > VAMConstants.HOUR / 2) { removeFile(null, null, va.getGuid()); } } } // clear the file whose parent can't be found if ((clearFlag & VAMConstants.FILE_CLEAR_FLAG_NOT_FIND_PARENT) != 0) { fileList = getAllFile(); Map<String, String> guidMap = new HashMap<String, String>(); for (int i = 0; i < fileList.size(); i++) { VAFile va = (VAFile) fileList.get(i); guidMap.put(va.getGuid(), "ok"); } for (int i = 0; i < fileList.size(); i++) { VAFile va = (VAFile) fileList.get(i); if (!va.getParent().equals(VAMConstants.NULL) && guidMap.get(va.getParent()) == null && va.getRef() == 0) { removeFile(null, null, va.getGuid()); } } } }
/** * add file. * * @param vafile file object * @param storeLoc the file location * @param deleteSrcFile whether delete the source file after add file * @return file object * @throws Exception */ public VAFile addFile(VAFile vafile, String storeLoc, boolean deleteSrcFile) throws Exception { // check whether the store file is existed String storePath = storeLoc; if (storePath != null) { File storeFile = new File(storePath); if (!storeFile.exists()) { throw new Exception("The file \"" + storePath + "\" is not existed!"); } vafile.setSize(storeFile.length()); } // check file path VAMUtil.checkFile(null, vafile.getLocation()); // get file data access object VAFileDao fileDao = DaoFactory.getVAFileDao(); // add the file to the database vafile = fileDao.add(vafile); if (vafile == null) { throw new Exception("Can't add the file to the database."); } try { // if the file type is disk, convert disk format if (vafile.getFileType().equals(VAMConstants.VAF_FILE_TYPE_DISK)) { vafile.setSrcPath(storeLoc); DiskInfo info = VAMUtil.getDiskInfo(storeLoc); String format = null; String backingFile = null; if (info != null) { format = info.getFormat(); backingFile = info.getBackingFile(); } if (format != null && format.equalsIgnoreCase(VAMConfig.getImageFormat()) && backingFile == null) { VADisk disk = new VADisk(vafile); disk.setCapacity(info.getVirtualSize()); disk.setSize(info.getDiskSize()); FileOperation.getInstance() .moveFile( VAMConfig.getNfsHost(), VAMConfig.getNfsUser(), disk, storeLoc, disk.getSavePath()); } else { convertDiskFormat(vafile, storeLoc, VAMConfig.getImageFormat(), deleteSrcFile); } } else if (storeLoc != null) { // move file FileOperation.getInstance() .moveFile( VAMConfig.getNfsHost(), VAMConfig.getNfsUser(), vafile, storeLoc, vafile.getSavePath()); } } catch (Exception e) { // remove the file in the database DaoFactory.getVAFileDao().remove(vafile.getGuid()); throw new Exception(e); } return vafile; }
/** * get disk capacity. * * @return disk capacity */ public long getCapacity() { if (VAMUtil.isBlankOrNull((String) this.getAttributes().get(VAMConstants.VAD_CAPACITY))) { return 0; } return Long.parseLong((String) this.getAttributes().get(VAMConstants.VAD_CAPACITY)); }