]> granicus.if.org Git - transmission/commitdiff
fix a race condition error in move-test
authorJordan Lee <jordan@transmissionbt.com>
Thu, 31 Jan 2013 05:10:08 +0000 (05:10 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Thu, 31 Jan 2013 05:10:08 +0000 (05:10 +0000)
libtransmission/libtransmission-test.c

index a5fcc697d3638f30cb40d49c7eb83e0b997c5fae..0ecd2ad0d28195dd2b3bdce1251dad25636648e1 100644 (file)
@@ -324,10 +324,9 @@ libtransmission_test_zero_torrent_init (void)
 
 #define verify_and_block_until_done(tor) \
   do { \
+    do { tr_wait_msec (10); } while (tor->verifyState != TR_VERIFY_NONE); \
     tr_torrentVerify (tor); \
-    do { \
-      tr_wait_msec (10); \
-    } while (tor->verifyState != TR_VERIFY_NONE); \
+    do { tr_wait_msec (10); } while (tor->verifyState != TR_VERIFY_NONE); \
   } while (0)
 
 
@@ -346,12 +345,15 @@ libtransmission_test_zero_torrent_populate (tr_torrent * tor, bool complete)
       const tr_file * file = &tor->info.files[i];
       struct stat sb;
 
-      path = tr_buildPath (tor->currentDir, file->name, NULL);
+      if (!complete && (i==0))
+        path = tr_strdup_printf ("%s%c%s.part", tor->currentDir, TR_PATH_DELIMITER, file->name);
+      else
+        path = tr_strdup_printf ("%s%c%s", tor->currentDir, TR_PATH_DELIMITER, file->name);
       dirname = tr_dirname (path);
       tr_mkdirp (dirname, 0700);
       fp = fopen (path, "wb+");
       for (j=0; j<file->length; ++j)
-        fputc ('\0', fp);
+        fputc (((!complete) && (i==0) && (j<tor->info.pieceSize)) ? '\1' : '\0', fp);
       fclose (fp);
 
       tr_free (dirname);
@@ -365,25 +367,10 @@ libtransmission_test_zero_torrent_populate (tr_torrent * tor, bool complete)
     }
 
   sync ();
+  verify_and_block_until_done (tor);
 
-  if (!complete)
-    {
-      FILE * fp;
-      char * oldpath = tr_torrentFindFile (tor, 0);
-      char * newpath = tr_strdup_printf ("%s.part", oldpath);
-
-      rename (oldpath, newpath);
-
-      /* invalidate one piece */
-      fp = fopen (newpath, "rb+");
-      fputc ('\1', fp);
-      fclose (fp);
-
-      tr_free (newpath);
-      tr_free (oldpath);
-
-      sync ();
-      verify_and_block_until_done (tor);
-      assert (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
-    }
+  if (complete)
+    assert (tr_torrentStat(tor)->leftUntilDone == 0);
+  else
+    assert (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
 }