Changeset 13829


Ignore:
Timestamp:
Jan 22, 2013, 12:25:42 AM (8 years ago)
Author:
jordan
Message:

(libT) #1220 'change top folder names' -- when the root file/folder is changed, update tr_info.name too.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/metainfo.c

    r13722 r13829  
    3737{
    3838  size_t i;
    39   const size_t name_len = strlen (inf->name);
    40   char * ret = tr_strdup_printf ("%s.%16.16s", inf->name, inf->hashString);
     39  const char * name = inf->originalName;
     40  const size_t name_len = strlen (name);
     41  char * ret = tr_strdup_printf ("%s.%16.16s", name, inf->hashString);
    4142
    4243  for (i=0; i<name_len; ++i)
     
    406407            {
    407408              tr_free (inf->name);
     409              tr_free (inf->originalName);
    408410              inf->name = tr_strndup (str, len);
     411              inf->originalName = tr_strndup (str, len);
    409412            }
    410413
    411414          if (!inf->name)
    412             inf->name = tr_strdup (inf->hashString);
     415              inf->name = tr_strdup (inf->hashString);
     416          if (!inf->originalName)
     417              inf->originalName = tr_strdup (inf->hashString);
    413418        }
    414419      else /* not a magnet link and has no info dict... */
     
    440445        return "name";
    441446      tr_free (inf->name);
     447      tr_free (inf->originalName);
    442448      inf->name = tr_utf8clean (str, len);
     449      inf->originalName = tr_strdup (inf->name);
    443450    }
    444451
     
    561568  tr_free (inf->creator);
    562569  tr_free (inf->torrent);
     570  tr_free (inf->originalName);
    563571  tr_free (inf->name);
    564572
  • trunk/libtransmission/rename-test.c

    r13812 r13829  
    155155test_single_filename_torrent (void)
    156156{
     157  uint64_t loaded;
    157158  tr_torrent * tor;
    158159  char * tmpstr;
     
    208209  tmpstr = tr_buildPath (tor->downloadDir, "hello-world.txt", NULL);
    209210  check (tr_fileExists (tmpstr, NULL));
     211  check_streq ("hello-world.txt", tr_torrentName(tor));
    210212  check_int_eq (0, torrentRenameAndWait (tor, "hello-world.txt", "foobar"));
    211213  check (!tr_fileExists (tmpstr, NULL));
    212214  check (tor->info.files[0].is_renamed);
    213215  check_streq ("foobar", tor->info.files[0].name);
     216  check_streq ("foobar", tr_torrentName(tor));
     217  check (strstr (tor->info.torrent, "foobar") == NULL);
     218  check (testFileExistsAndConsistsOfThisString (tor, 0, "hello, world!\n"));
    214219  tr_free (tmpstr);
    215   check (testFileExistsAndConsistsOfThisString (tor, 0, "hello, world!\n"));
     220
     221  /* (while it's renamed: confirm that the .resume file remembers the changes) */
     222  tr_torrentSaveResume (tor);
     223  sync ();
     224  loaded = tr_torrentLoadResume (tor, ~0, ctor);
     225  check_streq ("foobar", tr_torrentName(tor));
     226  check ((loaded & TR_FR_NAME) != 0);
    216227
    217228  /***
     
    225236  check (tor->info.files[0].is_renamed);
    226237  check_streq ("hello-world.txt", tor->info.files[0].name);
     238  check_streq ("hello-world.txt", tr_torrentName(tor));
    227239  tr_free (tmpstr);
    228240  check (testFileExistsAndConsistsOfThisString (tor, 0, "hello, world!\n"));
     
    266278test_multifile_torrent (void)
    267279{
    268   //tr_file_stat * file_stats;
    269   //tr_file_index_t n;
    270280  tr_file_index_t i;
    271281  uint64_t loaded;
    272282  tr_torrent * tor;
    273   //tr_file_index_t i;
    274283  tr_ctor * ctor;
    275284  char * str;
  • trunk/libtransmission/resume.c

    r13807 r13829  
    335335
    336336    return ret;
     337}
     338
     339/***
     340****
     341***/
     342
     343static void
     344saveName (tr_variant * dict, const tr_torrent * tor)
     345{
     346  tr_variantDictAddStr (dict, TR_KEY_name, tr_torrentName(tor));
     347}
     348
     349static uint64_t
     350loadName (tr_variant * dict, tr_torrent * tor)
     351{
     352  uint64_t ret = 0;
     353  const char * name;
     354
     355  if (tr_variantDictFindStr (dict, TR_KEY_name, &name, NULL))
     356    {
     357      ret = TR_FR_NAME;
     358
     359      if (tr_strcmp0 (tr_torrentName(tor), name))
     360        {
     361          tr_free (tor->info.name);
     362          tor->info.name = tr_strdup (name);
     363        }
     364    }
     365
     366  return ret;
    337367}
    338368
     
    651681    saveIdleLimits (&top, tor);
    652682    saveFilenames (&top, tor);
     683    saveName (&top, tor);
    653684
    654685    filename = getResumeFilename (tor);
     
    806837        fieldsLoaded |= loadFilenames (&top, tor);
    807838
     839    if (fieldsToLoad & TR_FR_NAME)
     840        fieldsLoaded |= loadName (&top, tor);
     841
    808842    /* loading the resume file triggers of a lot of changes,
    809843     * but none of them needs to trigger a re-saving of the
  • trunk/libtransmission/resume.h

    r13807 r13829  
    4040    TR_FR_TIME_SEEDING        = (1 << 18),
    4141    TR_FR_TIME_DOWNLOADING    = (1 << 19),
    42     TR_FR_FILENAMES           = (1 << 20)
     42    TR_FR_FILENAMES           = (1 << 20),
     43    TR_FR_NAME                = (1 << 21),
    4344};
    4445
     
    5455void     tr_torrentRemoveResume (const tr_torrent * tor);
    5556
     57int      tr_torrentRenameResume (const tr_torrent * tor,
     58                                 const char       * newname);
     59
    5660#endif
  • trunk/libtransmission/torrent-magnet.c

    r13781 r13829  
    375375}
    376376
     377/* FIXME: this should be renamed tr_metainfoGetMagnetLink() and moved to metainfo.c for consistency */
    377378char *
    378379tr_torrentInfoGetMagnetLink (const tr_info * inf)
  • trunk/libtransmission/torrent.c

    r13810 r13829  
    34423442
    34433443static void
     3444torrentRenameDone (tr_torrent * tor UNUSED,
     3445                   const char * oldpath UNUSED,
     3446                   const char * newname UNUSED,
     3447                   int          error,
     3448                   void       * user_data)
     3449{
     3450  *(int*)user_data = error;
     3451}
     3452
     3453static void
    34443454torrentRenamePath (void * vdata)
    34453455{
     
    34783488          if (!error)
    34793489            {
     3490              /* update tr_info.files */
    34803491              for (i=0; i<n; ++i)
    34813492                renameTorrentFileString(tor, oldpath, newname, file_indices[i]);
     3493
     3494              /* update tr_info.name if user changed the toplevel */
     3495              if ((n == tor->info.fileCount) && (strchr(oldpath,'/')==NULL))
     3496                tr_torrentRename (tor, newname, torrentRenameDone, &error);
     3497
    34823498              tr_torrentSetDirty (tor);
    34833499            }
     
    35223538  tr_runInEventThread (tor->session, torrentRenamePath, data);
    35233539}
     3540
     3541/**
     3542***
     3543**/
     3544
     3545static void
     3546torrentRename (void * vdata)
     3547{
     3548  int error = 0;
     3549  struct rename_data * data = vdata;
     3550  tr_torrent * const tor = data->tor;
     3551
     3552  tr_free (tor->info.name);
     3553  tor->info.name = data->newname;
     3554  tr_torrentSetDirty (tor);
     3555  tor->anyDate = tr_time ();
     3556
     3557  /* callback */
     3558  if (data->callback != NULL)
     3559    (*data->callback)(tor, data->oldpath, data->newname, error, data->callback_user_data);
     3560
     3561  /* cleanup */
     3562  tr_free (data);
     3563}
     3564
     3565void
     3566tr_torrentRename (tr_torrent                   * tor,
     3567                  const char                   * newname,
     3568                  tr_torrent_rename_done_func    callback,
     3569                  void                         * callback_user_data)
     3570{
     3571  struct rename_data * data;
     3572
     3573  data = tr_new0 (struct rename_data, 1);
     3574  data->tor = tor;
     3575  data->newname = tr_strdup (newname);
     3576  data->callback = callback;
     3577  data->callback_user_data = callback_user_data;
     3578
     3579  tr_runInEventThread (tor->session, torrentRename, data);
     3580}
  • trunk/libtransmission/transmission.h

    r13809 r13829  
    11101110 * @callback_data: the pointer to pass in the callback's user_data arg
    11111111 *
     1112 * As a special case, renaming the root file in a torrent will call
     1113 * tr_torrentRename (tor, newname).
     1114 *
    11121115 * EXAMPLES
    11131116 *
     
    11171120 *
    11181121 *   1. tr_torrentRenamePath (tor, "frobnitz-linux", "foo") will rename
    1119  *      the "frotbnitz-linux" folder as "foo" and update files[*].name.
     1122 *      the "frotbnitz-linux" folder as "foo", update info.files[*].name,
     1123 *      and also call tr_torrentRename(tor,"foo").
    11201124 *
    11211125 *   2. tr_torrentRenamePath (tor, "frobnitz-linux/checksum", "foo") will
     
    11261130 *
    11271131 *   Changing tr_info's contents requires a session lock, so this function
    1128  *   returns asynchronously to avoid blocking. If you don't care about error
    1129  *   checking, you can pass NULL as the callback and callback_user_data arg.
     1132 *   returns asynchronously to avoid blocking. If you don't want to be notified
     1133 *   when the function has finished, you can pass NULL as the callback arg.
    11301134 *
    11311135 *   On success, the callback's error argument will be 0.
     
    11441148                           void                        * callback_user_data);
    11451149
     1150
     1151/**
     1152 * @brief Changes the torrent's name.
     1153 * @see-also tr_torrentRenamePath
     1154 *
     1155 * This function changes tr_info.name.
     1156 *
     1157 * Changing tr_info's contents requires a session lock, so this function
     1158 * returns asynchronously to avoid blocking. If you don't want to be notified
     1159 * when the function has finished, you can pass NULL as the callback arg.
     1160 */
     1161void tr_torrentRename (tr_torrent                   * tor,
     1162                       const char                   * newname,
     1163                       tr_torrent_rename_done_func    callback,
     1164                       void                         * callback_user_data);
    11461165
    11471166enum
     
    17581777    uint64_t           totalSize;
    17591778
    1760     /* the torrent's name */
     1779    /* The original name that came in this torrent's metainfo.
     1780     * This may differ from "name" if tr_torrentRename() is called.
     1781     * CLIENT CODE: NOT USE THIS FIELD. */
     1782    char             * originalName;
     1783
     1784    /* The torrent's name. */
    17611785    char             * name;
    17621786
  • trunk/qt/session.cc

    r13810 r13829  
    828828                      {
    829829                        // let's get the updated file list
    830                         char * req = tr_strdup_printf ("{ \"arguments\": { \"fields\": [ \"files\", \"id\" ], \"ids\": %d }, \"method\": \"torrent-get\", \"tag\": %d }",
     830                        char * req = tr_strdup_printf ("{ \"arguments\": { \"fields\": [ \"files\", \"id\", \"name\" ], \"ids\": %d }, \"method\": \"torrent-get\", \"tag\": %d }",
    831831                                                       int(id),
    832832                                                       int(TAG_SOME_TORRENTS));
Note: See TracChangeset for help on using the changeset viewer.