public static boolean isModified(SyncFile syncFile, Path filePath) { if (filePath == null) { return true; } try { FileTime fileTime = Files.getLastModifiedTime(filePath); long modifiedTime = syncFile.getModifiedTime(); if (OSDetector.isUnix()) { modifiedTime = modifiedTime / 1000 * 1000; } if ((fileTime.toMillis() <= modifiedTime) && FileKeyUtil.hasFileKey(filePath, syncFile.getSyncFileId())) { return false; } } catch (IOException ioe) { if (_logger.isDebugEnabled()) { _logger.debug(ioe.getMessage(), ioe); } } try { if ((syncFile.getSize() > 0) && (syncFile.getSize() != Files.size(filePath))) { return true; } } catch (IOException ioe) { if (_logger.isDebugEnabled()) { _logger.debug(ioe.getMessage(), ioe); } } try { String checksum = getChecksum(filePath); return !checksumsEqual(checksum, syncFile.getChecksum()); } catch (IOException ioe) { if (_logger.isDebugEnabled()) { _logger.debug(ioe.getMessage(), ioe); } return true; } }
public static long getFileKey(Path filePath) { if (!Files.exists(filePath)) { return -1; } try { if (OSDetector.isApple()) { Xattrj xattrj = getXattrj(); if (xattrj == null) { return -1; } String fileKey = xattrj.readAttribute(filePath.toFile(), "fileKey"); if (fileKey == null) { return -1; } return Long.parseLong(fileKey); } else { UserDefinedFileAttributeView userDefinedFileAttributeView = Files.getFileAttributeView(filePath, UserDefinedFileAttributeView.class); List<String> list = userDefinedFileAttributeView.list(); if (!list.contains("fileKey")) { return -1; } ByteBuffer byteBuffer = ByteBuffer.allocate(userDefinedFileAttributeView.size("fileKey")); userDefinedFileAttributeView.read("fileKey", byteBuffer); CharBuffer charBuffer = _CHARSET.decode((ByteBuffer) byteBuffer.flip()); return Long.parseLong(charBuffer.toString()); } } catch (Exception e) { _logger.error(e.getMessage(), e); return -1; } }
public static void writeFileKey(Path filePath, String fileKey) { if (!OSDetector.isWindows()) { return; } File file = filePath.toFile(); if (!file.canWrite()) { file.setWritable(true); } UserDefinedFileAttributeView userDefinedFileAttributeView = Files.getFileAttributeView(filePath, UserDefinedFileAttributeView.class); try { userDefinedFileAttributeView.write("fileKey", _CHARSET.encode(CharBuffer.wrap(fileKey))); } catch (Exception e) { _logger.error(e.getMessage(), e); } }
public static String getFileKey(Path filePath) { if (!Files.exists(filePath)) { return ""; } try { if (OSDetector.isWindows()) { UserDefinedFileAttributeView userDefinedFileAttributeView = Files.getFileAttributeView(filePath, UserDefinedFileAttributeView.class); List<String> list = userDefinedFileAttributeView.list(); if (!list.contains("fileKey")) { return ""; } ByteBuffer byteBuffer = ByteBuffer.allocate(userDefinedFileAttributeView.size("fileKey")); userDefinedFileAttributeView.read("fileKey", byteBuffer); CharBuffer charBuffer = _CHARSET.decode((ByteBuffer) byteBuffer.flip()); return charBuffer.toString(); } else { BasicFileAttributes basicFileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class); Object fileKey = basicFileAttributes.fileKey(); return fileKey.toString(); } } catch (Exception e) { _logger.error(e.getMessage(), e); return ""; } }
protected static void doWriteFileKey(Path filePath, String fileKey) { if (hasFileKey(filePath, Long.parseLong(fileKey))) { return; } if (OSDetector.isApple()) { Xattrj xattrj = getXattrj(); if (xattrj == null) { return; } File file = filePath.toFile(); if (!file.canWrite()) { file.setWritable(true); } xattrj.writeAttribute(file, "fileKey", fileKey); } else { File file = filePath.toFile(); if (!file.canWrite()) { file.setWritable(true); } UserDefinedFileAttributeView userDefinedFileAttributeView = Files.getFileAttributeView(filePath, UserDefinedFileAttributeView.class); try { userDefinedFileAttributeView.write("fileKey", _CHARSET.encode(CharBuffer.wrap(fileKey))); } catch (Exception e) { _logger.error(e.getMessage(), e); } } }
private static long _parseLong(String value, long defaultValue) { if (_useJDKParseLong == null) { if (OSDetector.isAIX() && ServerDetector.isWebSphere() && JavaDetector.isIBM() && JavaDetector.is64bit()) { _useJDKParseLong = Boolean.TRUE; } else { _useJDKParseLong = Boolean.FALSE; } } if (_useJDKParseLong) { try { return Long.parseLong(value); } catch (NumberFormatException nfe) { return defaultValue; } } int length = value.length(); if (length <= 0) { return defaultValue; } int pos = 0; long limit = -Long.MAX_VALUE; boolean negative = false; char c = value.charAt(0); if (c < CharPool.NUMBER_0) { if (c == CharPool.MINUS) { limit = Long.MIN_VALUE; negative = true; } else if (c != CharPool.PLUS) { return defaultValue; } if (length == 1) { return defaultValue; } pos++; } long smallLimit = limit / 10; long result = 0; while (pos < length) { if (result < smallLimit) { return defaultValue; } c = value.charAt(pos++); if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) { return defaultValue; } int number = c - CharPool.NUMBER_0; result *= 10; if (result < (limit + number)) { return defaultValue; } result -= number; } if (negative) { return result; } else { return -result; } }