示例#1
0
    private PartialFileHistory guessLastFileHistoryForFileWithMatchingChecksum(
        FileProperties fileProperties,
        Collection<PartialFileHistory> fileHistoriesWithSameChecksum) {
      PartialFileHistory lastFileHistory = null;

      // Check if they do not exist anymore --> assume it has moved!
      // We choose the best fileHistory to base on as follows:

      // 1. Ensure that it was modified at the same time and is the same size
      // 2. Check the fileHistory was deleted and the file does not actually exists
      // 3. Choose the one with the longest matching tail of the path to the new path

      for (PartialFileHistory fileHistoryWithSameChecksum : fileHistoriesWithSameChecksum) {
        FileVersion lastVersion = fileHistoryWithSameChecksum.getLastVersion();

        if (fileProperties.getLastModified() != lastVersion.getLastModified().getTime()
            || fileProperties.getSize() != lastVersion.getSize()) {
          continue;
        }

        File lastVersionOnLocalDisk =
            new File(config.getLocalDir() + File.separator + lastVersion.getPath());

        if (lastVersion.getStatus() != FileStatus.DELETED
            && !FileUtil.exists(lastVersionOnLocalDisk)) {
          if (lastFileHistory == null) {
            lastFileHistory = fileHistoryWithSameChecksum;
          } else {
            String filePath = fileProperties.getRelativePath();
            String currentPreviousPath = lastFileHistory.getLastVersion().getPath();
            String candidatePreviousPath = fileHistoryWithSameChecksum.getLastVersion().getPath();

            for (int i = 0; i < filePath.length(); i++) {
              if (!filePath.regionMatches(
                  filePath.length() - i,
                  candidatePreviousPath,
                  candidatePreviousPath.length() - i,
                  i)) {
                // The candidate no longer matches, take the current path.
                break;
              }
              if (!filePath.regionMatches(
                  filePath.length() - i,
                  currentPreviousPath,
                  currentPreviousPath.length() - i,
                  i)) {
                // The current previous path no longer matches, take the new candidate
                lastFileHistory = fileHistoryWithSameChecksum;
                break;
              }
            }
          }
        }
      }

      return lastFileHistory;
    }
示例#2
0
  private PartialFileHistory getFileHistoryByPathFromDatabaseVersion(
      DatabaseVersion databaseVersion, String path) {
    // TODO [medium] Extremely performance intensive, because this is called inside a loop above.
    // Implement better caching for database version!!!

    for (PartialFileHistory fileHistory : databaseVersion.getFileHistories()) {
      FileVersion lastVersion = fileHistory.getLastVersion();

      if (lastVersion.getStatus() != FileStatus.DELETED && lastVersion.getPath().equals(path)) {
        return fileHistory;
      }
    }

    return null;
  }
示例#3
0
  private void removeDeletedFiles(
      DatabaseVersion newDatabaseVersion, List<PartialFileHistory> fileHistoriesWithLastVersion) {
    logger.log(Level.FINER, "- Looking for deleted files ...");

    for (PartialFileHistory fileHistory : fileHistoriesWithLastVersion) {
      // Ignore this file history if it has been updated in this database version before (file
      // probably renamed!)
      if (newDatabaseVersion.getFileHistory(fileHistory.getFileHistoryId()) != null) {
        continue;
      }

      // Check if file exists, remove if it doesn't
      FileVersion lastLocalVersion = fileHistory.getLastVersion();
      File lastLocalVersionOnDisk =
          new File(config.getLocalDir() + File.separator + lastLocalVersion.getPath());

      // Ignore this file history if the last version is marked "DELETED"
      if (lastLocalVersion.getStatus() == FileStatus.DELETED) {
        continue;
      }

      // Add this file history if a new file with this name has been added (file type change)
      PartialFileHistory newFileWithSameName =
          getFileHistoryByPathFromDatabaseVersion(
              newDatabaseVersion, fileHistory.getLastVersion().getPath());

      // If file has VANISHED, mark as DELETED
      if (!FileUtil.exists(lastLocalVersionOnDisk) || newFileWithSameName != null) {
        PartialFileHistory deletedFileHistory =
            new PartialFileHistory(fileHistory.getFileHistoryId());
        FileVersion deletedVersion = lastLocalVersion.clone();

        deletedVersion.setStatus(FileStatus.DELETED);
        deletedVersion.setVersion(fileHistory.getLastVersion().getVersion() + 1);
        deletedVersion.setUpdated(new Date());

        logger.log(Level.FINER, "  + Deleted: Adding DELETED version: {0}", deletedVersion);
        logger.log(Level.FINER, "                           based on: {0}", lastLocalVersion);

        deletedFileHistory.addFileVersion(deletedVersion);
        newDatabaseVersion.addFileHistory(deletedFileHistory);
      }
    }
  }
