Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy_file implemented for Windows, Linux and Darwin #3186

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/os/file_windows.odin
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,14 @@ pipe :: proc() -> (r, w: Handle, err: Errno) {
return
}

copy_file :: proc(src: string, dst: string, overwrite: bool = false) -> Errno {
wide_src := win32.utf8_to_wstring(src)
Smilex marked this conversation as resolved.
Show resolved Hide resolved
wide_dst := win32.utf8_to_wstring(dst)
fail_if_exists : win32.BOOL = !overwrite
Smilex marked this conversation as resolved.
Show resolved Hide resolved
ok := win32.CopyFileW(wide_src, wide_dst, fail_if_exists)
if !ok {
return Errno(win32.GetLastError())
}

return ERROR_NONE
}
47 changes: 47 additions & 0 deletions core/os/os_darwin.odin
Original file line number Diff line number Diff line change
Expand Up @@ -1118,3 +1118,50 @@ fcntl :: proc(fd: int, cmd: int, arg: int) -> (int, Errno) {
}
return int(result), ERROR_NONE
}

copy_file :: proc(src: string, dst: string, overwrite: bool = false) -> Errno {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
src_handle, src_err := open(src)
if src_err != ERROR_NONE {
return src_err
}
defer close(src_handle)

flags := O_WRONLY | O_CREATE
if !overwrite {
flags |= O_EXCL
}

dst_handle, dst_err := open(dst, flags, 0666)
if dst_err != ERROR_NONE {
return dst_err
}
defer close(dst_handle)

buf, buf_err := make([]u8, 4096, context.temp_allocator)
if buf_err != .None {
return ENOMEM
}

for {
bytes_read, read_err := read(src_handle, buf)
if read_err != ERROR_NONE {
return read_err
} else if (bytes_read == 0) {
break;
}

total_written := 0
for total_written < bytes_read {
bytes_written, write_err := write(dst_handle, buf[total_written:bytes_read])
Smilex marked this conversation as resolved.
Show resolved Hide resolved
if write_err != ERROR_NONE {
return write_err
}

total_written += bytes_written
}
}


return ERROR_NONE
}
47 changes: 47 additions & 0 deletions core/os/os_linux.odin
Original file line number Diff line number Diff line change
Expand Up @@ -1144,3 +1144,50 @@ ppoll :: proc(fds: []pollfd, timeout: ^unix.timespec, sigmask: ^sigset_t) -> (in
}
return result, ERROR_NONE
}

copy_file :: proc(src: string, dst: string, overwrite: bool = false) -> Errno {
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
src_handle, src_err := open(src)
if src_err != ERROR_NONE {
return src_err
}
defer close(src_handle)

flags := O_WRONLY | O_CREATE
if !overwrite {
flags |= O_EXCL
}

dst_handle, dst_err := open(dst, flags, 0666)
if dst_err != ERROR_NONE {
return dst_err
}
defer close(dst_handle)

buf, buf_err := make([]u8, 4096, context.temp_allocator)
if buf_err != .None {
return ENOMEM
}

for {
bytes_read, read_err := read(src_handle, buf)
if read_err != ERROR_NONE {
return read_err
} else if (bytes_read == 0) {
break;
}

total_written := 0
for total_written < bytes_read {
bytes_written, write_err := write(dst_handle, buf[total_written:bytes_read])
Smilex marked this conversation as resolved.
Show resolved Hide resolved
if write_err != ERROR_NONE {
return write_err
}

total_written += bytes_written
}
}


return ERROR_NONE
}