Ticket #16540: 0001-Improve-utility-of-DescriptorCollectorImpl.patch

File 0001-Improve-utility-of-DescriptorCollectorImpl.patch, 5.7 KB (added by leeroy, 4 years ago)

Revised fix.

  • src/org/torproject/descriptor/impl/DescriptorCollectorImpl.java

    From e09e68ccb196f32a65ba287aab360f9db7fe20a3 Mon Sep 17 00:00:00 2001
    From: leeroy <ter.one.leeboi@hush.com>
    Date: Mon, 13 Jul 2015 18:50:12 -0400
    Subject: [PATCH] Improve utility of DescriptorCollectorImpl.
    
    ---
     .../descriptor/impl/DescriptorCollectorImpl.java   | 48 ++++++++++++++++++----
     1 file changed, 39 insertions(+), 9 deletions(-)
    
    diff --git a/src/org/torproject/descriptor/impl/DescriptorCollectorImpl.java b/src/org/torproject/descriptor/impl/DescriptorCollectorImpl.java
    index 8b5f2ca..7ab7d8a 100644
    a b import org.torproject.descriptor.DescriptorCollector; 
    3030
    3131public class DescriptorCollectorImpl implements DescriptorCollector {
    3232
     33  int localFilesBefore = 0, foundRemoteFiles = 0,
     34      downloadedFiles = 0, deletedLocalFiles = 0;
     35
    3336  @Override
    3437  public void collectDescriptors(String collecTorBaseUrl,
    3538      String[] remoteDirectories, long minLastModified,
    public class DescriptorCollectorImpl implements DescriptorCollector { 
    4750    }
    4851    SortedMap<String, Long> localFiles =
    4952        this.statLocalDirectory(localDirectory);
     53    localFilesBefore = localFiles.size();
    5054    SortedMap<String, String> fetchedDirectoryListings =
    5155        this.fetchRemoteDirectories(collecTorBaseUrl, remoteDirectories);
    5256    SortedSet<String> parsedDirectories = new TreeSet<String>();
    public class DescriptorCollectorImpl implements DescriptorCollector { 
    6367      parsedDirectories.add(remoteDirectory);
    6468      remoteFiles.putAll(parsedRemoteFiles);
    6569    }
    66     this.fetchRemoteFiles(collecTorBaseUrl, remoteFiles, minLastModified,
     70    foundRemoteFiles = remoteFiles.size();
     71    downloadedFiles = this.fetchRemoteFiles(
     72        collecTorBaseUrl, remoteFiles, minLastModified,
    6773        localDirectory, localFiles);
    6874    if (deleteExtraneousLocalFiles) {
    69       this.deleteExtraneousLocalFiles(parsedDirectories, remoteFiles,
    70           localDirectory, localFiles);
     75      deletedLocalFiles = this.deleteExtraneousLocalFiles(
     76          parsedDirectories, remoteFiles, localDirectory, localFiles);
    7177    }
    7278  }
    7379
    public class DescriptorCollectorImpl implements DescriptorCollector { 
    97103    SortedMap<String, String> fetchedDirectoryListings =
    98104        new TreeMap<String, String>();
    99105    for (String remoteDirectory : remoteDirectories) {
     106      if (remoteDirectory.length() == 0) continue;
    100107      String remoteDirectoryWithSlashAtBeginAndEnd =
    101108          (remoteDirectory.startsWith("/") ? "" : "/") + remoteDirectory
    102109          + (remoteDirectory.endsWith("/") ? "" : "/");
    public class DescriptorCollectorImpl implements DescriptorCollector { 
    169176    return remoteFiles;
    170177  }
    171178
    172   void fetchRemoteFiles(String collecTorBaseUrl,
     179  int fetchRemoteFiles(String collecTorBaseUrl,
    173180      SortedMap<String, Long> remoteFiles, long minLastModified,
    174181      File localDirectory, SortedMap<String, Long> localFiles) {
     182    int fetched = 0;
    175183    for (Map.Entry<String, Long> e : remoteFiles.entrySet()) {
    176184      String filename = e.getKey();
    177185      long lastModifiedMillis = e.getValue();
    public class DescriptorCollectorImpl implements DescriptorCollector { 
    183191      String url = collecTorBaseUrl + filename;
    184192      File destinationFile = new File(localDirectory.getPath()
    185193          + filename);
    186       this.fetchRemoteFile(url, destinationFile, lastModifiedMillis);
     194      if (fetchRemoteFile(url, destinationFile, lastModifiedMillis)) {
     195        fetched++;
     196      }
    187197    }
     198    return fetched;
    188199  }
    189200
    190   void fetchRemoteFile(String url, File destinationFile,
     201  boolean fetchRemoteFile(String url, File destinationFile,
    191202      long lastModifiedMillis) {
     203    boolean fetched = false;
    192204    HttpURLConnection huc = null;
    193205    try {
    194206      File destinationDirectory = destinationFile.getParentFile();
    195207      destinationDirectory.mkdirs();
    196       File tempDestinationFile = new File(destinationDirectory, "."
    197           + destinationFile.getName());
     208      File tempDestinationFile = new File(
     209          destinationDirectory, destinationFile.getName());
    198210      BufferedOutputStream bos = new BufferedOutputStream(
    199211          new FileOutputStream(tempDestinationFile));
    200212      URL u = new URL(url);
    public class DescriptorCollectorImpl implements DescriptorCollector { 
    223235        bos.close();
    224236        tempDestinationFile.renameTo(destinationFile);
    225237        destinationFile.setLastModified(lastModifiedMillis);
     238        fetched = true;
    226239      }
    227240    } catch (IOException e) {
    228241      e.printStackTrace();
    public class DescriptorCollectorImpl implements DescriptorCollector { 
    230243        huc.disconnect();
    231244      }
    232245    }
     246    return fetched;
    233247  }
    234248
    235   void deleteExtraneousLocalFiles(
     249  int deleteExtraneousLocalFiles(
    236250      SortedSet<String> parsedDirectories,
    237251      SortedMap<String, Long> remoteFiles, File localDirectory,
    238252      SortedMap<String, Long> localFiles) {
     253    int deleted = 0;
    239254    for (String localPath : localFiles.keySet()) {
    240255      for (String remoteDirectory : parsedDirectories) {
    241256        if (localPath.startsWith(remoteDirectory)) {
    242257          if (!remoteFiles.containsKey(localPath)) {
    243258            new File(localDirectory.getPath() + localPath).delete();
     259            deleted++;
    244260          }
    245261        }
    246262      }
    247263    }
     264    return deleted;
     265  }
     266
     267  public int getLocalFilesBefore() {
     268    return this.localFilesBefore;
     269  }
     270  public int getFoundRemoteFiles() {
     271    return this.foundRemoteFiles;
     272  }
     273  public int getDownloadedFiles() {
     274    return this.downloadedFiles;
     275  }
     276  public int getDeletedLocalFiles() {
     277    return this.deletedLocalFiles;
    248278  }
    249279}
    250280