char * util_alloc_tmp_file(const char * path, const char * prefix , bool include_pid ) { // Should be reimplemented to use mkstemp() const int pid_digits = 6; const int random_digits = 6; const int random_max = 1000000; #ifdef HAVE_PID_T const int pid_max = 1000000; pid_t pid = getpid() % pid_max; #else int pid = 0; #endif char * file = util_calloc(strlen(path) + 1 + strlen(prefix) + 1 + pid_digits + 1 + random_digits + 1 , sizeof * file ); char * tmp_prefix = util_alloc_string_copy( prefix ); if (!util_is_directory(path)) util_make_path(path); util_string_tr( tmp_prefix , UTIL_PATH_SEP_CHAR , '_'); /* removing path seps. */ do { long int rand_int = rand() % random_max; if (include_pid) sprintf(file , "%s%c%s-%d-%ld" , path , UTIL_PATH_SEP_CHAR , tmp_prefix , pid , rand_int); else sprintf(file , "%s%c%s-%ld" , path , UTIL_PATH_SEP_CHAR , tmp_prefix , rand_int); } while (util_file_exists(file)); free( tmp_prefix ); return file; }
int main(int argc , char **argv) { test_create(); test_assert_true( util_file_exists( argv[0] )); test_copy( argv[0] ); test_enter( argv[0] ); }
rng_type * rng_config_init_rng__(const rng_config_type * rng_config, rng_type * rng) { const char * seed_load = rng_config_get_seed_load_file( rng_config ); const char * seed_store = rng_config_get_seed_store_file( rng_config ); if (seed_load != NULL) { if (util_file_exists( seed_load)) { FILE * stream = util_fopen( seed_load , "r"); rng_fscanf_state( rng , stream ); fclose( stream ); } else { /* In the special case that seed_load == seed_store; we accept a seed_load argument pointing to a non-existant file. */ if (seed_store) { if (util_string_equal( seed_store , seed_load)) rng_init( rng , INIT_DEV_URANDOM ); else util_abort("%s: tried to load random seed from non-existing file:%s \n",__func__ , seed_load); } } } else rng_init( rng , INIT_DEV_URANDOM ); if (seed_store != NULL) { FILE * stream = util_mkdir_fopen( seed_store , "w"); rng_fprintf_state( rng , stream ); fclose( stream ); } return rng; }
bool ecl_util_fmt_file(const char *filename , bool * __fmt_file) { /*const int min_size = 32768;*/ const int min_size = 256; /* Veeeery small */ int report_nr; ecl_file_enum file_type; bool status = true; bool fmt_file; if (util_file_exists(filename)) { file_type = ecl_util_get_file_type(filename , &fmt_file , &report_nr); if (file_type == ECL_OTHER_FILE) { if (util_file_size(filename) > min_size) fmt_file = util_fmt_bit8(filename); else status = false; // Do not know ?? } } else { file_type = ecl_util_get_file_type(filename , &fmt_file , &report_nr); if (file_type == ECL_OTHER_FILE) status = false; // Do not know ?? } *__fmt_file = fmt_file; return status; }
bool summary_key_set_fread(summary_key_set_type * set, const char * filename) { bool file_exists = false; pthread_rwlock_wrlock( &set->rw_lock ); { hash_clear(set->key_set); if (util_file_exists(filename)) { FILE * stream = util_fopen(filename, "r"); if (stream) { stringlist_type * key_set = stringlist_fread_alloc(stream); for (int i = 0; i < stringlist_get_size(key_set); i++) { hash_insert_int(set->key_set, stringlist_iget(key_set, i), 1); } stringlist_free(key_set); fclose( stream ); } else { util_abort("%s: failed to open: %s for reading \n",__func__ , filename ); } file_exists = true; } } pthread_rwlock_unlock( &set->rw_lock ); return file_exists; }
bool hook_manager_run_hook_workflow( const hook_manager_type * hook_manager , void * self) { const char * export_file = runpath_list_get_export_file( hook_manager->runpath_list ); if (!util_file_exists( export_file )) fprintf(stderr,"** Warning: the file:%s with a list of runpath directories was not found - workflow will probably fail.\n" , export_file); return hook_workflow_run_workflow(hook_manager->hook_workflow, hook_manager->workflow_list, self); }
/* * util_file_get_type -- checks whether the path points to a device dax, * normal file or non-existent file */ enum file_type util_file_get_type(const char *path) { LOG(3, "path \"%s\"", path); if (path == NULL) { ERR("invalid (NULL) path"); errno = EINVAL; return OTHER_ERROR; } int exists = util_file_exists(path); if (exists < 0) return OTHER_ERROR; if (!exists) return NOT_EXISTS; #ifdef _WIN32 return TYPE_NORMAL; #else os_stat_t st; if (os_stat(path, &st) < 0) { ERR("!stat"); return OTHER_ERROR; } return util_stat_get_type(&st); #endif }
void ecl_config_set_init_section(ecl_config_type * ecl_config, const char * input_init_section) { if (ecl_config->can_restart) { /* The <INIT> tag is set. */ ecl_config->input_init_section = util_realloc_string_copy(ecl_config->input_init_section, input_init_section); /* input_init_section = path/to/init_section */ if (util_file_exists(ecl_config->input_init_section)) { /* init_section = $CWD/path/to/init_section */ util_safe_free(ecl_config->init_section); ecl_config->init_section = util_alloc_realpath(input_init_section); } else { char * path; util_alloc_file_components(ecl_config->input_init_section, &path, NULL, NULL ); if (path != NULL ) fprintf(stderr, "** Warning: %s: When INIT_SECTION:%s points to a non-existing file - you can not have any path components.\n", __func__, input_init_section); else ecl_config->init_section = util_alloc_string_copy(input_init_section); util_safe_free(path); } } else /* The <INIT> tag is not set - we can not utilize the input_init_section info, and we just ignore it. */ fprintf(stderr, "** Warning: <INIT> tag was not found in datafile - can not utilize INIT_SECTION keyword - ignored.\n"); }
int autostart_autodetect_opt_prgname(const char *file_prog_name, unsigned int alt_prg_number, unsigned int runmode) { char *tmp; int result; /* Check for image:prg -format. */ tmp = strrchr(file_prog_name, ':'); if (tmp) { char *autostart_prg_name; char *autostart_file; autostart_file = lib_stralloc(file_prog_name); autostart_prg_name = strrchr(autostart_file, ':'); *autostart_prg_name++ = '\0'; /* Does the image exist? */ if (util_file_exists(autostart_file)) { char *name; charset_petconvstring((BYTE *)autostart_prg_name, 0); name = charset_replace_hexcodes(autostart_prg_name); result = autostart_autodetect(autostart_file, name, 0, runmode); lib_free(name); } else { result = autostart_autodetect(file_prog_name, NULL, alt_prg_number, runmode); } lib_free(autostart_file); } else { result = autostart_autodetect(file_prog_name, NULL, alt_prg_number, runmode); } return result; }
static bool enkf_main_current_case_file_exists( const enkf_main_type * enkf_main) { const char * ens_path = model_config_get_enspath( enkf_main->model_config); char * current_case_file = util_alloc_filename(ens_path, CURRENT_CASE_FILE, NULL); bool exists = util_file_exists(current_case_file); free(current_case_file); return exists; }
/* The input template file must point to an existing file. */ void gen_kw_config_set_template_file( gen_kw_config_type * config , const char * template_file ) { if (template_file != NULL) { if (!util_file_exists(template_file)) util_abort("%s: the template_file:%s does not exist - aborting.\n",__func__ , template_file); } config->template_file = util_realloc_string_copy( config->template_file , template_file ); }
void cases_config_fread( cases_config_type * config , const char * filename) { if (util_file_exists( filename )) { FILE * stream = util_fopen( filename , "r"); int iteration_number = util_fread_int( stream ); cases_config_set_iteration_number(config,iteration_number); fclose( stream ); } }
void test_enter( const char* argv0 ) { ERT::TestArea ta; test_assert_throw( ta.copyFile( argv0 ) , std::runtime_error ); ta.enter("test/enter"); ta.copyFile( argv0 ); test_assert_true( util_file_exists( LOCAL_ARGV0 )); }
static void ShowContents(HWND hwnd, char *image_name) { image_contents_t *image; image_contents_screencode_t *line; image_contents_screencode_t *lines; // // delete listbox contents // const HWND hwnd2 = WinWindowFromID(hwnd, DID_CONTENTS_LB); LboxFreeContents(hwnd2); // // don't call the all the vice stuff if file doesn't exist // if (!util_file_exists(image_name)) { return; } // // try to open as a disk or tape image // image = diskcontents_read(image_name, 0); if (!image) { return; } // // set the wanted font // WinSendMsg(hwnd2, LM_SETITEMHEIGHT, (MPARAM)9, 0); // // convert image contents to screencodes // lines = image_contents_to_screencode(image); // // Loop over all entries // { int idx = 0; line = lines; do { WinInsertLboxItem(hwnd2, LIT_END, ""); WinLboxSetItemHandle(hwnd2, idx, (long)line); WinSetLboxItemText(hwnd2, idx, ""); idx++; } while ((line = line->next)); } // // free image structure // image_contents_destroy(image); }
ui_return_type * ecl_config_validate_schedule_file(const ecl_config_type * ecl_config , const char * schedule_file) { if ((ecl_config->start_date != -1) && (util_file_exists(schedule_file))) return ui_return_alloc(UI_RETURN_OK); else { ui_return_type * ui_return = ui_return_alloc(UI_RETURN_FAIL); if (ecl_config->start_date == -1) ui_return_add_error(ui_return, "You must set the ECLIPSE datafile before you can set the SCHEDULE file."); if (!util_file_exists(schedule_file)) { char * error_msg = util_alloc_sprintf("SCHEDULE file:%s not found" , schedule_file); ui_return_add_error(ui_return , error_msg); free(error_msg); } return ui_return; } }
state_map_type * state_map_fread_alloc( const char * filename ) { state_map_type * map = state_map_alloc(); if (util_file_exists( filename )) { FILE * stream = util_fopen( filename , "r"); int_vector_fread( map->state , stream ); fclose( stream ); } return map; }
static int enkf_fs_get_fs_version__(const char * config_file) { int version = -1; if (util_file_exists(config_file)) { FILE * stream = util_fopen(config_file , "r"); version = enkf_fs_fread_fs_version__(stream); fclose(stream); } return version; }
bool ert_test_context_install_workflow( ert_test_context_type * test_context , const char * workflow_name , const char * workflow_file) { if (util_file_exists( workflow_file )) { enkf_main_type * enkf_main = ert_test_context_get_main( test_context ); ert_workflow_list_type * workflow_list = enkf_main_get_workflow_list( enkf_main ); ert_workflow_list_add_workflow( workflow_list , workflow_file , workflow_name ); return ert_workflow_list_has_workflow( workflow_list , workflow_name); } else return false; }
void test_assert_file_content__( const char * input_file , const char * expected, const char * src_file , int line) { if (util_file_exists( input_file )) { char * content = util_fread_alloc_file_content(input_file, NULL); if (!util_string_equal( content , expected)) test_error_exit("%s:%d content difference \n",src_file , line); free( content ); } else test_error_exit("%s:%d => No such file:%s \n", src_file , line , input_file); }
static bool util_addr2line_lookup__(const void * bt_addr , char ** func_name , char ** file_name , int * line_nr, bool subtract_base_adress) { *func_name = NULL; // If dladdr() succeeds, but addr2line fails the func_name pointer will be set, but the function will return false anyway. *file_name = NULL; *line_nr = 0; { bool address_found = false; Dl_info dl_info; #if defined(__APPLE__) return false; #else if (dladdr(bt_addr , &dl_info)) { const char * executable = dl_info.dli_fname; *func_name = util_alloc_string_copy( dl_info.dli_sname ); if (util_file_exists( executable )) { char *stdout_file = util_alloc_tmp_file("/tmp" , "addr2line" , true); /* 1: Run addr2line application */ { char ** argv = util_calloc(3 , sizeof * argv ); argv[0] = util_alloc_string_copy("--functions"); argv[1] = util_alloc_sprintf("--exe=%s" , executable ); { char * rel_address = (char *) bt_addr; if (subtract_base_adress) rel_address -= (size_t) dl_info.dli_fbase; argv[2] = util_alloc_sprintf("%p" , (void *) rel_address); } util_spawn_blocking("addr2line", 3, (const char **) argv, stdout_file, NULL); util_free_stringlist(argv , 3); } /* 2: Parse stdout output */ { bool at_eof; FILE * stream = util_fopen(stdout_file , "r"); char * tmp_fname = util_fscanf_alloc_line(stream , &at_eof); if (strcmp(tmp_fname , UNDEFINED_FUNCTION) != 0) { char * stdout_file_name = util_fscanf_alloc_line(stream , &at_eof); char * line_string = NULL; util_binary_split_string( stdout_file_name , ":" , false , file_name , &line_string); if (line_string && util_sscanf_int( line_string , line_nr)) address_found = true; free( stdout_file_name ); util_safe_free( line_string ); } free( tmp_fname ); fclose(stream); } util_unlink_existing(stdout_file); free( stdout_file ); } } return address_found; #endif } }
bool qc_module_run_workflow( const qc_module_type * qc_module , void * self) { bool verbose = false; if (qc_module->qc_workflow != NULL ) { if (!util_file_exists( qc_module->runpath_list_file )) fprintf(stderr,"** Warning: the file:%s with a list of runpath directories was not found - QC workflow wil probably fail.\n" , qc_module->runpath_list_file); return ert_workflow_list_run_workflow__( qc_module->workflow_list , qc_module->qc_workflow , verbose , self); } else return false; }
void test_load() { test_work_area_type * work_area = test_work_area_alloc("unsmry_loader"); ecl_sum_type * ecl_sum = write_ecl_sum(); test_assert_true( util_file_exists("CASE.SMSPEC") ); test_assert_true( util_file_exists("CASE.UNSMRY") ); ecl::unsmry_loader * loader = new ecl::unsmry_loader(ecl_sum_get_smspec(ecl_sum), "CASE.UNSMRY", 0); const std::vector<double> FOPT_value = loader->get_vector(1); const std::vector<double> BPR_value = loader->get_vector(2); const std::vector<double> WWCT_value = loader->get_vector(3); test_assert_int_equal( FOPT_value.size(), 4 ); test_assert_double_equal( FOPT_value[3] , 6.0 ); test_assert_double_equal( BPR_value[2] , 10.0 ); test_assert_double_equal( WWCT_value[1] , 10.0 ); delete loader; ecl_sum_free(ecl_sum); test_work_area_free(work_area); }
void test_copy_file( const char * src_file ) { char * filename = util_split_alloc_filename( src_file ); test_work_area_type * work_area = test_work_area_alloc( "copy-file" ); test_work_area_copy_file( work_area , src_file ); test_assert_true( util_file_exists( filename )); test_work_area_free( work_area ); free( filename ); }
ui_return_type * ecl_config_validate_data_file(const ecl_config_type * ecl_config, const char * data_file) { if (util_file_exists(data_file)) return ui_return_alloc(UI_RETURN_OK); else { ui_return_type * ui_return = ui_return_alloc(UI_RETURN_FAIL); char * error_msg = util_alloc_sprintf("File not found:%s" , data_file); ui_return_add_error(ui_return , error_msg); free( error_msg ); return ui_return; } }
FortIO::FortIO(const std::string& filename , std::ios_base::openmode mode , bool fmt_file , bool endian_flip_header) { if (mode == std::ios_base::in) { if (util_file_exists( filename.c_str() )) { fortio_type * c_ptr = fortio_open_reader( filename.c_str() , fmt_file , endian_flip_header); m_fortio.reset( c_ptr , fortio_fclose ); } else throw std::invalid_argument("File " + filename + " does not exist"); } else { fortio_type * c_ptr = fortio_open_writer( filename.c_str() , fmt_file , endian_flip_header); m_fortio.reset( c_ptr , fortio_fclose ); } }
void test_copy( const char* argv0 ) { ERT::TestArea ta("test/copy"); test_assert_throw( ta.copyFile( "does/not/exist" ) , std::invalid_argument ); test_assert_throw( ta.copyDirectory( argv0 ) , std::invalid_argument ); ta.copyFile( argv0 ); test_assert_true( util_file_exists( LOCAL_ARGV0 )); util_unlink_existing( LOCAL_ARGV0 ); test_assert_false( util_file_exists( LOCAL_ARGV0)); ta.copyParentContent( argv0 ); test_assert_true( util_file_exists( LOCAL_ARGV0)); { ERT::TestArea ta2("test2/copy"); ta2.copyFile( LOCAL_ARGV0 ); test_assert_true( util_file_exists( LOCAL_ARGV0)); } }
void proj_open_proj_okCB( Widget widget, XtPointer client_data, XmSelectionBoxCallbackStruct *call_data ) { STRING proj_filename = (STRING) NULL; XmString xm_buf = (XmString) NULL; DtbObjectHelpData help_data = NULL; proj_filename = (STRING)objxm_xmstr_to_str(call_data->value); /* If the file chooser selection text field is empty, return */ if ( util_strempty(proj_filename) ) { dtb_proj_no_name_msg_initialize(&dtb_proj_no_name_msg); (void)dtb_show_modal_message(widget, &dtb_proj_no_name_msg, NULL, NULL, NULL); } else if (!util_file_exists(proj_filename)) { sprintf(Buf, catgets(Dtb_project_catd, 100, 8, "The file %s does not exist."), proj_filename); util_printf_err(Buf); } else if (!util_file_is_regular_file(proj_filename)) { sprintf(Buf, catgets(Dtb_project_catd, 100, 69, "Cannot open %s.\n%s is not a regular file."), proj_filename, proj_filename); xm_buf = XmStringCreateLocalized(Buf); dtb_proj_error_msg_initialize(&dtb_proj_error_msg); help_data = (DtbObjectHelpData) util_malloc(sizeof(DtbObjectHelpDataRec)); help_data->help_text = catgets(Dtb_project_catd, 100, 89, "The file you specified is a directory or\nanother special file."); help_data->help_volume = ""; help_data->help_locationID = ""; (void)dtb_show_modal_message(widget, &dtb_proj_error_msg, xm_buf, help_data, NULL); util_free(help_data); XmStringFree(xm_buf); } else { XtUnmanageChild(widget); /* pop down the chooser */ ab_check_and_open_bip(proj_filename); } }
int main(int argc, char** argv) { char * filename = util_alloc_dump_filename(); if (util_file_exists(filename)) { remove(filename); } test_assert_false(util_file_exists(filename)); pid_t child_pid = fork(); if (child_pid == 0) { util_abort("I was terminated with the util_abort function"); } else { waitpid(child_pid, NULL, 0); test_assert_true(util_file_exists(filename)); free(filename); } return (EXIT_SUCCESS); }
/* GIT_COMMIT and COMPILE_TIME_STAMP are env variables set by the makefile. Will exit if the config file does not exist. */ void enkf_welcome(const char * config_file) { if (util_file_exists(config_file)) { char * abs_path = util_alloc_realpath(config_file); char * config_file_msg = util_alloc_sprintf("Configuration file...: %s \n",abs_path); /* This will be printed if/when util_abort() is called on a later stage. */ /* The svn_version and compile_time are added with the functione enkf_main_init_debug(). */ util_abort_append_version_info(config_file_msg); free(config_file_msg); free(abs_path); } else util_exit(" ** Sorry: can not locate configuration file: %s \n\n", config_file); }
static bool job_queue_node_status_update_confirmed_running__(job_queue_node_type * node) { if (node->confirmed_running) return true; if (!node->status_file) { node->confirmed_running = true; return true; } if (util_file_exists(node->status_file)) node->confirmed_running = true; return node->confirmed_running; }