public static SyncFile updateFileSyncFile(Path filePath, long syncAccountId, SyncFile syncFile)
      throws Exception {

    // Local sync file

    if (FileUtil.getFileKey(filePath) != syncFile.getSyncFileId()) {
      FileUtil.writeFileKey(filePath, String.valueOf(syncFile.getSyncFileId()));
    }

    Path deltaFilePath = null;

    String name = _getName(filePath, syncFile);
    String sourceChecksum = syncFile.getChecksum();
    String sourceFileName = syncFile.getName();
    long sourceVersionId = syncFile.getVersionId();
    String targetChecksum = FileUtil.getChecksum(filePath);

    if (!FileUtil.checksumsEqual(sourceChecksum, targetChecksum)
        && !IODeltaUtil.isIgnoredFilePatchingExtension(syncFile)) {

      deltaFilePath = Files.createTempFile(String.valueOf(filePath.getFileName()), ".tmp");

      deltaFilePath =
          IODeltaUtil.delta(filePath, IODeltaUtil.getChecksumsFilePath(syncFile), deltaFilePath);

      IODeltaUtil.checksums(syncFile);
    }

    syncFile.setChecksum(targetChecksum);
    syncFile.setFilePathName(filePath.toString());
    syncFile.setName(name);

    update(syncFile);

    // Remote sync file

    if ((syncFile.getState() != SyncFile.STATE_ERROR)
        && (syncFile.getState() != SyncFile.STATE_UNSYNCED)) {

      FileEventUtil.updateFile(
          filePath,
          syncAccountId,
          syncFile,
          deltaFilePath,
          name,
          sourceChecksum,
          sourceFileName,
          sourceVersionId,
          targetChecksum);
    }

    return syncFile;
  }
  public static SyncFile addSyncFile(
      String changeLog,
      String checksum,
      String description,
      String filePathName,
      String mimeType,
      String name,
      long parentFolderId,
      long repositoryId,
      int state,
      long syncAccountId,
      String type)
      throws Exception {

    SyncFile syncFile = new SyncFile();

    syncFile.setChangeLog(changeLog);
    syncFile.setChecksum(checksum);
    syncFile.setDescription(description);
    syncFile.setFilePathName(filePathName);
    syncFile.setLocalSyncTime(System.currentTimeMillis());
    syncFile.setMimeType(mimeType);
    syncFile.setName(name);
    syncFile.setParentFolderId(parentFolderId);
    syncFile.setRepositoryId(repositoryId);
    syncFile.setState(state);
    syncFile.setSyncAccountId(syncAccountId);
    syncFile.setType(type);

    _syncFilePersistence.create(syncFile);

    FileUtil.writeFileKey(Paths.get(filePathName), String.valueOf(syncFile.getSyncFileId()));

    IODeltaUtil.checksums(syncFile);

    return syncFile;
  }