int open(far char *name, int flags, mode_t mode) { int x; struct flos_file *flosfile; for (x=0; x<MAX_FLOS_FILES; x++) if (strcmp(name,flos_files[x*sizeof(struct flos_file)]) == 0) return (-1); x=0; while (flos_files[x*sizeof(struct flos_file)] != 0) { if (x == MAX_FLOS_FILES) return (-1); x++; } flosfile=&flos_files[x*sizeof(struct flos_file)]; switch (mode) { case O_RDONLY: if (find_file(name, flosfile) == 0) { flosfile->name[0]=0; return (-1); } flosfile->position=0; //set_load_length(1); break; case O_WRONLY: if (find_file(name, flosfile) != 0) erase_file(name); create_file(name); if (find_file(name, flosfile) == 0) { flosfile->name[0]=0; return (-1); } flosfile->position=0; break; case O_APPEND: if (find_file(name, flosfile) != 0) { flosfile->position=flosfile->size-1; } else { erase_file(name); create_file(name); flosfile->position=0; } break; default: return(-1); break; } flosfile->mode=mode; flos_lastfile=flosfile; return(flosfile); }
int main(void) { char option = 0; do{ option = get_menu_option(); switch( option ){ case '1': system("clear"); write_file(); break; case '2': system("clear"); read_file(); break; case '3': system("clear"); erase_file(); break; case '4': system("clear"); show_fat_table(); break; case '5': //Leave program break; } }while( option != '5' ); system("clear"); printf("Hard Disk had turned off...\n"); return 0; }
//-------------------------------------------------------------------------- bool init( uword file_count, std::string folder, const std::string& prefix, const std::string& suffix, const std::deque<std::string>& previous ) { assert (file_count == 0 || file_count > 1); try { append_separator_if (folder); uword max_name = folder.size() + prefix.size() + fixed_chars_in_name + suffix.size(); for (uword i = 0; i < previous.size(); ++i) { max_name = (previous[i].size() > max_name) ? previous[i].size() : max_name; } ++max_name; //trailing null std::deque<std::string> prev; m_buffer.resize (max_name, 0); prev = previous; while (file_count && ((prev.size() > file_count))) { erase_file (prev.front().c_str()); prev.pop_front(); } m_folder = folder; m_prefix = prefix; m_suffix = suffix; if (file_count) { m_name_strings.free(); if (!m_name_strings.init (max_name, file_count)) { return false; } for (auto it = prev.begin(); it != prev.end(); ++it) { m_name_strings.push_tail(); std::memcpy (m_name_strings.tail(), &(*it)[0], it->size()); } } return true; } catch (...) { assert (false && "log: memory exception"); std::cerr << "[logger] memory exception\n"; return false; } }
//-------------------------------------------------------------------------- bool can_write_in_folder (std::string folder) { try { append_separator_if (folder); if (m_buffer.size() < (folder.size() + fixed_chars_in_name + 1)) { m_buffer.resize (folder.size() + fixed_chars_in_name + 1, -1); } for (uword i = 0; ; ++i) { std::string empty; new_file_name_c_str_in_buffer (folder, empty, empty, i ,i + 1); const char* fn = filename_in_buffer(); std::ofstream file (fn); file.write ((const char*) &fn, m_buffer.size()); if (file.good()) { file.close(); erase_file (fn); break; } erase_file (fn); if (i == 20) //FIXME { std::cerr << "[logger] unable to create verification file: " "\"" << fn << "\", does folder exist? has this user write " "access to it?\n"; assert( false && "log: couldn't create or write a test file" ); return false; } th::this_thread::sleep_for (ch::milliseconds (1)); } } catch (...) { assert (false && "log: memory exception"); return false; } return true; }
//-------------------------------------------------------------------------- void keep_newer_files (uword keep_count) { assert (rotates()); while (m_name_strings.size() > keep_count) { erase_file ((const char*) m_name_strings.head()); m_name_strings.pop_head(); } }
//-------------------------------------------------------------------------- bool can_write_in_folder (const std::string& folder) { try { if (m_buffer.size() < (folder.size() + fixed_chars_in_name + 1)) { m_buffer.resize (folder.size() + fixed_chars_in_name + 1, -1); } for (uword i = 0; ; ++i) { std::string empty; new_file_name_c_str_in_buffer (folder, empty, empty, i ,i + 1); const char* fn = filename_in_buffer(); std::ofstream file (fn); file.write ((const char*) &fn, m_buffer.size()); if (file.good()) { file.close(); erase_file (fn); break; } erase_file (fn); if (i == 20) //FIXME { assert( false && "log: couldn't create or write a test file" ); return false; } th::this_thread::sleep_for (ch::milliseconds (1)); } } catch (...) { assert (false && "log: memory exception"); return false; } return true; }
TEST_F(TestObSingleLogReader, test_init) { ObSingleLogReader log_reader; char longf[BUFSIZ]; memset(longf, 'A', BUFSIZ); longf[BUFSIZ - 1] = '\0'; ASSERT_EQ(OB_INVALID_ARGUMENT, log_reader.init(NULL)); ASSERT_EQ(OB_INVALID_ARGUMENT, log_reader.init(longf)); ASSERT_EQ(OB_NOT_INIT, log_reader.open(100)); ASSERT_EQ(OB_NOT_INIT, log_reader.close()); // prepare log file 101 int file1 = 101; ASSERT_EQ(0, create_file(file1)); ASSERT_EQ(OB_SUCCESS, log_reader.init(log_dir)); ASSERT_EQ(OB_INIT_TWICE, log_reader.init(log_dir)); ASSERT_EQ(OB_SUCCESS, log_reader.reset()); ASSERT_EQ(OB_SUCCESS, log_reader.init(log_dir)); ASSERT_EQ(OB_SUCCESS, log_reader.open(file1)); ASSERT_EQ(OB_SUCCESS, log_reader.close()); ASSERT_EQ(0, erase_file(file1)); LogCommand cmd1; uint64_t s1; char* log1; int64_t len1; int file2 = 12345; ASSERT_EQ(OB_SUCCESS, log_reader.close()); ASSERT_EQ(OB_FILE_NOT_EXIST, log_reader.open(file2)); // prepare log file file2 ASSERT_EQ(0, create_file(file2)); ASSERT_EQ(OB_SUCCESS, log_reader.open(file2)); ASSERT_EQ(OB_READ_NOTHING, log_reader.read_log(cmd1, s1, log1, len1)); ASSERT_EQ(OB_SUCCESS, log_reader.close()); ASSERT_EQ(0, erase_file(file2)); }
//-------------------------------------------------------------------------- bool init( uword file_count, const std::string& folder, const std::string& prefix, const std::string& suffix, const std::deque<std::string>& previous ) { assert (file_count == 0 || file_count > 1); uword max_name = folder.size() + prefix.size() + fixed_chars_in_name + suffix.size(); for (uword i = 0; i < previous.size(); ++i) { max_name = (previous[i].size() > max_name) ? previous[i].size() : max_name; } ++max_name; //trailing null try { m_buffer.resize (max_name, 0); auto prev = previous; while (file_count && ((prev.size() >= file_count))) { erase_file (prev.front().c_str()); prev.pop_front(); } m_folder = folder; m_prefix = prefix; m_suffix = suffix; } catch (...) { assert (false && "log: memory exception"); return false; } if (file_count) { m_name_strings.free(); if (!m_name_strings.init (max_name, file_count)) { return false; } } return true; }
TEST_F(TestObUpsLogMgr, test_init) { int64_t log_file_max_size = 1L << 28; ObRoleMgr role_mgr; SlaveMgr4Test sm4t; ObUpsLogMgr log_mgr1; ASSERT_EQ(OB_SUCCESS, log_mgr1.init(log_dir, log_file_max_size, sm4t.get_slave_mgr(), &role_mgr, 0)); ASSERT_EQ(1U, log_mgr1.get_replay_point()); erase_file(1); uint64_t log_file_max_id = 1100; uint64_t log_max_seq = 22000; char lf[BUFSIZ]; int64_t lf_pos = 0; ObUpsMutator mutator; ASSERT_EQ(OB_SUCCESS, mutator.serialize(lf, BUFSIZ, lf_pos)); uint64_t new_log_file_id; ObLogWriter log_writer; ASSERT_EQ(OB_SUCCESS, log_writer.init(log_dir, log_file_max_size, sm4t.get_slave_mgr(), 0)); ASSERT_EQ(OB_SUCCESS, log_writer.start_log(log_file_max_id, log_max_seq)); ASSERT_EQ(OB_SUCCESS, log_writer.switch_log_file(new_log_file_id)); ASSERT_EQ(OB_SUCCESS, log_writer.switch_log_file(new_log_file_id)); ASSERT_EQ(OB_SUCCESS, log_writer.write_log(OB_LOG_UPS_MUTATOR, lf, lf_pos)); ASSERT_EQ(OB_SUCCESS, log_writer.flush_log()); ASSERT_EQ(OB_SUCCESS, log_writer.switch_log_file(new_log_file_id)); ASSERT_EQ(OB_SUCCESS, log_writer.write_log(OB_LOG_UPS_MUTATOR, lf, lf_pos)); ASSERT_EQ(OB_SUCCESS, log_writer.flush_log()); ASSERT_EQ(OB_SUCCESS, log_writer.switch_log_file(new_log_file_id)); ASSERT_EQ(false, exists(ObUpsLogMgr::UPS_LOG_REPLAY_POINT_FILE)); ObUpsLogMgr log_mgr2; ASSERT_EQ(OB_SUCCESS, log_mgr2.init(log_dir, log_file_max_size, sm4t.get_slave_mgr(), &role_mgr, 0)); ASSERT_EQ(log_file_max_id, log_mgr2.get_replay_point()); log_mgr2.set_replay_point(100); ASSERT_EQ(100U, log_mgr2.get_replay_point()); log_mgr2.set_replay_point(log_file_max_id + 1); ASSERT_EQ(log_file_max_id + 1, log_mgr2.get_replay_point()); ObUpsTableMgr table_mgr; ASSERT_EQ(OB_SUCCESS, table_mgr.init()); ASSERT_EQ(0U, log_mgr2.get_cur_log_seq()); ASSERT_EQ(OB_SUCCESS, log_mgr2.replay_log(table_mgr)); ASSERT_NE(0U, log_mgr2.get_cur_log_seq()); ASSERT_EQ(OB_SUCCESS, log_mgr2.write_log(OB_LOG_UPS_MUTATOR, lf, lf_pos)); ASSERT_EQ(OB_SUCCESS, log_mgr2.flush_log()); ASSERT_EQ(OB_SUCCESS, log_mgr2.switch_log_file(new_log_file_id)); log_mgr2.set_replay_point(new_log_file_id); }
void do_test (std::size_t N, nsize_t block_size, float load_factor) { temp_dir td; error_code ec; auto const dp = td.file ("nudb.dat"); auto const kp = td.file ("nudb.key"); auto const lp = td.file ("nudb.log"); Sequence seq; store db; create<xxhasher>(dp, kp, lp, appnumValue, saltValue, sizeof(key_type), block_size, load_factor, ec); if(! expect(! ec, ec.message())) return; finisher f( [&] { { error_code ev; native_file::erase(dp, ev); expect(! ev, ev.message()); } { error_code ev; native_file::erase(kp, ev); expect(! ev, ev.message()); } { error_code ev; erase_file(lp, ev); expect(! ev, ev.message()); } }); db.open(dp, kp, lp, arenaAllocSize, ec); if(! expect(! ec, ec.message())) return; Storage s; // insert for(std::size_t i = 0; i < N; ++i) { auto const v = seq[i]; auto const success = db.insert(&v.key, v.data, v.size, ec); if(! expect(! ec, ec.message())) return; expect(success); } // fetch for(std::size_t i = 0; i < N; ++i) { auto const v = seq[i]; bool const found = db.fetch (&v.key, s, ec); if(! expect(! ec, ec.message())) return; expect(found); expect(s.size() == v.size); expect(std::memcmp(s.get(), v.data, v.size) == 0); } // insert duplicates for(std::size_t i = 0; i < N; ++i) { auto const v = seq[i]; auto const success = db.insert( &v.key, v.data, v.size, ec); if(! expect(! ec, ec.message())) return; expect(! success); } // insert/fetch for(std::size_t i = 0; i < N; ++i) { auto v = seq[i]; bool const found = db.fetch (&v.key, s, ec); if(! expect(! ec, ec.message())) return; expect(found); expect(s.size() == v.size); expect(memcmp(s.get(), v.data, v.size) == 0); v = seq[i + N]; auto const success = db.insert( &v.key, v.data, v.size, ec); if(! expect(! ec, ec.message())) return; expect(success); } db.close(ec); if(! expect(! ec, ec.message())) return; verify_info info; verify<xxhasher>( info, dp, kp, 0, no_progress{}, ec); if(! expect(! ec, ec.message())) return; expect(info.hist[1] > 0); log << info; }