ftnword f77name(qdfind)(ftnword *iun) { int ind; ind = file_index(*iun); ind = (ind != ERR_NO_FILE) ? ind : 9999; return((ftnword) ind); }
void posix_storage::initialize(aux::session_settings const&, storage_error& ec) { m_stat_cache.reserve(files().num_files()); // first, create zero-sized files std::string last_path; file_storage const& fs = files(); for (file_index_t file_index(0); file_index < fs.end_file(); ++file_index) { // ignore files that have priority 0 if (m_file_priority.end_index() > file_index && m_file_priority[file_index] == dont_download) { continue; } // ignore pad files if (files().pad_file_at(file_index)) continue; error_code err; std::int64_t size = m_stat_cache.get_filesize(file_index, files() , m_save_path, err); if (err && err != boost::system::errc::no_such_file_or_directory) { ec.file(file_index); ec.operation = operation_t::file_stat; ec.ec = err; break; } // if the file already exists, but is larger than what // it's supposed to be, truncate it // if the file is empty, just create it either way. std::int64_t const file_size = files().file_size(file_index); if ((!err && size > file_size) || files().file_size(file_index) == 0) { FILE* f = open_file(file_index, aux::open_mode::write, 0, ec); if (ec) return; #ifndef TORRENT_WINDOWS if (file_size > 0) { int const ret = ftruncate(fileno(f), file_size); if (ret < 0) { fclose(f); ec.file(file_index); ec.operation = operation_t::file_fallocate; return; } } #endif fclose(f); } ec.ec.clear(); } }
void file_storage::optimize(int pad_file_limit, int alignment) { // it doesn't make any sense to pad files that // are smaller than one block if (pad_file_limit >= 0 && pad_file_limit < 0x4000) pad_file_limit = 0x4000; // also, it doesn't make any sense to pad files // that are smaller than the alignment, since they // won't get aligned anyway; they are used as padding if (pad_file_limit >= 0 && pad_file_limit < alignment) pad_file_limit = alignment; size_type off = 0; int padding_file = 0; for (std::vector<internal_file_entry>::iterator i = m_files.begin(); i != m_files.end(); ++i) { if ((off & (alignment-1)) == 0) { // this file position is aligned, pick the largest // available file to put here std::vector<internal_file_entry>::iterator best_match = std::max_element(i, m_files.end() , &compare_file_entry_size); if (best_match != i) { int index = file_index(*best_match); int cur_index = file_index(*i); reorder_file(index, cur_index); i = m_files.begin() + cur_index; } } else if (pad_file_limit >= 0 && i->size > pad_file_limit && i->pad_file == false) { // if we have pad files enabled, and this file is // not piece-aligned and the file size exceeds the // limit, and it's not a padding file itself. // so add a padding file in front of it int pad_size = alignment - (off & (alignment-1)); // find the largest file that fits in pad_size std::vector<internal_file_entry>::iterator best_match = m_files.end(); for (std::vector<internal_file_entry>::iterator j = i+1; j < m_files.end(); ++j) { if (j->size > pad_size) continue; if (best_match == m_files.end() || j->size > best_match->size) best_match = j; } if (best_match != m_files.end()) { // we found one // We cannot have found i, because i->size > pad_file_limit // which is forced to be no less than alignment. We only // look for files <= pad_size, which never is greater than // alignment TORRENT_ASSERT(best_match != i); int index = file_index(*best_match); int cur_index = file_index(*i); reorder_file(index, cur_index); i = m_files.begin() + cur_index; i->offset = off; off += i->size; continue; } // we could not find a file that fits in pad_size // add a padding file // note that i will be set to point to the // new pad file. Once we're done adding it, we need // to increment i to point to the current file again // first add the pad file to the end of the file list // then swap it in place. This minimizes the amount // of copying of internal_file_entry, which is somewhat // expensive (until we have move semantics) int cur_index = file_index(*i); int index = m_files.size(); m_files.push_back(internal_file_entry()); internal_file_entry& e = m_files.back(); // i may have been invalidated, refresh it i = m_files.begin() + cur_index; e.size = pad_size; e.offset = off; char name[30]; snprintf(name, sizeof(name), ".____padding_file/%d", padding_file); std::string path = combine_path(m_name, name); e.set_name(path.c_str()); e.pad_file = true; off += pad_size; ++padding_file; if (!m_mtime.empty()) m_mtime.resize(index + 1, 0); if (!m_file_hashes.empty()) m_file_hashes.resize(index + 1, NULL); if (!m_file_base.empty()) m_file_base.resize(index + 1, 0); reorder_file(index, cur_index); TORRENT_ASSERT((off & (alignment-1)) == 0); continue; } i->offset = off; off += i->size; } m_total_size = off; }
void file_storage::optimize(int pad_file_limit) { // the main purpuse of padding is to optimize disk // I/O. This is a conservative memory page size assumption int alignment = 8*1024; // it doesn't make any sense to pad files that // are smaller than one piece if (pad_file_limit >= 0 && pad_file_limit < alignment) pad_file_limit = alignment; size_type off = 0; int padding_file = 0; for (std::vector<internal_file_entry>::iterator i = m_files.begin(); i != m_files.end(); ++i) { if ((off & (alignment-1)) == 0) { // this file position is aligned, pick the largest // available file to put here std::vector<internal_file_entry>::iterator best_match = std::max_element(i, m_files.end() , &compare_file_entry_size); if (best_match != i) { int index = file_index(*best_match); int cur_index = file_index(*i); reorder_file(index, cur_index); i = m_files.begin() + cur_index; } } else if (pad_file_limit >= 0 && (off & (alignment-1)) != 0 && i->size > pad_file_limit && i->pad_file == false) { // if we have pad files enabled, and this file is // not piece-aligned and the file size exceeds the // limit, and it's not a padding file itself. // so add a padding file in front of it int pad_size = alignment - (off & (alignment-1)); // find the largest file that fits in pad_size std::vector<internal_file_entry>::iterator best_match = m_files.end(); for (std::vector<internal_file_entry>::iterator j = i+1; j < m_files.end(); ++j) { if (j->size > pad_size) continue; if (best_match == m_files.end() || j->size > best_match->size) best_match = j; } if (best_match != m_files.end()) { // we found one // We cannot have found i, because i->size > pad_file_limit // which is forced to be no less than alignment. We only // look for files <= pad_size, which never is greater than // alignment TORRENT_ASSERT(best_match != i); int index = file_index(*best_match); int cur_index = file_index(*i); reorder_file(index, cur_index); i = m_files.begin() + cur_index; i->offset = off; off += i->size; continue; } // we could not find a file that fits in pad_size // add a padding file // note that i will be set to point to the // new pad file. Once we're done adding it, we need // to increment i to point to the current file again internal_file_entry e; i = m_files.insert(i, e); i->size = pad_size; i->offset = off; char name[30]; snprintf(name, sizeof(name), ".____padding_file/%d", padding_file); std::string path = combine_path(m_name, name); i->set_name(path.c_str()); i->pad_file = true; off += pad_size; ++padding_file; // skip the pad file we just added and point // at the current file again ++i; } i->offset = off; off += i->size; } m_total_size = off; }
void Buyer::Buyticket() { Time time; int tag,selln; string line,type,dep_station,arr_station,temp,schedule; if(!time.Time_check()) { cout<<"Today's train schedules are not updated!"<<endl; return; } chdir(time.Show_file_curdir()); ifstream file_index("index.txt"); cout<<"which station start:";cin>>dep_station; cout<<"which station arrive:";cin>>arr_station; cout<<endl; while(getline(file_index,line)) { if(line.find("[dep_station] " + dep_station) != -1) { getline(file_index,line); if(line.find("[arr_station] " + arr_station) != -1) { getline(file_index,line); schedule = line; line = line + ".txt"; ifstream file_train(line); while(getline(file_train,line)) cout<<line<<endl; file_train.close(); cout<<endl; cout<<"Noseat => 1"<<endl; cout<<"Hardseat => 2"<<endl; cout<<"Hardbed => 3"<<endl; cout<<"Softbed => 4"<<endl; cout<<endl; cout<<"Buy what kind of ticket:"<<endl; cout<<"Please enter the number behind the type:";cin>>tag; chdir("..\\.."); ifstream file_check(File_user()); while(getline(file_check,line)) { if(line.find("[dep_station] " + dep_station) != -1) { getline(file_check,line); if(line.find("[arr_station] " + arr_station) != -1) { cout<<"repeat!"<<endl; return; } } } switch(tag) { case 1 : type = "noseat"; break; case 2 : type = "hardseat"; break; case 3 : type = "hardbed"; break; case 4 : type = "softbed"; break; default: cout<<"No such select !"<<endl; return; } ofstream file_user(File_user(), ios::out | ios::app); file_user<<endl; file_user<<"[schedule] "<<schedule<<endl; file_user<<"[date] "<<time.Show_cur()<<endl; file_user<<"[dep_station] "<<dep_station<<endl; file_user<<"[arr_station] "<<arr_station<<endl; file_user<<"[ticket_tag] "<<type<<endl; file_user.close(); chdir(time.Show_file_curdir()); ifstream infile(schedule + ".txt"); ofstream outfile("temp.txt"); while(getline(infile,line)) { if(line.find(type + "_selln") != -1) { stringstream str(line); while(str >> temp) selln = atoi(temp.c_str()); ++selln; outfile<<"["<<type<<"_selln] "<<selln<<endl; } else outfile<<line<<endl; }