int save_all_map(void) { int x,y,z; char info[160]; char *txt; if ((z=check_map(&x,&y))!=0) { WINDOW *w; unselect_map(); if ((w=find_window(tool_bar))!=NULL) close_window(w); tool_sel=30; create_map_win(-1); open_sector_win(); jdi_na_sektor(x); } sprintf(info,"Chyba %02d na pozici %d:%d",-z,x,y); switch (z) { case -1:msg_box(info,'\01',"Chyb� definice st�ny!","OK",NULL);break; case -2:msg_box(info,'\01',"Chodba vede do neexistuj�c�ho sektoru!","OK",NULL);break; case -3:msg_box(info,'\01',"Schody jsou �patn� spojen� se sousedn�m� sektory!","OK",NULL);break; case -4:msg_box(info,'\01',"Ud�lost v neexistuj�c�m sektoru!","OK",NULL);break; } if (!backup) { create_backup(filename); create_backup(SHOP_NAME); create_backup(ITEMS_DAT); create_backup(MOB_FILE); create_backup(MOB_SOUND); backup=1; } if (save_map(filename)) { sprintf(info,"Nedok��u ulo�it soubor %s.",filename); msg_box("Chyba I/O",'\01',info,"!Panika!",NULL); } else txt=pripona(filename,TXT); save_items(); if (_access(txt,0)!=0) { FILE *f; f=fopen(txt,"w"); if (f) { fputs("-1\n",f); fclose(f); } } if (!mglob.local_monsters) { save_mobs(); save_sound_map(); } save_all_shops(); validate_sound_map(); return z; }
void Base::delete_entry(const char* file_name) { string prefix; ifstream file; int resp; int flag_er = 0; try{ create_backup(file_name); file.open(file_name, ios::in); /* Move all entries in vector. * Handle the entries in vector. * Copy vector entries in file. */ vector<string> data; string temp; //transfer file entries in vector while(!file.eof() ) { getline(file, temp); data.push_back(temp); } cout << "Give a prefix to delete from database:" << endl; getline(cin, prefix); do{ resp = sure(); }while(!resp); // delete operation in vector for(int i = 0; i < (int)data.size(); i++) { if(data[i].substr(0, prefix.length()) == prefix){ cout << "Prefix \"" << prefix << "\" found and erased!" << endl; flag_er = 1; data.erase(data.begin()+i); } } if(!flag_er) cout << "Prefix \"" << prefix << "\" not found!"<< endl; //write the new prefixes entries ofstream out(filename, ios::out | ios::trunc); for(vector<string>::const_iterator i = data.begin(); i != data.end(); ++i) { out << *i << endl; } out.flush(); out.close(); } catch(exception& e){ cout << "Error at opening file" << e.what() << endl; } }
static void test_clear(void) { HANDLE handle; BOOL ret; const char backup[] = "backup.evt"; const char backup2[] = "backup2.evt"; SetLastError(0xdeadbeef); ret = ClearEventLogA(NULL, NULL); ok(!ret, "Expected failure\n"); ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError()); /* Make a backup eventlog to work with */ if (!create_backup(backup)) return; SetLastError(0xdeadbeef); ret = ClearEventLogA(NULL, backup); ok(!ret, "Expected failure\n"); ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError()); handle = OpenBackupEventLogA(NULL, backup); todo_wine ok(handle != NULL, "Expected a handle\n"); /* A real eventlog would fail with ERROR_ALREADY_EXISTS */ SetLastError(0xdeadbeef); ret = ClearEventLogA(handle, backup); ok(!ret, "Expected failure\n"); /* The eventlog service runs under an account that doesn't have the necessary * permissions on the users home directory on a default Vista+ system. */ ok(GetLastError() == ERROR_INVALID_HANDLE || GetLastError() == ERROR_ACCESS_DENIED, /* Vista+ */ "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError()); /* Show that ClearEventLog only works for real eventlogs. */ SetLastError(0xdeadbeef); ret = ClearEventLogA(handle, backup2); ok(!ret, "Expected failure\n"); ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError()); ok(GetFileAttributesA(backup2) == INVALID_FILE_ATTRIBUTES, "Expected no backup file\n"); SetLastError(0xdeadbeef); ret = ClearEventLogA(handle, NULL); ok(!ret, "Expected failure\n"); ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError()); CloseEventLog(handle); todo_wine ok(DeleteFileA(backup), "Could not delete the backup file\n"); }
void move_file (char const *from, bool *from_needs_removal, struct stat const *fromst, char const *to, mode_t mode, bool backup) { struct stat to_st; int to_errno; to_errno = stat_file (to, &to_st); if (backup) create_backup (to, to_errno ? NULL : &to_st, false); if (! to_errno) insert_file_id (&to_st, OVERWRITTEN); if (from) { if (S_ISLNK (mode)) { bool to_dir_known_to_exist = false; /* FROM contains the contents of the symlink we have patched; need to convert that back into a symlink. */ char *buffer = xmalloc (PATH_MAX); int fd, size = 0, i; if ((fd = open (from, O_RDONLY | O_BINARY)) < 0) pfatal ("Can't reopen file %s", quotearg (from)); while ((i = read (fd, buffer + size, PATH_MAX - size)) > 0) size += i; if (i != 0 || close (fd) != 0) read_fatal (); buffer[size] = 0; if (! backup) { if (unlink (to) == 0) to_dir_known_to_exist = true; } if (symlink (buffer, to) != 0) { if (errno == ENOENT && ! to_dir_known_to_exist) makedirs (to); if (symlink (buffer, to) != 0) pfatal ("Can't create %s %s", "symbolic link", to); } free (buffer); if (lstat (to, &to_st) != 0) pfatal ("Can't get file attributes of %s %s", "symbolic link", to); insert_file_id (&to_st, CREATED); } else { if (debug & 4) say ("Renaming file %s to %s\n", quotearg_n (0, from), quotearg_n (1, to)); if (rename (from, to) != 0) { bool to_dir_known_to_exist = false; if (errno == ENOENT && (to_errno == -1 || to_errno == ENOENT)) { makedirs (to); to_dir_known_to_exist = true; if (rename (from, to) == 0) goto rename_succeeded; } if (errno == EXDEV) { struct stat tost; if (! backup) { if (unlink (to) == 0) to_dir_known_to_exist = true; else if (errno != ENOENT) pfatal ("Can't remove file %s", quotearg (to)); } copy_file (from, to, &tost, 0, mode, to_dir_known_to_exist); insert_file_id (&tost, CREATED); return; } pfatal ("Can't rename file %s to %s", quotearg_n (0, from), quotearg_n (1, to)); } rename_succeeded: insert_file_id (fromst, CREATED); /* Do not clear *FROM_NEEDS_REMOVAL if it's possible that the rename returned zero because FROM and TO are hard links to the same file. */ if ((0 < to_errno || (to_errno == 0 && to_st.st_nlink <= 1)) && from_needs_removal) *from_needs_removal = false; } } else if (! backup) { if (debug & 4) say ("Removing file %s\n", quotearg (to)); if (unlink (to) != 0 && errno != ENOENT) pfatal ("Can't remove file %s", quotearg (to)); } }
int main(int argc, char *argv[]) { QDir dir; QString path=dir.absolutePath(); QDate data = QDate::currentDate(); int DaysInMonths = data.daysInMonth(); int ActualDay = data.day(); int currentExitCode = 0; do { QApplication a(argc, argv); a.setStyle("fusion"); QFont font("Calibri Light", 12, QFont::Light ,false); QFont font_main("Calibri Light", 10, QFont::Light ,false); QFont splash_font("Calibri Light", 24, QFont::Bold); LoginDialog *logindialog = new LoginDialog; MainWindow w; logindialog->setWindowFlags(((logindialog->windowFlags() | Qt::CustomizeWindowHint) & Qt::WindowCloseButtonHint & ~Qt::WindowContextHelpButtonHint) ); /* Ekran startowy*/ if(currentExitCode != MainWindow::EXIT_CODE_REBOOT) { QSplashScreen * splash = new QSplashScreen; splash->setPixmap(QPixmap(path+"/obrazy/splash.png")); splash->setFont(splash_font); splash->show(); splash->showMessage(QObject::tr("Uruchamianie programu "), Qt::AlignLeft | Qt::AlignTop, Qt::black); //This line represents the alignment of text, color and position qApp->processEvents(); QTimer::singleShot(2000,splash,SLOT(close())); QString Splash_string = "Uruchamianie programu"; QString Splash_string_add = "Uruchamianie programu"; for(int i=0;i<12;i++) { QThread::msleep(150); Splash_string_add = Splash_string_add + "."; if(i==3 || i==7 || i==11) Splash_string_add = Splash_string; splash->showMessage(Splash_string_add, Qt::AlignLeft | Qt::AlignTop, Qt::black); //This line represents the alignment of text, color and position } } /* signal-slot - connect login and password from logindialog with MainWindow */ QObject::connect(logindialog, SIGNAL(sendAccess(QString,QString)), &w, SLOT(receiveAccess(QString,QString))); logindialog->setWindowIcon(QIcon(path+"/obrazy/log_icon.png")); logindialog->setWindowTitle("SERWIS - Logowanie"); logindialog->setFont(font); if (logindialog->exec() != QDialog::Accepted) { a.quit(); return 0; } else { delete logindialog; w.showMaximized(); w.setWindowTitle("SERWIS"); w.setWindowIcon(QIcon(path+"/obrazy/services_icon.png")); w.setFont(font_main); w.show(); QFile file; QString file_name="AUTO_BACKUP.txt"; file.setFileName(file_name); if(!file.exists() && (DaysInMonths-ActualDay)==1){ QMessageBox::warning(&w,"Informacja","****************** Do końca miesiąca został 1 dzień! *******************\n" "Wykonany zostanie automatyczny zapis kopii zapasowej bazy danych. \n" "*************************************************************************"); QTimer::singleShot(500,&w,SLOT(create_backup())); qDebug() << "Doesn't exists: "<<file_name; file.open(QIODevice::ReadWrite | QIODevice::Text); QTextStream stream(&file); file.close(); }else if (file.exists() && (DaysInMonths-ActualDay)!=1){ qDebug() << file_name <<" removing ..."; file.remove(); }else if (file.exists() && (DaysInMonths-ActualDay)==1) { qDebug() << file_name <<" already created ..."; } } currentExitCode = a.exec(); } while( currentExitCode == MainWindow::EXIT_CODE_REBOOT ); return currentExitCode; }
static void test_openbackup(void) { HANDLE handle, handle2, file; DWORD written; const char backup[] = "backup.evt"; const char text[] = "Just some text"; SetLastError(0xdeadbeef); handle = OpenBackupEventLogA(NULL, NULL); ok(handle == NULL, "Didn't expect a handle\n"); ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError()); SetLastError(0xdeadbeef); handle = OpenBackupEventLogA(NULL, "idontexist.evt"); ok(handle == NULL, "Didn't expect a handle\n"); ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError()); SetLastError(0xdeadbeef); handle = OpenBackupEventLogA("IDontExist", NULL); ok(handle == NULL, "Didn't expect a handle\n"); ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError()); SetLastError(0xdeadbeef); handle = OpenBackupEventLogA("IDontExist", "idontexist.evt"); ok(handle == NULL, "Didn't expect a handle\n"); ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR, /* Some Vista and Win7 */ "Expected RPC_S_SERVER_UNAVAILABLE, got %d\n", GetLastError()); /* Make a backup eventlog to work with */ if (create_backup(backup)) { /* FIXME: Wine stops here */ if (GetFileAttributesA(backup) == INVALID_FILE_ATTRIBUTES) { skip("We don't have a backup eventlog to work with\n"); return; } SetLastError(0xdeadbeef); handle = OpenBackupEventLogA("IDontExist", backup); ok(handle == NULL, "Didn't expect a handle\n"); ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR, /* Some Vista and Win7 */ "Expected RPC_S_SERVER_UNAVAILABLE, got %d\n", GetLastError()); /* Empty servername should be read as local server */ handle = OpenBackupEventLogA("", backup); ok(handle != NULL, "Expected a handle\n"); CloseEventLog(handle); handle = OpenBackupEventLogA(NULL, backup); ok(handle != NULL, "Expected a handle\n"); /* Can we open that same backup eventlog more than once? */ handle2 = OpenBackupEventLogA(NULL, backup); ok(handle2 != NULL, "Expected a handle\n"); ok(handle2 != handle, "Didn't expect the same handle\n"); CloseEventLog(handle2); CloseEventLog(handle); DeleteFileA(backup); } /* Is there any content checking done? */ file = CreateFileA(backup, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL); CloseHandle(file); SetLastError(0xdeadbeef); handle = OpenBackupEventLogA(NULL, backup); ok(handle == NULL, "Didn't expect a handle\n"); ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY || GetLastError() == ERROR_EVENTLOG_FILE_CORRUPT, /* Vista and Win7 */ "Expected ERROR_NOT_ENOUGH_MEMORY, got %d\n", GetLastError()); CloseEventLog(handle); DeleteFileA(backup); file = CreateFileA(backup, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL); WriteFile(file, text, sizeof(text), &written, NULL); CloseHandle(file); SetLastError(0xdeadbeef); handle = OpenBackupEventLogA(NULL, backup); ok(handle == NULL, "Didn't expect a handle\n"); ok(GetLastError() == ERROR_EVENTLOG_FILE_CORRUPT, "Expected ERROR_EVENTLOG_FILE_CORRUPT, got %d\n", GetLastError()); CloseEventLog(handle); DeleteFileA(backup); }
void Base::manage(const char* file_name) { string choice; do{ cout << "SubMenu:" << endl << "1 Insert prefix entry" << endl << "2 Delete prefix entry" << endl << "3 Search prefix" << endl << "4 Create a back up" << endl << "5 Return to main Menu" << endl; getline(cin, choice); int ch = atoi(choice.c_str()); switch(ch){ // insert a prefix along its meaning and its example case 1: { Entry e; if(e.set_prefix(file_name)) if(e.set_meaning()) if(e.set_example()) { if(Base::insert_entry(file_name, e.prefix, e.meaning, e.example)) cout << "Successful insertion" << endl <<endl; else cout << "Unsuccessful insertion" << endl; } break; } case 2: { // Delete a prefix entry delete_entry(file_name); break; } case 3: { string prefix; ifstream file; try{ file.open(file_name, ios::in); int flag = 0; do { cout << "Give the prefix:"; getline(cin, prefix); if(check_string(prefix)) { cout << "Invalid prefix ( !@#$%^&*()-=_+1234567890 )" << endl; flag = 1; continue; } if(srch_prefix(file, prefix)) cout << "Prefix \"" << prefix << "\" exists"<< endl; else cout << "Prefix \"" << prefix << "\" doesn't exist" << endl; }while(flag); } catch(exception& e){ cerr << "Error upon searching entry: " << e.what() << endl; } break; } case 4: { create_backup(file_name); break; } default: break; } }while(choice.compare("5") != 0); }
int main(int argc, char *argv[]) { char *password_file = NULL; char *username = NULL; bool create_new = false; bool delete_user = false; FILE *fptr, *ftmp; char password[MAX_BUFFER_LEN]; int rc; bool do_update_file = false; char *backup_file; signal(SIGINT, handle_sigint); signal(SIGTERM, handle_sigint); OpenSSL_add_all_digests(); if(argc == 4){ if(!strcmp(argv[1], "-c")){ create_new = true; }else if(!strcmp(argv[1], "-D")){ delete_user = true; } password_file = argv[2]; username = argv[3]; }else if(argc == 3){ if(!strcmp(argv[1], "-U")){ do_update_file = true; password_file = argv[2]; }else{ password_file = argv[1]; username = argv[2]; } }else{ print_usage(); return 1; } if(create_new){ rc = get_password(password, 1024); if(rc) return rc; fptr = fopen(password_file, "wt"); if(!fptr){ fprintf(stderr, "Error: Unable to open file %s for writing. %s.\n", password_file, strerror(errno)); return 1; } rc = output_new_password(fptr, username, password); fclose(fptr); return rc; }else{ fptr = fopen(password_file, "r+t"); if(!fptr){ fprintf(stderr, "Error: Unable to open password file %s. %s.\n", password_file, strerror(errno)); return 1; } backup_file = malloc(strlen(password_file)+5); snprintf(backup_file, strlen(password_file)+5, "%s.tmp", password_file); if(create_backup(backup_file, fptr)){ fclose(fptr); free(backup_file); return 1; } ftmp = tmpfile(); if(!ftmp){ fprintf(stderr, "Error: Unable to open temporary file. %s.\n", strerror(errno)); fclose(fptr); free(backup_file); return 1; } if(delete_user){ rc = delete_pwuser(fptr, ftmp, username); }else if(do_update_file){ rc = update_file(fptr, ftmp); }else{ rc = get_password(password, 1024); if(rc){ fclose(fptr); fclose(ftmp); unlink(backup_file); free(backup_file); return rc; } /* Update password for individual user */ rc = update_pwuser(fptr, ftmp, username, password); } if(rc){ fclose(fptr); fclose(ftmp); unlink(backup_file); free(backup_file); return rc; } if(copy_contents(ftmp, fptr)){ fclose(fptr); fclose(ftmp); fprintf(stderr, "Error occurred updating password file.\n"); fprintf(stderr, "Password file may be corrupt, check the backup file: %s.\n", backup_file); free(backup_file); return 1; } fclose(fptr); fclose(ftmp); /* Everything was ok so backup no longer needed. May contain old * passwords so shouldn't be kept around. */ unlink(backup_file); free(backup_file); } return 0; }
int commonio_close(struct commonio_db *db) { char buf[1024]; int errors = 0; struct stat sb; if (!db->isopen) { errno = EINVAL; return 0; } db->isopen = 0; if (!db->changed || db->readonly) { fclose(db->fp); db->fp = NULL; goto success; } memzero(&sb, sizeof sb); if (db->fp) { if (fstat(fileno(db->fp), &sb)) { fclose(db->fp); db->fp = NULL; goto fail; } /* * Create backup file. */ snprintf(buf, sizeof buf, "%s-", db->filename); if (create_backup(buf, db->fp)) errors++; if (fclose(db->fp)) errors++; if (errors) { db->fp = NULL; goto fail; } } else { /* * Default permissions for new [g]shadow files. * (passwd and group always exist...) */ sb.st_mode = 0400; sb.st_uid = 0; sb.st_gid = 0; } snprintf(buf, sizeof buf, "%s+", db->filename); db->fp = fopen_set_perms(buf, "w", &sb); if (!db->fp) goto fail; if (write_all(db)) errors++; if (fflush(db->fp)) errors++; #ifdef HAVE_FSYNC if (fsync(fileno(db->fp))) errors++; #else sync(); #endif if (fclose(db->fp)) errors++; db->fp = NULL; if (errors) { unlink(buf); goto fail; } if (rename(buf, db->filename)) goto fail; nscd_need_reload = 1; success: free_linked_list(db); return 1; fail: free_linked_list(db); return 0; }
int main(int argc, char **argv) { struct dirent **namelist; // struct dirent **backuplist; struct dirent **list; int m, n, i, b, j = 0; pthread_t tid[MAX_T]; struct stat buf; struct stat backup_buf; char *aux_filename; aux_filename = (char *)calloc(FILE_NAME_SIZE,sizeof(char)); int *tret; if (argc == 1) { n = scandir(".", &namelist, NULL, alphasort); m = scandir("..", &list, NULL, alphasort); i = 2; while(i < m){ if(!strcmp(list[i]->d_name, "BACKUP")) break; else{ if(i == (m - 1)){ create_backup(backup_path); i++; } else{ i++; continue; } } } } else { n = scandir(argv[1], &namelist, NULL, alphasort); // s = create_backup_b(argv[1]); } if (n < 0) perror("scandir()"); else { i = 2; while (i < n) { stat(namelist[i]->d_name, &buf); b = busca(namelist[i]->d_name); if(b < 0){ perror("busca()"); } else if(b == 0){ printf("\n1 - copiando arquivo: %s\n", namelist[i]->d_name); pthread_create(&tid[i], NULL, &thread_copia, &namelist[i]->d_name); pthread_join(tid[i], (void **)&tret); j++; } else if(b > 0){ printf("copiando %s - em %s\n", backup_path, aux_filename); strcpy(aux_filename, backup_path); printf("concatenando %s - em %s\n", namelist[i]->d_name, aux_filename); strcat(aux_filename, namelist[i]->d_name); printf("resultando em %s\n", aux_filename); stat(aux_filename, &backup_buf); if(buf.st_mtime != backup_buf.st_mtime){ printf("\n2 - copiando arquivo: %s\n", namelist[i]->d_name); pthread_create(&tid[i], NULL, &thread_copia, &namelist[i]->d_name); pthread_join(tid[i], (void **)&tret); } } free(namelist[i]); i++; } free(namelist); } return 0; }