示例#4
0
    private PartialFileHistory guessLastFileHistoryForFolderOrSymlink(
        FileProperties fileProperties) {
      PartialFileHistory lastFileHistory = filePathCache.get(fileProperties.getRelativePath());

      if (lastFileHistory == null) {
        logger.log(
            Level.FINER,
            "   * No old file history found, starting new history (path: "
                + fileProperties.getRelativePath()
                + ", "
                + fileProperties.getType()
                + ")");
        return null;
      } else {
        FileVersion lastFileVersion = lastFileHistory.getLastVersion();

        if (lastFileVersion.getStatus() != FileStatus.DELETED
            && lastFileVersion.getType() == fileProperties.getType()) {
          logger.log(
              Level.FINER,
              "   * Found old file history "
                  + lastFileHistory.getFileHistoryId()
                  + " (by path: "
                  + fileProperties.getRelativePath()
                  + "), "
                  + fileProperties.getType()
                  + ", appending new version.");
          return lastFileHistory;
        } else {
          logger.log(
              Level.FINER,
              "   * No old file history found, starting new history (path: "
                  + fileProperties.getRelativePath()
                  + ", "
                  + fileProperties.getType()
                  + ")");
          return null;
        }
      }
    }
  private void updateTable(LsFolderRequest lsRequest, LsFolderResponse lsResponse) {
    logger.log(
        Level.INFO,
        "Detail panel: Updating table with "
            + lsResponse.getResult().getFileVersions().size()
            + " file versions ...");

    // Add new file version items
    List<PartialFileHistory> fileVersions =
        new ArrayList<>(lsResponse.getResult().getFileVersions().values());
    selectedFileHistory = fileVersions.get(0);

    for (FileVersion fileVersion : selectedFileHistory.getFileVersions().values()) {
      String checksumStr =
          (fileVersion.getChecksum() != null) ? fileVersion.getChecksum().toString() : "";

      TableItem tableItem = new TableItem(historyTable, SWT.NONE);

      tableItem.setData(fileVersion);
      tableItem.setImage(COLUMN_INDEX_STATUS, getStatusImage(fileVersion.getStatus()));
      tableItem.setText(COLUMN_INDEX_PATH, fileVersion.getPath());
      tableItem.setText(COLUMN_INDEX_VERSION, Long.toString(fileVersion.getVersion()));
      tableItem.setText(COLUMN_INDEX_TYPE, fileVersion.getType().toString());
      tableItem.setText(COLUMN_INDEX_SIZE, FileUtil.formatFileSize(fileVersion.getSize()));
      tableItem.setText(COLUMN_INDEX_POSIX_PERMS, fileVersion.getPosixPermissions());
      tableItem.setText(COLUMN_INDEX_DOS_ATTRS, fileVersion.getDosAttributes());
      tableItem.setText(COLUMN_INDEX_CHECKSUM, checksumStr);
      tableItem.setText(COLUMN_INDEX_LAST_MODIFIED, "" + fileVersion.getLastModified());
      tableItem.setText(COLUMN_INDEX_UPDATED, "" + fileVersion.getUpdated());
    }

    if (historyTable.getItemCount() > 0) {
      restoreButton.setEnabled(false);
      historyTable.select(historyTable.getItemCount() - 1);
    }

    resizeColumns();
  }