/** * remove file by GUID. * * @param guid file GUID * @return whether remove the file successfully * @throws Exception */ public boolean removeFile(String host, String user, String guid) throws Exception { // get file data access object VAFileDao fileDao = DaoFactory.getVAFileDao(); // get the VAFile object with the GUID VAFile vafile = fileDao.query(guid); if (vafile == null) { // if the file is already deleted, should not throw exception return true; // throw new Exception("The file with guid \"" + guid // + "\" is not existed or available"); } if (vafile.getParent().equals(VAMConstants.NULL)) { if (fileDao.getActiveReference(vafile.getGuid()) > 0) { throw new Exception("The file is in used"); } } else { if (vafile.getRef() > 0) { throw new Exception("The file is in used"); } } int oldState = vafile.getState(); try { // remove file FileOperation.getInstance().removeFile(host, user, vafile, vafile.getSavePath()); } catch (RuntimeException e) { // restore state vafile.setState(oldState); fileDao.update(vafile); throw new Exception(e); } return true; }
/** * 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()); } } } }