void add_files_impl(file_storage& fs, std::string const& p , std::string const& l, boost::function<bool(std::string)> pred, boost::uint32_t flags) { std::string f = combine_path(p, l); if (!pred(f)) return; error_code ec; file_status s; stat_file(f, &s, ec, (flags & create_torrent::symlinks) ? dont_follow_links : 0); if (ec) return; // recurse into directories bool recurse = (s.mode & file_status::directory) != 0; // if the file is not a link or we're following links, and it's a directory // only then should we recurse #ifndef TORRENT_WINDOWS if ((s.mode & file_status::link) && (flags & create_torrent::symlinks)) recurse = false; #endif if (recurse) { fs.add_path(parent_path(combine_path(l, "x")), s.mode & 0777); for (directory i(f, ec); !i.done(); i.next(ec)) { std::string leaf = i.file(); if (ignore_subdir(leaf)) continue; add_files_impl(fs, p, combine_path(l, leaf), pred, flags); } } else if (s.mode & (file_status::regular_file | file_status::link)) { // #error use the fields from s int file_flags = get_file_attributes(f, (flags & create_torrent::symlinks) ? dont_follow_links : 0); // mask all bits to check if the file is a symlink if ((file_flags & file_storage::attribute_symlink) && (flags & create_torrent::symlinks)) { std::string sym_path = get_symlink_path(f); fs.add_file(l, 0, file_flags, s.mtime, sym_path, s.mode & 0777); } else { fs.add_file(l, s.file_size, file_flags, s.mtime, "", s.mode & 0777); } } if (fs.num_files() == 0) fs.set_name(l); }
void setup_test_storage(file_storage& st) { st.add_file(combine_path("test", "a"), 10000); st.add_file(combine_path("test", "b"), 20000); st.add_file(combine_path("test", combine_path("c", "a")), 30000); st.add_file(combine_path("test", combine_path("c", "b")), 40000); st.set_piece_length(0x4000); st.set_num_pieces((int(st.total_size()) + st.piece_length() - 1) / 0x4000); TEST_EQUAL(st.file_name(0), "a"); TEST_EQUAL(st.file_name(1), "b"); TEST_EQUAL(st.file_name(2), "a"); TEST_EQUAL(st.file_name(3), "b"); TEST_EQUAL(st.name(), "test"); TEST_EQUAL(st.file_path(0), combine_path("test", "a")); TEST_EQUAL(st.file_path(1), combine_path("test", "b")); TEST_EQUAL(st.file_path(2), combine_path("test", combine_path("c", "a"))); TEST_EQUAL(st.file_path(3), combine_path("test", combine_path("c", "b"))); TEST_EQUAL(st.file_size(0), 10000); TEST_EQUAL(st.file_size(1), 20000); TEST_EQUAL(st.file_size(2), 30000); TEST_EQUAL(st.file_size(3), 40000); TEST_EQUAL(st.file_offset(0), 0); TEST_EQUAL(st.file_offset(1), 10000); TEST_EQUAL(st.file_offset(2), 30000); TEST_EQUAL(st.file_offset(3), 60000); TEST_EQUAL(st.total_size(), 100000); TEST_EQUAL(st.piece_length(), 0x4000); printf("%d\n", st.num_pieces()); TEST_EQUAL(st.num_pieces(), (100000 + 0x3fff) / 0x4000); }