void upgrade_database(const char *dbname) { /** public function - see header */ //check database is open check_db_open(GET_CALL_INFO); load_db_game_data(); //compare database version entry with the hard-coded version in the code-base if(game_data.database_version==REQUIRED_DATABASE_VERSION) { fprintf(stderr,"Database [%s] is already up to date at version [%i]\n", dbname, REQUIRED_DATABASE_VERSION); log_event(EVENT_ERROR, "Database [%s] is already up to date at version [%i]", dbname, REQUIRED_DATABASE_VERSION); stop_server(); } else if(game_data.database_version>REQUIRED_DATABASE_VERSION) { fprintf(stderr,"Database [%s] is version [%i] which is newer than this codebase version [%i]\n", dbname, game_data.database_version, REQUIRED_DATABASE_VERSION); log_event(EVENT_ERROR, "Database [%s] is version [%i] which is newer than this codebase version [%i]", dbname, game_data.database_version, REQUIRED_DATABASE_VERSION); stop_server(); } while(game_data.database_version<REQUIRED_DATABASE_VERSION) { // find the function that upgrades the database to the next version const struct upgrade_array_entry *entry = find_upgrade_entry(game_data.database_version); if(!entry){ log_event(EVENT_ERROR, "can't find entry for database in function %s: module %s: line %i", GET_CALL_INFO); stop_server(); } fprintf(stderr,"Database version update %d to %d\n", entry->from_version, entry->to_version); log_event(EVENT_ERROR, "Database version update %d to %d:", entry->from_version, entry->to_version); // backup is created before calling each upgrade function create_backup_file(dbname); if(entry->fn(dbname)==0) { game_data.database_version = entry->to_version; fprintf(stderr,"Database update successful\n"); log_event(EVENT_ERROR, "Database update successful"); } else { fprintf(stderr,"Database update failed\n"); log_event(EVENT_ERROR, "Database update failed"); stop_server(); } } }
bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool aCreateBackupFile ) { // please, keep it simple. prompting goes elsewhere. wxFileName pcbFileName = aFileName; if( pcbFileName.GetExt() == LegacyPcbFileExtension ) pcbFileName.SetExt( KiCadPcbFileExtension ); if( !IsWritable( pcbFileName ) ) { wxString msg = wxString::Format( _( "No access rights to write to file '%s'" ), GetChars( pcbFileName.GetFullPath() ) ); DisplayError( this, msg ); return false; } wxString backupFileName; // aCreateBackupFile == false is mainly used to write autosave files // or new files in save as... command if( aCreateBackupFile ) { backupFileName = create_backup_file( aFileName ); } GetBoard()->m_Status_Pcb &= ~CONNEXION_OK; GetBoard()->SynchronizeNetsAndNetClasses(); // Select default Netclass before writing file. // Useful to save default values in headers SetCurrentNetClass( NETCLASS::Default ); ClearMsgPanel(); wxString upperTxt; wxString lowerTxt; try { PLUGIN::RELEASER pi( IO_MGR::PluginFind( IO_MGR::KICAD ) ); wxASSERT( pcbFileName.IsAbsolute() ); pi->Save( pcbFileName.GetFullPath(), GetBoard(), NULL ); } catch( const IO_ERROR& ioe ) { wxString msg = wxString::Format( _( "Error saving board file '%s'.\n%s" ), GetChars( pcbFileName.GetFullPath() ), GetChars( ioe.errorText ) ); DisplayError( this, msg ); lowerTxt.Printf( _( "Failed to create '%s'" ), GetChars( pcbFileName.GetFullPath() ) ); AppendMsgPanel( upperTxt, lowerTxt, CYAN ); return false; } GetBoard()->SetFileName( pcbFileName.GetFullPath() ); UpdateTitle(); // Put the saved file in File History, unless aCreateBackupFile // is false. // aCreateBackupFile == false is mainly used to write autosave files // and not need to have an autosave file in file history if( aCreateBackupFile ) UpdateFileHistory( GetBoard()->GetFileName() ); // Delete auto save file on successful save. wxFileName autoSaveFileName = pcbFileName; autoSaveFileName.SetName( wxString( autosavePrefix ) + pcbFileName.GetName() ); if( autoSaveFileName.FileExists() ) wxRemoveFile( autoSaveFileName.GetFullPath() ); if( !!backupFileName ) upperTxt.Printf( _( "Backup file: '%s'" ), GetChars( backupFileName ) ); lowerTxt.Printf( _( "Wrote board file: '%s'" ), GetChars( pcbFileName.GetFullPath() ) ); AppendMsgPanel( upperTxt, lowerTxt, CYAN ); GetScreen()->ClrModify(); GetScreen()->ClrSave(); return true; }
/** * @brief UpiBackupData * * Backup data from IC to system routine * * @para data address of BackupDataType * @return _UPI_NULL_ */ void UpiBackupData(BackupDataType *data) { _backup_bool_ rtnBool; _backup_u8_ rtnU8; #ifndef UG31XX_BACKUP_FILE_ENABLE rtnBool = _UPI_TRUE_; data->backupFileSts = BACKUP_FILE_STS_COMPARE; #endif ///< end of UG31XX_BACKUP_FILE_ENABLE switch(data->backupFileSts) { case BACKUP_FILE_STS_CHECKING: /// [AT-PM] : Check backup file existed or not ; 02/21/2013 rtnBool = is_file_exist(data->backupFileName); UG31_LOGN("[%s]: is_file_exist() = %d.\n", __func__, rtnBool); if(rtnBool == BACKUP_BOOL_TRUE) { data->backupFileSts = BACKUP_FILE_STS_EXIST; data->backupFileRetryCnt = 0; } else { data->backupFileRetryCnt = data->backupFileRetryCnt + 1; UG31_LOGN("[%s]: Check backup file retry count = %d\n", __func__, data->backupFileRetryCnt); if(data->backupFileRetryCnt > RETRY_CHECKING_THRESHOLD) { data->backupFileSts = BACKUP_FILE_STS_NOT_EXIST; UG31_LOGE("[%s]: Backup file is not exist.\n", __func__); } } break; case BACKUP_FILE_STS_NOT_EXIST: /// [AT-PM] : Create backup file ; 02/21/2013 PrepareData(data); rtnBool = create_backup_file(data->backupFileName, data->backupBuffer, data->backupBufferSize); UG31_LOGN("[%s]: create_backup_file() = %d.\n", __func__, rtnBool); if(rtnBool == BACKUP_BOOL_TRUE) { data->backupFileSts = BACKUP_FILE_STS_EXIST; data->icDataAvailable = BACKUP_BOOL_TRUE; } else { UG31_LOGE("[%s]: Create backup file fail.\n", __func__); } break; case BACKUP_FILE_STS_EXIST: data->backupFileSts = BACKUP_FILE_STS_COMPARE; case BACKUP_FILE_STS_COMPARE: if(data->icDataAvailable == BACKUP_BOOL_TRUE) { /// [AT-PM] : Check content of file is consist with IC or not ; 02/21/2013 rtnU8 = CheckBackupFile(data); UG31_LOGN("[%s]: CheckBackupFile() = %d.\n", __func__, rtnU8); if(rtnU8 == CHECK_BACKUP_FILE_STS_VERSION_MISMATCH) { data->backupFileSts = BACKUP_FILE_STS_UPDATE_BY_VERSION; } else if(rtnU8 == CHECK_BACKUP_FILE_STS_NEED_UPDATE) { data->backupFileSts = BACKUP_FILE_STS_UPDATE; } else if(rtnU8 == CHECK_BACKUP_FILE_STS_PASS) { data->backupFileSts = BACKUP_FILE_STS_COMPARE; } else { data->backupFileSts = BACKUP_FILE_STS_UPDATE; } } else { data->backupFileSts = BACKUP_FILE_STS_CHECKING; } break; case BACKUP_FILE_STS_UPDATE: case BACKUP_FILE_STS_UPDATE_BY_VERSION: if(data->icDataAvailable == BACKUP_BOOL_TRUE) { rtnU8 = UpdateBackupFile(data); UG31_LOGN("[%s]: UpdateBackupFile() = %d.\n", __func__, rtnU8); if(rtnU8 == CHECK_BACKUP_FILE_STS_PASS) { if(data->backupFileSts == BACKUP_FILE_STS_UPDATE_BY_VERSION) { data->backupFileSts = BACKUP_FILE_STS_VERSION_MISMATCH; } else { data->backupFileSts = BACKUP_FILE_STS_COMPARE; } } } else { data->backupFileSts = BACKUP_FILE_STS_CHECKING; } break; default: /// [AT-PM] : Un-known state ; 02/21/2013 data->backupFileSts = BACKUP_FILE_STS_NOT_EXIST; break; } }
static void vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (void)) { const char *editor; pid_t pid; struct stat st1, st2; int status; FILE *f; /* FIXME: the following should have variable sizes */ char filebackup[1024], fileedit[1024]; char *to_rename; snprintf (filebackup, sizeof filebackup, "%s-", file); #ifdef WITH_TCB if (tcb_mode) { if ( (mkdir (TCB_DIR "/" SHADOWTCB_SCRATCHDIR, 0700) != 0) && (errno != EEXIST)) { vipwexit (_("failed to create scratch directory"), errno, 1); } if (shadowtcb_drop_priv () == SHADOWTCB_FAILURE) { vipwexit (_("failed to drop privileges"), errno, 1); } snprintf (fileedit, sizeof fileedit, TCB_DIR "/" SHADOWTCB_SCRATCHDIR "/.vipw.shadow.%s", user); } else { #endif /* WITH_TCB */ snprintf (fileedit, sizeof fileedit, "%s.edit", file); #ifdef WITH_TCB } #endif /* WITH_TCB */ unlock = file_unlock; filename = file; fileeditname = fileedit; if (access (file, F_OK) != 0) { vipwexit (file, 1, 1); } #ifdef WITH_SELINUX /* if SE Linux is enabled then set the context of all new files to be the context of the file we are editing */ if (is_selinux_enabled () != 0) { security_context_t passwd_context=NULL; int ret = 0; if (getfilecon (file, &passwd_context) < 0) { vipwexit (_("Couldn't get file context"), errno, 1); } ret = setfscreatecon (passwd_context); freecon (passwd_context); if (0 != ret) { vipwexit (_("setfscreatecon () failed"), errno, 1); } } #endif /* WITH_SELINUX */ #ifdef WITH_TCB if (tcb_mode && (shadowtcb_gain_priv () == SHADOWTCB_FAILURE)) { vipwexit (_("failed to gain privileges"), errno, 1); } #endif /* WITH_TCB */ if (file_lock () == 0) { vipwexit (_("Couldn't lock file"), errno, 5); } filelocked = true; #ifdef WITH_TCB if (tcb_mode && (shadowtcb_drop_priv () == SHADOWTCB_FAILURE)) { vipwexit (_("failed to drop privileges"), errno, 1); } #endif /* WITH_TCB */ /* edited copy has same owners, perm */ if (stat (file, &st1) != 0) { vipwexit (file, 1, 1); } f = fopen (file, "r"); if (NULL == f) { vipwexit (file, 1, 1); } #ifdef WITH_TCB if (tcb_mode && (shadowtcb_gain_priv () == SHADOWTCB_FAILURE)) vipwexit (_("failed to gain privileges"), errno, 1); #endif /* WITH_TCB */ if (create_backup_file (f, fileedit, &st1) != 0) { vipwexit (_("Couldn't make backup"), errno, 1); } (void) fclose (f); createedit = true; editor = getenv ("VISUAL"); if (NULL == editor) { editor = getenv ("EDITOR"); } if (NULL == editor) { editor = DEFAULT_EDITOR; } pid = fork (); if (-1 == pid) { vipwexit ("fork", 1, 1); } else if (0 == pid) { /* use the system() call to invoke the editor so that it accepts command line args in the EDITOR and VISUAL environment vars */ char *buf; buf = (char *) malloc (strlen (editor) + strlen (fileedit) + 2); snprintf (buf, strlen (editor) + strlen (fileedit) + 2, "%s %s", editor, fileedit); if (system (buf) != 0) { fprintf (stderr, "%s: %s: %s\n", progname, editor, strerror (errno)); exit (1); } else { exit (0); } } for (;;) { pid = waitpid (pid, &status, WUNTRACED); if ((pid != -1) && (WIFSTOPPED (status) != 0)) { /* The child (editor) was suspended. * Suspend vipw. */ kill (getpid (), SIGSTOP); /* wake child when resumed */ kill (pid, SIGCONT); } else { break; } } if ( (-1 == pid) || (WIFEXITED (status) == 0) || (WEXITSTATUS (status) != 0)) { vipwexit (editor, 1, 1); } if (stat (fileedit, &st2) != 0) { vipwexit (fileedit, 1, 1); } if (st1.st_mtime == st2.st_mtime) { vipwexit (0, 0, 0); } #ifdef WITH_SELINUX /* unset the fscreatecon */ if (is_selinux_enabled () != 0) { if (setfscreatecon (NULL) != 0) { vipwexit (_("setfscreatecon () failed"), errno, 1); } } #endif /* WITH_SELINUX */ /* * XXX - here we should check fileedit for errors; if there are any, * ask the user what to do (edit again, save changes anyway, or quit * without saving). Use pwck or grpck to do the check. --marekm */ createedit = false; #ifdef WITH_TCB if (tcb_mode) { f = fopen (fileedit, "r"); if (NULL == f) { vipwexit (_("failed to open scratch file"), errno, 1); } if (unlink (fileedit) != 0) { vipwexit (_("failed to unlink scratch file"), errno, 1); } if (shadowtcb_drop_priv () == SHADOWTCB_FAILURE) { vipwexit (_("failed to drop privileges"), errno, 1); } if (stat (file, &st1) != 0) { vipwexit (_("failed to stat edited file"), errno, 1); } to_rename = malloc (strlen (file) + 2); if (NULL == to_rename) { vipwexit (_("failed to allocate memory"), errno, 1); } snprintf (to_rename, strlen (file) + 2, "%s+", file); if (create_backup_file (f, to_rename, &st1) != 0) { free (to_rename); vipwexit (_("failed to create backup file"), errno, 1); } (void) fclose (f); } else { #endif /* WITH_TCB */ to_rename = fileedit; #ifdef WITH_TCB } #endif /* WITH_TCB */ unlink (filebackup); link (file, filebackup); if (rename (to_rename, file) == -1) { fprintf (stderr, _("%s: can't restore %s: %s (your changes are in %s)\n"), progname, file, strerror (errno), to_rename); #ifdef WITH_TCB if (tcb_mode) { free (to_rename); } #endif /* WITH_TCB */ vipwexit (0, 0, 1); } #ifdef WITH_TCB if (tcb_mode) { free (to_rename); if (shadowtcb_gain_priv () == SHADOWTCB_FAILURE) { vipwexit (_("failed to gain privileges"), errno, 1); } } #endif /* WITH_TCB */ if ((*file_unlock) () == 0) { fprintf (stderr, _("%s: failed to unlock %s\n"), progname, fileeditname); SYSLOG ((LOG_ERR, "failed to unlock %s", fileeditname)); /* continue */ } SYSLOG ((LOG_INFO, "file %s edited", fileeditname)); }