/* * This gets the client name from which the backup was made */ static int get_client_name(UAContext *ua, RESTORE_CTX *rx) { int i; CLIENT_DBR cr; /* * If no client name specified yet, get it now */ if (!rx->ClientName) { /* * Try command line argument */ i = find_arg_with_value(ua, NT_("client")); if (i < 0) { i = find_arg_with_value(ua, NT_("backupclient")); } if (i >= 0) { if (!is_name_valid(ua->argv[i], &ua->errmsg)) { ua->error_msg("%s argument: %s", ua->argk[i], ua->errmsg); return 0; } rx->ClientName = bstrdup(ua->argv[i]); return 1; } memset(&cr, 0, sizeof(cr)); if (!get_client_dbr(ua, &cr)) { return 0; } rx->ClientName = bstrdup(cr.Name); } return 1; }
/* * This is where we pick up a client name to restore to. */ static int get_restore_client_name(UAContext *ua, RESTORE_CTX &rx) { /* Start with same name as backup client */ bstrncpy(rx.RestoreClientName, rx.ClientName, sizeof(rx.RestoreClientName)); /* try command line argument */ int i = find_arg_with_value(ua, NT_("restoreclient")); if (i >= 0) { if (!is_name_valid(ua->argv[i], &ua->errmsg)) { ua->error_msg("%s argument: %s", ua->argk[i], ua->errmsg); return 0; } bstrncpy(rx.RestoreClientName, ua->argv[i], sizeof(rx.RestoreClientName)); return 1; } return 1; }
/* * This routine is ONLY for resource names * Store a name at specified address. */ void store_name(LEX *lc, RES_ITEM *item, int index, int pass) { POOLMEM *msg = get_pool_memory(PM_EMSG); lex_get_token(lc, T_NAME); if (!is_name_valid(lc->str, &msg)) { scan_err1(lc, "%s\n", msg); return; } free_pool_memory(msg); /* Store the name both pass 1 and pass 2 */ if (*(item->value)) { scan_err2(lc, _("Attempt to redefine name \"%s\" to \"%s\"."), *(item->value), lc->str); return; } *(item->value) = bstrdup(lc->str); scan_to_eol(lc); set_bit(index, res_all.hdr.item_present); }
/* * This is where we pick up a client name to restore to. */ static int get_restore_client_name(UAContext *ua, RESTORE_CTX &rx) { int i; /* * Try command line argument */ i = find_arg_with_value(ua, NT_("restoreclient")); if (i >= 0) { if (!is_name_valid(ua->argv[i], &ua->errmsg)) { ua->error_msg("%s argument: %s", ua->argk[i], ua->errmsg); return 0; } rx.RestoreClientName = bstrdup(ua->argv[i]); return 1; } rx.RestoreClientName = bstrdup(rx.ClientName); return 1; }
/* * This gets the client name from which the backup was made */ static bool get_client_name(UAContext *ua, RESTORE_CTX *rx) { int i; CLIENT_DBR cr; memset(&cr, 0, sizeof(cr)); /* * If no client name specified yet, get it now */ if (!rx->ClientName) { /* * Try command line argument */ i = find_arg_with_value(ua, NT_("client")); if (i < 0) { i = find_arg_with_value(ua, NT_("backupclient")); } if (i >= 0) { if (!is_name_valid(ua->argv[i], ua->errmsg)) { ua->error_msg("%s argument: %s", ua->argk[i], ua->errmsg); return false; } bstrncpy(cr.Name, ua->argv[i], sizeof(cr.Name)); if (!db_get_client_record(ua->jcr, ua->db, &cr)) { ua->error_msg("invalid %s argument: %s\n", ua->argk[i], ua->argv[i]); return false; } rx->ClientName = bstrdup(ua->argv[i]); return true; } if (!get_client_dbr(ua, &cr)) { return false; } rx->ClientName = bstrdup(cr.Name); } return true; }
/* * This is where we pick up a client name to restore to. */ static bool get_restore_client_name(UAContext *ua, RESTORE_CTX &rx) { int i; /* * Try command line argument */ i = find_arg_with_value(ua, NT_("restoreclient")); if (i >= 0) { if (!is_name_valid(ua->argv[i], ua->errmsg)) { ua->error_msg("%s argument: %s", ua->argk[i], ua->errmsg); return false; } if (!GetClientResWithName(ua->argv[i])) { ua->error_msg("invalid %s argument: %s\n", ua->argk[i], ua->argv[i]); return false; } rx.RestoreClientName = bstrdup(ua->argv[i]); return true; } rx.RestoreClientName = bstrdup(rx.ClientName); return true; }
/* This version expects the length of the item which we must check. */ bool acl_access_ok(UAContext *ua, int acl, const char *item, int len) { /* The resource name contains nasty characters */ if (acl != Where_ACL && !is_name_valid(item, NULL)) { Dmsg1(1400, "Access denied for item=%s\n", item); return false; } /* If no console resource => default console and all is permitted */ if (!ua || !ua->cons) { Dmsg0(1400, "Root cons access OK.\n"); return true; /* No cons resource -> root console OK for everything */ } alist *list = ua->cons->ACL_lists[acl]; if (!list) { /* empty list */ if (len == 0 && acl == Where_ACL) { return true; /* Empty list for Where => empty where */ } return false; /* List empty, reject everything */ } /* Special case *all* gives full access */ if (list->size() == 1 && strcasecmp("*all*", (char *)list->get(0)) == 0) { return true; } /* Search list for item */ for (int i=0; i<list->size(); i++) { if (strcasecmp(item, (char *)list->get(i)) == 0) { Dmsg3(1400, "ACL found %s in %d %s\n", item, acl, (char *)list->get(i)); return true; } } return false; }
int process_args(int argc, char *argv[], char batch_mode) { int c; char mode = 0; char time_ok = 0; char room = 0; char *filename; record *rec = calloc(1, sizeof(record)); record *first = NULL; record *last = NULL; EVP_CIPHER_CTX en_ctx, de_ctx; optind = 0; opterr = 0; while ((c = getopt(argc, argv, ":T:K:E:G:ALR:B:")) != -1) switch (c) { case 'T': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (strtoi(optarg, &(rec->timestamp)) || !rec->timestamp) return print_error(ARG_ERROR); time_ok = 1; break; case 'K': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (!is_token_valid(optarg)) return print_error(ARG_ERROR); rec->token = optarg; break; case 'E': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (rec->ptype == GUEST) return print_error(ARG_ERROR); rec->ptype = EMPLOYEE; if (!is_name_valid(optarg)) return print_error(ARG_ERROR); rec->name = optarg; break; case 'G': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (rec->ptype == EMPLOYEE) return print_error(ARG_ERROR); rec->ptype = GUEST; if (!is_name_valid(optarg)) return print_error(ARG_ERROR); rec->name = optarg; break; case 'A': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (rec->etype == DEPARTURE) return print_error(ARG_ERROR); rec->etype = ARRIVAL; break; case 'L': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (rec->etype == ARRIVAL) return print_error(ARG_ERROR); rec->etype = DEPARTURE; break; case 'R': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (strtoi(optarg, &(rec->room_id))) return print_error(ARG_ERROR); room = 1; break; case 'B': if (mode == DEFAULT_MODE || batch_mode) return print_error(ARG_ERROR); mode = BATCH_MODE; filename = optarg; break; default: return print_error(ARG_ERROR); } if (mode == BATCH_MODE) { free(rec); if (batch(filename)) return print_error(ARG_ERROR); return NO_ERROR; } //Checa opcoes mandatorias if (mode != DEFAULT_MODE || !time_ok || rec->token == NULL || rec->name == NULL || rec->etype == UNDEF_E || optind >= argc) return print_error(ARG_ERROR); //Ajusta o evento de acordo com a presenca de sala if (!room) { if (rec->etype == ARRIVAL) rec->etype = G_ARRIVAL; else rec->etype = G_DEPARTURE; } //Inicia contextos de seguranca if ((aes_init((unsigned char *) rec->token, strlen(rec->token), &en_ctx, &de_ctx))) return print_error(ARG_ERROR); //validacoes de consistencia c = read_records(&first, argv[optind], &last, &de_ctx); if (c) { EVP_CIPHER_CTX_cleanup(&en_ctx); EVP_CIPHER_CTX_cleanup(&de_ctx); return c; } if ((c = check_record(first, rec))) { del_records(&first); free(rec); EVP_CIPHER_CTX_cleanup(&en_ctx); EVP_CIPHER_CTX_cleanup(&de_ctx); return print_error(ARG_ERROR); } if (!first) first = rec; else { if (!last) last = first; last->next = rec; } write_record(first, argv[optind], &en_ctx); //TODO em batch, esses registros poderiam ser mantidos para evitar reler o arquivo. if (first != rec) { last->next = NULL; del_records(&first); } free(rec); EVP_CIPHER_CTX_cleanup(&en_ctx); EVP_CIPHER_CTX_cleanup(&de_ctx); return NO_ERROR; }
int main(int argc, char* argv[]) { // 1. parse arguments if (argc != 4) { print_help(argv[0]); return EXIT_FAILURE; } const std::string name = argv[1]; const int size = std::atoi(argv[2]); const int count = std::atoi(argv[3]); if (!is_name_valid(name)) { printf("Unknown function name '%s'.\n", name.c_str()); return EXIT_FAILURE; } if (size <= 0) { printf("Size must be greater than 0.\n"); return EXIT_FAILURE; } if (count <= 0) { printf("Iteration count must be greater than 0.\n"); return EXIT_FAILURE; } // 2. initialize memory uint8_t* data = static_cast<uint8_t*>(malloc(size)); if (data == nullptr) { printf("allocation failed"); return EXIT_FAILURE; } for (int i=0; i < size; i++) { data[i] = i; } // 3. run the test size_t n = 0; size_t k = count; std::printf("running %-30s...", name.c_str()); std::fflush(stdout); const auto t1 = std::chrono::high_resolution_clock::now(); if (name == "lookup-8") { while (k-- > 0) { n += popcnt_lookup_8bit(data, size); } } else if (name == "lookup-64") { while (k-- > 0) { n += popcnt_lookup_64bit(data, size); } } else if (name == "bit-parallel") { while (k-- > 0) { n += popcnt_parallel_64bit_naive(data, size); } } else if (name == "bit-parallel-optimized") { while (k-- > 0) { n += popcnt_parallel_64bit_optimized(data, size); } } else if (name == "sse-bit-parallel") { while (k-- > 0) { n += popcnt_SSE_bit_parallel(data, size); } } else if (name == "sse-lookup") { while (k-- > 0) { n += popcnt_SSE_lookup(data, size); } } else if (name == "cpu") { while (k-- > 0) { n += popcnt_cpu_64bit(data, size); } } else if (name == "lookup-3") { while (k-- > 0) { n += popcnt_lookup3_8bit(data, size); } } else if (name == "lookup-4") { while (k-- > 0) { n += popcnt_lookup4_8bit(data, size); } } else if (name == "lookup-7") { while (k-- > 0) { n += popcnt_lookup7_8bit(data, size); } } else { assert(false && "wrong function name handling"); } const auto t2 = std::chrono::high_resolution_clock::now(); const std::chrono::duration<double> td = t2-t1; printf("reference result = %lu, time = %10.6f s\n", n, td.count()); free(data); return EXIT_SUCCESS; }
/* * Implement Bareos bconsole command purge action * purge action= pool= volume= storage= devicetype= */ static int action_on_purge_cmd(UAContext *ua, const char *cmd) { bool allpools = false; int drive = -1; int nb = 0; uint32_t *results = NULL; const char *action = "all"; STORERES *store = NULL; POOLRES *pool = NULL; MEDIA_DBR mr; POOL_DBR pr; BSOCK *sd = NULL; memset(&pr, 0, sizeof(pr)); /* Look at arguments */ for (int i=1; i<ua->argc; i++) { if (bstrcasecmp(ua->argk[i], NT_("allpools"))) { allpools = true; } else if (bstrcasecmp(ua->argk[i], NT_("volume")) && is_name_valid(ua->argv[i], NULL)) { bstrncpy(mr.VolumeName, ua->argv[i], sizeof(mr.VolumeName)); } else if (bstrcasecmp(ua->argk[i], NT_("devicetype")) && ua->argv[i]) { bstrncpy(mr.MediaType, ua->argv[i], sizeof(mr.MediaType)); } else if (bstrcasecmp(ua->argk[i], NT_("drive")) && ua->argv[i]) { drive = atoi(ua->argv[i]); } else if (bstrcasecmp(ua->argk[i], NT_("action")) && is_name_valid(ua->argv[i], NULL)) { action=ua->argv[i]; } } /* Choose storage */ ua->jcr->res.wstore = store = get_storage_resource(ua, false); if (!store) { goto bail_out; } switch (store->Protocol) { case APT_NDMPV2: case APT_NDMPV3: case APT_NDMPV4: ua->warning_msg(_("Storage has non-native protocol.\n")); goto bail_out; default: break; } if (!open_db(ua)) { Dmsg0(100, "Can't open db\n"); goto bail_out; } if (!allpools) { /* force pool selection */ pool = get_pool_resource(ua); if (!pool) { Dmsg0(100, "Can't get pool resource\n"); goto bail_out; } bstrncpy(pr.Name, pool->name(), sizeof(pr.Name)); if (!db_get_pool_record(ua->jcr, ua->db, &pr)) { Dmsg0(100, "Can't get pool record\n"); goto bail_out; } mr.PoolId = pr.PoolId; } /* * Look for all Purged volumes that can be recycled, are enabled and * have more the 10,000 bytes. */ mr.Recycle = 1; mr.Enabled = 1; mr.VolBytes = 10000; set_storageid_in_mr(store, &mr); bstrncpy(mr.VolStatus, "Purged", sizeof(mr.VolStatus)); if (!db_get_media_ids(ua->jcr, ua->db, &mr, &nb, &results)) { Dmsg0(100, "No results from db_get_media_ids\n"); goto bail_out; } if (!nb) { ua->send_msg(_("No Volumes found to perform %s action.\n"), action); goto bail_out; } if ((sd = open_sd_bsock(ua)) == NULL) { Dmsg0(100, "Can't open connection to sd\n"); goto bail_out; } /* * Loop over the candidate Volumes and actually truncate them */ for (int i=0; i < nb; i++) { mr.clear(); mr.MediaId = results[i]; if (db_get_media_record(ua->jcr, ua->db, &mr)) { /* TODO: ask for drive and change Pool */ if (bstrcasecmp("truncate", action) || bstrcasecmp("all", action)) { do_truncate_on_purge(ua, &mr, pr.Name, store->dev_name(), drive, sd); } } else { Dmsg1(0, "Can't find MediaId=%lld\n", (uint64_t) mr.MediaId); } } bail_out: close_db(ua); if (sd) { sd->signal(BNET_TERMINATE); sd->close(); ua->jcr->store_bsock = NULL; } ua->jcr->res.wstore = NULL; if (results) { free(results); } return 1; }