os: close opened file descriptors in `cp()` (#6142)

pull/6144/head
Yarila682 2020-08-16 05:55:10 +03:00 committed by GitHub
parent bab5c21224
commit 29528196f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -87,7 +87,7 @@ pub fn cp(old, new string) ? {
fp_to := C.open(charptr(new.str), C.O_WRONLY | C.O_CREAT | C.O_TRUNC, C.S_IWUSR | C.S_IRUSR) fp_to := C.open(charptr(new.str), C.O_WRONLY | C.O_CREAT | C.O_TRUNC, C.S_IWUSR | C.S_IRUSR)
if fp_to < 0 { // Check if file opened (permissions problems ...) if fp_to < 0 { // Check if file opened (permissions problems ...)
C.close(fp_from) C.close(fp_from)
return error_with_code('cp: failed to write to $new', int(fp_to)) return error_with_code('cp (permission): failed to write to $new (fp_to: $fp_to)', int(fp_to))
} }
mut buf := [1024]byte{} mut buf := [1024]byte{}
mut count := 0 mut count := 0
@ -109,6 +109,8 @@ pub fn cp(old, new string) ? {
if C.chmod(charptr(new.str), from_attr.st_mode) < 0 { if C.chmod(charptr(new.str), from_attr.st_mode) < 0 {
return error_with_code('failed to set permissions for $new', int(-1)) return error_with_code('failed to set permissions for $new', int(-1))
} }
C.close(fp_to)
C.close(fp_from)
} }
} }