/** Get a value as string to a corresponding key if available, else set to default \param key key \param defvalue default value \returns value */ const char *value_as_string_default(const char *key, const char *defvalue) { if (key_exists(key)) { return value_as_string(key); } else { return defvalue; } }
/** Get a value as unsigned int to a corresponding key if available, else set to default \param key key \param defvalue default value \returns value */ unsigned int value_as_unsigned_int_default(const char *key, unsigned int defvalue) { if (key_exists(key)) { return value_as_unsigned_int(key); } else { return defvalue; } }
/** Get a value as double to a corresponding key if available, else set to default \param key key \param defvalue default value \returns value */ double value_as_double_default(const char *key, double defvalue) { if (key_exists(key)) { return value_as_double(key); } else { return defvalue; } }
/** Get a value as bool to a corresponding key if available, else set to default \param key key \param defvalue default value \returns value */ bool value_as_bool_default(const char *key, bool defvalue) { if (key_exists(key)) { return value_as_bool(key); } else { return defvalue; } }
void cmd_exists (flux_t h, int argc, char **argv) { int i; if (argc == 0) msg_exit ("exist: specify one or more keys"); for (i = 0; i < argc; i++) { if (!key_exists (h, argv[i])) exit (1); } }
static unsigned int try_to_flush_duplicates(const char *new_key, unsigned int buf_len) { unsigned int key_size, new_record_size, ret = 0, can_rollback = 0; struct record record, previous_record; char sector_buff[STORAGE_SIZE]; struct iter_state is; memcpy(sector_buff, STORAGE_ADDRESS, STORAGE_SIZE); if(check_for_duplicates(sector_buff) || key_exists(sector_buff, new_key, §or_buff[STORAGE_SIZE], 0, NULL) || check_for_empty_records(sector_buff)) { fs_erase(); record_iter_init(&is, sector_buff, STORAGE_SIZE); while(record_iter_next(&is, &record, NULL)) { if(is_empty(&record)) continue; if(!key_exists((char *)STORAGE_ADDRESS, record.key, STORAGE_ADDRESS + STORAGE_SIZE, 1, NULL)) { struct record rec; if(!key_exists(sector_buff, record.key, §or_buff[STORAGE_SIZE], 0, &rec)) continue; if(strcmp(new_key, record.key) == 0) { // If we are about to write this key we don't keep the old value. previous_record = rec; // This holds the old record in case we need it back (for instance if new record is too long) can_rollback = 1; } else fs_write(record.key, rec.value, rec.value_len); } } ret = 1; } key_size = strlen(new_key) + 1; new_record_size = key_size + buf_len + sizeof(new_record_size); if(can_rollback && new_record_size > get_free_space()) { fs_write(new_key, previous_record.value, previous_record.value_len); } return ret; }
static char check_for_duplicates(char *buff) { struct record record, following_record; struct iter_state is; int no_error; record_iter_init(&is, buff, STORAGE_SIZE); no_error = record_iter_next(&is, &record, NULL); while(no_error) { no_error = record_iter_next(&is, &following_record, NULL); if(no_error && key_exists(following_record.raw_record, record.key, &buff[STORAGE_SIZE], 1, NULL)) return 1; record = following_record; } return 0; }
int make_current (char *group) { if (!currentgroup) { if (!(currentgroup = malloc(GROUPNAMELEN + 1))) { LOG("make_current:no memory"); return -1; } strcpy(currentgroup, "no.group.selected.yet"); /* Better than random crap... */ } /* prevent =junk from being selected */ if (!key_exists(group, strlen(group))) return -1; if (-1 == group_info(group, ¤tinfo)) return -1; strcpy(currentgroup, group); currentserial = -1; return 0; }
kz_t *kz_open (flux_t *h, const char *name, int flags) { kz_t *kz = xzmalloc (sizeof (*kz)); kz->flags = flags; kz->name = xstrdup (name); if ((kz->stream = strchr (kz->name, '.'))) kz->stream++; else kz->stream = kz->name; kz->h = h; if ((flags & KZ_FLAGS_WRITE)) { if (key_exists (h, name)) { if (!(flags & KZ_FLAGS_TRUNC)) { errno = EEXIST; goto error; } else if (kvs_unlink (h, name) < 0) goto error; } if (kvs_mkdir (h, name) < 0) /* N.B. does not catch EEXIST */ goto error; if (!(flags & KZ_FLAGS_NOCOMMIT_OPEN)) { if (kvs_commit (h) < 0) goto error; } } else if ((flags & KZ_FLAGS_READ)) { if (!(flags & KZ_FLAGS_NOEXIST)) { if (kvs_get_dir (h, &kz->dir, "%s", name) < 0) goto error; } } return kz; error: kz_destroy (kz); return NULL; }
s32 populate_inst(ocrParamList_t **inst_param, ocrMappable_t **instance, s32 *type_counts, char ***factory_names, void ***all_factories, ocrMappable_t ***all_instances, type_enum index, dictionary *dict, char *secname) { s32 i, low, high, j; char *inststr; char key[MAX_KEY_SZ]; void *factory; s32 value; read_range(dict, secname, "id", &low, &high); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "type"); INI_GET_STR (key, inststr, ""); for (i = 0; i < type_counts[index]; i++) { if (factory_names[index][i] && (0 == strncmp(factory_names[index][i], inststr, strlen(factory_names[index][i])))) break; } if (factory_names[index][i] == NULL || strncmp(factory_names[index][i], inststr, strlen(factory_names[index][i]))) { DPRINTF(DEBUG_LVL_WARN, "Unknown type %s\n", inststr); return 0; } // find factory based on i factory = all_factories[index][i]; // Use the factory's instantiate() to create instance switch (index) { case guid_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListGuidProviderInst_t); instance[j] = (ocrMappable_t *)((ocrGuidProviderFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created guid provider of type %s, index %d\n", inststr, j); } break; case memplatform_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListMemPlatformInst_t); instance[j] = (ocrMappable_t *)((ocrMemPlatformFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created memplatform of type %s, index %d\n", inststr, j); } break; case memtarget_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListMemTargetInst_t); instance[j] = (ocrMappable_t *)((ocrMemTargetFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created memtarget of type %s, index %d\n", inststr, j); } break; case allocator_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListAllocatorInst_t); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "size"); INI_GET_INT (key, value, -1); ((paramListAllocatorInst_t *)inst_param[j])->size = value; instance[j] = (ocrMappable_t *)((ocrAllocatorFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created allocator of type %s, index %d\n", inststr, j); } break; case compplatform_type: for (j = low; j<=high; j++) { compPlatformType_t mytype = -1; TO_ENUM (mytype, inststr, compPlatformType_t, compplatform_types, compPlatformMax_id); switch (mytype) { case compPlatformPthread_id: { ALLOC_PARAM_LIST(inst_param[j], paramListCompPlatformPthread_t); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "ismasterthread"); INI_GET_BOOL (key, value, -1); ((paramListCompPlatformPthread_t *)inst_param[j])->isMasterThread = value; snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "stacksize"); INI_GET_INT (key, value, -1); ((paramListCompPlatformPthread_t *)inst_param[j])->stackSize = (value==-1)?0:value; if (key_exists(dict, secname, "binding")) { value = get_key_value(dict, secname, "binding", j-low); // printf("Binding value for %s;%d is %d\n", secname, j-low, value); } // else printf("No binding for %s\n", secname); } break; default: ALLOC_PARAM_LIST(inst_param[j], paramListCompPlatformInst_t); break; } instance[j] = (ocrMappable_t *)((ocrCompPlatformFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created compplatform of type %s, index %d\n", inststr, j); } break; case comptarget_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListCompTargetInst_t); instance[j] = (ocrMappable_t *)((ocrCompTargetFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created comptarget of type %s, index %d\n", inststr, j); } break; case workpile_type: for (j = low; j<=high; j++) { ALLOC_PARAM_LIST(inst_param[j], paramListWorkpileInst_t); instance[j] = (ocrMappable_t *)((ocrWorkpileFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created workpile of type %s, index %d\n", inststr, j); } break; case worker_type: for (j = low; j<=high; j++) { workerType_t mytype = -1; TO_ENUM (mytype, inststr, workerType_t, worker_types, workerMax_id); switch (mytype) { case workerHc_id: { ALLOC_PARAM_LIST(inst_param[j], paramListWorkerHcInst_t); ((paramListWorkerHcInst_t *)inst_param[j])->workerId = j; // using "id" for now; TODO: decide if a separate key is needed } break; default: ALLOC_PARAM_LIST(inst_param[j], paramListWorkerInst_t); break; } instance[j] = (ocrMappable_t *)((ocrWorkerFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created worker of type %s, index %d\n", inststr, j); } break; case scheduler_type: for (j = low; j<=high; j++) { schedulerType_t mytype = -1; TO_ENUM (mytype, inststr, schedulerType_t, scheduler_types, schedulerMax_id); switch (mytype) { case schedulerHc_id: { ALLOC_PARAM_LIST(inst_param[j], paramListSchedulerHcInst_t); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "workeridfirst"); INI_GET_INT (key, value, -1); ((paramListSchedulerHcInst_t *)inst_param[j])->workerIdFirst = value; } break; default: ALLOC_PARAM_LIST(inst_param[j], paramListSchedulerInst_t); break; } instance[j] = (ocrMappable_t *)((ocrSchedulerFactory_t *)factory)->instantiate(factory, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created scheduler of type %s, index %d\n", inststr, j); } break; case policydomain_type: for (j = low; j<=high; j++) { ocrTaskFactory_t *tf; ocrTaskTemplateFactory_t *ttf; ocrDataBlockFactory_t *dbf; ocrEventFactory_t *ef; ocrPolicyCtxFactory_t *cf; ocrGuidProvider_t *gf; ocrLockFactory_t *lf; ocrAtomic64Factory_t *af; s32 low, high; s32 schedulerCount, workerCount, computeCount, workpileCount, allocatorCount, memoryCount; schedulerCount = read_range(dict, secname, "scheduler", &low, &high); workerCount = read_range(dict, secname, "worker", &low, &high); computeCount = read_range(dict, secname, "comptarget", &low, &high); workpileCount = read_range(dict, secname, "workpile", &low, &high); allocatorCount = read_range(dict, secname, "allocator", &low, &high); memoryCount = read_range(dict, secname, "memtarget", &low, &high); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "taskfactory"); INI_GET_STR (key, inststr, ""); tf = create_factory_task(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "tasktemplatefactory"); INI_GET_STR (key, inststr, ""); ttf = create_factory_tasktemplate(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "datablockfactory"); INI_GET_STR (key, inststr, ""); dbf = create_factory_datablock(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "eventfactory"); INI_GET_STR (key, inststr, ""); ef = create_factory_event(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "contextfactory"); INI_GET_STR (key, inststr, ""); cf = create_factory_context(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "sync"); INI_GET_STR (key, inststr, ""); lf = create_factory_lock(inststr, NULL); snprintf(key, MAX_KEY_SZ, "%s:%s", secname, "sync"); INI_GET_STR (key, inststr, ""); af = create_factory_atomic64(inststr, NULL); // Ugly but special case read_range(dict, secname, "guid", &low, &high); ASSERT (low == high); // We don't expect more than one guid provider gf = (ocrGuidProvider_t *)(all_instances[guid_type][low]); ASSERT (gf); ALLOC_PARAM_LIST(inst_param[j], paramListPolicyDomainInst_t); instance[j] = (ocrMappable_t *)((ocrPolicyDomainFactory_t *)factory)->instantiate(factory, schedulerCount, workerCount, computeCount, workpileCount, allocatorCount, memoryCount, tf, ttf, dbf, ef, cf, gf, lf, af, NULL, inst_param[j]); if (instance[j]) DPRINTF(DEBUG_LVL_INFO, "Created policy domain of index %d\n", j); setBootPD((ocrPolicyDomain_t *)instance[j]); } break; default: DPRINTF(DEBUG_LVL_WARN, "Error: %d index unexpected\n", index); break; } return 0; }
int pack_file(char *file, TCXSTR *root_key, bool resume) { struct stat st; int ecode; TCXSTR *key; key = tcxstrdup(root_key); size_t file_path_len; file_path_len = strlen(file); char *file_name; file_name = get_file_name(file, file_path_len); tcxstrcat2(key, file_name); if (resume && key_exists(tcxstrptr(key))) { fprintf(stdout, "already exists"); return; } if (-1 == stat(file, &st)) { return 1; } if (!S_ISREG(st.st_mode)) { fprintf(stdout, "Not regular file: %s", file); return 2; } int fd; if (-1 == (fd = open(file, O_RDONLY))) { fprintf(stdout, "***Failed open file %s", file); return 1; } void *fmap; if (MAP_FAILED == (fmap = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0))) { fprintf(stdout, "mmaping failed for %s", file); close(fd); return -1; } /* store records */ if (use_cabinet_lib) { if (!tchdbput(hdb, tcxstrptr(key), tcxstrsize(key), fmap, st.st_size)) { ecode = tchdbecode(hdb); fprintf(stdout, "put error: %s", tchdberrmsg(ecode)); } } else { if (!tcrdbput(tdb, tcxstrptr(key), tcxstrsize(key), fmap, st.st_size)) { ecode = tcrdbecode(tdb); fprintf(stdout, "put error: %s", tcrdberrmsg(ecode)); } } fprintf(stdout, "%d bytes", st.st_size); munmap(fmap, st.st_size); close(fd); tcxstrdel(key); }