/** * Returns the database. If this function returns false, this has not * succeeded, and the caller should create the database after the * process has been daemonized. */ static bool glue_db_init_and_load(void) { const struct config_param *path = config_get_param(CONF_DB_FILE); GError *error = NULL; bool ret; if (!mapper_has_music_directory()) { if (path != NULL) g_message("Found " CONF_DB_FILE " setting without " CONF_MUSIC_DIR " - disabling database"); db_init(NULL, NULL); return true; } if (path == NULL) MPD_ERROR(CONF_DB_FILE " setting missing"); if (!db_init(path, &error)) MPD_ERROR("%s", error->message); ret = db_load(&error); if (!ret) MPD_ERROR("%s", error->message); /* run database update after daemonization? */ return db_exists(); }
static VALUE db_search(VALUE self, VALUE database_name, VALUE q_mol, VALUE block) { char * filename; struct CompoundDB db; struct Query query; filename = StringValuePtr(database_name); if(strlen(filename) > 40){ rb_raise(rb_eException, "length of database name must less than 40!"); } query_setup(q_mol, & query); db.mat = db_file_open(filename, ".mat"); db.idx = db_file_open(filename, ".idx"); db.typ = db_file_open(filename, ".typ"); db_load(& db, & query); query_free(& query); fclose(db.mat); fclose(db.idx); fclose(db.typ); }
bool DB::Load(const string& filename) { if (!Empty()) { config_.Log(Error, "internal usage error: DB::read on a non-empty db!\n"); return false; } return db_load(this, filename); }
int main(int argc, char **argv) { struct state state; memset(&state, 0, sizeof(struct state)); if (rc_read_options(&state, argc, argv) < 0) return EXIT_FAILURE; switch (state.rc.action) { case ACTION_NONE: /* this should never happen... */ assert(0); break; case ACTION_USAGE: display_usage(); break; case ACTION_VERSION: display_version(); break; case ACTION_ANALYZE: case ACTION_RANK: case ACTION_PREDICT: db_load(&state); break; } return EXIT_SUCCESS; }
struct DB *dbcreate(char *file, struct DBC *config) { struct DB *db = (struct DB *)calloc(1, sizeof(struct DB)); int db_exists = access(file, F_OK); int dbmeta_exists = meta_check(file); if (db_exists == 0) { check(dbmeta_exists == 0, "No metafile exists, but DB exists. Exiting"); db_load(db, file, config->cache_size); } else { db_init(db, file, config->page_size, config->pool_size, config->cache_size); } return db; error: exit(-1); }
int main(int argc, char **argv) { struct termios ttystate, ttysave; char c; srand(time(NULL)); tcgetattr(STDIN_FILENO, &ttystate); ttysave=ttystate; ttystate.c_lflag&=~(ICANON); ttystate.c_cc[VMIN] = 1; tcsetattr(STDIN_FILENO, TCSANOW, &ttystate); db_load(); puts("\"People\" 2013 Axel Isaksson"); print_menu(); for(;;) { putchar('>'); c=getchar(); putchar('\n'); switch(c) { case '1': case '2': case '3': case '4': case '5': menu[c-'1'].action(); case '\n': print_menu(); break; case '6': case 'q': case 'Q': goto quit; case 's': case 'S': db_save(); break; default: puts("?"); } } quit: db_save(); tcsetattr(STDIN_FILENO, TCSANOW, &ttysave); return 0; }
struct DB *db_touch(const char *const meta_dir, const char *const cm_conf_fn) { // cm conf assert(cm_conf_fn); // TODO: free cm_conf at later time struct ContainerMapConf *const cm_conf = db_load_cm_conf(cm_conf_fn); assert(cm_conf); // touch dir assert(meta_dir); const int r_dir = access(meta_dir, F_OK); struct DB *db = NULL; if (r_dir == 0) { // has dir // try load DB db = db_load(meta_dir, cm_conf); } // create anyway if (!db) { db = db_create(meta_dir, cm_conf); } if (db) { db_spawn_threads(db); } return db; }
/** * Returns the database. If this function returns false, this has not * succeeded, and the caller should create the database after the * process has been daemonized. */ static bool glue_db_init_and_load(void) { const char *path = config_get_path(CONF_DB_FILE); bool ret; GError *error = NULL; if (!mapper_has_music_directory()) { if (path != NULL) g_message("Found " CONF_DB_FILE " setting without " CONF_MUSIC_DIR " - disabling database"); db_init(NULL); return true; } if (path == NULL) g_error(CONF_DB_FILE " setting missing"); db_init(path); ret = db_load(&error); if (!ret) { g_warning("Failed to load database: %s", error->message); g_error_free(error); if (!db_check()) exit(EXIT_FAILURE); db_clear(); /* run database update after daemonization */ return false; } return true; }
/* 同步读取 master 发送的 db 数据 */ void repl_syncReadDB(eventloop* el, int fd, void *data, int mask){ clientContext *cc = (clientContext*)data; size_t nbytes; int off = 0; // 读取 master 传输的 db 数据 nbytes = recv(fd, cc->recvbuf+cc->bytesrecved, RECVBUF_LEN-cc->bytesrecved, 0); if (nbytes == -1){ if (errno==EAGAIN || errno==EINTR){ nbytes = 0; } else { xlog(LOG_DEBUG, "server recv: %s\n", "数据接收失败"); cc_freeClient(cc); goto werr; } } else if (nbytes == 0){ cc_freeClient(cc); goto werr; } // 设置接收到的字节数 cc->bytesrecved += nbytes; // 第一次获得传送 db 的大小 if (server.repl_dbsize == (uint32_t)-1){ if(cc->bytesrecved < sizeof(request_header)) return; net_getHeader(cc); off = sizeof(request_header); server.repl_dbsize = cc->reqheader.bodylen; server.repl_dbkvcount = cc->reqheader.kvcount; xlog(LOG_INFO, "db recv: 待同步 db 文件信息 kvcount=%d, dbsize=%d\n", server.repl_dbkvcount, server.repl_dbsize); memmove(cc->recvbuf, cc->recvbuf+sizeof(request_header), cc->bytesrecved-sizeof(request_header)); cc->bytesrecved -= sizeof(request_header); } // 计算需要写到 db 文件中的数据大小, 避免将非 db 中的数据写到db文件中 int nwrite = cc->bytesrecved<server.repl_dbsize?cc->bytesrecved:server.repl_dbsize; if (write(server.repl_transfer_fd, cc->recvbuf, nwrite) != nwrite){ xlog(LOG_WARN, "sync db; 同步写 db 文件错误 %s\n", strerror(errno)); cc_freeClient(cc); goto werr; } cc->bytesrecved -= nwrite; memmove(cc->recvbuf, cc->recvbuf+nwrite, cc->bytesrecved); server.repl_dbsize -= nwrite; if(server.repl_dbsize == 0){ // db 文件同步完成 if(rename(server.repl_transfer_tmpfile, server.dbfilename) < 0){ xlog(LOG_WARN, "rename dbfile: 重命名 db 文件错误 %s\n", strerror(errno)); cc_freeClient(cc); goto werr; } // empty dict dict_clear(server.db); multi_deleteFileEvent(server.el, cc->fd, MULTI_READABLE); // 加载 db 文件 if (db_load(server.dbfilename) < 0){ xlog(LOG_WARN, "db load: db 文件加载失败\n"); cc_freeClient(cc); goto werr; } sys_free(server.repl_transfer_tmpfile); close(server.repl_transfer_fd); server.master = cc; server.master->flag |= RWCACHED_MASTER; server.repl_state = REPL_CONNECTED; if (multi_createFileEvent(server.el, cc->fd, MULTI_READABLE, readDataFromClient, cc) < 0){ xlog(LOG_WARN, "add event: 创建同步读取事件失败\n"); goto werr; } xlog(LOG_INFO, "master <-->slave 建立同步成功\n"); } return; werr: multi_deleteFileEvent(server.el, server.repl_transfer_s, MULTI_READABLE); close(server.repl_transfer_s); close(server.repl_transfer_fd); unlink(server.repl_transfer_tmpfile); sys_free(server.repl_transfer_tmpfile); server.repl_state = REPL_CONNECT; return; }
int main(int argc, char *argv[]) { bool have_conf = false; bool have_log = false; char buf[32]; int i, pid, r; FILE *pid_file; const char *pidfilename = RUNDIR "/atheme.pid"; #ifdef HAVE_GETRLIMIT struct rlimit rlim; #endif curr_uplink = NULL; mowgli_init(); /* Prepare gettext */ #ifdef ENABLE_NLS setlocale(LC_ALL, ""); bindtextdomain(PACKAGE_NAME, LOCALEDIR); textdomain(PACKAGE_NAME); #endif /* change to our local directory */ if (chdir(PREFIX) < 0) { perror(PREFIX); return 20; } #ifdef HAVE_GETRLIMIT /* it appears certian systems *ahem*linux*ahem* * don't dump cores by default, so we do this here. */ if (!getrlimit(RLIMIT_CORE, &rlim)) { rlim.rlim_cur = rlim.rlim_max; setrlimit(RLIMIT_CORE, &rlim); } #endif /* do command-line options */ while ((r = getopt(argc, argv, "c:dhrl:np:v")) != -1) { switch (r) { case 'c': config_file = sstrdup(optarg); have_conf = true; break; case 'd': log_force = true; break; case 'h': print_help(); exit(EXIT_SUCCESS); break; case 'r': readonly = true; break; case 'l': log_path = sstrdup(optarg); have_log = true; break; case 'n': runflags |= RF_LIVE; break; case 'p': pidfilename = optarg; break; case 'v': print_version(); exit(EXIT_SUCCESS); break; default: printf("usage: atheme [-dhnvr] [-c conf] [-l logfile] [-p pidfile]\n"); exit(EXIT_SUCCESS); break; } } if (!have_conf) config_file = sstrdup(SYSCONFDIR "/atheme.conf"); if (!have_log) log_path = sstrdup(LOGDIR "/atheme.log"); cold_start = true; runflags |= RF_STARTING; me.kline_id = 0; me.start = time(NULL); CURRTIME = me.start; srand(arc4random()); me.execname = argv[0]; /* set signal handlers */ init_signal_handlers(); /* initialize strshare */ strshare_init(); /* open log */ log_open(); mowgli_log_set_cb(process_mowgli_log); slog(LG_INFO, "%s is starting up...", PACKAGE_STRING); /* check for pid file */ if ((pid_file = fopen(pidfilename, "r"))) { if (fgets(buf, 32, pid_file)) { pid = atoi(buf); if (!kill(pid, 0)) { fprintf(stderr, "atheme: daemon is already running\n"); exit(EXIT_FAILURE); } } fclose(pid_file); } #if HAVE_UMASK /* file creation mask */ umask(077); #endif event_init(); hooks_init(); init_netio(); init_socket_queues(); db_init(); translation_init(); #ifdef ENABLE_NLS language_init(); #endif init_nodes(); init_confprocess(); init_newconf(); servtree_init(); modules_init(); pcommand_init(); conf_init(); if (!conf_parse(config_file)) { slog(LG_ERROR, "Error loading config file %s, aborting", config_file); exit(EXIT_FAILURE); } if (config_options.languagefile) { slog(LG_DEBUG, "Using language: %s", config_options.languagefile); if (!conf_parse(config_options.languagefile)) slog(LG_INFO, "Error loading language file %s, continuing", config_options.languagefile); } authcookie_init(); common_ctcp_init(); if (!backend_loaded && authservice_loaded) { slog(LG_ERROR, "atheme: no backend modules loaded, see your configuration file."); exit(EXIT_FAILURE); } /* we've done the critical startup steps now */ cold_start = false; /* load our db */ if (db_load) db_load(); else if (backend_loaded) { slog(LG_ERROR, "atheme: backend module does not provide db_load()!"); exit(EXIT_FAILURE); } db_check(); #ifdef HAVE_FORK /* fork into the background */ if (!(runflags & RF_LIVE)) { close(0); if (open("/dev/null", O_RDWR) != 0) { slog(LG_ERROR, "unable to open /dev/null??"); exit(EXIT_FAILURE); } if ((i = fork()) < 0) { slog(LG_ERROR, "can't fork into the background"); exit(EXIT_FAILURE); } /* parent */ else if (i != 0) { slog(LG_INFO, "pid %d", i); slog(LG_INFO, "running in background mode from %s", PREFIX); exit(EXIT_SUCCESS); } /* parent is gone, just us now */ if (setsid() < 0) { slog(LG_ERROR, "unable to create new session: %s", strerror(errno)); exit(EXIT_FAILURE); } dup2(0, 1); dup2(0, 2); } else { slog(LG_INFO, "pid %d", getpid()); slog(LG_INFO, "running in foreground mode from %s", PREFIX); } #else slog(LG_INFO, "running in foreground mode from %s", PREFIX); #endif #ifdef HAVE_GETPID /* write pid */ if ((pid_file = fopen(pidfilename, "w"))) { fprintf(pid_file, "%d\n", getpid()); fclose(pid_file); } else { fprintf(stderr, "atheme: unable to write pid file\n"); exit(EXIT_FAILURE); } #endif /* no longer starting */ runflags &= ~RF_STARTING; /* we probably have a few open already... */ me.maxfd = 3; /* DB commit interval is configurable */ if (db_save && !readonly) event_add("db_save", db_save, NULL, config_options.commit_interval); /* check expires every hour */ event_add("expire_check", expire_check, NULL, 3600); /* check k/x/q line expires every minute */ event_add("kline_expire", kline_expire, NULL, 60); event_add("xline_expire", xline_expire, NULL, 60); event_add("qline_expire", qline_expire, NULL, 60); /* check authcookie expires every ten minutes */ event_add("authcookie_expire", authcookie_expire, NULL, 600); /* reseed rng a little every five minutes */ event_add("rng_reseed", rng_reseed, NULL, 293); me.connected = false; uplink_connect(); /* main loop */ io_loop(); /* we're shutting down */ hook_call_shutdown(); if (db_save && !readonly) db_save(NULL); remove(pidfilename); errno = 0; if (curr_uplink != NULL && curr_uplink->conn != NULL) sendq_flush(curr_uplink->conn); connection_close_all(); me.connected = false; /* should we restart? */ if (runflags & RF_RESTART) { slog(LG_INFO, "main(): restarting"); #ifdef HAVE_EXECVE execv(BINDIR "/atheme-services", argv); #endif } slog(LG_INFO, "main(): shutting down"); log_shutdown(); return 0; }
void ProviderDialog::loadProvider(QString provider) { _provider = provider; db_load(); }
int main (int argc, char **argv) { int c, i; opterr = 0; bool iactive = false; strcpy (homedir, argv[0]); for (i = strlen (homedir)-1; i >= 0; i--) { bool ended = false; if (homedir[i] == '/') ended = true; homedir[i] = '\0'; if (ended) break; } strcpy (demuxer, DEFAULT_DEMUXER); strcpy (provider, DEFAULT_OTV_PROVIDER); while ((c = getopt (argc, argv, "h:d:x:l:p:k:riyz")) != -1) { switch (c) { case 'd': db_root = optarg; break; case 'x': strcpy (demuxer, optarg); break; case 'l': strcpy (homedir, optarg); break; case 'i': printf ("WARNING! Option -i is deprecated\n"); break; case 'p': strcpy (provider, optarg); break; case 'k': nice (atoi(optarg)); break; case 'r': log_disable (); interactive_enable (); iactive = true; break; case 'y': huffman_debug_summaries = true; break; case 'z': huffman_debug_titles = true; break; case '?': printf ("Usage:\n"); printf (" ./crossepg_downloader [options]\n"); printf ("Options:\n"); printf (" -d db_root crossepg db root folder\n"); printf (" default: %s\n", db_root); printf (" -x demuxer dvb demuxer\n"); printf (" default: %s\n", demuxer); printf (" -l homedir home directory\n"); printf (" default: %s\n", homedir); printf (" -p provider opentv provider\n"); printf (" default: %s\n", provider); printf (" -k nice see \"man nice\"\n"); printf (" -r interactive mode\n"); printf (" -y debug mode for huffman dictionary (summaries)\n"); printf (" -z debug mode for huffman dictionary (titles)\n"); printf (" -h show this help\n"); return 0; } } while (homedir[strlen (homedir) - 1] == '/') homedir[strlen (homedir) - 1] = '\0'; while (db_root[strlen (db_root) - 1] == '/') db_root[strlen (db_root) - 1] = '\0'; mkdir (db_root, S_IRWXU|S_IRWXG|S_IRWXO); log_open (db_root); log_banner ("CrossEPG Downloader"); xmltv_encodings_init (); if (iactive) interactive_manager (); else { char opentv_file[256]; sprintf (opentv_file, "%s/providers/%s.conf", homedir, provider); if (providers_read (opentv_file)) { if (providers_get_protocol () == 1) { log_add ("Provider %s identified as opentv", provider); if (!db_load ()) goto error; download_opentv (); if (epgdb_save (NULL)) log_add ("Data saved"); else log_add ("Error saving data"); db_close (); } else if (providers_get_protocol () == 2) { log_add ("Provider %s identified as xmltv", provider); log_add ("Preferred language: %s", providers_get_xmltv_plang ()); if (!db_load ()) goto error; xmltv_channels_init (); for (i=0; i<10; i++) { if (strlen(providers_get_xmltv_channels (i)) == 0) { log_add ("No more url available"); log_add ("Error downloading/parsing channels file"); goto error; } log_add ("Download channels from url: %s (%d)", providers_get_xmltv_channels (i), i); if (xmltv_downloader_channels (providers_get_xmltv_channels (i), db_root, NULL, NULL, &stop)) break; } xmltv_parser_set_iso639 (providers_get_xmltv_plang ()); for (i=0; i<10; i++) { if (strlen(providers_get_xmltv_url (i)) == 0) { log_add ("No more url available"); log_add ("Error downloading/parsing events file"); goto error; } log_add ("Download events from url: %s", providers_get_xmltv_url (i)); if (xmltv_downloader_events (providers_get_xmltv_url (i), db_root, NULL, NULL, &stop)) { if (epgdb_save (NULL)) log_add ("Data saved"); else log_add ("Error saving data"); break; } } xmltv_channels_cleanup (); db_close (); } else if (providers_get_protocol () == 3) { log_add ("Provider %s identified as xepgdb", provider); log_add ("Headers url: %s", providers_get_xepgdb_headers_url ()); log_add ("Descriptors url: %s", providers_get_xepgdb_descriptors_url ()); if (!db_load ()) goto error; if (dbmerge_downloader (providers_get_xepgdb_headers_url (), providers_get_xepgdb_descriptors_url (), db_root, NULL, NULL, &stop)) { if (epgdb_save (NULL)) log_add ("Data saved"); else log_add ("Error saving data"); } else log_add ("Error downloading/parsing xepgdb files"); db_close (); } else if (providers_get_protocol () == 4) { char filename[1024], tmp[1024], *tmp2; log_add ("Provider %s identified as script", provider); log_add ("Script file name: %s", providers_get_script_filename ()); tmp2 = replace_str (providers_get_script_arguments (), "%%dbroot%%", db_root); strcpy (tmp, tmp2); tmp2 = replace_str (tmp, "%%homedir%%", homedir); sprintf (filename, "LD_LIBRARY_PATH=%s %s/scripts/%s %s", homedir, homedir, providers_get_script_filename (), tmp2); log_add ("Executing script %s ...", filename); system (filename); log_add ("Script terminated"); } } else log_add ("Cannot load provider configuration (%s)", opentv_file); } memory_stats (); error: log_close (); return 0; }
void *interactive (void *args) { char buffer[4096], byte; bool run = true; pthread_t thread; interactive_send (ACTION_READY); while (run) { int i = 0, size = 0; memset (buffer, '\0', 4096); while ((size = fread (&byte, 1, 1, stdin))) { if (byte == '\n') break; buffer[i] = byte; i++; } if (memcmp (buffer, CMD_QUIT, strlen (CMD_QUIT)) == 0 || quit || size == 0) { log_add ("Interactive: QUIT cmd received"); run = false; stop = true; } else if (memcmp (buffer, CMD_OPEN, strlen (CMD_OPEN)) == 0) { log_add ("Interactive: OPEN cmd received"); if (!db_load ()) { interactive_send_text (ACTION_ERROR, "cannot open crossepg database"); log_add ("Interactive: ERROR action sent (cannot open crossepg database)"); } else { interactive_send (ACTION_OK); log_add ("Interactive: OK action sent"); } } else if (memcmp (buffer, CMD_CLOSE, strlen (CMD_CLOSE)) == 0) { log_add ("Interactive: CLOSE cmd received"); db_close (); interactive_send (ACTION_OK); log_add ("Interactive: OK action sent"); } else if (memcmp (buffer, CMD_DEMUXER, strlen (CMD_DEMUXER)) == 0) { log_add ("Interactive: DEMUXER cmd received"); if (!exec) { if (strlen (buffer) > strlen (CMD_DEMUXER)+1) { strcpy (demuxer, buffer + strlen (CMD_DEMUXER)+1); interactive_send (ACTION_OK); log_add ("Interactive: OK action sent"); } else { interactive_send_text (ACTION_ERROR, "required one parameter"); log_add ("Interactive: ERROR action sent (required one parameter)"); } } else { interactive_send_text (ACTION_ERROR, "cannot do it... other operations in background"); log_add ("Interactive: ERROR action sent (cannot do it... other operations in background)"); } timeout_enable = true; } else if (memcmp (buffer, CMD_DOWNLOAD, strlen (CMD_DOWNLOAD)) == 0) { log_add ("Interactive: DOWNLOAD cmd received"); if (!exec) { if (strlen (buffer) > strlen (CMD_DOWNLOAD)+1) { strcpy (provider, buffer + strlen (CMD_DOWNLOAD)+1); stop = false; exec = true; pthread_create (&thread, NULL, download, NULL); } else { interactive_send_text (ACTION_ERROR, "required one parameter"); log_add ("Interactive: ERROR action sent (required one parameter)"); } } else { interactive_send_text (ACTION_ERROR, "cannot do it... other operations in background"); log_add ("Interactive: ERROR action sent (cannot do it... other operations in background)"); } timeout_enable = true; } else if (memcmp (buffer, CMD_WAIT, strlen (CMD_WAIT)) == 0) { log_add ("Interactive: WAIT cmd received"); timeout_enable = false; } else if (memcmp (buffer, CMD_SAVE, strlen (CMD_SAVE)) == 0) { log_add ("Interactive: SAVE cmd received"); if (!exec) { timeout_enable = false; interactive_send (ACTION_START); interactive_send_text (ACTION_PROGRESS, "ON"); if (!epgdb_save (progress_callback)) { interactive_send_text (ACTION_ERROR, "cannot save data"); log_add ("Interactive: ERROR action sent (cannot save data)"); } interactive_send (ACTION_END); interactive_send_text (ACTION_PROGRESS, "OFF"); } else { interactive_send_text (ACTION_ERROR, "cannot do it... other operations in background"); log_add ("Interactive: ERROR action sent (cannot do it... other operations in background)"); } timeout_enable = true; } else if (memcmp (buffer, CMD_STOP, strlen (CMD_STOP)) == 0) { log_add ("Interactive: STOP cmd received"); stop = true; timeout_enable = true; } else { interactive_send_text (ACTION_ERROR, "unknow command"); log_add ("Interactive: ERROR action sent (unknow command)"); timeout_enable = true; } pthread_mutex_lock (&mutex); timeout = 0; pthread_mutex_unlock (&mutex); } quit = true; if (exec) pthread_join (thread, NULL); return NULL; }
void process_commands(enum CID cid) { cursor_t old_c; char * dbName; switch (cid) { case NOPE: puts("-- команда не распознана --"); break; case SET_NAME: if (db_ship()) { printf("Имя>"); strncpy(db_ship()->name,input_str(),64); show(); } break; case SET_YEAR: if (db_ship()) { printf("Год>"); db_ship()->year = strtoul( input_str(),NULL, 10); show(); } break; case COPY: db_copy(); break; case PASTE: db_paste(); show(); break; case ALL: old_c = db_get_cursor(); db_first(); while (db_get_cursor() != db_get_end()) { db_load(); if (!db_is_deleted()) show(); db_next(); } db_set_cursor(old_c); db_load(); break; case REFRESH: db_load(); case SHOW: show(); break; case NEW: db_new(); show(); break; case REMOVE: db_delete(); break; case SAVE: db_save(); break; case HELP: show_help(); break; case OPEN: printf("Файл>"); dbName = input_str(); db_open( strlen(dbName) == 0 ? "ships.db" : dbName); break; case CLOSE: db_close(); break; case FIRST: first: db_first(); while (db_get_cursor() != db_get_end()) { db_load(); if (!db_is_deleted()) { show(); return; } db_next(); } puts("-- в базе нет записей. --"); break; case NEXT: db_next(); while (db_get_cursor() != db_get_end()) { db_load(); if (!db_is_deleted()) { show(); return; } db_next(); } puts("-- больше записей нет. перемотка в начало. --"); goto first; case PREV: while (db_get_cursor() != db_get_begin()) { db_prev(); db_load(); if (!db_is_deleted()) { show(); return; } } puts("-- первая запись --"); goto first; break; default: puts("== команда не обработана =="); } }
#include <stdarg.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include "crypto.h" #include "db.h" #include "test.h" static uint8_t kek[KEY_LEN] = { 0 }; idx *db_update(uint8_t *id, entry *entry, bool delete) { kdfp kdfp = { .N = 2, .r = 1, .p = 1}; idx *idx = db_load(kek, &kdfp); update_db(idx, kek, &kdfp, id, entry, delete); close_index(idx); return db_load(kek, &kdfp); } idx *db_add_entry(char *text, uint8_t *id) { uint32_t line; entry *e = parse_entry((uint8_t *) text, strlen(text), &line); randombytes(id, ID_LEN); idx *idx = db_update(id, e, false); free(e); return idx; } idx *db_update_entry(char *text, uint8_t *id) {
int atheme_main(int argc, char *argv[]) { int daemonize_pipe[2]; bool have_conf = false; bool have_log = false; bool have_datadir = false; char buf[32]; int pid, r; FILE *pid_file; const char *pidfilename = RUNDIR "/atheme.pid"; char *log_p = NULL; mowgli_getopt_option_t long_opts[] = { { NULL, 0, NULL, 0, 0 }, }; atheme_bootstrap(); /* do command-line options */ while ((r = mowgli_getopt_long(argc, argv, "c:dhrl:np:D:v", long_opts, NULL)) != -1) { switch (r) { case 'c': config_file = sstrdup(mowgli_optarg); have_conf = true; break; case 'd': log_force = true; break; case 'h': print_help(); exit(EXIT_SUCCESS); break; case 'r': readonly = true; break; case 'l': log_p = sstrdup(mowgli_optarg); have_log = true; break; case 'n': runflags |= RF_LIVE; break; case 'p': pidfilename = mowgli_optarg; break; case 'D': datadir = mowgli_optarg; have_datadir = true; break; case 'v': print_version(); exit(EXIT_SUCCESS); break; default: printf("usage: atheme [-dhnvr] [-c conf] [-l logfile] [-p pidfile]\n"); exit(EXIT_FAILURE); break; } } if (!have_conf) config_file = sstrdup(SYSCONFDIR "/atheme.conf"); if (!have_log) log_p = sstrdup(LOGDIR "/atheme.log"); if (!have_datadir) datadir = sstrdup(DATADIR); cold_start = true; runflags |= RF_STARTING; atheme_init(argv[0], log_p); slog(LG_INFO, "%s is starting up...", PACKAGE_STRING); /* check for pid file */ #ifndef MOWGLI_OS_WIN if ((pid_file = fopen(pidfilename, "r"))) { if (fgets(buf, 32, pid_file)) { pid = atoi(buf); if (!kill(pid, 0)) { fprintf(stderr, "atheme: daemon is already running\n"); exit(EXIT_FAILURE); } } fclose(pid_file); } #endif if (!(runflags & RF_LIVE)) daemonize(daemonize_pipe); atheme_setup(); conf_init(); if (!conf_parse(config_file)) { slog(LG_ERROR, "Error loading config file %s, aborting", config_file); exit(EXIT_FAILURE); } if (config_options.languagefile) { slog(LG_DEBUG, "Using language: %s", config_options.languagefile); if (!conf_parse(config_options.languagefile)) slog(LG_INFO, "Error loading language file %s, continuing", config_options.languagefile); } if (!backend_loaded && authservice_loaded) { slog(LG_ERROR, "atheme: no backend modules loaded, see your configuration file."); exit(EXIT_FAILURE); } /* we've done the critical startup steps now */ cold_start = false; /* load our db */ if (db_load) db_load(NULL); else if (backend_loaded) { slog(LG_ERROR, "atheme: backend module does not provide db_load()!"); exit(EXIT_FAILURE); } db_check(); #ifdef HAVE_GETPID /* write pid */ if ((pid_file = fopen(pidfilename, "w"))) { fprintf(pid_file, "%d\n", getpid()); fclose(pid_file); } else { fprintf(stderr, "atheme: unable to write pid file\n"); exit(EXIT_FAILURE); } #endif /* detach from terminal */ if ((runflags & RF_LIVE) || !detach_console(daemonize_pipe)) { #ifdef HAVE_GETPID slog(LG_INFO, "pid %d", getpid()); #endif slog(LG_INFO, "running in foreground mode from %s", PREFIX); } /* no longer starting */ runflags &= ~RF_STARTING; /* we probably have a few open already... */ me.maxfd = 3; /* DB commit interval is configurable */ if (db_save && !readonly) mowgli_timer_add(base_eventloop, "db_save", db_save, NULL, config_options.commit_interval); /* check expires every hour */ mowgli_timer_add(base_eventloop, "expire_check", expire_check, NULL, 3600); /* check k/x/q line expires every minute */ mowgli_timer_add(base_eventloop, "kline_expire", kline_expire, NULL, 60); mowgli_timer_add(base_eventloop, "xline_expire", xline_expire, NULL, 60); mowgli_timer_add(base_eventloop, "qline_expire", qline_expire, NULL, 60); /* check authcookie expires every ten minutes */ mowgli_timer_add(base_eventloop, "authcookie_expire", authcookie_expire, NULL, 600); /* reseed rng a little every five minutes */ mowgli_timer_add(base_eventloop, "rng_reseed", rng_reseed, NULL, 293); me.connected = false; uplink_connect(); /* main loop */ io_loop(); /* we're shutting down */ hook_call_shutdown(); if (db_save && !readonly) db_save(NULL); remove(pidfilename); errno = 0; if (curr_uplink != NULL && curr_uplink->conn != NULL) sendq_flush(curr_uplink->conn); connection_close_all(); me.connected = false; /* should we restart? */ if (runflags & RF_RESTART) { slog(LG_INFO, "main(): restarting"); #ifdef HAVE_EXECVE execv(BINDIR "/atheme-services", argv); #endif } slog(LG_INFO, "main(): shutting down"); mowgli_eventloop_destroy(base_eventloop); log_shutdown(); return 0; }
Dict::Dict(char* path, char* filename, int h_num) { _wordDict = db_load(path, filename, h_num); return; }