i2psnark: Prevent changing infohash in metainfo edit CLI

Tweaks to previous MR:
_ Use new constructor for edit CLI
- Javadocs for new constructor
- Add new_url_list param to new constructor
- Set creation date if unset
This commit is contained in:
zzz
2024-09-20 09:47:58 -04:00
parent 7296bdc32c
commit bca5b65b1a
2 changed files with 17 additions and 8 deletions

View File

@@ -145,7 +145,19 @@ public class MetaInfo
this.info_hash = calculateInfoHash();
}
public MetaInfo(MetaInfo old, String new_announce, List<List<String>> new_announce_list, String new_comment, String new_created_by)
/**
* Will not change infohash.
* Retains creation date of old MetaInfo if nonzero.
*
* @parm new_announce may be null
* @parm new_announce_list may be null
* @parm new_comment may be null
* @parm new_created_by may be null
* @parm new_url_list may be null
* @since 0.9.64
*/
public MetaInfo(MetaInfo old, String new_announce, List<List<String>> new_announce_list, String new_comment,
String new_created_by, List<String> new_url_list)
{
this.announce = new_announce;
this.info_hash = old.info_hash;
@@ -162,8 +174,8 @@ public class MetaInfo
this.announce_list = new_announce_list;
this.comment = new_comment;
this.created_by = new_created_by;
this.creation_date = old.creation_date;
this.url_list = old.url_list;
this.creation_date = old.creation_date > 0 ? old.creation_date : I2PAppContext.getGlobalContext().clock().now();
this.url_list = new_url_list;
this.infoMap = old.infoMap;
this.infoBytesLength = old.infoBytesLength;
}
@@ -934,10 +946,7 @@ public class MetaInfo
String an = announce != null ? announce : meta.getAnnounce();
String cm = comment != null ? comment : meta.getComment();
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.getPrivateTrackerStatus(),
meta.getAnnounceList(), cb, urls, cm);
MetaInfo meta2 = new MetaInfo(meta, an, meta.getAnnounceList(), cm, cb, urls);
java.io.File from = new java.io.File(args[i]);
java.io.File to = new java.io.File(args[i] + ".bak");
if (net.i2p.util.FileUtil.copy(from, to, true, false)) {

View File

@@ -4886,7 +4886,7 @@ public class I2PSnarkServlet extends BasicServlet {
newComment = null;
if (newCreatedBy.equals(""))
newCreatedBy = null;
MetaInfo newMeta = new MetaInfo(meta, thePrimary, newAnnList, newComment, newCreatedBy);
MetaInfo newMeta = new MetaInfo(meta, thePrimary, newAnnList, newComment, newCreatedBy, meta.getWebSeedURLs());
File f = new File(_manager.util().getTempDir(), "edit-" + _manager.util().getContext().random().nextLong() + ".torrent");
OutputStream out = null;
try {