From 90aab3700263772f151668b7cf9b9f719194f129 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Fri, 30 Oct 2020 16:37:54 +0000
Subject: [PATCH] i2psnark: Add support for comment and url_list to Storage CLI
 Add support for multiple urls to MetaInfo CLI Add comment to release torrent

---
 .../java/src/org/klomp/snark/MetaInfo.java    | 12 ++---
 .../java/src/org/klomp/snark/Storage.java     | 44 +++++++++++++++++--
 build.xml                                     |  8 ++++
 3 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/apps/i2psnark/java/src/org/klomp/snark/MetaInfo.java b/apps/i2psnark/java/src/org/klomp/snark/MetaInfo.java
index 728af47b6f..98f844b88a 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/MetaInfo.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/MetaInfo.java
@@ -792,7 +792,7 @@ public class MetaInfo
       boolean error = false;
       String created_by = null;
       String announce = null;
-      String url = null;
+      List<String> url_list = null;
       String comment = null;
       Getopt g = new Getopt("Storage", args, "a:c:m:w:");
       try {
@@ -812,7 +812,9 @@ public class MetaInfo
                   break;
 
               case 'w':
-                  url = g.getOptarg();
+                  if (url_list == null)
+                      url_list = new ArrayList<String>();
+                  url_list.add(g.getOptarg());
                   break;
 
               case '?':
@@ -827,7 +829,7 @@ public class MetaInfo
           error = true;
       }
       if (error || args.length - g.getOptind() <= 0) {
-          System.err.println("Usage: MetaInfo [-a announceURL] [-c created-by] [-m comment] [-w web-seed-url] file.torrent [file2.torrent...]");
+          System.err.println("Usage: MetaInfo [-a announceURL] [-c created-by] [-m comment] [-w webseed-url]* file.torrent [file2.torrent...]");
           System.exit(1);
       }
       for (int i = g.getOptind(); i < args.length; i++) {
@@ -842,11 +844,11 @@ public class MetaInfo
                                  "\nWebSeed URLs: " + meta.getWebSeedURLs() +
                                  "\nCreated By:   " + meta.getCreatedBy() +
                                  "\nComment:      " + meta.getComment());
-              if (created_by != null || announce != null || url != null || comment != null) {
+              if (created_by != null || announce != null || url_list != null || comment != null) {
                   String cb = created_by != null ? created_by : meta.getCreatedBy();
                   String an = announce != null ? announce : meta.getAnnounce();
                   String cm = comment != null ? comment : meta.getComment();
-                  List<String> urls = url != null ? Collections.singletonList(url) : meta.getWebSeedURLs();
+                  List<String> urls = url_list != null ? url_list : meta.getWebSeedURLs();
                   // changes/adds creation date
                   MetaInfo meta2 = new MetaInfo(an, meta.getName(), null, meta.getFiles(), meta.getLengths(),
                                                 meta.getPieceLength(0), meta.getPieceHashes(), meta.getTotalLength(), meta.isPrivate(),
diff --git a/apps/i2psnark/java/src/org/klomp/snark/Storage.java b/apps/i2psnark/java/src/org/klomp/snark/Storage.java
index 363a631e3b..f7036a1aa4 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/Storage.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/Storage.java
@@ -144,6 +144,30 @@ public class Storage implements Closeable
                  String created_by,
                  boolean privateTorrent, StorageListener listener)
     throws IOException
+  {
+      this(util, baseFile, announce, announce_list, created_by, privateTorrent, null, null, listener);
+  }
+
+  /**
+   * Creates a storage from the existing file or directory.
+   * Creates an in-memory metainfo but does not save it to
+   * a file, caller must do that.
+   *
+   * Creates the metainfo, this may take a LONG time. BLOCKING.
+   *
+   * @param announce may be null
+   * @param listener may be null
+   * @param created_by may be null
+   * @param url_list may be null
+   * @param comment may be null
+   * @throws IOException when creating and/or checking files fails.
+   * @since 0.9.48
+   */
+  public Storage(I2PSnarkUtil util, File baseFile, String announce,
+                 List<List<String>> announce_list,
+                 String created_by,
+                 boolean privateTorrent, List<String> url_list, String comment, StorageListener listener)
+    throws IOException
   {
     _util = util;
     _base = baseFile;
@@ -210,7 +234,7 @@ public class Storage implements Closeable
     byte[] piece_hashes = fast_digestCreate();
     metainfo = new MetaInfo(announce, baseFile.getName(), null, files,
                             lengthsList, piece_size, piece_hashes, total, privateTorrent,
-                            announce_list, created_by, null, null);
+                            announce_list, created_by, url_list, comment);
 
   }
 
@@ -1685,7 +1709,9 @@ public class Storage implements Closeable
       boolean error = false;
       String created_by = null;
       String announce = null;
-      Getopt g = new Getopt("Storage", args, "a:c:");
+      List<String> url_list = null;
+      String comment = null;
+      Getopt g = new Getopt("Storage", args, "a:c:m:w:");
       try {
           int c;
           while ((c = g.getopt()) != -1) {
@@ -1698,6 +1724,16 @@ public class Storage implements Closeable
                   created_by = g.getOptarg();
                   break;
 
+              case 'm':
+                  comment = g.getOptarg();
+                  break;
+
+              case 'w':
+                  if (url_list == null)
+                      url_list = new ArrayList<String>();
+                  url_list.add(g.getOptarg());
+                  break;
+
               case '?':
               case ':':
               default:
@@ -1710,7 +1746,7 @@ public class Storage implements Closeable
           error = true;
       }
       if (error || args.length - g.getOptind() != 1) {
-          System.err.println("Usage: Storage [-a announceURL] [-c created-by] file-or-dir");
+          System.err.println("Usage: Storage [-a announceURL] [-c created-by] [-m comment] [-w webseed-url]* file-or-dir");
           System.exit(1);
       }
       File base = new File(args[g.getOptind()]);
@@ -1719,7 +1755,7 @@ public class Storage implements Closeable
       File file = null;
       FileOutputStream out = null;
       try {
-          Storage storage = new Storage(util, base, announce, null, created_by, false, null);
+          Storage storage = new Storage(util, base, announce, null, created_by, false, url_list, comment, null);
           MetaInfo meta = storage.getMetaInfo();
           file = new File(storage.getBaseName() + ".torrent");
           out = new FileOutputStream(file);
diff --git a/build.xml b/build.xml
index 85366f4edb..e6dde31e93 100644
--- a/build.xml
+++ b/build.xml
@@ -141,6 +141,8 @@
                 <arg value="http://tracker2.postman.i2p/announce.php" />
                 <arg value="-c" />
                 <arg value="${build.built-by}" />
+                <arg value="-m" />
+                <arg value="Official torrent for version ${full.version}" />
                 <arg value="@{file}" />
             </java>
         </sequential>
@@ -1686,6 +1688,12 @@
     <target name="signed-updater200WithJavadoc" depends="-pre-sign, updater200WithJavadoc, -sign-update" />
     <target name="signed-updater200WithJavadocAndJetty" depends="-pre-sign, updater200WithJavadocAndJetty, -sign-update" />
 
+    <!-- just to test the torrent creation macro -->
+    <target name="updater-torrent" depends="updater">
+        <copy file="i2pupdate.zip" tofile="i2pupdate-${full.version}.zip" />
+        <mktorrent file="i2pupdate-${full.version}.zip" />
+    </target>
+
     <target name="zipit" depends="getReleaseNumber">
         <!--
              As of release 0.8.8, the router will enforce a zipfile comment equal to the
-- 
GitLab