1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-31 03:12:44 +00:00

(trunk, utils) in transmission-create, return an error if a user tries to manually set a piece size that isn't a power of two.

This commit is contained in:
Jordan Lee 2014-06-10 00:43:21 +00:00
parent f433f2ee17
commit ef9370783a
5 changed files with 66 additions and 22 deletions

View file

@ -165,15 +165,37 @@ tr_metaInfoBuilderCreate (const char * topFileArg)
return ret; return ret;
} }
void static bool
isValidPieceSize (uint32_t n)
{
const bool isPowerOfTwo = !(n == 0) && !(n & (n - 1));
return isPowerOfTwo;
}
bool
tr_metaInfoBuilderSetPieceSize (tr_metainfo_builder * b, tr_metaInfoBuilderSetPieceSize (tr_metainfo_builder * b,
uint32_t bytes) uint32_t bytes)
{ {
if (!isValidPieceSize (bytes))
{
char wanted[32];
char gotten[32];
tr_formatter_mem_B (wanted, bytes, sizeof(wanted));
tr_formatter_mem_B (gotten, b->pieceSize, sizeof(gotten));
tr_logAddError (_("Failed to set piece size to %s, leaving it at %s"),
wanted,
gotten);
return false;
}
b->pieceSize = bytes; b->pieceSize = bytes;
b->pieceCount = (int)(b->totalSize / b->pieceSize); b->pieceCount = (int)(b->totalSize / b->pieceSize);
if (b->totalSize % b->pieceSize) if (b->totalSize % b->pieceSize)
++b->pieceCount; ++b->pieceCount;
return true;
} }

View file

@ -93,8 +93,10 @@ tr_metainfo_builder * tr_metaInfoBuilderCreate (const char * topFile);
/** /**
* Call this before tr_makeMetaInfo() to override the builder.pieceSize * Call this before tr_makeMetaInfo() to override the builder.pieceSize
* and builder.pieceCount values that were set by tr_metainfoBuilderCreate() * and builder.pieceCount values that were set by tr_metainfoBuilderCreate()
*
* @return false if the piece size isn't valid; eg, isn't a power of two.
*/ */
void tr_metaInfoBuilderSetPieceSize (tr_metainfo_builder * builder, bool tr_metaInfoBuilderSetPieceSize (tr_metainfo_builder * builder,
uint32_t bytes); uint32_t bytes);
void tr_metaInfoBuilderFree (tr_metainfo_builder*); void tr_metaInfoBuilderFree (tr_metainfo_builder*);

View file

@ -18,6 +18,8 @@
#include <libtransmission/utils.h> #include <libtransmission/utils.h>
#include <libtransmission/version.h> #include <libtransmission/version.h>
#include "units.h"
#define MY_NAME "transmission-create" #define MY_NAME "transmission-create"
#define MAX_TRACKERS 128 #define MAX_TRACKERS 128
@ -133,6 +135,9 @@ main (int argc, char * argv[])
tr_metainfo_builder * b = NULL; tr_metainfo_builder * b = NULL;
tr_logSetLevel (TR_LOG_ERROR); tr_logSetLevel (TR_LOG_ERROR);
tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR);
tr_formatter_size_init (DISK_K, DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR);
tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR);
if (parseCommandLine (argc, (const char**)argv)) if (parseCommandLine (argc, (const char**)argv))
return EXIT_FAILURE; return EXIT_FAILURE;

View file

@ -24,29 +24,11 @@
#include <libtransmission/variant.h> #include <libtransmission/variant.h>
#include <libtransmission/version.h> #include <libtransmission/version.h>
#include "units.h"
#define MY_NAME "transmission-show" #define MY_NAME "transmission-show"
#define TIMEOUT_SECS 30 #define TIMEOUT_SECS 30
#define MEM_K 1024
#define MEM_K_STR "KiB"
#define MEM_M_STR "MiB"
#define MEM_G_STR "GiB"
#define MEM_T_STR "TiB"
#define DISK_K 1000
#define DISK_B_STR "B"
#define DISK_K_STR "kB"
#define DISK_M_STR "MB"
#define DISK_G_STR "GB"
#define DISK_T_STR "TB"
#define SPEED_K 1000
#define SPEED_B_STR "B/s"
#define SPEED_K_STR "kB/s"
#define SPEED_M_STR "MB/s"
#define SPEED_G_STR "GB/s"
#define SPEED_T_STR "TB/s"
static tr_option options[] = static tr_option options[] =
{ {
{ 'm', "magnet", "Give a magnet link for the specified torrent", "m", 0, NULL }, { 'm', "magnet", "Give a magnet link for the specified torrent", "m", 0, NULL },

33
utils/units.h Normal file
View file

@ -0,0 +1,33 @@
/*
* This file Copyright (C) 2012-2014 Mnemosyne LLC
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*
* $Id:$
*/
#ifndef SHOW_H
#define SHOW_H
#define MEM_K 1024
#define MEM_K_STR "KiB"
#define MEM_M_STR "MiB"
#define MEM_G_STR "GiB"
#define MEM_T_STR "TiB"
#define DISK_K 1000
#define DISK_B_STR "B"
#define DISK_K_STR "kB"
#define DISK_M_STR "MB"
#define DISK_G_STR "GB"
#define DISK_T_STR "TB"
#define SPEED_K 1000
#define SPEED_B_STR "B/s"
#define SPEED_K_STR "kB/s"
#define SPEED_M_STR "MB/s"
#define SPEED_G_STR "GB/s"
#define SPEED_T_STR "TB/s"
#endif