From a57e29dfc520b4db45dc27b3b358292cdf6574a4 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 4 Dec 2019 23:03:12 +0300 Subject: [PATCH] os: replace dir_exists with is_dir; file_exists() => exists() --- tools/fast/fast.v | 4 +-- tools/gen_vc.v | 34 ++++++++++---------- tools/modules/testing/common.v | 2 +- tools/vpm.v | 2 +- tools/vrepl.v | 2 +- tools/vtest-compiler.v | 6 ++-- tools/vtest.v | 4 +-- tools/vup.v | 2 +- vlib/builtin/bare/syscallwrapper_test.v | 4 +-- vlib/compiler/cc.v | 20 ++++++------ vlib/compiler/cgen.v | 2 +- vlib/compiler/comptime.v | 4 +-- vlib/compiler/live.v | 2 +- vlib/compiler/main.v | 22 ++++++------- vlib/compiler/module_header.v | 2 +- vlib/compiler/modules.v | 2 +- vlib/compiler/msvc.v | 6 ++-- vlib/compiler/scanner.v | 2 +- vlib/compiler/vtmp.v | 2 +- vlib/compiler/vtools.v | 2 +- vlib/freetype/freetype.v | 4 +-- vlib/gg/gg.v | 2 +- vlib/os/os.v | 42 +++++++++++++++++-------- vlib/os/os_nix.v | 12 +++---- vlib/os/os_test.v | 4 +-- vlib/os/os_windows.v | 16 +++++----- vlib/vweb/assets/assets.v | 6 ++-- vlib/vweb/tmpl/tmpl.v | 2 +- 28 files changed, 116 insertions(+), 98 deletions(-) diff --git a/tools/fast/fast.v b/tools/fast/fast.v index 468fcbe692..2c7b426f7d 100644 --- a/tools/fast/fast.v +++ b/tools/fast/fast.v @@ -9,7 +9,7 @@ fn main() { exe := os.executable() dir := os.dir(exe) vdir := os.dir(os.dir(dir)) - if !os.file_exists('$vdir/v') && !os.dir_exists('$vdir/vlib') { + if !os.exists('$vdir/v') && !os.is_dir('$vdir/vlib') { println('fast.html generator needs to be located in `v/tools/fast/`') } println('fast.html generator\n') @@ -18,7 +18,7 @@ fn main() { exec('git pull --rebase') mut commit_hash := exec('git rev-parse HEAD') commit_hash = commit_hash[..7] - if !os.file_exists('table.html') { + if !os.exists('table.html') { os.create('table.html') or { panic(err) } } mut table := os.read_file('table.html') or { panic(err) } diff --git a/tools/gen_vc.v b/tools/gen_vc.v index cf27b3311c..7de7a76231 100644 --- a/tools/gen_vc.v +++ b/tools/gen_vc.v @@ -114,18 +114,18 @@ fn main() { fp.version(app_version) fp.description(app_description) fp.skip_executable() - + show_help:=fp.bool('help', false, 'Show this help screen\n') flag_options := parse_flags(mut fp) - + if( show_help ){ println( fp.usage() ) exit(0) } - + fp.finalize() or { eprintln(err) println(fp.usage()) return } - + // webhook server mode if flag_options.serve { app := WebhookServer{ gen_vc: new_gen_vc(flag_options) } @@ -198,11 +198,11 @@ fn (gen_vc mut GenVC) generate() { gen_vc.gen_error = false // check if gen_vc dir exists - if !os.dir_exists(gen_vc.options.work_dir) { + if !os.is_dir(gen_vc.options.work_dir) { // try create os.mkdir(gen_vc.options.work_dir) or { panic(err) } // still dosen't exist... we have a problem - if !os.dir_exists(gen_vc.options.work_dir) { + if !os.is_dir(gen_vc.options.work_dir) { gen_vc.logger.error('error creating directory: $gen_vc.options.work_dir') gen_vc.gen_error = true return @@ -211,12 +211,12 @@ fn (gen_vc mut GenVC) generate() { // cd to gen_vc dir os.chdir(gen_vc.options.work_dir) - + // if we are not running with the --serve flag (webhook server) // rather than deleting and re-downloading the repo each time // first check to see if the local v repo is behind master // if it isn't behind theres no point continuing further - if !gen_vc.options.serve && os.dir_exists(git_repo_dir_v) { + if !gen_vc.options.serve && os.is_dir(git_repo_dir_v) { gen_vc.cmd_exec('git -C $git_repo_dir_v checkout master') // fetch the remote repo just in case there are newer commits there gen_vc.cmd_exec('git -C $git_repo_dir_v fetch') @@ -229,11 +229,11 @@ fn (gen_vc mut GenVC) generate() { // delete repos gen_vc.purge_repos() - + // clone repos gen_vc.cmd_exec('git clone --depth 1 https://$git_repo_v $git_repo_dir_v') gen_vc.cmd_exec('git clone --depth 1 https://$git_repo_vc $git_repo_dir_vc') - + // get output of git log -1 (last commit) git_log_v := gen_vc.cmd_exec('git -C $git_repo_dir_v log -1 --format="commit %H%nDate: %ci%nDate Unix: %ct"') git_log_vc := gen_vc.cmd_exec('git -C $git_repo_dir_vc log -1 --format="Commit %H%nDate: %ci%nDate Unix: %ct"') @@ -241,7 +241,7 @@ fn (gen_vc mut GenVC) generate() { // date of last commit in each repo ts_v := git_log_v.find_between('Date:', '\n').trim_space() ts_vc := git_log_vc.find_between('Date:', '\n').trim_space() - + // parse time as string to time.Time last_commit_time_v := time.parse(ts_v) last_commit_time_vc := time.parse(ts_vc) @@ -259,7 +259,7 @@ fn (gen_vc mut GenVC) generate() { gen_vc.logger.debug('last commit time ($git_repo_v): ' + last_commit_time_v.format_ss()) gen_vc.logger.debug('last commit time ($git_repo_vc): ' + last_commit_time_vc.format_ss()) gen_vc.logger.debug('last commit hash ($git_repo_v): $last_commit_hash_v') - + // if vc repo already has a newer commit than the v repo, assume it's up to date if t_unix_vc >= t_unix_v { gen_vc.logger.warn('vc repository is already up to date.') @@ -271,7 +271,7 @@ fn (gen_vc mut GenVC) generate() { v_exec := '$git_repo_dir_v/v' // check if make was successful gen_vc.assert_file_exists_and_is_not_too_short(v_exec, err_msg_make) - + // build v.c for each os for os_name in vc_build_oses { vc_suffix := if os_name == 'nix' { '' } else { '_${os_name[..3]}' } @@ -292,7 +292,7 @@ fn (gen_vc mut GenVC) generate() { } // check if the vc repo actually changed - git_status := gen_vc.cmd_exec('git -C $git_repo_dir_vc status') + git_status := gen_vc.cmd_exec('git -C $git_repo_dir_vc status') if git_status.contains('nothing to commit') { gen_vc.logger.error('no changes to vc repo: something went wrong.') gen_vc.gen_error = true @@ -349,12 +349,12 @@ fn (gen_vc mut GenVC) command_execute_dry(cmd string) string { fn (gen_vc mut GenVC) purge_repos() { // delete old repos (better to be fully explicit here, since these are destructive operations) mut repo_dir := '$gen_vc.options.work_dir/$git_repo_dir_v' - if os.dir_exists(repo_dir) { + if os.is_dir(repo_dir) { gen_vc.logger.info('purging local repo: "$repo_dir"') gen_vc.cmd_exec('rm -rf $repo_dir') } repo_dir = '$gen_vc.options.work_dir/$git_repo_dir_vc' - if os.dir_exists(repo_dir) { + if os.is_dir(repo_dir) { gen_vc.logger.info('purging local repo: "$repo_dir"') gen_vc.cmd_exec('rm -rf $repo_dir') } @@ -362,7 +362,7 @@ fn (gen_vc mut GenVC) purge_repos() { // check if file size is too short fn (gen_vc mut GenVC) assert_file_exists_and_is_not_too_short(f string, emsg string){ - if !os.file_exists(f) { + if !os.exists(f) { gen_vc.logger.error('$err_msg_build: $emsg .') gen_vc.gen_error = true return diff --git a/tools/modules/testing/common.v b/tools/modules/testing/common.v index 8a5254dfe3..0e09ed42bd 100644 --- a/tools/modules/testing/common.v +++ b/tools/modules/testing/common.v @@ -94,7 +94,7 @@ pub fn (ts mut TestSession) test() { pub fn vlib_should_be_present( parent_dir string ) { vlib_dir := filepath.join( parent_dir, 'vlib' ) - if !os.dir_exists( vlib_dir ){ + if !os.is_dir( vlib_dir ){ eprintln('$vlib_dir is missing, it must be next to the V executable') exit(1) } diff --git a/tools/vpm.v b/tools/vpm.v index 1241fd1f62..d1906207a7 100644 --- a/tools/vpm.v +++ b/tools/vpm.v @@ -152,7 +152,7 @@ fn get_vmodules_dir_path() string { fn ensure_vmodules_dir_exist() { home_vmodules := get_vmodules_dir_path() - if !os.dir_exists( home_vmodules ) { + if !os.is_dir( home_vmodules ) { println('Creating $home_vmodules/ ...') os.mkdir(home_vmodules) or { panic(err) } } diff --git a/tools/vrepl.v b/tools/vrepl.v index a15b8549a8..3b445aa9d2 100644 --- a/tools/vrepl.v +++ b/tools/vrepl.v @@ -200,7 +200,7 @@ fn print_output(s os.Result) { } fn main() { - if os.args.len < 2 || !os.file_exists(os.args[1]) { + if os.args.len < 2 || !os.exists(os.args[1]) { println('Usage:') println(' vrepl vexepath\n') println(' ... where vexepath is the full path to the v executable file') diff --git a/tools/vtest-compiler.v b/tools/vtest-compiler.v index 67d697a448..33da2720c0 100644 --- a/tools/vtest-compiler.v +++ b/tools/vtest-compiler.v @@ -26,7 +26,7 @@ fn v_test_compiler(vargs string){ os.chdir( parent_dir ) /* - if !os.file_exists(parent_dir + '/v.v') { + if !os.exists(parent_dir + '/v.v') { eprintln('v.v is missing, it must be next to the V executable') exit(1) } @@ -34,7 +34,7 @@ fn v_test_compiler(vargs string){ // Make sure v.c can be compiled without warnings $if mac { - if os.file_exists('/v.v') { + if os.exists('/v.v') { os.system('$vexe -o v.c v.v') if os.system('cc -Werror v.c') != 0 { eprintln('cc failed to build v.c without warnings') @@ -63,7 +63,7 @@ fn v_test_compiler(vargs string){ if ret != 0 { eprintln('failed to run v install') } - if !os.file_exists(v_modules_path + '/nedpals/args') { + if !os.exists(v_modules_path + '/nedpals/args') { eprintln('v failed to install a test module') } vmark.stop() diff --git a/tools/vtest.v b/tools/vtest.v index cbd9d613b5..ba192501b8 100644 --- a/tools/vtest.v +++ b/tools/vtest.v @@ -32,11 +32,11 @@ pub fn main() { mut ts := testing.new_test_sesion(args_before) for targ in args_after.split(' ') { - if os.file_exists(targ) && targ.ends_with('_test.v') { + if os.exists(targ) && targ.ends_with('_test.v') { ts.files << targ continue } - if os.dir_exists(targ) { + if os.is_dir(targ) { // Fetch all tests from the directory ts.files << os.walk_ext( targ.trim_right(os.path_separator), '_test.v') continue diff --git a/tools/vup.v b/tools/vup.v index fd156e4b90..8369b39573 100644 --- a/tools/vup.v +++ b/tools/vup.v @@ -8,7 +8,7 @@ fn main() { println(s.output) $if windows { v_backup_file := '$vroot/v_old.exe' - if os.file_exists( v_backup_file ) { + if os.exists( v_backup_file ) { os.rm( v_backup_file ) } os.mv_by_cp('$vroot/v.exe', v_backup_file) or { panic(err) } diff --git a/vlib/builtin/bare/syscallwrapper_test.v b/vlib/builtin/bare/syscallwrapper_test.v index f98797f104..b293978c8e 100644 --- a/vlib/builtin/bare/syscallwrapper_test.v +++ b/vlib/builtin/bare/syscallwrapper_test.v @@ -8,11 +8,11 @@ fn test_syscallwrappers() { vdir := os.dir(exe) if vdir.len > 1 { dot_checks := vdir + "/.checks" - assert os.dir_exists(dot_checks) + assert os.is_dir(dot_checks) os.chdir(dot_checks) checks_v := "checks.v" - assert os.file_exists(checks_v) + assert os.exists(checks_v) rc := os.exec("v run $checks_v") or { panic(err) } assert !rc.output.contains("V panic: An assertion failed.") assert !rc.output.contains("failed") diff --git a/vlib/compiler/cc.v b/vlib/compiler/cc.v index 6dd822cf43..8e1ecc9dc9 100644 --- a/vlib/compiler/cc.v +++ b/vlib/compiler/cc.v @@ -38,7 +38,7 @@ fn (v mut V) cc() { $if !js { if v.out_name.ends_with('.js') { vjs_path := vexe + 'js' - if !os.file_exists(vjs_path) { + if !os.exists(vjs_path) { println('V.js compiler not found, building...') // Build V.js. Specifying `-os js` makes V include // only _js.v files and ignore _c.v files. @@ -98,7 +98,7 @@ fn (v mut V) cc() { tcc_3rd := '$vdir/thirdparty/tcc/bin/tcc' //println('tcc third "$tcc_3rd"') tcc_path := '/var/tmp/tcc/bin/tcc' - if os.file_exists(tcc_3rd) && !os.file_exists(tcc_path) { + if os.exists(tcc_3rd) && !os.exists(tcc_path) { //println('moving tcc') // if there's tcc in thirdparty/, that means this is // a prebuilt V_linux.zip. @@ -106,7 +106,7 @@ fn (v mut V) cc() { // it to /var/tmp/ os.system('mv $vdir/thirdparty/tcc /var/tmp/') } - if v.pref.ccompiler == 'cc' && os.file_exists(tcc_path) { + if v.pref.ccompiler == 'cc' && os.exists(tcc_path) { // TODO tcc bug, needs an empty libtcc1.a fila //os.mkdir('/var/tmp/tcc/lib/tcc/') or { panic(err) } //os.create('/var/tmp/tcc/lib/tcc/libtcc1.a') @@ -136,7 +136,7 @@ fn (v mut V) cc() { '$v_modules_path${os.path_separator}$v.dir' } pdir := out_dir.all_before_last(os.path_separator) - if !os.dir_exists(pdir) { + if !os.is_dir(pdir) { os.mkdir_all(pdir) } v.out_name = '${out_dir}.o' //v.out_name @@ -202,7 +202,7 @@ fn (v mut V) cc() { else if v.pref.is_cache { builtin_o_path := filepath.join(v_modules_path, 'cache', 'vlib', 'builtin.o') a << builtin_o_path.replace('builtin.o', 'strconv.o') // TODO hack no idea why this is needed - if os.file_exists(builtin_o_path) { + if os.exists(builtin_o_path) { libs = builtin_o_path } else { println('$builtin_o_path not found... building module builtin') @@ -215,7 +215,7 @@ fn (v mut V) cc() { imp_path := imp.replace('.', os.path_separator) path := '$v_modules_path${os.path_separator}cache${os.path_separator}vlib${os.path_separator}${imp_path}.o' //println('adding ${imp_path}.o') - if os.file_exists(path) { + if os.exists(path) { libs += ' ' + path } else { println('$path not found... building module $imp') @@ -253,7 +253,7 @@ fn (v mut V) cc() { // // Output executable name a << '-o "$v.out_name"' - if os.dir_exists(v.out_name) { + if os.is_dir(v.out_name) { verror('\'$v.out_name\' is a directory') } // macOS code can include objective C TODO remove once objective C is replaced with C @@ -426,7 +426,7 @@ fn (c mut V) cc_windows_cross() { mut libs := '' if c.pref.build_mode == .default_mode { libs = '"$v_modules_path/vlib/builtin.o"' - if !os.file_exists(libs) { + if !os.exists(libs) { println('`$libs` not found') exit(1) } @@ -442,7 +442,7 @@ fn (c mut V) cc_windows_cross() { } println('Cross compiling for Windows...') winroot := '$v_modules_path/winroot' - if !os.dir_exists(winroot) { + if !os.is_dir(winroot) { winroot_url := 'https://github.com/vlang/v/releases/download/v0.1.10/winroot.zip' println('"$winroot" not found.') println('Download it from $winroot_url and save it in $v_modules_path') @@ -502,7 +502,7 @@ fn find_c_compiler() string { fn find_c_compiler_default() string { //fast_clang := '/usr/local/Cellar/llvm/8.0.0/bin/clang' - //if os.file_exists(fast_clang) { + //if os.exists(fast_clang) { // return fast_clang //} // TODO fix $if after 'string' diff --git a/vlib/compiler/cgen.v b/vlib/compiler/cgen.v index 10757df406..c0815cbaf8 100644 --- a/vlib/compiler/cgen.v +++ b/vlib/compiler/cgen.v @@ -255,7 +255,7 @@ fn (g mut CGen) add_to_main(s string) { fn build_thirdparty_obj_file(path string, moduleflags []CFlag) { obj_path := os.realpath(path) - if os.file_exists(obj_path) { + if os.exists(obj_path) { return } println('$obj_path not found, building it...') diff --git a/vlib/compiler/comptime.v b/vlib/compiler/comptime.v index 242781e7cf..dce8a84d97 100644 --- a/vlib/compiler/comptime.v +++ b/vlib/compiler/comptime.v @@ -153,12 +153,12 @@ fn (p mut Parser) comp_time() { if p.pref.is_debug { println('compiling tmpl $path') } - if !os.file_exists(path) { + if !os.exists(path) { // Can't find the template file in current directory, // try looking next to the vweb program, in case it's run with // v path/to/vweb_app.v path = os.dir(p.scanner.file_path) + '/' + path - if !os.file_exists(path) { + if !os.exists(path) { p.error('vweb HTML template "$path" not found') } } diff --git a/vlib/compiler/live.v b/vlib/compiler/live.v index 963d201461..846022f090 100644 --- a/vlib/compiler/live.v +++ b/vlib/compiler/live.v @@ -191,7 +191,7 @@ void reload_so() { sprintf(compile_cmd, "$vexe $msvc -o %s -shared $file", new_so_base); os__system(tos2(compile_cmd)); - if( !os__file_exists(tos2(new_so_name)) ) { + if( !os__exists(tos2(new_so_name)) ) { fprintf(stderr, "Errors while compiling $file\\n"); continue; } diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index a14a0be049..ccc30672e7 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -587,13 +587,13 @@ pub fn (v V) run_compiled_executable_and_exit() { pub fn (v &V) v_files_from_dir(dir string) []string { mut res := []string - if !os.file_exists(dir) { - if dir == 'compiler' && os.dir_exists('vlib') { + if !os.exists(dir) { + if dir == 'compiler' && os.is_dir('vlib') { println('looks like you are trying to build V with an old command') println('use `v -o v v.v` instead of `v -o v compiler`') } verror("$dir doesn't exist") - } else if !os.dir_exists(dir) { + } else if !os.is_dir(dir) { verror("$dir isn't a directory") } mut files := os.ls(dir) or { panic(err) } @@ -639,7 +639,7 @@ pub fn (v mut V) add_v_files_to_compile() { } // Builtin cache exists? Use it. builtin_vh := '${v.pref.vlib_path}${os.path_separator}builtin.vh' - if v.pref.is_cache && os.file_exists(builtin_vh) { + if v.pref.is_cache && os.exists(builtin_vh) { v.cached_mods << 'builtin' builtin_files = [builtin_vh] } @@ -685,7 +685,7 @@ pub fn (v mut V) add_v_files_to_compile() { if v.pref.vpath != '' && v.pref.build_mode != .build_module && !mod.contains('vweb') { mod_path := mod.replace('.', os.path_separator) vh_path := '$v_modules_path${os.path_separator}vlib${os.path_separator}${mod_path}.vh' - if v.pref.is_cache && os.file_exists(vh_path) { + if v.pref.is_cache && os.exists(vh_path) { eprintln('using cached module `$mod`: $vh_path') v.cached_mods << mod v.files << vh_path @@ -850,7 +850,7 @@ pub fn (v &V) log(s string) { pub fn new_v(args[]string) &V { // Create modules dirs if they are missing - if !os.dir_exists(v_modules_path) { + if !os.is_dir(v_modules_path) { os.mkdir(v_modules_path) or { panic(err) } os.mkdir('$v_modules_path${os.path_separator}cache') or { panic(err) } } @@ -923,7 +923,7 @@ pub fn new_v(args[]string) &V { } is_test := dir.ends_with('_test.v') is_script := dir.ends_with('.v') || dir.ends_with('.vsh') - if is_script && !os.file_exists(dir) { + if is_script && !os.exists(dir) { println('`$dir` does not exist') exit(1) } @@ -933,7 +933,7 @@ pub fn new_v(args[]string) &V { // Building V? Use v2, since we can't overwrite a running // executable on Windows + the precompiled V is more // optimized. - if out_name == 'v' && os.dir_exists('vlib/compiler') { + if out_name == 'v' && os.is_dir('vlib/compiler') { println('Saving the resulting V executable in `./v2`') println('Use `v -o v v.v` if you want to replace current '+ 'V executable.') @@ -948,7 +948,7 @@ pub fn new_v(args[]string) &V { // `v -o dir/exec`, create "dir/" if it doesn't exist if out_name.contains(os.path_separator) { d := out_name.all_before_last(os.path_separator) - if !os.dir_exists(d) { + if !os.is_dir(d) { println('creating a new directory "$d"') os.mkdir(d) or { panic(err) } } @@ -989,7 +989,7 @@ pub fn new_v(args[]string) &V { } //println('VROOT=$vroot') // v.exe's parent directory should contain vlib - if !os.dir_exists(vlib_path) || !os.dir_exists(vlib_path + os.path_separator + 'builtin') { + if !os.is_dir(vlib_path) || !os.is_dir(vlib_path + os.path_separator + 'builtin') { //println('vlib not found, downloading it...') /* ret := os.system('git clone --depth=1 https://github.com/vlang/v .') @@ -1104,7 +1104,7 @@ pub fn env_vflags_and_os_args() []string { pub fn vfmt(args[]string) { println('running vfmt...') file := args.last() - if !os.file_exists(file) { + if !os.exists(file) { println('"$file" does not exist') exit(1) } diff --git a/vlib/compiler/module_header.v b/vlib/compiler/module_header.v index 2d02a0ebd1..f75c5c00bc 100644 --- a/vlib/compiler/module_header.v +++ b/vlib/compiler/module_header.v @@ -34,7 +34,7 @@ fn generate_vh(mod string) { dir := if mod.starts_with('vlib') { '$compiler.v_modules_path${os.path_separator}$mod' } else { mod } path := dir + '.vh' pdir := dir.all_before_last(os.path_separator) - if !os.dir_exists(pdir) { + if !os.is_dir(pdir) { os.mkdir_all(pdir) // os.mkdir(os.realpath(dir)) or { panic(err) } } diff --git a/vlib/compiler/modules.v b/vlib/compiler/modules.v index bb6f7b0a7e..2a3a0888eb 100644 --- a/vlib/compiler/modules.v +++ b/vlib/compiler/modules.v @@ -170,7 +170,7 @@ fn (v &V) find_module_path(mod string) ?string { } for try_path in tried_paths { if v.pref.is_verbose { println(' >> trying to find $mod in $try_path ...') } - if os.dir_exists(try_path) { + if os.is_dir(try_path) { return try_path } } diff --git a/vlib/compiler/msvc.v b/vlib/compiler/msvc.v index 9ef79b3e3d..b18f0fe4c8 100644 --- a/vlib/compiler/msvc.v +++ b/vlib/compiler/msvc.v @@ -166,7 +166,7 @@ fn find_vs(vswhere_dir string, host_arch string) ?VsInstallation { lib_path := '$res.output\\VC\\Tools\\MSVC\\$v\\lib\\$host_arch' include_path := '$res.output\\VC\\Tools\\MSVC\\$v\\include' - if os.file_exists('$lib_path\\vcruntime.lib') { + if os.exists('$lib_path\\vcruntime.lib') { p := '$res.output\\VC\\Tools\\MSVC\\$v\\bin\\Host$host_arch\\$host_arch' // println('$lib_path $include_path') @@ -274,7 +274,7 @@ pub fn (v mut V) cc_msvc() { /* b := os.realpath( '$v_modules_path/vlib/builtin.obj' ) alibs << '"$b"' - if !os.file_exists(b) { + if !os.exists(b) { println('`builtin.obj` not found') exit(1) } @@ -390,7 +390,7 @@ fn build_thirdparty_obj_file_with_msvc(path string, moduleflags []CFlag) { obj_path = os.realpath(obj_path) - if os.file_exists(obj_path) { + if os.exists(obj_path) { println('$obj_path already build.') return } diff --git a/vlib/compiler/scanner.v b/vlib/compiler/scanner.v index bb101c563b..67b42f87e5 100644 --- a/vlib/compiler/scanner.v +++ b/vlib/compiler/scanner.v @@ -48,7 +48,7 @@ mut: // new scanner from file. fn new_scanner_file(file_path string) &Scanner { - if !os.file_exists(file_path) { + if !os.exists(file_path) { verror("$file_path doesn't exist") } diff --git a/vlib/compiler/vtmp.v b/vlib/compiler/vtmp.v index 5797e36b89..48158cb655 100644 --- a/vlib/compiler/vtmp.v +++ b/vlib/compiler/vtmp.v @@ -9,7 +9,7 @@ import filepath pub fn get_vtmp_folder() string { vtmp := filepath.join(os.tmpdir(),'v') - if !os.dir_exists( vtmp ) { + if !os.is_dir( vtmp ) { os.mkdir(vtmp) or { panic(err) } } return vtmp diff --git a/vlib/compiler/vtools.v b/vlib/compiler/vtools.v index fe8562dda2..50777fcb18 100644 --- a/vlib/compiler/vtools.v +++ b/vlib/compiler/vtools.v @@ -15,7 +15,7 @@ pub fn launch_tool(tname string){ //println('Launching: "$tool_command" ...') mut tool_should_be_recompiled := false - if !os.file_exists( tool_exe ) { + if !os.exists( tool_exe ) { // fresh checkout tool_should_be_recompiled = true }else{ diff --git a/vlib/freetype/freetype.v b/vlib/freetype/freetype.v index e1649c94f6..a1898e60e9 100644 --- a/vlib/freetype/freetype.v +++ b/vlib/freetype/freetype.v @@ -173,12 +173,12 @@ pub fn new_context(cfg gg.Cfg) &FreeType { if font_path == '' { font_path = 'RobotoMono-Regular.ttf' } - if !os.file_exists(font_path) { + if !os.exists(font_path) { exe_path := os.executable() exe_dir := os.basedir(exe_path) font_path = '$exe_dir/$font_path' } - if !os.file_exists(font_path) { + if !os.exists(font_path) { println('failed to load $font_path') return 0 } diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index d453a1fc5a..208664780d 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -363,7 +363,7 @@ pub fn create_image(file string) u32 { if file.contains('twitch') { return u32(0)// TODO } - if !os.file_exists(file) { + if !os.exists(file) { println('gg create image no such file "$file"') return u32(0) } diff --git a/vlib/os/os.v b/vlib/os/os.v index f0f0b35857..b2a5862d5f 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -164,7 +164,7 @@ pub fn cp(old, new string) ?bool { pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool{ source_path := os.realpath( osource_path ) dest_path := os.realpath( odest_path ) - if !os.file_exists(source_path) { + if !os.exists(source_path) { return error('Source path doesn\'t exist') } //single file copy @@ -174,7 +174,7 @@ pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool{ } else { dest_path } - if os.file_exists(adjasted_path) { + if os.exists(adjasted_path) { if overwrite { os.rm(adjasted_path) } @@ -523,16 +523,21 @@ pub fn unsetenv(name string) int { } } -// file_exists returns true if `path` exists. -pub fn file_exists(_path string) bool { +// exists returns true if `path` exists. +pub fn exists(path string) bool { $if windows { - path := _path.replace('/', '\\') - return C._waccess(path.to_wide(), 0) != -1 + p := path.replace('/', '\\') + return C._waccess(p.to_wide(), 0) != -1 } $else { - return C.access(_path.str, 0 ) != -1 + return C.access(path.str, 0 ) != -1 } } +[deprecated] +pub fn file_exists(_path string) bool { + panic('use os.exists(path) instead of os.file_exists(path)') +} + // rm removes file in `path`. pub fn rm(path string) { $if windows { @@ -822,13 +827,24 @@ pub fn executable() string { return os.args[0] } +[deprecated] +pub fn dir_exists(path string) bool { + panic('use os.is_dir()') + //return false +} + // is_dir returns a boolean indicating whether the given path is a directory. pub fn is_dir(path string) bool { $if windows { - return dir_exists(path) - //val := int(C.GetFileAttributes(path.to_wide())) - // Note: this return is broke (wrong). we have dir_exists already how will this differ? - //return (val &FILE_ATTRIBUTE_DIRECTORY) > 0 + _path := path.replace('/', '\\') + attr := C.GetFileAttributesW(_path.to_wide()) + if int(attr) == int(C.INVALID_FILE_ATTRIBUTES) { + return false + } + if (int(attr) & C.FILE_ATTRIBUTE_DIRECTORY) != 0 { + return true + } + return false } $else { statbuf := C.stat{} @@ -927,7 +943,7 @@ pub fn walk(path string, fnc fn(path string)) { if os.is_dir(p) { walk(p, fnc) } - else if os.file_exists(p) { + else if os.exists(p) { fnc(p) } } @@ -996,7 +1012,7 @@ pub fn mkdir_all(path string) { mut p := if path.starts_with(os.path_separator) { os.path_separator } else { '' } for subdir in path.split(os.path_separator) { p += subdir + os.path_separator - if !os.dir_exists(p) { + if !os.is_dir(p) { os.mkdir(p) or { panic(err) } } } diff --git a/vlib/os/os_nix.v b/vlib/os/os_nix.v index 2989ad1088..ddb7ac5266 100644 --- a/vlib/os/os_nix.v +++ b/vlib/os/os_nix.v @@ -47,12 +47,11 @@ pub fn ls(path string) ?[]string { return res } -pub fn dir_exists(path string) bool { - /* - $if linux { - C.syscall(4, path.str) // sys_newstat - } - */ +/* +pub fn is_dir(path string) bool { + //$if linux { + //C.syscall(4, path.str) // sys_newstat + //} dir := C.opendir(path.str) res := !isnil(dir) if res { @@ -60,6 +59,7 @@ pub fn dir_exists(path string) bool { } return res } +*/ // mkdir creates a new directory with the specified path. pub fn mkdir(path string) ?bool { diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 911cc5d506..81daaa4120 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -76,14 +76,14 @@ fn test_write_and_read_bytes() { fn test_create_and_delete_folder() { folder := './test1' os.mkdir(folder) or { panic(err) } - assert os.dir_exists(folder) + assert os.is_dir(folder) folder_contents := os.ls(folder) or { panic(err) } assert folder_contents.len == 0 os.rmdir(folder) - folder_exists := os.dir_exists(folder) + folder_exists := os.is_dir(folder) assert folder_exists == false } diff --git a/vlib/os/os_windows.v b/vlib/os/os_windows.v index 33a3dd1a4a..69ef881d92 100644 --- a/vlib/os/os_windows.v +++ b/vlib/os/os_windows.v @@ -46,7 +46,7 @@ mut: } struct StartupInfo { -mut: +mut: cb u32 lpReserved &u16 lpDesktop &u16 @@ -89,13 +89,13 @@ fn init_os_args(argc int, argv &byteptr) []string { pub fn ls(path string) ?[]string { mut find_file_data := Win32finddata{} mut dir_files := []string - // We can also check if the handle is valid. but using dir_exists instead + // We can also check if the handle is valid. but using is_dir instead // h_find_dir := C.FindFirstFile(path.str, &find_file_data) // if (INVALID_HANDLE_VALUE == h_find_dir) { // return dir_files // } // C.FindClose(h_find_dir) - if !dir_exists(path) { + if !is_dir(path) { return error('ls() couldnt open dir "$path": directory does not exist') } // NOTE: Should eventually have path struct & os dependant path seperator (eg os.PATH_SEPERATOR) @@ -118,7 +118,8 @@ pub fn ls(path string) ?[]string { return dir_files } -pub fn dir_exists(path string) bool { +/* +pub fn is_dir(path string) bool { _path := path.replace('/', '\\') attr := C.GetFileAttributesW(_path.to_wide()) if int(attr) == int(C.INVALID_FILE_ATTRIBUTES) { @@ -129,6 +130,7 @@ pub fn dir_exists(path string) bool { } return false } +*/ @@ -250,7 +252,7 @@ pub fn exec(cmd string) ?Result { panic('exec failed (SetHandleInformation): $error_msg') } - proc_info := ProcessInformation{} + proc_info := ProcessInformation{} mut start_info := StartupInfo{} start_info.cb = sizeof(C.PROCESS_INFORMATION) start_info.hStdInput = child_stdin @@ -265,7 +267,7 @@ pub fn exec(cmd string) ?Result { return error('exec failed (CreateProcess): $error_msg') } C.CloseHandle(child_stdin) - C.CloseHandle(child_stdout_write) + C.CloseHandle(child_stdout_write) buf := [1000]byte mut bytes_read := u32(0) mut read_data := '' @@ -274,7 +276,7 @@ pub fn exec(cmd string) ?Result { read_data += tos(buf, int(bytes_read)) if readfile_result == false || int(bytes_read) == 0 { break - } + } } read_data = read_data.trim_space() exit_code := u32(0) diff --git a/vlib/vweb/assets/assets.v b/vlib/vweb/assets/assets.v index 963653f4e9..c3a01f9fab 100644 --- a/vlib/vweb/assets/assets.v +++ b/vlib/vweb/assets/assets.v @@ -76,7 +76,7 @@ fn (am mut AssetManager) combine(asset_type string, to_file bool) string { out_file := '$am.cache_dir/${cache_key}.$asset_type' mut out := '' // use cache - if os.file_exists(out_file) { + if os.exists(out_file) { if to_file { return out_file } @@ -102,7 +102,7 @@ fn (am mut AssetManager) combine(asset_type string, to_file bool) string { if !to_file { return out } - if !os.dir_exists(am.cache_dir) { + if !os.is_dir(am.cache_dir) { os.mkdir(am.cache_dir) or { panic(err) } } mut file := os.create(out_file) or { @@ -153,7 +153,7 @@ fn (am mut AssetManager) include(asset_type string, combine bool) string { // dont return option until size limit is removed // fn (am mut AssetManager) add(asset_type, file string) ?bool { fn (am mut AssetManager) add(asset_type, file string) bool { - if !os.file_exists(file) { + if !os.exists(file) { // return error('vweb.assets: cannot add asset $file, it does not exist') return false } diff --git a/vlib/vweb/tmpl/tmpl.v b/vlib/vweb/tmpl/tmpl.v index 9d5fe72956..f85b452577 100644 --- a/vlib/vweb/tmpl/tmpl.v +++ b/vlib/vweb/tmpl/tmpl.v @@ -19,7 +19,7 @@ pub fn compile_template(path string) string { panic('html failed') } mut header := '' - if os.file_exists('header.html') { + if os.exists('header.html') { h := os.read_file('header.html') or { panic('html failed') }