Changeset 14080


Ignore:
Timestamp:
May 23, 2013, 3:20:18 AM (9 years ago)
Author:
jordan
Message:

whitespace/indentation

Location:
trunk/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/platform.c

    r14033 r14080  
    358358          s = tr_strdup (s);
    359359        }
    360         else
     360      else
    361361        {
    362362#ifdef SYS_DARWIN
  • trunk/libtransmission/resume.c

    r13970 r14080  
    2727enum
    2828{
    29     MAX_REMEMBERED_PEERS = 200
     29  MAX_REMEMBERED_PEERS = 200
    3030};
    3131
     
    3333getResumeFilename (const tr_torrent * tor)
    3434{
    35     char * base = tr_metainfoGetBasename (tr_torrentInfo (tor));
    36     char * filename = tr_strdup_printf ("%s" TR_PATH_DELIMITER_STR "%s.resume",
    37                                         tr_getResumeDir (tor->session), base);
    38     tr_free (base);
    39     return filename;
     35  char * base = tr_metainfoGetBasename (tr_torrentInfo (tor));
     36  char * filename = tr_strdup_printf ("%s" TR_PATH_DELIMITER_STR "%s.resume",
     37                                      tr_getResumeDir (tor->session), base);
     38  tr_free (base);
     39  return filename;
    4040}
    4141
     
    4747savePeers (tr_variant * dict, const tr_torrent * tor)
    4848{
    49     int count;
    50     tr_pex * pex;
    51 
    52     count = tr_peerMgrGetPeers ((tr_torrent*) tor, &pex, TR_AF_INET, TR_PEERS_INTERESTING, MAX_REMEMBERED_PEERS);
    53     if (count > 0)
    54         tr_variantDictAddRaw (dict, TR_KEY_peers2, pex, sizeof (tr_pex) * count);
    55     tr_free (pex);
    56 
    57     count = tr_peerMgrGetPeers ((tr_torrent*) tor, &pex, TR_AF_INET6, TR_PEERS_INTERESTING, MAX_REMEMBERED_PEERS);
    58     if (count > 0)
    59         tr_variantDictAddRaw (dict, TR_KEY_peers2_6, pex, sizeof (tr_pex) * count);
    60 
    61     tr_free (pex);
     49  int count;
     50  tr_pex * pex;
     51
     52  count = tr_peerMgrGetPeers ((tr_torrent*) tor, &pex, TR_AF_INET, TR_PEERS_INTERESTING, MAX_REMEMBERED_PEERS);
     53  if (count > 0)
     54    tr_variantDictAddRaw (dict, TR_KEY_peers2, pex, sizeof (tr_pex) * count);
     55  tr_free (pex);
     56
     57  count = tr_peerMgrGetPeers ((tr_torrent*) tor, &pex, TR_AF_INET6, TR_PEERS_INTERESTING, MAX_REMEMBERED_PEERS);
     58  if (count > 0)
     59    tr_variantDictAddRaw (dict, TR_KEY_peers2_6, pex, sizeof (tr_pex) * count);
     60  tr_free (pex);
    6261}
    6362
     
    6564addPeers (tr_torrent * tor, const uint8_t * buf, int buflen)
    6665{
    67     int i;
    68     int numAdded = 0;
    69     const int count = buflen / sizeof (tr_pex);
    70 
    71     for (i=0; i<count && numAdded<MAX_REMEMBERED_PEERS; ++i)
    72     {
    73         tr_pex pex;
    74         memcpy (&pex, buf + (i * sizeof (tr_pex)), sizeof (tr_pex));
    75         if (tr_isPex (&pex))
    76         {
    77             tr_peerMgrAddPex (tor, TR_PEER_FROM_RESUME, &pex, -1);
    78             ++numAdded;
     66  int i;
     67  int numAdded = 0;
     68  const int count = buflen / sizeof (tr_pex);
     69
     70  for (i=0; i<count && numAdded<MAX_REMEMBERED_PEERS; ++i)
     71    {
     72      tr_pex pex;
     73      memcpy (&pex, buf + (i * sizeof (tr_pex)), sizeof (tr_pex));
     74      if (tr_isPex (&pex))
     75        {
     76          tr_peerMgrAddPex (tor, TR_PEER_FROM_RESUME, &pex, -1);
     77          ++numAdded;
    7978        }
    8079    }
     
    8786loadPeers (tr_variant * dict, tr_torrent * tor)
    8887{
    89     uint64_t        ret = 0;
    90     const uint8_t * str;
    91     size_t          len;
    92 
    93     if (tr_variantDictFindRaw (dict, TR_KEY_peers, &str, &len))
    94     {
    95         const int numAdded = addPeers (tor, str, len);
    96         tr_logAddTorDbg (tor, "Loaded %d IPv4 peers from resume file", numAdded);
    97         ret = TR_FR_PEERS;
    98     }
    99 
    100     if (tr_variantDictFindRaw (dict, TR_KEY_peers6, &str, &len))
    101     {
    102         const int numAdded = addPeers (tor, str, len);
    103         tr_logAddTorDbg (tor, "Loaded %d IPv6 peers from resume file", numAdded);
    104         ret = TR_FR_PEERS;
    105     }
    106 
    107     return ret;
     88  const uint8_t * str;
     89  size_t len;
     90  uint64_t ret = 0;
     91
     92  if (tr_variantDictFindRaw (dict, TR_KEY_peers, &str, &len))
     93    {
     94      const int numAdded = addPeers (tor, str, len);
     95      tr_logAddTorDbg (tor, "Loaded %d IPv4 peers from resume file", numAdded);
     96      ret = TR_FR_PEERS;
     97    }
     98
     99  if (tr_variantDictFindRaw (dict, TR_KEY_peers6, &str, &len))
     100    {
     101      const int numAdded = addPeers (tor, str, len);
     102      tr_logAddTorDbg (tor, "Loaded %d IPv6 peers from resume file", numAdded);
     103      ret = TR_FR_PEERS;
     104    }
     105
     106  return ret;
    108107}
    109108
     
    115114saveDND (tr_variant * dict, const tr_torrent * tor)
    116115{
    117     tr_variant * list;
    118     tr_file_index_t i;
    119     const tr_info * const inf = tr_torrentInfo (tor);
    120     const tr_file_index_t n = inf->fileCount;
    121 
    122     list = tr_variantDictAddList (dict, TR_KEY_dnd, n);
    123     for (i=0; i<n; ++i)
    124         tr_variantListAddInt (list, inf->files[i].dnd ? 1 : 0);
     116  tr_variant * list;
     117  tr_file_index_t i;
     118  const tr_info * const inf = tr_torrentInfo (tor);
     119  const tr_file_index_t n = inf->fileCount;
     120
     121  list = tr_variantDictAddList (dict, TR_KEY_dnd, n);
     122  for (i=0; i<n; ++i)
     123    tr_variantListAddInt (list, inf->files[i].dnd ? 1 : 0);
    125124}
    126125
     
    128127loadDND (tr_variant * dict, tr_torrent * tor)
    129128{
    130     uint64_t ret = 0;
    131     tr_variant * list = NULL;
    132     const tr_file_index_t n = tor->info.fileCount;
    133 
    134     if (tr_variantDictFindList (dict, TR_KEY_dnd, &list)
     129  uint64_t ret = 0;
     130  tr_variant * list = NULL;
     131  const tr_file_index_t n = tor->info.fileCount;
     132
     133  if (tr_variantDictFindList (dict, TR_KEY_dnd, &list)
    135134      && (tr_variantListSize (list) == n))
    136135    {
    137         int64_t           tmp;
    138         tr_file_index_t * dl = tr_new (tr_file_index_t, n);
    139         tr_file_index_t * dnd = tr_new (tr_file_index_t, n);
    140         tr_file_index_t   i, dlCount = 0, dndCount = 0;
    141 
    142         for (i=0; i<n; ++i)
    143         {
    144             if (tr_variantGetInt (tr_variantListChild (list, i), &tmp) && tmp)
    145                 dnd[dndCount++] = i;
    146             else
    147                 dl[dlCount++] = i;
    148         }
    149 
    150         if (dndCount)
    151         {
    152             tr_torrentInitFileDLs (tor, dnd, dndCount, false);
    153             tr_logAddTorDbg (tor, "Resume file found %d files listed as dnd",
    154                        dndCount);
    155         }
    156         if (dlCount)
    157         {
    158             tr_torrentInitFileDLs (tor, dl, dlCount, true);
    159             tr_logAddTorDbg (tor,
    160                        "Resume file found %d files marked for download",
    161                        dlCount);
    162         }
    163 
    164         tr_free (dnd);
    165         tr_free (dl);
    166         ret = TR_FR_DND;
    167     }
    168     else
    169     {
    170         tr_logAddTorDbg (
    171             tor,
    172             "Couldn't load DND flags. DND list (%p) has %zu children; torrent has %d files",
    173             list, tr_variantListSize (list), (int)n);
    174     }
    175 
    176     return ret;
     136      int64_t tmp;
     137      tr_file_index_t * dl = tr_new (tr_file_index_t, n);
     138      tr_file_index_t * dnd = tr_new (tr_file_index_t, n);
     139      tr_file_index_t i, dlCount = 0, dndCount = 0;
     140
     141      for (i=0; i<n; ++i)
     142        {
     143          if (tr_variantGetInt (tr_variantListChild (list, i), &tmp) && tmp)
     144            dnd[dndCount++] = i;
     145          else
     146            dl[dlCount++] = i;
     147        }
     148
     149      if (dndCount)
     150        {
     151          tr_torrentInitFileDLs (tor, dnd, dndCount, false);
     152          tr_logAddTorDbg (tor, "Resume file found %d files listed as dnd", dndCount);
     153        }
     154
     155      if (dlCount)
     156        {
     157          tr_torrentInitFileDLs (tor, dl, dlCount, true);
     158          tr_logAddTorDbg (tor, "Resume file found %d files marked for download", dlCount);
     159        }
     160
     161      tr_free (dnd);
     162      tr_free (dl);
     163      ret = TR_FR_DND;
     164    }
     165  else
     166    {
     167      tr_logAddTorDbg (tor, "Couldn't load DND flags. DND list (%p) has %zu children; torrent has %d files",
     168                       list, tr_variantListSize (list), (int)n);
     169    }
     170
     171  return ret;
    177172}
    178173
     
    184179saveFilePriorities (tr_variant * dict, const tr_torrent * tor)
    185180{
    186     tr_variant * list;
    187     tr_file_index_t i;
    188     const tr_info * const inf = tr_torrentInfo (tor);
    189     const tr_file_index_t n = inf->fileCount;
    190 
    191     list = tr_variantDictAddList (dict, TR_KEY_priority, n);
    192     for (i = 0; i < n; ++i)
    193         tr_variantListAddInt (list, inf->files[i].priority);
     181  tr_variant * list;
     182  tr_file_index_t i;
     183  const tr_info * const inf = tr_torrentInfo (tor);
     184  const tr_file_index_t n = inf->fileCount;
     185
     186  list = tr_variantDictAddList (dict, TR_KEY_priority, n);
     187  for (i=0; i<n; ++i)
     188    tr_variantListAddInt (list, inf->files[i].priority);
    194189}
    195190
     
    197192loadFilePriorities (tr_variant * dict, tr_torrent * tor)
    198193{
    199     tr_variant * list;
    200     uint64_t ret = 0;
    201     const tr_file_index_t n = tor->info.fileCount;
    202 
    203     if (tr_variantDictFindList (dict, TR_KEY_priority, &list)
     194  tr_variant * list;
     195  uint64_t ret = 0;
     196  const tr_file_index_t n = tor->info.fileCount;
     197
     198  if (tr_variantDictFindList (dict, TR_KEY_priority, &list)
    204199      && (tr_variantListSize (list) == n))
    205200    {
    206         int64_t priority;
    207         tr_file_index_t i;
    208         for (i = 0; i < n; ++i)
    209             if (tr_variantGetInt (tr_variantListChild (list, i), &priority))
    210                 tr_torrentInitFilePriority (tor, i, priority);
    211         ret = TR_FR_FILE_PRIORITIES;
     201      int64_t priority;
     202      tr_file_index_t i;
     203
     204      for (i=0; i<n; ++i)
     205        if (tr_variantGetInt (tr_variantListChild (list, i), &priority))
     206          tr_torrentInitFilePriority (tor, i, priority);
     207
     208      ret = TR_FR_FILE_PRIORITIES;
    212209    }
    213210
     
    222219saveSingleSpeedLimit (tr_variant * d, tr_torrent * tor, tr_direction dir)
    223220{
    224     tr_variantDictReserve (d, 3);
    225     tr_variantDictAddInt (d, TR_KEY_speed_Bps, tr_torrentGetSpeedLimit_Bps (tor, dir));
    226     tr_variantDictAddBool (d, TR_KEY_use_global_speed_limit, tr_torrentUsesSessionLimits (tor));
    227     tr_variantDictAddBool (d, TR_KEY_use_speed_limit, tr_torrentUsesSpeedLimit (tor, dir));
     221  tr_variantDictReserve (d, 3);
     222  tr_variantDictAddInt (d, TR_KEY_speed_Bps, tr_torrentGetSpeedLimit_Bps (tor, dir));
     223  tr_variantDictAddBool (d, TR_KEY_use_global_speed_limit, tr_torrentUsesSessionLimits (tor));
     224  tr_variantDictAddBool (d, TR_KEY_use_speed_limit, tr_torrentUsesSpeedLimit (tor, dir));
    228225}
    229226
     
    231228saveSpeedLimits (tr_variant * dict, tr_torrent * tor)
    232229{
    233     saveSingleSpeedLimit (tr_variantDictAddDict (dict, TR_KEY_speed_limit_down, 0), tor, TR_DOWN);
    234     saveSingleSpeedLimit (tr_variantDictAddDict (dict, TR_KEY_speed_limit_up, 0), tor, TR_UP);
     230  saveSingleSpeedLimit (tr_variantDictAddDict (dict, TR_KEY_speed_limit_down, 0), tor, TR_DOWN);
     231  saveSingleSpeedLimit (tr_variantDictAddDict (dict, TR_KEY_speed_limit_up, 0), tor, TR_UP);
    235232}
    236233
     
    238235saveRatioLimits (tr_variant * dict, tr_torrent * tor)
    239236{
    240     tr_variant * d = tr_variantDictAddDict (dict, TR_KEY_ratio_limit, 2);
    241     tr_variantDictAddReal (d, TR_KEY_ratio_limit, tr_torrentGetRatioLimit (tor));
    242     tr_variantDictAddInt (d, TR_KEY_ratio_mode, tr_torrentGetRatioMode (tor));
     237  tr_variant * d = tr_variantDictAddDict (dict, TR_KEY_ratio_limit, 2);
     238  tr_variantDictAddReal (d, TR_KEY_ratio_limit, tr_torrentGetRatioLimit (tor));
     239  tr_variantDictAddInt (d, TR_KEY_ratio_mode, tr_torrentGetRatioMode (tor));
    243240}
    244241
     
    246243saveIdleLimits (tr_variant * dict, tr_torrent * tor)
    247244{
    248     tr_variant * d = tr_variantDictAddDict (dict, TR_KEY_idle_limit, 2);
    249     tr_variantDictAddInt (d, TR_KEY_idle_limit, tr_torrentGetIdleLimit (tor));
    250     tr_variantDictAddInt (d, TR_KEY_idle_mode, tr_torrentGetIdleMode (tor));
     245  tr_variant * d = tr_variantDictAddDict (dict, TR_KEY_idle_limit, 2);
     246  tr_variantDictAddInt (d, TR_KEY_idle_limit, tr_torrentGetIdleLimit (tor));
     247  tr_variantDictAddInt (d, TR_KEY_idle_mode, tr_torrentGetIdleMode (tor));
    251248}
    252249
     
    254251loadSingleSpeedLimit (tr_variant * d, tr_direction dir, tr_torrent * tor)
    255252{
    256     int64_t i;
    257     bool boolVal;
    258 
    259     if (tr_variantDictFindInt (d, TR_KEY_speed_Bps, &i))
    260         tr_torrentSetSpeedLimit_Bps (tor, dir, i);
    261     else if (tr_variantDictFindInt (d, TR_KEY_speed, &i))
    262         tr_torrentSetSpeedLimit_Bps (tor, dir, i*1024);
    263 
    264     if (tr_variantDictFindBool (d, TR_KEY_use_speed_limit, &boolVal))
    265         tr_torrentUseSpeedLimit (tor, dir, boolVal);
    266 
    267     if (tr_variantDictFindBool (d, TR_KEY_use_global_speed_limit, &boolVal))
    268         tr_torrentUseSessionLimits (tor, boolVal);
    269 }
    270 
    271 enum old_speed_modes
    272 {
    273     TR_SPEEDLIMIT_GLOBAL,   /* only follow the overall speed limit */
    274     TR_SPEEDLIMIT_SINGLE    /* only follow the per-torrent limit */
    275 };
     253  int64_t i;
     254  bool boolVal;
     255
     256  if (tr_variantDictFindInt (d, TR_KEY_speed_Bps, &i))
     257    tr_torrentSetSpeedLimit_Bps (tor, dir, i);
     258  else if (tr_variantDictFindInt (d, TR_KEY_speed, &i))
     259    tr_torrentSetSpeedLimit_Bps (tor, dir, i*1024);
     260
     261  if (tr_variantDictFindBool (d, TR_KEY_use_speed_limit, &boolVal))
     262    tr_torrentUseSpeedLimit (tor, dir, boolVal);
     263
     264  if (tr_variantDictFindBool (d, TR_KEY_use_global_speed_limit, &boolVal))
     265    tr_torrentUseSessionLimits (tor, boolVal);
     266}
    276267
    277268static uint64_t
    278269loadSpeedLimits (tr_variant * dict, tr_torrent * tor)
    279270{
    280     tr_variant * d;
    281     uint64_t ret = 0;
    282 
    283 
    284     if (tr_variantDictFindDict (dict, TR_KEY_speed_limit_up, &d))
    285     {
    286         loadSingleSpeedLimit (d, TR_UP, tor);
    287         ret = TR_FR_SPEEDLIMIT;
    288     }
    289     if (tr_variantDictFindDict (dict, TR_KEY_speed_limit_down, &d))
    290     {
    291         loadSingleSpeedLimit (d, TR_DOWN, tor);
    292         ret = TR_FR_SPEEDLIMIT;
    293     }
    294 
    295     return ret;
     271  tr_variant * d;
     272  uint64_t ret = 0;
     273
     274  if (tr_variantDictFindDict (dict, TR_KEY_speed_limit_up, &d))
     275    {
     276      loadSingleSpeedLimit (d, TR_UP, tor);
     277      ret = TR_FR_SPEEDLIMIT;
     278    }
     279
     280  if (tr_variantDictFindDict (dict, TR_KEY_speed_limit_down, &d))
     281    {
     282      loadSingleSpeedLimit (d, TR_DOWN, tor);
     283      ret = TR_FR_SPEEDLIMIT;
     284    }
     285
     286  return ret;
    296287}
    297288
     
    299290loadRatioLimits (tr_variant * dict, tr_torrent * tor)
    300291{
    301     tr_variant * d;
    302     uint64_t ret = 0;
    303 
    304     if (tr_variantDictFindDict (dict, TR_KEY_ratio_limit, &d))
    305     {
    306         int64_t i;
    307         double dratio;
    308         if (tr_variantDictFindReal (d, TR_KEY_ratio_limit, &dratio))
    309             tr_torrentSetRatioLimit (tor, dratio);
    310         if (tr_variantDictFindInt (d, TR_KEY_ratio_mode, &i))
    311             tr_torrentSetRatioMode (tor, i);
     292  tr_variant * d;
     293  uint64_t ret = 0;
     294
     295  if (tr_variantDictFindDict (dict, TR_KEY_ratio_limit, &d))
     296    {
     297      int64_t i;
     298      double dratio;
     299
     300      if (tr_variantDictFindReal (d, TR_KEY_ratio_limit, &dratio))
     301        tr_torrentSetRatioLimit (tor, dratio);
     302
     303      if (tr_variantDictFindInt (d, TR_KEY_ratio_mode, &i))
     304        tr_torrentSetRatioMode (tor, i);
     305
    312306      ret = TR_FR_RATIOLIMIT;
    313307    }
    314308
    315     return ret;
     309  return ret;
    316310}
    317311
     
    319313loadIdleLimits (tr_variant * dict, tr_torrent * tor)
    320314{
    321     tr_variant * d;
    322     uint64_t ret = 0;
    323 
    324     if (tr_variantDictFindDict (dict, TR_KEY_idle_limit, &d))
    325     {
    326         int64_t i;
    327         int64_t imin;
    328         if (tr_variantDictFindInt (d, TR_KEY_idle_limit, &imin))
    329             tr_torrentSetIdleLimit (tor, imin);
    330         if (tr_variantDictFindInt (d, TR_KEY_idle_mode, &i))
    331             tr_torrentSetIdleMode (tor, i);
     315  tr_variant * d;
     316  uint64_t ret = 0;
     317
     318  if (tr_variantDictFindDict (dict, TR_KEY_idle_limit, &d))
     319    {
     320      int64_t i;
     321      int64_t imin;
     322
     323      if (tr_variantDictFindInt (d, TR_KEY_idle_limit, &imin))
     324        tr_torrentSetIdleLimit (tor, imin);
     325
     326      if (tr_variantDictFindInt (d, TR_KEY_idle_mode, &i))
     327        tr_torrentSetIdleMode (tor, i);
     328
    332329      ret = TR_FR_IDLELIMIT;
    333330    }
    334331
    335     return ret;
     332  return ret;
    336333}
    337334
     
    428425bitfieldToBenc (const tr_bitfield * b, tr_variant * benc)
    429426{
    430     if (tr_bitfieldHasAll (b))
    431         tr_variantInitStr (benc, "all", 3);
    432     else if (tr_bitfieldHasNone (b))
    433         tr_variantInitStr (benc, "none", 4);
    434     else {
    435         size_t byte_count = 0;
    436         uint8_t * raw = tr_bitfieldGetRaw (b, &byte_count);
    437         tr_variantInitRaw (benc, raw, byte_count);
    438         tr_free (raw);
     427  if (tr_bitfieldHasAll (b))
     428    {
     429      tr_variantInitStr (benc, "all", 3);
     430    }
     431  else if (tr_bitfieldHasNone (b))
     432    {
     433      tr_variantInitStr (benc, "none", 4);
     434    }
     435  else
     436    {
     437      size_t byte_count = 0;
     438      uint8_t * raw = tr_bitfieldGetRaw (b, &byte_count);
     439      tr_variantInitRaw (benc, raw, byte_count);
     440      tr_free (raw);
    439441    }
    440442}
     
    444446saveProgress (tr_variant * dict, tr_torrent * tor)
    445447{
    446     tr_variant * l;
    447     tr_variant * prog;
    448     tr_file_index_t fi;
    449     const tr_info * inf = tr_torrentInfo (tor);
    450     const time_t now = tr_time ();
    451 
    452     prog = tr_variantDictAddDict (dict, TR_KEY_progress, 3);
    453 
    454     /* add the file/piece check timestamps... */
    455     l = tr_variantDictAddList (prog, TR_KEY_time_checked, inf->fileCount);
    456     for (fi=0; fi<inf->fileCount; ++fi)
    457     {
    458         const tr_piece * p;
    459         const tr_piece * pend;
    460         time_t oldest_nonzero = now;
    461         time_t newest = 0;
    462         bool has_zero = false;
    463         const time_t mtime = tr_torrentGetFileMTime (tor, fi);
    464         const tr_file * f = &inf->files[fi];
    465 
    466         /* get the oldest and newest nonzero timestamps for pieces in this file */
    467         for (p=&inf->pieces[f->firstPiece], pend=&inf->pieces[f->lastPiece]; p!=pend; ++p)
    468         {
    469             if (!p->timeChecked)
    470                 has_zero = true;
    471             else if (oldest_nonzero > p->timeChecked)
    472                 oldest_nonzero = p->timeChecked;
    473             if (newest < p->timeChecked)
    474                 newest = p->timeChecked;
    475         }
    476 
    477         /* If some of a file's pieces have been checked more recently than
    478            the file's mtime, and some lest recently, then that file will
    479            have a list containing timestamps for each piece.
    480 
    481            However, the most common use case is that the file doesn't change
    482            after it's downloaded. To reduce overhead in the .resume file,
    483            only a single timestamp is saved for the file if *all* or *none*
    484            of the pieces were tested more recently than the file's mtime. */
    485 
    486         if (!has_zero && (mtime <= oldest_nonzero)) /* all checked */
    487             tr_variantListAddInt (l, oldest_nonzero);
    488         else if (newest < mtime) /* none checked */
    489             tr_variantListAddInt (l, newest);
    490         else { /* some are checked, some aren't... so list piece by piece */
    491             const int offset = oldest_nonzero - 1;
    492             tr_variant * ll = tr_variantListAddList (l, 2 + f->lastPiece - f->firstPiece);
    493             tr_variantListAddInt (ll, offset);
    494             for (p=&inf->pieces[f->firstPiece], pend=&inf->pieces[f->lastPiece]+1; p!=pend; ++p)
    495                 tr_variantListAddInt (ll, p->timeChecked ? p->timeChecked - offset : 0);
    496         }
    497     }
    498 
    499     /* add the progress */
    500     if (tor->completeness == TR_SEED)
    501         tr_variantDictAddStr (prog, TR_KEY_have, "all");
    502 
    503     /* add the blocks bitfield */
    504     bitfieldToBenc (&tor->completion.blockBitfield, tr_variantDictAdd (prog, TR_KEY_blocks));
     448  tr_variant * l;
     449  tr_variant * prog;
     450  tr_file_index_t fi;
     451  const tr_info * inf = tr_torrentInfo (tor);
     452  const time_t now = tr_time ();
     453
     454  prog = tr_variantDictAddDict (dict, TR_KEY_progress, 3);
     455
     456  /* add the file/piece check timestamps... */
     457  l = tr_variantDictAddList (prog, TR_KEY_time_checked, inf->fileCount);
     458  for (fi=0; fi<inf->fileCount; ++fi)
     459    {
     460      const tr_piece * p;
     461      const tr_piece * pend;
     462      time_t oldest_nonzero = now;
     463      time_t newest = 0;
     464      bool has_zero = false;
     465      const time_t mtime = tr_torrentGetFileMTime (tor, fi);
     466      const tr_file * f = &inf->files[fi];
     467
     468      /* get the oldest and newest nonzero timestamps for pieces in this file */
     469      for (p=&inf->pieces[f->firstPiece], pend=&inf->pieces[f->lastPiece]; p!=pend; ++p)
     470        {
     471          if (!p->timeChecked)
     472            has_zero = true;
     473          else if (oldest_nonzero > p->timeChecked)
     474            oldest_nonzero = p->timeChecked;
     475
     476          if (newest < p->timeChecked)
     477            newest = p->timeChecked;
     478        }
     479
     480      /* If some of a file's pieces have been checked more recently than
     481         the file's mtime, and some less recently, then that file will
     482         have a list containing timestamps for each piece.
     483
     484         However, the most common use case is that the file doesn't change
     485         after it's downloaded. To reduce overhead in the .resume file,
     486         only a single timestamp is saved for the file if *all* or *none*
     487         of the pieces were tested more recently than the file's mtime. */
     488
     489      if (!has_zero && (mtime <= oldest_nonzero)) /* all checked */
     490        {
     491          tr_variantListAddInt (l, oldest_nonzero);
     492        }
     493      else if (newest < mtime) /* none checked */
     494        {
     495          tr_variantListAddInt (l, newest);
     496        }
     497      else /* some are checked, some aren't... so list piece by piece */
     498        {
     499          const int offset = oldest_nonzero - 1;
     500          tr_variant * ll = tr_variantListAddList (l, 2 + f->lastPiece - f->firstPiece);
     501          tr_variantListAddInt (ll, offset);
     502          for (p=&inf->pieces[f->firstPiece], pend=&inf->pieces[f->lastPiece]+1; p!=pend; ++p)
     503            tr_variantListAddInt (ll, p->timeChecked ? p->timeChecked - offset : 0);
     504        }
     505    }
     506
     507  /* add the progress */
     508  if (tor->completeness == TR_SEED)
     509    tr_variantDictAddStr (prog, TR_KEY_have, "all");
     510
     511  /* add the blocks bitfield */
     512  bitfieldToBenc (&tor->completion.blockBitfield, tr_variantDictAdd (prog, TR_KEY_blocks));
    505513}
    506514
     
    508516loadProgress (tr_variant * dict, tr_torrent * tor)
    509517{
    510     size_t i, n;
    511     uint64_t ret = 0;
    512     tr_variant * prog;
    513     const tr_info * inf = tr_torrentInfo (tor);
    514 
    515     for (i=0, n=inf->pieceCount; i<n; ++i)
    516         inf->pieces[i].timeChecked = 0;
    517 
    518     if (tr_variantDictFindDict (dict, TR_KEY_progress, &prog))
    519     {
    520         const char * err;
    521         const char * str;
    522         const uint8_t * raw;
    523         size_t rawlen;
    524         tr_variant * l;
    525         tr_variant * b;
    526         struct tr_bitfield blocks = TR_BITFIELD_INIT;
    527 
    528         if (tr_variantDictFindList (prog, TR_KEY_time_checked, &l))
    529         {
    530             /* per-piece timestamps were added in 2.20.
    531 
    532                If some of a file's pieces have been checked more recently than
    533                the file's mtime, and some lest recently, then that file will
    534                have a list containing timestamps for each piece.
    535 
    536                However, the most common use case is that the file doesn't change
    537                after it's downloaded. To reduce overhead in the .resume file,
    538                only a single timestamp is saved for the file if *all* or *none*
    539                of the pieces were tested more recently than the file's mtime. */
    540 
    541             tr_file_index_t fi;
    542 
    543             for (fi=0; fi<inf->fileCount; ++fi)
     518  size_t i, n;
     519  uint64_t ret = 0;
     520  tr_variant * prog;
     521  const tr_info * inf = tr_torrentInfo (tor);
     522
     523  for (i=0, n=inf->pieceCount; i<n; ++i)
     524    inf->pieces[i].timeChecked = 0;
     525
     526  if (tr_variantDictFindDict (dict, TR_KEY_progress, &prog))
     527    {
     528      const char * err;
     529      const char * str;
     530      const uint8_t * raw;
     531      size_t rawlen;
     532      tr_variant * l;
     533      tr_variant * b;
     534      struct tr_bitfield blocks = TR_BITFIELD_INIT;
     535
     536      if (tr_variantDictFindList (prog, TR_KEY_time_checked, &l))
     537        {
     538          /* per-piece timestamps were added in 2.20.
     539
     540             If some of a file's pieces have been checked more recently than
     541             the file's mtime, and some lest recently, then that file will
     542             have a list containing timestamps for each piece.
     543
     544             However, the most common use case is that the file doesn't change
     545             after it's downloaded. To reduce overhead in the .resume file,
     546             only a single timestamp is saved for the file if *all* or *none*
     547             of the pieces were tested more recently than the file's mtime. */
     548
     549          tr_file_index_t fi;
     550
     551          for (fi=0; fi<inf->fileCount; ++fi)
    544552            {
    545                 tr_variant * b = tr_variantListChild (l, fi);
    546                 const tr_file * f = &inf->files[fi];
    547                 tr_piece * p = &inf->pieces[f->firstPiece];
    548                 const tr_piece * pend = &inf->pieces[f->lastPiece]+1;
    549 
    550                 if (tr_variantIsInt (b))
     553              tr_variant * b = tr_variantListChild (l, fi);
     554              const tr_file * f = &inf->files[fi];
     555              tr_piece * p = &inf->pieces[f->firstPiece];
     556              const tr_piece * pend = &inf->pieces[f->lastPiece]+1;
     557
     558              if (tr_variantIsInt (b))
    551559                {
    552                     int64_t t;
    553                     tr_variantGetInt (b, &t);
    554                     for (; p!=pend; ++p)
    555                         p->timeChecked = (time_t)t;
     560                  int64_t t;
     561                  tr_variantGetInt (b, &t);
     562                  for (; p!=pend; ++p)
     563                    p->timeChecked = (time_t)t;
    556564                }
    557                 else if (tr_variantIsList (b))
     565              else if (tr_variantIsList (b))
    558566                {
    559                     int i = 0;
    560                     int64_t offset = 0;
    561                     const int pieces = f->lastPiece + 1 - f->firstPiece;
    562 
    563                     tr_variantGetInt (tr_variantListChild (b, 0), &offset);
    564 
    565                     for (i=0; i<pieces; ++i)
     567                  int i = 0;
     568                  int64_t offset = 0;
     569                  const int pieces = f->lastPiece + 1 - f->firstPiece;
     570
     571                  tr_variantGetInt (tr_variantListChild (b, 0), &offset);
     572
     573                  for (i=0; i<pieces; ++i)
    566574                    {
    567                         int64_t t = 0;
    568                         tr_variantGetInt (tr_variantListChild (b, i+1), &t);
    569                         inf->pieces[f->firstPiece+i].timeChecked = (time_t)(t ? t + offset : 0);
     575                      int64_t t = 0;
     576                      tr_variantGetInt (tr_variantListChild (b, i+1), &t);
     577                      inf->pieces[f->firstPiece+i].timeChecked = (time_t)(t ? t + offset : 0);
    570578                    }
    571579                }
    572580            }
    573581        }
    574         else if (tr_variantDictFindList (prog, TR_KEY_mtimes, &l))
    575         {
    576             tr_file_index_t fi;
    577 
    578             /* Before 2.20, we stored the files' mtimes in the .resume file.
    579                When loading the .resume file, a torrent's file would be flagged
    580                as untested if its stored mtime didn't match its real mtime. */
    581 
    582             for (fi=0; fi<inf->fileCount; ++fi)
     582      else if (tr_variantDictFindList (prog, TR_KEY_mtimes, &l))
     583        {
     584          tr_file_index_t fi;
     585
     586          /* Before 2.20, we stored the files' mtimes in the .resume file.
     587             When loading the .resume file, a torrent's file would be flagged
     588             as untested if its stored mtime didn't match its real mtime. */
     589
     590          for (fi=0; fi<inf->fileCount; ++fi)
    583591            {
    584                 int64_t t;
    585 
    586                 if (tr_variantGetInt (tr_variantListChild (l, fi), &t))
     592              int64_t t;
     593
     594              if (tr_variantGetInt (tr_variantListChild (l, fi), &t))
    587595                {
    588                     const tr_file * f = &inf->files[fi];
    589                     tr_piece * p = &inf->pieces[f->firstPiece];
    590                     const tr_piece * pend = &inf->pieces[f->lastPiece];
    591                     const time_t mtime = tr_torrentGetFileMTime (tor, fi);
    592                     const time_t timeChecked = mtime==t ? mtime : 0;
    593 
    594                     for (; p!=pend; ++p)
    595                         p->timeChecked = timeChecked;
     596                  const tr_file * f = &inf->files[fi];
     597                  tr_piece * p = &inf->pieces[f->firstPiece];
     598                  const tr_piece * pend = &inf->pieces[f->lastPiece];
     599                  const time_t mtime = tr_torrentGetFileMTime (tor, fi);
     600                  const time_t timeChecked = mtime==t ? mtime : 0;
     601
     602                  for (; p!=pend; ++p)
     603                    p->timeChecked = timeChecked;
    596604                }
    597605            }
    598606        }
    599607
    600         err = NULL;
    601         tr_bitfieldConstruct (&blocks, tor->blockCount);
    602 
    603         if ((b = tr_variantDictFind (prog, TR_KEY_blocks)))
    604         {
    605             size_t buflen;
    606             const uint8_t * buf;
    607 
    608             if (!tr_variantGetRaw (b, &buf, &buflen))
    609                 err = "Invalid value for \"blocks\"";
    610             else if ((buflen == 3) && !memcmp (buf, "all", 3))
    611                 tr_bitfieldSetHasAll (&blocks);
    612             else if ((buflen == 4) && !memcmp (buf, "none", 4))
    613                 tr_bitfieldSetHasNone (&blocks);
    614             else
    615                 tr_bitfieldSetRaw (&blocks, buf, buflen, true);
    616         }
    617         else if (tr_variantDictFindStr (prog, TR_KEY_have, &str, NULL))
    618         {
    619             if (!strcmp (str, "all"))
    620                 tr_bitfieldSetHasAll (&blocks);
    621             else
    622                 err = "Invalid value for HAVE";
    623         }
    624         else if (tr_variantDictFindRaw (prog, TR_KEY_bitfield, &raw, &rawlen))
    625         {
    626             tr_bitfieldSetRaw (&blocks, raw, rawlen, true);
    627         }
    628         else err = "Couldn't find 'pieces' or 'have' or 'bitfield'";
    629 
    630         if (err != NULL)
    631             tr_logAddTorDbg (tor, "Torrent needs to be verified - %s", err);
    632         else
    633             tr_cpBlockInit (&tor->completion, &blocks);
    634 
    635         tr_bitfieldDestruct (&blocks);
    636         ret = TR_FR_PROGRESS;
    637     }
    638 
    639     return ret;
     608      err = NULL;
     609      tr_bitfieldConstruct (&blocks, tor->blockCount);
     610
     611      if ((b = tr_variantDictFind (prog, TR_KEY_blocks)))
     612        {
     613          size_t buflen;
     614          const uint8_t * buf;
     615
     616          if (!tr_variantGetRaw (b, &buf, &buflen))
     617            err = "Invalid value for \"blocks\"";
     618          else if ((buflen == 3) && !memcmp (buf, "all", 3))
     619            tr_bitfieldSetHasAll (&blocks);
     620          else if ((buflen == 4) && !memcmp (buf, "none", 4))
     621            tr_bitfieldSetHasNone (&blocks);
     622          else
     623            tr_bitfieldSetRaw (&blocks, buf, buflen, true);
     624        }
     625      else if (tr_variantDictFindStr (prog, TR_KEY_have, &str, NULL))
     626        {
     627          if (!strcmp (str, "all"))
     628            tr_bitfieldSetHasAll (&blocks);
     629          else
     630            err = "Invalid value for HAVE";
     631        }
     632      else if (tr_variantDictFindRaw (prog, TR_KEY_bitfield, &raw, &rawlen))
     633        {
     634          tr_bitfieldSetRaw (&blocks, raw, rawlen, true);
     635        }
     636      else err = "Couldn't find 'pieces' or 'have' or 'bitfield'";
     637
     638      if (err != NULL)
     639        tr_logAddTorDbg (tor, "Torrent needs to be verified - %s", err);
     640      else
     641        tr_cpBlockInit (&tor->completion, &blocks);
     642
     643      tr_bitfieldDestruct (&blocks);
     644      ret = TR_FR_PROGRESS;
     645    }
     646
     647  return ret;
    640648}
    641649
     
    647655tr_torrentSaveResume (tr_torrent * tor)
    648656{
    649     int err;
    650     tr_variant top;
    651     char * filename;
    652 
    653     if (!tr_isTorrent (tor))
    654         return;
    655 
    656     tr_variantInitDict (&top, 50); /* arbitrary "big enough" number */
    657     tr_variantDictAddInt (&top, TR_KEY_seeding_time_seconds, tor->secondsSeeding);
    658     tr_variantDictAddInt (&top, TR_KEY_downloading_time_seconds, tor->secondsDownloading);
    659     tr_variantDictAddInt (&top, TR_KEY_activity_date, tor->activityDate);
    660     tr_variantDictAddInt (&top, TR_KEY_added_date, tor->addedDate);
    661     tr_variantDictAddInt (&top, TR_KEY_corrupt, tor->corruptPrev + tor->corruptCur);
    662     tr_variantDictAddInt (&top, TR_KEY_done_date, tor->doneDate);
    663     tr_variantDictAddStr (&top, TR_KEY_destination, tor->downloadDir);
    664     if (tor->incompleteDir != NULL)
    665         tr_variantDictAddStr (&top, TR_KEY_incomplete_dir, tor->incompleteDir);
    666     tr_variantDictAddInt (&top, TR_KEY_downloaded, tor->downloadedPrev + tor->downloadedCur);
    667     tr_variantDictAddInt (&top, TR_KEY_uploaded, tor->uploadedPrev + tor->uploadedCur);
    668     tr_variantDictAddInt (&top, TR_KEY_max_peers, tor->maxConnectedPeers);
    669     tr_variantDictAddInt (&top, TR_KEY_bandwidth_priority, tr_torrentGetPriority (tor));
    670     tr_variantDictAddBool (&top, TR_KEY_paused, !tor->isRunning);
    671     savePeers (&top, tor);
    672     if (tr_torrentHasMetadata (tor))
    673     {
    674         saveFilePriorities (&top, tor);
    675         saveDND (&top, tor);
    676         saveProgress (&top, tor);
    677     }
    678     saveSpeedLimits (&top, tor);
    679     saveRatioLimits (&top, tor);
    680     saveIdleLimits (&top, tor);
    681     saveFilenames (&top, tor);
    682     saveName (&top, tor);
    683 
    684     filename = getResumeFilename (tor);
    685     if ((err = tr_variantToFile (&top, TR_VARIANT_FMT_BENC, filename)))
    686         tr_torrentSetLocalError (tor, "Unable to save resume file: %s", tr_strerror (err));
    687     tr_free (filename);
    688 
    689     tr_variantFree (&top);
     657  int err;
     658  tr_variant top;
     659  char * filename;
     660
     661  if (!tr_isTorrent (tor))
     662    return;
     663
     664  tr_variantInitDict (&top, 50); /* arbitrary "big enough" number */
     665  tr_variantDictAddInt (&top, TR_KEY_seeding_time_seconds, tor->secondsSeeding);
     666  tr_variantDictAddInt (&top, TR_KEY_downloading_time_seconds, tor->secondsDownloading);
     667  tr_variantDictAddInt (&top, TR_KEY_activity_date, tor->activityDate);
     668  tr_variantDictAddInt (&top, TR_KEY_added_date, tor->addedDate);
     669  tr_variantDictAddInt (&top, TR_KEY_corrupt, tor->corruptPrev + tor->corruptCur);
     670  tr_variantDictAddInt (&top, TR_KEY_done_date, tor->doneDate);
     671  tr_variantDictAddStr (&top, TR_KEY_destination, tor->downloadDir);
     672  if (tor->incompleteDir != NULL)
     673    tr_variantDictAddStr (&top, TR_KEY_incomplete_dir, tor->incompleteDir);
     674  tr_variantDictAddInt (&top, TR_KEY_downloaded, tor->downloadedPrev + tor->downloadedCur);
     675  tr_variantDictAddInt (&top, TR_KEY_uploaded, tor->uploadedPrev + tor->uploadedCur);
     676  tr_variantDictAddInt (&top, TR_KEY_max_peers, tor->maxConnectedPeers);
     677  tr_variantDictAddInt (&top, TR_KEY_bandwidth_priority, tr_torrentGetPriority (tor));
     678  tr_variantDictAddBool (&top, TR_KEY_paused, !tor->isRunning);
     679  savePeers (&top, tor);
     680  if (tr_torrentHasMetadata (tor))
     681    {
     682      saveFilePriorities (&top, tor);
     683      saveDND (&top, tor);
     684      saveProgress (&top, tor);
     685    }
     686  saveSpeedLimits (&top, tor);
     687  saveRatioLimits (&top, tor);
     688  saveIdleLimits (&top, tor);
     689  saveFilenames (&top, tor);
     690  saveName (&top, tor);
     691
     692  filename = getResumeFilename (tor);
     693  if ((err = tr_variantToFile (&top, TR_VARIANT_FMT_BENC, filename)))
     694    tr_torrentSetLocalError (tor, "Unable to save resume file: %s", tr_strerror (err));
     695  tr_free (filename);
     696
     697  tr_variantFree (&top);
    690698}
    691699
     
    693701loadFromFile (tr_torrent * tor, uint64_t fieldsToLoad)
    694702{
    695     size_t len;
    696     int64_t  i;
    697     const char * str;
    698     char * filename;
    699     tr_variant top;
    700     bool boolVal;
    701     uint64_t fieldsLoaded = 0;
    702     const bool wasDirty = tor->isDirty;
    703 
    704     assert (tr_isTorrent (tor));
    705 
    706     filename = getResumeFilename (tor);
    707 
    708     if (tr_variantFromFile (&top, TR_VARIANT_FMT_BENC, filename))
    709     {
    710         tr_logAddTorDbg (tor, "Couldn't read \"%s\"", filename);
    711 
    712         tr_free (filename);
    713         return fieldsLoaded;
    714     }
    715 
    716     tr_logAddTorDbg (tor, "Read resume file \"%s\"", filename);
    717 
    718     if ((fieldsToLoad & TR_FR_CORRUPT)
     703  size_t len;
     704  int64_t  i;
     705  const char * str;
     706  char * filename;
     707  tr_variant top;
     708  bool boolVal;
     709  uint64_t fieldsLoaded = 0;
     710  const bool wasDirty = tor->isDirty;
     711
     712  assert (tr_isTorrent (tor));
     713
     714  filename = getResumeFilename (tor);
     715
     716  if (tr_variantFromFile (&top, TR_VARIANT_FMT_BENC, filename))
     717    {
     718      tr_logAddTorDbg (tor, "Couldn't read \"%s\"", filename);
     719
     720      tr_free (filename);
     721      return fieldsLoaded;
     722    }
     723
     724  tr_logAddTorDbg (tor, "Read resume file \"%s\"", filename);
     725
     726  if ((fieldsToLoad & TR_FR_CORRUPT)
    719727      && tr_variantDictFindInt (&top, TR_KEY_corrupt, &i))
    720728    {
    721         tor->corruptPrev = i;
    722         fieldsLoaded |= TR_FR_CORRUPT;
    723     }
    724 
    725     if ((fieldsToLoad & (TR_FR_PROGRESS | TR_FR_DOWNLOAD_DIR))
     729      tor->corruptPrev = i;
     730      fieldsLoaded |= TR_FR_CORRUPT;
     731    }
     732
     733  if ((fieldsToLoad & (TR_FR_PROGRESS | TR_FR_DOWNLOAD_DIR))
    726734      && (tr_variantDictFindStr (&top, TR_KEY_destination, &str, &len))
    727735      && (str && *str))
    728736    {
    729         const bool is_current_dir = tor->currentDir == tor->downloadDir;
    730         tr_free (tor->downloadDir);
    731         tor->downloadDir = tr_strndup (str, len);
    732         if (is_current_dir)
    733           tor->currentDir = tor->downloadDir;
    734         fieldsLoaded |= TR_FR_DOWNLOAD_DIR;
    735     }
    736 
    737     if ((fieldsToLoad & (TR_FR_PROGRESS | TR_FR_INCOMPLETE_DIR))
     737      const bool is_current_dir = tor->currentDir == tor->downloadDir;
     738      tr_free (tor->downloadDir);
     739      tor->downloadDir = tr_strndup (str, len);
     740      if (is_current_dir)
     741        tor->currentDir = tor->downloadDir;
     742      fieldsLoaded |= TR_FR_DOWNLOAD_DIR;
     743    }
     744
     745  if ((fieldsToLoad & (TR_FR_PROGRESS | TR_FR_INCOMPLETE_DIR))
    738746      && (tr_variantDictFindStr (&top, TR_KEY_incomplete_dir, &str, &len))
    739747      && (str && *str))
    740748    {
    741         const bool is_current_dir = tor->currentDir == tor->incompleteDir;
    742         tr_free (tor->incompleteDir);
    743         tor->incompleteDir = tr_strndup (str, len);
    744         if (is_current_dir)
    745           tor->currentDir = tor->incompleteDir;
    746         fieldsLoaded |= TR_FR_INCOMPLETE_DIR;
    747     }
    748 
    749     if ((fieldsToLoad & TR_FR_DOWNLOADED)
     749      const bool is_current_dir = tor->currentDir == tor->incompleteDir;
     750      tr_free (tor->incompleteDir);
     751      tor->incompleteDir = tr_strndup (str, len);
     752      if (is_current_dir)
     753        tor->currentDir = tor->incompleteDir;
     754      fieldsLoaded |= TR_FR_INCOMPLETE_DIR;
     755    }
     756
     757  if ((fieldsToLoad & TR_FR_DOWNLOADED)
    750758      && tr_variantDictFindInt (&top, TR_KEY_downloaded, &i))
    751759    {
    752         tor->downloadedPrev = i;
    753         fieldsLoaded |= TR_FR_DOWNLOADED;
    754     }
    755 
    756     if ((fieldsToLoad & TR_FR_UPLOADED)
     760      tor->downloadedPrev = i;
     761      fieldsLoaded |= TR_FR_DOWNLOADED;
     762    }
     763
     764  if ((fieldsToLoad & TR_FR_UPLOADED)
    757765      && tr_variantDictFindInt (&top, TR_KEY_uploaded, &i))
    758766    {
    759         tor->uploadedPrev = i;
    760         fieldsLoaded |= TR_FR_UPLOADED;
    761     }
    762 
    763     if ((fieldsToLoad & TR_FR_MAX_PEERS)
     767      tor->uploadedPrev = i;
     768      fieldsLoaded |= TR_FR_UPLOADED;
     769    }
     770
     771  if ((fieldsToLoad & TR_FR_MAX_PEERS)
    764772      && tr_variantDictFindInt (&top, TR_KEY_max_peers, &i))
    765773    {
    766         tor->maxConnectedPeers = i;
    767         fieldsLoaded |= TR_FR_MAX_PEERS;
    768     }
    769 
    770     if ((fieldsToLoad & TR_FR_RUN)
     774      tor->maxConnectedPeers = i;
     775      fieldsLoaded |= TR_FR_MAX_PEERS;
     776    }
     777
     778  if ((fieldsToLoad & TR_FR_RUN)
    771779      && tr_variantDictFindBool (&top, TR_KEY_paused, &boolVal))
    772780    {
    773         tor->isRunning = !boolVal;
    774         fieldsLoaded |= TR_FR_RUN;
    775     }
    776 
    777     if ((fieldsToLoad & TR_FR_ADDED_DATE)
     781      tor->isRunning = !boolVal;
     782      fieldsLoaded |= TR_FR_RUN;
     783    }
     784
     785  if ((fieldsToLoad & TR_FR_ADDED_DATE)
    778786      && tr_variantDictFindInt (&top, TR_KEY_added_date, &i))
    779787    {
    780         tor->addedDate = i;
    781         fieldsLoaded |= TR_FR_ADDED_DATE;
    782     }
    783 
    784     if ((fieldsToLoad & TR_FR_DONE_DATE)
     788      tor->addedDate = i;
     789      fieldsLoaded |= TR_FR_ADDED_DATE;
     790    }
     791
     792  if ((fieldsToLoad & TR_FR_DONE_DATE)
    785793      && tr_variantDictFindInt (&top, TR_KEY_done_date, &i))
    786794    {
    787         tor->doneDate = i;
    788         fieldsLoaded |= TR_FR_DONE_DATE;
    789     }
    790 
    791     if ((fieldsToLoad & TR_FR_ACTIVITY_DATE)
     795      tor->doneDate = i;
     796      fieldsLoaded |= TR_FR_DONE_DATE;
     797    }
     798
     799  if ((fieldsToLoad & TR_FR_ACTIVITY_DATE)
    792800      && tr_variantDictFindInt (&top, TR_KEY_activity_date, &i))
    793801    {
    794         tr_torrentSetActivityDate (tor, i);
    795         fieldsLoaded |= TR_FR_ACTIVITY_DATE;
    796     }
    797 
    798     if ((fieldsToLoad & TR_FR_TIME_SEEDING)
     802      tr_torrentSetActivityDate (tor, i);
     803      fieldsLoaded |= TR_FR_ACTIVITY_DATE;
     804    }
     805
     806  if ((fieldsToLoad & TR_FR_TIME_SEEDING)
    799807      && tr_variantDictFindInt (&top, TR_KEY_seeding_time_seconds, &i))
    800808    {
    801         tor->secondsSeeding = i;
    802         fieldsLoaded |= TR_FR_TIME_SEEDING;
    803     }
    804 
    805     if ((fieldsToLoad & TR_FR_TIME_DOWNLOADING)
     809      tor->secondsSeeding = i;
     810      fieldsLoaded |= TR_FR_TIME_SEEDING;
     811    }
     812
     813  if ((fieldsToLoad & TR_FR_TIME_DOWNLOADING)
    806814      && tr_variantDictFindInt (&top, TR_KEY_downloading_time_seconds, &i))
    807815    {
    808         tor->secondsDownloading = i;
    809         fieldsLoaded |= TR_FR_TIME_DOWNLOADING;
    810     }
    811 
    812     if ((fieldsToLoad & TR_FR_BANDWIDTH_PRIORITY)
     816      tor->secondsDownloading = i;
     817      fieldsLoaded |= TR_FR_TIME_DOWNLOADING;
     818    }
     819
     820  if ((fieldsToLoad & TR_FR_BANDWIDTH_PRIORITY)
    813821      && tr_variantDictFindInt (&top, TR_KEY_bandwidth_priority, &i)
    814822      && tr_isPriority (i))
    815823    {
    816         tr_torrentSetPriority (tor, i);
    817         fieldsLoaded |= TR_FR_BANDWIDTH_PRIORITY;
    818     }
    819 
    820     if (fieldsToLoad & TR_FR_PEERS)
    821         fieldsLoaded |= loadPeers (&top, tor);
    822 
    823     if (fieldsToLoad & TR_FR_FILE_PRIORITIES)
    824         fieldsLoaded |= loadFilePriorities (&top, tor);
    825 
    826     if (fieldsToLoad & TR_FR_PROGRESS)
    827         fieldsLoaded |= loadProgress (&top, tor);
    828 
    829     if (fieldsToLoad & TR_FR_DND)
    830         fieldsLoaded |= loadDND (&top, tor);
    831 
    832     if (fieldsToLoad & TR_FR_SPEEDLIMIT)
    833         fieldsLoaded |= loadSpeedLimits (&top, tor);
    834 
    835     if (fieldsToLoad & TR_FR_RATIOLIMIT)
    836         fieldsLoaded |= loadRatioLimits (&top, tor);
    837 
    838     if (fieldsToLoad & TR_FR_IDLELIMIT)
    839         fieldsLoaded |= loadIdleLimits (&top, tor);
    840 
    841     if (fieldsToLoad & TR_FR_FILENAMES)
    842         fieldsLoaded |= loadFilenames (&top, tor);
    843 
    844     if (fieldsToLoad & TR_FR_NAME)
    845         fieldsLoaded |= loadName (&top, tor);
    846 
    847     /* loading the resume file triggers of a lot of changes,
    848      * but none of them needs to trigger a re-saving of the
    849      * same resume information... */
    850     tor->isDirty = wasDirty;
    851 
    852     tr_variantFree (&top);
    853     tr_free (filename);
    854     return fieldsLoaded;
     824      tr_torrentSetPriority (tor, i);
     825      fieldsLoaded |= TR_FR_BANDWIDTH_PRIORITY;
     826    }
     827
     828  if (fieldsToLoad & TR_FR_PEERS)
     829    fieldsLoaded |= loadPeers (&top, tor);
     830
     831  if (fieldsToLoad & TR_FR_FILE_PRIORITIES)
     832    fieldsLoaded |= loadFilePriorities (&top, tor);
     833
     834  if (fieldsToLoad & TR_FR_PROGRESS)
     835    fieldsLoaded |= loadProgress (&top, tor);
     836
     837  if (fieldsToLoad & TR_FR_DND)
     838    fieldsLoaded |= loadDND (&top, tor);
     839
     840  if (fieldsToLoad & TR_FR_SPEEDLIMIT)
     841    fieldsLoaded |= loadSpeedLimits (&top, tor);
     842
     843  if (fieldsToLoad & TR_FR_RATIOLIMIT)
     844    fieldsLoaded |= loadRatioLimits (&top, tor);
     845
     846  if (fieldsToLoad & TR_FR_IDLELIMIT)
     847    fieldsLoaded |= loadIdleLimits (&top, tor);
     848
     849  if (fieldsToLoad & TR_FR_FILENAMES)
     850    fieldsLoaded |= loadFilenames (&top, tor);
     851
     852  if (fieldsToLoad & TR_FR_NAME)
     853    fieldsLoaded |= loadName (&top, tor);
     854
     855  /* loading the resume file triggers of a lot of changes,
     856   * but none of them needs to trigger a re-saving of the
     857   * same resume information... */
     858  tor->isDirty = wasDirty;
     859
     860  tr_variantFree (&top);
     861  tr_free (filename);
     862  return fieldsLoaded;
    855863}
    856864
     
    858866setFromCtor (tr_torrent * tor, uint64_t fields, const tr_ctor * ctor, int mode)
    859867{
    860     uint64_t ret = 0;
    861 
    862     if (fields & TR_FR_RUN)
    863     {
    864         bool isPaused;
    865         if (!tr_ctorGetPaused (ctor, mode, &isPaused))
    866         {
    867             tor->isRunning = !isPaused;
    868             ret |= TR_FR_RUN;
    869         }
    870     }
    871 
    872     if (fields & TR_FR_MAX_PEERS)
    873         if (!tr_ctorGetPeerLimit (ctor, mode, &tor->maxConnectedPeers))
    874             ret |= TR_FR_MAX_PEERS;
    875 
    876     if (fields & TR_FR_DOWNLOAD_DIR)
    877     {
    878         const char * path;
    879         if (!tr_ctorGetDownloadDir (ctor, mode, &path) && path && *path)
    880         {
    881             ret |= TR_FR_DOWNLOAD_DIR;
    882             tr_free (tor->downloadDir);
    883             tor->downloadDir = tr_strdup (path);
    884         }
    885     }
    886 
    887     return ret;
     868  uint64_t ret = 0;
     869
     870  if (fields & TR_FR_RUN)
     871    {
     872      bool isPaused;
     873
     874      if (!tr_ctorGetPaused (ctor, mode, &isPaused))
     875        {
     876          tor->isRunning = !isPaused;
     877          ret |= TR_FR_RUN;
     878        }
     879    }
     880
     881  if (fields & TR_FR_MAX_PEERS)
     882    if (!tr_ctorGetPeerLimit (ctor, mode, &tor->maxConnectedPeers))
     883      ret |= TR_FR_MAX_PEERS;
     884
     885  if (fields & TR_FR_DOWNLOAD_DIR)
     886    {
     887      const char * path;
     888
     889      if (!tr_ctorGetDownloadDir (ctor, mode, &path) && path && *path)
     890        {
     891          ret |= TR_FR_DOWNLOAD_DIR;
     892          tr_free (tor->downloadDir);
     893          tor->downloadDir = tr_strdup (path);
     894        }
     895    }
     896
     897  return ret;
    888898}
    889899
     
    891901useManditoryFields (tr_torrent * tor, uint64_t fields, const tr_ctor * ctor)
    892902{
    893     return setFromCtor (tor, fields, ctor, TR_FORCE);
     903  return setFromCtor (tor, fields, ctor, TR_FORCE);
    894904}
    895905
     
    897907useFallbackFields (tr_torrent * tor, uint64_t fields, const tr_ctor * ctor)
    898908{
    899     return setFromCtor (tor, fields, ctor, TR_FALLBACK);
     909  return setFromCtor (tor, fields, ctor, TR_FALLBACK);
    900910}
    901911
     
    905915                      const tr_ctor * ctor)
    906916{
    907     uint64_t ret = 0;
    908 
    909     assert (tr_isTorrent (tor));
    910 
    911     ret |= useManditoryFields (tor, fieldsToLoad, ctor);
    912     fieldsToLoad &= ~ret;
    913     ret |= loadFromFile (tor, fieldsToLoad);
    914     fieldsToLoad &= ~ret;
    915     ret |= useFallbackFields (tor, fieldsToLoad, ctor);
    916 
    917     return ret;
     917  uint64_t ret = 0;
     918
     919  assert (tr_isTorrent (tor));
     920
     921  ret |= useManditoryFields (tor, fieldsToLoad, ctor);
     922  fieldsToLoad &= ~ret;
     923  ret |= loadFromFile (tor, fieldsToLoad);
     924  fieldsToLoad &= ~ret;
     925  ret |= useFallbackFields (tor, fieldsToLoad, ctor);
     926
     927  return ret;
    918928}
    919929
     
    921931tr_torrentRemoveResume (const tr_torrent * tor)
    922932{
    923     char * filename = getResumeFilename (tor);
    924     tr_remove (filename);
    925     tr_free (filename);
    926 }
     933  char * filename = getResumeFilename (tor);
     934  tr_remove (filename);
     935  tr_free (filename);
     936}
Note: See TracChangeset for help on using the changeset viewer.