mirror of
				https://github.com/vlang/v.git
				synced 2023-08-10 21:13:21 +03:00 
			
		
		
		
	os: mkdir() error handling
This commit is contained in:
		
				
					committed by
					
						
						Alexander Medvednikov
					
				
			
			
				
	
			
			
			
						parent
						
							0fb0c43c0a
						
					
				
				
					commit
					3a6ccf7f31
				
			@@ -196,7 +196,7 @@ fn (gen_vc mut GenVC) generate() {
 | 
				
			|||||||
	// check if gen_vc dir exists
 | 
						// check if gen_vc dir exists
 | 
				
			||||||
	if !os.dir_exists(gen_vc.options.work_dir) {
 | 
						if !os.dir_exists(gen_vc.options.work_dir) {
 | 
				
			||||||
		// try create
 | 
							// try create
 | 
				
			||||||
		os.mkdir(gen_vc.options.work_dir)
 | 
							os.mkdir(gen_vc.options.work_dir) or { panic(err) }
 | 
				
			||||||
		// still dosen't exist... we have a problem
 | 
							// still dosen't exist... we have a problem
 | 
				
			||||||
		if !os.dir_exists(gen_vc.options.work_dir) {
 | 
							if !os.dir_exists(gen_vc.options.work_dir) {
 | 
				
			||||||
			gen_vc.logger.error('error creating directory: $gen_vc.options.work_dir')
 | 
								gen_vc.logger.error('error creating directory: $gen_vc.options.work_dir')
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -48,7 +48,7 @@ fn main() {
 | 
				
			|||||||
	c.description = os.get_line()
 | 
						c.description = os.get_line()
 | 
				
			||||||
	println('Initialising ...')
 | 
						println('Initialising ...')
 | 
				
			||||||
	if (os.is_dir(c.name)) { cerror('folder already exists') exit(3) }
 | 
						if (os.is_dir(c.name)) { cerror('folder already exists') exit(3) }
 | 
				
			||||||
	os.mkdir(c.name)
 | 
						os.mkdir(c.name) or { panic(err) }
 | 
				
			||||||
	c.write_vmod()
 | 
						c.write_vmod()
 | 
				
			||||||
	c.write_main()
 | 
						c.write_main()
 | 
				
			||||||
	println('Complete !')
 | 
						println('Complete !')
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -154,7 +154,7 @@ fn ensure_vmodules_dir_exist() {
 | 
				
			|||||||
	home_vmodules := get_vmodules_dir_path()
 | 
						home_vmodules := get_vmodules_dir_path()
 | 
				
			||||||
	if !os.dir_exists( home_vmodules ) {
 | 
						if !os.dir_exists( home_vmodules ) {
 | 
				
			||||||
		println('Creating $home_vmodules/ ...')
 | 
							println('Creating $home_vmodules/ ...')
 | 
				
			||||||
		os.mkdir(home_vmodules)
 | 
							os.mkdir(home_vmodules) or { panic(err) }
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -108,7 +108,7 @@ fn (v mut V) cc() {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
			if v.pref.ccompiler == 'cc' && os.file_exists(tcc_path) {
 | 
								if v.pref.ccompiler == 'cc' && os.file_exists(tcc_path) {
 | 
				
			||||||
				// TODO tcc bug, needs an empty libtcc1.a fila
 | 
									// TODO tcc bug, needs an empty libtcc1.a fila
 | 
				
			||||||
				//os.mkdir('/var/tmp/tcc/lib/tcc/')
 | 
									//os.mkdir('/var/tmp/tcc/lib/tcc/') or { panic(err) }
 | 
				
			||||||
				//os.create('/var/tmp/tcc/lib/tcc/libtcc1.a')
 | 
									//os.create('/var/tmp/tcc/lib/tcc/libtcc1.a')
 | 
				
			||||||
				v.pref.ccompiler = tcc_path
 | 
									v.pref.ccompiler = tcc_path
 | 
				
			||||||
				a << '-m64'
 | 
									a << '-m64'
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -843,8 +843,8 @@ pub fn (v &V) log(s string) {
 | 
				
			|||||||
pub fn new_v(args[]string) &V {
 | 
					pub fn new_v(args[]string) &V {
 | 
				
			||||||
	// Create modules dirs if they are missing
 | 
						// Create modules dirs if they are missing
 | 
				
			||||||
	if !os.dir_exists(v_modules_path) {
 | 
						if !os.dir_exists(v_modules_path) {
 | 
				
			||||||
		os.mkdir(v_modules_path)
 | 
							os.mkdir(v_modules_path) or { panic(err) }
 | 
				
			||||||
		os.mkdir('$v_modules_path${os.path_separator}cache')
 | 
							os.mkdir('$v_modules_path${os.path_separator}cache') or { panic(err) }
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	// Location of all vlib files
 | 
						// Location of all vlib files
 | 
				
			||||||
@@ -903,7 +903,7 @@ pub fn new_v(args[]string) &V {
 | 
				
			|||||||
		// Cross compiling? Use separate dirs for each os
 | 
							// Cross compiling? Use separate dirs for each os
 | 
				
			||||||
		/*
 | 
							/*
 | 
				
			||||||
		if target_os != os.user_os() {
 | 
							if target_os != os.user_os() {
 | 
				
			||||||
			os.mkdir('$TmpPath/vlib/$target_os')
 | 
								os.mkdir('$TmpPath/vlib/$target_os') or { panic(err) }
 | 
				
			||||||
			out_name = '$TmpPath/vlib/$target_os/${base}.o'
 | 
								out_name = '$TmpPath/vlib/$target_os/${base}.o'
 | 
				
			||||||
			println('target_os=$target_os user_os=${os.user_os()}')
 | 
								println('target_os=$target_os user_os=${os.user_os()}')
 | 
				
			||||||
			println('!Cross compiling $out_name')
 | 
								println('!Cross compiling $out_name')
 | 
				
			||||||
@@ -939,7 +939,7 @@ pub fn new_v(args[]string) &V {
 | 
				
			|||||||
		d := out_name.all_before_last(os.path_separator)
 | 
							d := out_name.all_before_last(os.path_separator)
 | 
				
			||||||
		if !os.dir_exists(d) {
 | 
							if !os.dir_exists(d) {
 | 
				
			||||||
			println('creating a new directory "$d"')
 | 
								println('creating a new directory "$d"')
 | 
				
			||||||
			os.mkdir(d)
 | 
								os.mkdir(d) or { panic(err) }
 | 
				
			||||||
		}	
 | 
							}	
 | 
				
			||||||
	}	
 | 
						}	
 | 
				
			||||||
	mut _os := OS.mac
 | 
						mut _os := OS.mac
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,7 +36,7 @@ fn generate_vh(mod string) {
 | 
				
			|||||||
	pdir := dir.all_before_last(os.path_separator)
 | 
						pdir := dir.all_before_last(os.path_separator)
 | 
				
			||||||
	if !os.dir_exists(pdir) {
 | 
						if !os.dir_exists(pdir) {
 | 
				
			||||||
		os.mkdir_all(pdir)
 | 
							os.mkdir_all(pdir)
 | 
				
			||||||
		// os.mkdir(os.realpath(dir))
 | 
							// os.mkdir(os.realpath(dir)) or { panic(err) }
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	out := os.create(path) or { panic(err) }
 | 
						out := os.create(path) or { panic(err) }
 | 
				
			||||||
	mod_path := mod.replace("\\", "/")
 | 
						mod_path := mod.replace("\\", "/")
 | 
				
			||||||
@@ -169,5 +169,3 @@ fn (g mut VhGen) generate_type() {
 | 
				
			|||||||
	//g.i = old
 | 
						//g.i = old
 | 
				
			||||||
	//g.i--
 | 
						//g.i--
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,7 +10,7 @@ import filepath
 | 
				
			|||||||
pub fn get_vtmp_folder() string {
 | 
					pub fn get_vtmp_folder() string {
 | 
				
			||||||
	vtmp := filepath.join(os.tmpdir(),'v')
 | 
						vtmp := filepath.join(os.tmpdir(),'v')
 | 
				
			||||||
	if !os.dir_exists( vtmp ) {
 | 
						if !os.dir_exists( vtmp ) {
 | 
				
			||||||
		os.mkdir(vtmp)
 | 
							os.mkdir(vtmp) or { panic(err) }
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return vtmp
 | 
						return vtmp
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -198,7 +198,7 @@ pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool{
 | 
				
			|||||||
		sp := filepath.join(source_path, file)
 | 
							sp := filepath.join(source_path, file)
 | 
				
			||||||
		dp := filepath.join(dest_path, file)
 | 
							dp := filepath.join(dest_path, file)
 | 
				
			||||||
		if os.is_dir(sp) {
 | 
							if os.is_dir(sp) {
 | 
				
			||||||
			os.mkdir(dp)
 | 
								os.mkdir(dp) or { panic(err) }
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		cp_r(sp, dp, overwrite) or {
 | 
							cp_r(sp, dp, overwrite) or {
 | 
				
			||||||
			os.rmdir(dp)
 | 
								os.rmdir(dp)
 | 
				
			||||||
@@ -991,7 +991,7 @@ pub fn mkdir_all(path string) {
 | 
				
			|||||||
	for subdir in path.split(os.path_separator) {
 | 
						for subdir in path.split(os.path_separator) {
 | 
				
			||||||
		p += subdir + os.path_separator
 | 
							p += subdir + os.path_separator
 | 
				
			||||||
		if !os.dir_exists(p) {
 | 
							if !os.dir_exists(p) {
 | 
				
			||||||
			os.mkdir(p)
 | 
								os.mkdir(p) or { panic(err) }
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -61,12 +61,14 @@ pub fn dir_exists(path string) bool {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// mkdir creates a new directory with the specified path.
 | 
					// mkdir creates a new directory with the specified path.
 | 
				
			||||||
pub fn mkdir(path string) {
 | 
					pub fn mkdir(path string) ?bool {
 | 
				
			||||||
	//$if linux {
 | 
						if path == '.' { return true }
 | 
				
			||||||
		//C.syscall(83, path.str, 511) // sys_mkdir
 | 
						apath := os.realpath( path )
 | 
				
			||||||
	//}	$else {
 | 
						r := int(C.mkdir(apath.str, 511))
 | 
				
			||||||
		C.mkdir(path.str, 511)// S_IRWXU | S_IRWXG | S_IRWXO
 | 
						if r == -1 {
 | 
				
			||||||
	//}
 | 
							return error(get_error_msg(C.errno))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return true
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// exec starts the specified command, waits for it to complete, and returns its output.
 | 
					// exec starts the specified command, waits for it to complete, and returns its output.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -75,7 +75,7 @@ fn test_write_and_read_bytes() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
fn test_create_and_delete_folder() {
 | 
					fn test_create_and_delete_folder() {
 | 
				
			||||||
  folder := './test1'
 | 
					  folder := './test1'
 | 
				
			||||||
  os.mkdir(folder)
 | 
					  os.mkdir(folder) or { panic(err) }
 | 
				
			||||||
	assert os.dir_exists(folder)
 | 
						assert os.dir_exists(folder)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  folder_contents := os.ls(folder) or { panic(err) }
 | 
					  folder_contents := os.ls(folder) or { panic(err) }
 | 
				
			||||||
@@ -106,7 +106,7 @@ fn walk_callback(file string) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
fn test_walk() {
 | 
					fn test_walk() {
 | 
				
			||||||
    folder := 'test_walk'
 | 
					    folder := 'test_walk'
 | 
				
			||||||
    os.mkdir(folder)
 | 
					    os.mkdir(folder) or { panic(err) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    file1 := folder+os.path_separator+'test1'
 | 
					    file1 := folder+os.path_separator+'test1'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -137,12 +137,12 @@ fn test_cp_r() {
 | 
				
			|||||||
  //fileX -> dir/fileX
 | 
					  //fileX -> dir/fileX
 | 
				
			||||||
  // NB: clean up of the files happens inside the cleanup_leftovers function
 | 
					  // NB: clean up of the files happens inside the cleanup_leftovers function
 | 
				
			||||||
  os.write_file('ex1.txt', 'wow!')
 | 
					  os.write_file('ex1.txt', 'wow!')
 | 
				
			||||||
  os.mkdir('ex')
 | 
					  os.mkdir('ex') or { panic(err) }
 | 
				
			||||||
  os.cp_r('ex1.txt', 'ex', false) or { panic(err) }
 | 
					  os.cp_r('ex1.txt', 'ex', false) or { panic(err) }
 | 
				
			||||||
  old := os.read_file('ex1.txt') or { panic(err) }
 | 
					  old := os.read_file('ex1.txt') or { panic(err) }
 | 
				
			||||||
  new := os.read_file('ex/ex1.txt') or { panic(err) }
 | 
					  new := os.read_file('ex/ex1.txt') or { panic(err) }
 | 
				
			||||||
  assert old == new
 | 
					  assert old == new
 | 
				
			||||||
  os.mkdir('ex/ex2')
 | 
					  os.mkdir('ex/ex2') or { panic(err) }
 | 
				
			||||||
  os.write_file('ex2.txt', 'great!')
 | 
					  os.write_file('ex2.txt', 'great!')
 | 
				
			||||||
  os.cp_r('ex2.txt', 'ex/ex2', false) or { panic(err) }
 | 
					  os.cp_r('ex2.txt', 'ex/ex2', false) or { panic(err) }
 | 
				
			||||||
  old2 := os.read_file('ex2.txt') or { panic(err) }
 | 
					  old2 := os.read_file('ex2.txt') or { panic(err) }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -131,14 +131,14 @@ pub fn dir_exists(path string) bool {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// mkdir creates a new directory with the specified path.
 | 
					// mkdir creates a new directory with the specified path.
 | 
				
			||||||
pub fn mkdir(path string) {
 | 
					pub fn mkdir(path string) ?bool {
 | 
				
			||||||
	_path := path.replace('/', '\\')
 | 
						if path == '.' { return true }
 | 
				
			||||||
	// Windows doesnt recursively create the folders
 | 
						apath := os.realpath( path )
 | 
				
			||||||
	// so we need to help it out here
 | 
						r := int(C.CreateDirectory(apath.to_wide(), 0))
 | 
				
			||||||
	if _path.last_index('\\') != -1 {
 | 
						if r == 0 {
 | 
				
			||||||
		mkdir(_path.all_before_last('\\'))
 | 
							return error('mkdir failed for "$apath", because CreateDirectory returned ' + get_error_msg(int(C.GetLastError())))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	C.CreateDirectory(_path.to_wide(), 0)
 | 
						return true 
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019
 | 
					// Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -103,7 +103,7 @@ fn (am mut AssetManager) combine(asset_type string, to_file bool) string {
 | 
				
			|||||||
		return out
 | 
							return out
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if !os.dir_exists(am.cache_dir) {
 | 
						if !os.dir_exists(am.cache_dir) {
 | 
				
			||||||
		os.mkdir(am.cache_dir)
 | 
							os.mkdir(am.cache_dir) or { panic(err) }
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	file := os.create(out_file) or {
 | 
						file := os.create(out_file) or {
 | 
				
			||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user