@@ -30,18 +30,20 @@ POSSIBILITY OF SUCH DAMAGE.
30
30
31
31
*/
32
32
33
+ #include < sys/stat.h> // for chmod
34
+
33
35
#include " libtorrent/session.hpp"
34
36
#include " test.hpp"
35
37
#include " setup_transfer.hpp"
36
38
#include " libtorrent/create_torrent.hpp"
37
- #include < sys/stat.h > // for chmod
39
+ #include " libtorrent/alert_types.hpp "
38
40
#include " libtorrent/torrent_info.hpp"
39
41
#include " libtorrent/torrent_status.hpp"
40
42
#include " libtorrent/hex.hpp" // to_hex
41
43
#include " libtorrent/aux_/path.hpp"
42
44
43
45
static const int file_sizes[] =
44
- { 5 , 16 - 5 , 16000 , 17 , 10 , 8000 , 8000 , 1 ,1 ,1 ,1 ,1 ,100 ,1 ,1 ,1 ,1 ,100 ,1 ,1 ,1 ,1 ,1 ,1
46
+ { 0 , 5 , 16 - 5 , 16000 , 17 , 10 , 8000 , 8000 , 1 ,1 ,1 ,1 ,1 ,100 ,1 ,1 ,1 ,1 ,100 ,1 ,1 ,1 ,1 ,1 ,1
45
47
,1 ,1 ,1 ,1 ,1 ,1 ,13 ,65000 ,34 ,75 ,2 ,30 ,400 ,500 ,23000 ,900 ,43000 ,400 ,4300 ,6 , 4 };
46
48
const int num_files = sizeof (file_sizes)/sizeof (file_sizes[0 ]);
47
49
56
58
corrupt_files = 2 ,
57
59
58
60
incomplete_files = 4 ,
61
+
62
+ // make the files not be there when starting up, move the files in place and
63
+ // force-recheck. Make sure the stat cache is cleared and let us pick up the
64
+ // new files
65
+ force_recheck = 8 ,
59
66
};
60
67
61
68
void test_checking (int flags = read_only_files)
62
69
{
63
70
using namespace lt ;
64
71
65
- std::printf (" \n ==== TEST CHECKING %s%s%s=====\n\n "
72
+ std::printf (" \n ==== TEST CHECKING %s%s%s%s =====\n\n "
66
73
, (flags & read_only_files) ? " read-only-files " :" "
67
74
, (flags & corrupt_files) ? " corrupt " :" "
68
- , (flags & incomplete_files) ? " incomplete " :" " );
69
-
70
- // make the files writable again
71
- for (int i = 0 ; i < num_files; ++i)
72
- {
73
- char name[1024 ];
74
- std::snprintf (name, sizeof (name), " test%d" , i);
75
- char dirname[200 ];
76
- std::snprintf (dirname, sizeof (dirname), " test_dir%d" , i / 5 );
77
- std::string path = combine_path (combine_path (" tmp1_checking" , " test_torrent_dir" ), dirname);
78
- path = combine_path (path, name);
79
- #ifdef TORRENT_WINDOWS
80
- SetFileAttributesA (path.c_str (), FILE_ATTRIBUTE_NORMAL);
81
- #else
82
- chmod (path.c_str (), S_IRUSR | S_IWUSR);
83
- #endif
84
- }
75
+ , (flags & incomplete_files) ? " incomplete " :" "
76
+ , (flags & force_recheck) ? " force_recheck " :" " );
85
77
86
- // in case the previous run was terminated
87
78
error_code ec;
88
- remove_all (" tmp1_checking" , ec);
89
- if (ec) std::printf (" ERROR: removing tmp1_checking: (%d) %s\n "
90
- , ec.value (), ec.message ().c_str ());
91
-
92
- create_directory (" tmp1_checking" , ec);
93
- if (ec) std::printf (" ERROR: creating directory tmp1_checking: (%d) %s\n "
94
- , ec.value (), ec.message ().c_str ());
95
- create_directory (combine_path (" tmp1_checking" , " test_torrent_dir" ), ec);
96
- if (ec) std::printf (" ERROR: creating directory test_torrent_dir: (%d) %s\n "
79
+ create_directory (" test_torrent_dir" , ec);
80
+ if (ec) fprintf (stdout, " ERROR: creating directory test_torrent_dir: (%d) %s\n "
97
81
, ec.value (), ec.message ().c_str ());
98
82
99
83
file_storage fs;
100
84
std::srand (10 );
101
85
int piece_size = 0x4000 ;
102
86
103
- create_random_files (combine_path (" tmp1_checking" , " test_torrent_dir" )
104
- , file_sizes, num_files);
87
+ create_random_files (" test_torrent_dir" , file_sizes, num_files, &fs);
105
88
106
- add_files (fs, combine_path (" tmp1_checking" , " test_torrent_dir" ));
107
89
lt::create_torrent t (fs, piece_size, 0x4000
108
90
, lt::create_torrent::optimize_alignment);
109
91
110
92
// calculate the hash for all pieces
111
- set_piece_hashes (t, " tmp1_checking " , ec);
93
+ set_piece_hashes (t, " . " , ec);
112
94
if (ec) std::printf (" ERROR: set_piece_hashes: (%d) %s\n "
113
95
, ec.value (), ec.message ().c_str ());
114
96
115
97
std::vector<char > buf;
116
98
bencode (std::back_inserter (buf), t.generate ());
117
99
auto ti = std::make_shared<torrent_info>(buf, ec, from_span);
118
100
119
- std::printf (" generated torrent: %s tmp1_checking/ test_torrent_dir\n "
101
+ std::printf (" generated torrent: %s test_torrent_dir\n "
120
102
, aux::to_hex (ti->info_hash ()).c_str ());
121
103
122
104
// truncate every file in half
@@ -128,7 +110,7 @@ void test_checking(int flags = read_only_files)
128
110
std::snprintf (name, sizeof (name), " test%d" , i);
129
111
char dirname[200 ];
130
112
std::snprintf (dirname, sizeof (dirname), " test_dir%d" , i / 5 );
131
- std::string path = combine_path (combine_path ( " tmp1_checking " , " test_torrent_dir" ) , dirname);
113
+ std::string path = combine_path (" test_torrent_dir" , dirname);
132
114
path = combine_path (path, name);
133
115
134
116
error_code ec;
@@ -148,9 +130,9 @@ void test_checking(int flags = read_only_files)
148
130
// increase the size of some files. When they're read only that forces
149
131
// the checker to open them in write-mode to truncate them
150
132
static const int file_sizes2[] =
151
- { 5 , 16 - 5 , 16001 , 30 , 10 , 8000 , 8000 , 1 ,1 ,1 ,1 ,1 ,100 ,1 ,1 ,1 ,1 ,100 ,1 ,1 ,1 ,1 ,1 ,1
133
+ { 0 , 5 , 16 - 5 , 16001 , 30 , 10 , 8000 , 8000 , 1 ,1 ,1 ,1 ,1 ,100 ,1 ,1 ,1 ,1 ,100 ,1 ,1 ,1 ,1 ,1 ,1
152
134
,1 ,1 ,1 ,1 ,1 ,1 ,13 ,65000 ,34 ,75 ,2 ,30 ,400 ,500 ,23000 ,900 ,43000 ,400 ,4300 ,6 , 4 };
153
- create_random_files (combine_path ( " tmp1_checking " , " test_torrent_dir" ) , file_sizes2, num_files);
135
+ create_random_files (" test_torrent_dir" , file_sizes2, num_files);
154
136
}
155
137
156
138
// make the files read only
@@ -164,7 +146,7 @@ void test_checking(int flags = read_only_files)
164
146
char dirname[200 ];
165
147
std::snprintf (dirname, sizeof (dirname), " test_dir%d" , i / 5 );
166
148
167
- std::string path = combine_path (combine_path ( " tmp1_checking " , " test_torrent_dir" ) , dirname);
149
+ std::string path = combine_path (" test_torrent_dir" , dirname);
168
150
path = combine_path (path, name);
169
151
std::printf (" %s\n " , path.c_str ());
170
152
@@ -176,6 +158,16 @@ void test_checking(int flags = read_only_files)
176
158
}
177
159
}
178
160
161
+ if (flags & force_recheck)
162
+ {
163
+ remove_all (" test_torrent_dir_tmp" , ec);
164
+ if (ec) std::printf (" ERROR: removing \" test_torrent_dir_tmp\" : (%d) %s\n "
165
+ , ec.value (), ec.message ().c_str ());
166
+ rename (" test_torrent_dir" , " test_torrent_dir_tmp" , ec);
167
+ if (ec) std::printf (" ERROR: renaming dir \" test_torrent_dir\" : (%d) %s\n "
168
+ , ec.value (), ec.message ().c_str ());
169
+ }
170
+
179
171
auto const mask = alert::all_categories
180
172
& ~(alert::progress_notification
181
173
| alert::performance_warning
@@ -192,11 +184,30 @@ void test_checking(int flags = read_only_files)
192
184
lt::session ses1 (pack);
193
185
194
186
add_torrent_params p;
195
- p.save_path = " tmp1_checking " ;
187
+ p.save_path = " . " ;
196
188
p.ti = ti;
197
189
torrent_handle tor1 = ses1.add_torrent (p, ec);
198
190
TEST_CHECK (!ec);
199
191
192
+ if (flags & force_recheck)
193
+ {
194
+ // first make sure the session tries to check for the file and can't find
195
+ // them
196
+ libtorrent::alert const * a = wait_for_alert (
197
+ ses1, torrent_checked_alert::alert_type, " checking" );
198
+ TEST_CHECK (a);
199
+
200
+ // now, move back the files and force-recheck. make sure we pick up the
201
+ // files this time
202
+ remove_all (" test_torrent_dir" , ec);
203
+ if (ec) fprintf (stdout, " ERROR: removing \" test_torrent_dir\" : (%d) %s\n "
204
+ , ec.value (), ec.message ().c_str ());
205
+ rename (" test_torrent_dir_tmp" , " test_torrent_dir" , ec);
206
+ if (ec) fprintf (stdout, " ERROR: renaming dir \" test_torrent_dir_tmp\" : (%d) %s\n "
207
+ , ec.value (), ec.message ().c_str ());
208
+ tor1.force_recheck ();
209
+ }
210
+
200
211
torrent_status st;
201
212
for (int i = 0 ; i < 20 ; ++i)
202
213
{
@@ -217,6 +228,7 @@ void test_checking(int flags = read_only_files)
217
228
if (st.errc ) break ;
218
229
std::this_thread::sleep_for (lt::milliseconds (500 ));
219
230
}
231
+
220
232
if (flags & incomplete_files)
221
233
{
222
234
TEST_CHECK (!st.is_seeding );
@@ -273,7 +285,7 @@ void test_checking(int flags = read_only_files)
273
285
char dirname[200 ];
274
286
std::snprintf (dirname, sizeof (dirname), " test_dir%d" , i / 5 );
275
287
276
- std::string path = combine_path (combine_path ( " tmp1_checking " , " test_torrent_dir" ) , dirname);
288
+ std::string path = combine_path (" test_torrent_dir" , dirname);
277
289
path = combine_path (path, name);
278
290
#ifdef TORRENT_WINDOWS
279
291
SetFileAttributesA (path.c_str (), FILE_ATTRIBUTE_NORMAL);
@@ -283,8 +295,8 @@ void test_checking(int flags = read_only_files)
283
295
}
284
296
}
285
297
286
- remove_all (" tmp1_checking " , ec);
287
- if (ec) std::printf (" ERROR: removing tmp1_checking : (%d) %s\n "
298
+ remove_all (" test_torrent_dir " , ec);
299
+ if (ec) std::printf (" ERROR: removing test_torrent_dir : (%d) %s\n "
288
300
, ec.value (), ec.message ().c_str ());
289
301
}
290
302
@@ -313,3 +325,8 @@ TORRENT_TEST(corrupt)
313
325
test_checking (corrupt_files);
314
326
}
315
327
328
+ TORRENT_TEST (force_recheck)
329
+ {
330
+ test_checking (force_recheck);
331
+ }
332
+
0 commit comments