static bool SetMasterProxy( Proxy *master, const Proxy *copy_src ) { int rc; std::string tmp_file; formatstr( tmp_file, "%s.tmp", master->proxy_filename ); rc = copy_file( copy_src->proxy_filename, tmp_file.c_str() ); if ( rc != 0 ) { return false; } rc = rotate_file( tmp_file.c_str(), master->proxy_filename ); if ( rc != 0 ) { MSC_SUPPRESS_WARNING_FIXME(6031) // warning: return value of 'unlink' ignored. unlink( tmp_file.c_str() ); return false; } master->expiration_time = copy_src->expiration_time; master->near_expired = copy_src->near_expired; Callback cb; master->m_callbacks.Rewind(); while ( master->m_callbacks.Next( cb ) ) { ((cb.m_data)->*(cb.m_func_ptr))(); } return true; }
void backend::consume(string_type const& formatted_message) { if (!impl_) return; if((impl_->file_.is_open() && (impl_->written_ + formatted_message.size() >= 512*1024) ) || !impl_->file_.good() ) { rotate_file(); } if (!impl_->file_.is_open()) { fs::create_directories(impl_->root_); impl_->file_.open((impl_->root_ / (impl_->name_ + L".log")).c_str(), std::ios_base::app | std::ios_base::out); if (!impl_->file_.is_open()) { throw bee::exception("Failed to open file '%s' for writing.", (impl_->root_ / (impl_->name_ + L".log")).string().c_str()); } impl_->written_ = static_cast<std::streamoff>(impl_->file_.tellp()); } impl_->file_.write(formatted_message.data(), static_cast<std::streamsize>(formatted_message.size())); impl_->file_.put(static_cast<string_type::value_type>('\n')); impl_->written_ += formatted_message.size() + 1; impl_->file_.flush(); }
RotatedFileLogger::RotatedFileLogger(const String &p_base_path, int p_max_files) { file = NULL; base_path = p_base_path.simplify_path(); max_files = p_max_files > 0 ? p_max_files : 1; rotate_file(); }
void AvailStats::checkpoint() { // Checkpoint our state to disk by serializing to a string // and writing the string to disk. It's not very efficient // to create this string each time, but it shouldn't be too big // (under 1KB), so I don't think it's worth worrying too much // about efficiency. if( ckpt_filename.Length() ) { FILE *fp = safe_fopen_wrapper_follow(tmp_ckpt_filename.Value(), "w"); if( fp ) { MyString state = serialize(); if( (int)fwrite(state.Value(), sizeof(char), state.Length(), fp) == state.Length() ) { fclose(fp); if ( rotate_file(tmp_ckpt_filename.Value(), ckpt_filename.Value()) < 0 ) { dprintf( D_ALWAYS, "AvailStats::checkpoint() failed to rotate " "%s to %s\n", tmp_ckpt_filename.Value(), ckpt_filename.Value() ); } } else { fclose(fp); } } } }
int main(const int argc, const char **argv) { if(argc < 2) { std::cout << "Wrong number of arguments!\n"; show_usage(); return 1; } std::string fname (argv[1]); if(fname.compare("-") == 0) { rotate_stdin(); } else { rotate_file(fname); } }
void Defrag::saveState() { ClassAd ad; ad.Assign(ATTR_LAST_POLL,(int)m_last_poll); std::string new_state_file; sprintf(new_state_file,"%s.new",m_state_file.c_str()); FILE *fp; if( !(fp = safe_fopen_wrapper_follow(new_state_file.c_str(), "w")) ) { EXCEPT("failed to save state to %s\n",new_state_file.c_str()); } else { ad.fPrint(fp); fclose( fp ); if( rotate_file(new_state_file.c_str(),m_state_file.c_str())!=0 ) { EXCEPT("failed to save state to %s\n",m_state_file.c_str()); } } }
void CCBServer::SaveAllReconnectInfo() { if( m_reconnect_fname.IsEmpty() ) { return; } CloseReconnectFile(); if( m_reconnect_info.getNumElements()==0 ) { remove( m_reconnect_fname.Value() ); return; } MyString orig_reconnect_fname = m_reconnect_fname; m_reconnect_fname.formatstr_cat(".new"); if( !OpenReconnectFile() ) { m_reconnect_fname = orig_reconnect_fname; return; } CCBReconnectInfo *reconnect_info=NULL; m_reconnect_info.startIterations(); while( m_reconnect_info.iterate(reconnect_info) ) { if( !SaveReconnectInfo(reconnect_info) ) { CloseReconnectFile(); m_reconnect_fname = orig_reconnect_fname; dprintf(D_ALWAYS,"CCB: aborting rewriting of %s\n", m_reconnect_fname.Value()); return; } } CloseReconnectFile(); int rc; rc = rotate_file( m_reconnect_fname.Value(),orig_reconnect_fname.Value() ); if( rc < 0 ) { dprintf(D_ALWAYS,"CCB: failed to rotate rewritten %s\n", m_reconnect_fname.Value()); } m_reconnect_fname = orig_reconnect_fname; }
bool ClassAdLog::TruncLog() { MyString tmp_log_filename; int new_log_fd; FILE *new_log_fp; dprintf(D_ALWAYS,"About to rotate ClassAd log %s\n",logFilename()); if(!SaveHistoricalLogs()) { dprintf(D_ALWAYS,"Skipping log rotation, because saving of historical log failed for %s.\n",logFilename()); return false; } tmp_log_filename.sprintf( "%s.tmp", logFilename()); new_log_fd = safe_open_wrapper_follow(tmp_log_filename.Value(), O_RDWR | O_CREAT | O_LARGEFILE, 0600); if (new_log_fd < 0) { dprintf(D_ALWAYS, "failed to rotate log: safe_open_wrapper(%s) returns %d\n", tmp_log_filename.Value(), new_log_fd); return false; } new_log_fp = fdopen(new_log_fd, "r+"); if (new_log_fp == NULL) { dprintf(D_ALWAYS, "failed to rotate log: fdopen(%s) returns NULL\n", tmp_log_filename.Value()); return false; } // Now it is time to move courageously into the future. historical_sequence_number++; LogState(new_log_fp); fclose(log_fp); log_fp = NULL; fclose(new_log_fp); // avoid sharing violation on move if (rotate_file(tmp_log_filename.Value(), logFilename()) < 0) { dprintf(D_ALWAYS, "failed to rotate job queue log!\n"); // Beat a hasty retreat into the past. historical_sequence_number--; int log_fd = safe_open_wrapper_follow(logFilename(), O_RDWR | O_APPEND | O_LARGEFILE, 0600); if (log_fd < 0) { EXCEPT("failed to reopen log %s, errno = %d after failing to rotate log.",logFilename(),errno); } log_fp = fdopen(log_fd, "a+"); if (log_fp == NULL) { EXCEPT("failed to refdopen log %s, errno = %d after failing to rotate log.",logFilename(),errno); } return false; } int log_fd = safe_open_wrapper_follow(logFilename(), O_RDWR | O_APPEND | O_LARGEFILE, 0600); if (log_fd < 0) { EXCEPT( "failed to open log in append mode: " "safe_open_wrapper(%s) returns %d\n", logFilename(), log_fd); } log_fp = fdopen(log_fd, "a+"); if (log_fp == NULL) { close(log_fd); EXCEPT("failed to fdopen log in append mode: " "fdopen(%s) returns %d\n", logFilename(), log_fd); } return true; }