From d71c11577adc3f7d1bc0daf3f980c5521d4455fe Mon Sep 17 00:00:00 2001 From: Carlos Esquerdo Bernat Date: Sat, 1 Aug 2020 23:07:37 +0200 Subject: [PATCH] os: move file logic to file.v (#6013) --- vlib/os/file.v | 89 ++++++++++++++++++++++++++++++ vlib/os/os.v | 120 ++++++----------------------------------- vlib/os/os_linux.c.v | 7 --- vlib/os/os_nix.c.v | 4 +- vlib/os/os_windows.c.v | 4 +- 5 files changed, 109 insertions(+), 115 deletions(-) create mode 100644 vlib/os/file.v diff --git a/vlib/os/file.v b/vlib/os/file.v new file mode 100644 index 0000000000..f6eaee91d3 --- /dev/null +++ b/vlib/os/file.v @@ -0,0 +1,89 @@ +module os + +pub struct File { + cfile voidptr // Using void* instead of FILE* +pub: + fd int +pub mut: + is_opened bool +} + +struct FileInfo { + name string + size int +} + +[deprecated] +pub fn (f File) is_opened() bool { + eprintln('warning: `file.is_opened()` has been deprecated, use `file.is_opened` instead') + return f.is_opened +} + +// **************************** Write ops *************************** +pub fn (mut f File) write(s string) { + if !f.is_opened { + return + } + /* + $if linux { + $if !android { + C.syscall(sys_write, f.fd, s.str, s.len) + return + } + } + */ + C.fwrite(s.str, s.len, 1, f.cfile) +} + +pub fn (mut f File) writeln(s string) { + if !f.is_opened { + return + } + /* + $if linux { + $if !android { + snl := s + '\n' + C.syscall(sys_write, f.fd, snl.str, snl.len) + return + } + } + */ + // TODO perf + C.fwrite(s.str, s.len, 1, f.cfile) + C.fputs('\n', f.cfile) +} + +pub fn (mut f File) write_bytes(data voidptr, size int) int { + return C.fwrite(data, 1, size, f.cfile) +} + +pub fn (mut f File) write_bytes_at(data voidptr, size, pos int) int { + C.fseek(f.cfile, pos, C.SEEK_SET) + res := C.fwrite(data, 1, size, f.cfile) + C.fseek(f.cfile, 0, C.SEEK_END) + return res +} + +// **************************** Read ops *************************** +// read_bytes reads an amount of bytes from the beginning of the file +pub fn (f &File) read_bytes(size int) []byte { + return f.read_bytes_at(size, 0) +} + +// read_bytes_at reads an amount of bytes at the given position in the file +pub fn (f &File) read_bytes_at(size, pos int) []byte { + mut arr := [`0`].repeat(size) + C.fseek(f.cfile, pos, C.SEEK_SET) + nreadbytes := C.fread(arr.data, 1, size, f.cfile) + C.fseek(f.cfile, 0, C.SEEK_SET) + return arr[0..nreadbytes] +} + +// **************************** Utility ops *********************** +// write any unwritten data in stream's buffer +pub fn (mut f File) flush() { + if !f.is_opened { + return + } + C.fflush(f.cfile) +} diff --git a/vlib/os/os.v b/vlib/os/os.v index 2cb0139d6d..85aacd77d2 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -8,91 +8,6 @@ pub const ( max_path_len = 4096 ) -pub struct File { - cfile voidptr // Using void* instead of FILE* -pub: - fd int -mut: - opened bool -} - -struct FileInfo { - name string - size int -} - -pub fn (f File) is_opened() bool { - return f.opened -} - -// Write ops - -pub fn (mut f File) write(s string) { - if !f.opened { - return - } - /* - $if linux { - $if !android { - C.syscall(sys_write, f.fd, s.str, s.len) - return - } - } - */ - C.fwrite(s.str, s.len, 1, f.cfile) -} - -pub fn (mut f File) writeln(s string) { - if !f.opened { - return - } - /* - $if linux { - $if !android { - snl := s + '\n' - C.syscall(sys_write, f.fd, snl.str, snl.len) - return - } - } - */ - // TODO perf - C.fwrite(s.str, s.len, 1, f.cfile) - C.fputs('\n', f.cfile) -} - -pub fn (mut f File) write_bytes(data voidptr, size int) int { - return C.fwrite(data, 1, size, f.cfile) -} - -pub fn (mut f File) write_bytes_at(data voidptr, size, pos int) int { - //$if linux { - //} - //$else { - C.fseek(f.cfile, pos, C.SEEK_SET) - res := C.fwrite(data, 1, size, f.cfile) - C.fseek(f.cfile, 0, C.SEEK_END) - //} - return res -} - -/***************************** Read ops ****************************/ - - -// read_bytes reads an amount of bytes from the beginning of the file -pub fn (f &File) read_bytes(size int) []byte { - return f.read_bytes_at(size, 0) -} - -// read_bytes_at reads an amount of bytes at the given position in the file -pub fn (f &File) read_bytes_at(size, pos int) []byte { - mut arr := [`0`].repeat(size) - C.fseek(f.cfile, pos, C.SEEK_SET) - nreadbytes := C.fread(arr.data, 1, size, f.cfile) - C.fseek(f.cfile, 0, C.SEEK_SET) - return arr[0..nreadbytes] -} - - pub fn read_bytes(path string) ?[]byte { mut fp := vfopen(path, 'rb') if isnil(fp) { @@ -128,15 +43,6 @@ pub fn read_file(path string) ?string { } } -/***************************** Utility ops ************************/ - -pub fn (mut f File) flush() { - if !f.opened { - return - } - C.fflush(f.cfile) -} - /***************************** OS ops ************************/ // file_size returns the size of the file located in `path`. pub fn file_size(path string) int { @@ -155,6 +61,7 @@ pub fn file_size(path string) int { return s.st_size } +// move files or folders from one path to other pub fn mv(old, new string) { $if windows { C._wrename(old.to_wide(), new.to_wide()) @@ -163,6 +70,7 @@ pub fn mv(old, new string) { } } +// copies files or folders from one path to other pub fn cp(old, new string) ? { $if windows { w_old := old.replace('/', '\\') @@ -327,7 +235,7 @@ pub fn open_append(path string) ?File { if isnil(file.cfile) { return error('failed to create(append) file "$path"') } - file.opened = true + file.is_opened = true return file } @@ -379,7 +287,7 @@ pub fn open_file(path string, mode string, options ...int) ?File { return File{ cfile: cfile fd: fd - opened: true + is_opened: true } } @@ -594,9 +502,11 @@ pub fn is_executable(path string) bool { } $if solaris { statbuf := C.stat{} - if C.stat(charptr(path.str), &statbuf) != 0 { - return false - } + unsafe { + if C.stat(charptr(path.str), &statbuf) != 0 { + return false + } + } return (int(statbuf.st_mode) & ( s_ixusr | s_ixgrp | s_ixoth )) != 0 } return C.access(charptr(path.str), x_ok) != -1 @@ -1010,7 +920,9 @@ pub fn executable() string { mut result := vcalloc(max_path_len) mib := [1/* CTL_KERN */, 14/* KERN_PROC */, 12/* KERN_PROC_PATHNAME */, -1] size := max_path_len - C.sysctl(mib.data, 4, result, &size, 0, 0) + unsafe { + C.sysctl(mib.data, 4, result, &size, 0, 0) + } return string(result) } // "Sadly there is no way to get the full path of the executed file in OpenBSD." @@ -1404,7 +1316,7 @@ pub fn open(path string) ?File { } return File{ fd: fd - opened: true + is_opened: true } } } @@ -1417,7 +1329,7 @@ pub fn open(path string) ?File { return File { cfile: cfile fd: fd - opened: true + is_opened: true } } @@ -1440,7 +1352,7 @@ pub fn create(path string) ?File { } file = File{ fd: fd - opened: true + is_opened: true } return file } @@ -1454,7 +1366,7 @@ pub fn create(path string) ?File { return File { cfile: cfile fd: fd - opened: true + is_opened: true } } diff --git a/vlib/os/os_linux.c.v b/vlib/os/os_linux.c.v index 7fcbe4ccbc..3dc29e7cf3 100644 --- a/vlib/os/os_linux.c.v +++ b/vlib/os/os_linux.c.v @@ -20,8 +20,6 @@ pub const ( sys_creat = 85 ) - - /* // TODO no pub => error pub fn write(fd int, data voidptr, nbytes int) int { @@ -47,9 +45,4 @@ pub fn malloc(n int) byteptr { println('malloc($n)') return mmap(0, n, 3, 4098, //prot_read|prot_write, -1,0) //map_private|map_anonymous, -} - -pub fn free(b byteptr) { - -} */ diff --git a/vlib/os/os_nix.c.v b/vlib/os/os_nix.c.v index 4aa7bf114d..a59d23fab4 100644 --- a/vlib/os/os_nix.c.v +++ b/vlib/os/os_nix.c.v @@ -171,10 +171,10 @@ pub fn get_error_msg(code int) string { } pub fn (mut f File) close() { - if !f.opened { + if !f.is_opened { return } - f.opened = false + f.is_opened = false /* $if linux { $if !android { diff --git a/vlib/os/os_windows.c.v b/vlib/os/os_windows.c.v index 8db0752bcb..d80bf0f7d0 100644 --- a/vlib/os/os_windows.c.v +++ b/vlib/os/os_windows.c.v @@ -305,10 +305,10 @@ pub fn symlink(origin, target string) ?bool { } pub fn (mut f File) close() { - if !f.opened { + if !f.is_opened { return } - f.opened = false + f.is_opened = false C.fflush(f.cfile) C.fclose(f.cfile) }