os: add utime() (#9796)
parent
b351aa1289
commit
8cc49b5e9e
|
@ -9,6 +9,7 @@ import strings
|
|||
#include <sys/types.h>
|
||||
#include <sys/ptrace.h>
|
||||
#include <glob.h>
|
||||
#include <utime.h>
|
||||
|
||||
pub const (
|
||||
path_separator = '/'
|
||||
|
@ -41,6 +42,14 @@ pub const (
|
|||
s_ixoth = 0o0001 // Execute by others
|
||||
)
|
||||
|
||||
[typedef]
|
||||
struct C.glob_t {
|
||||
mut:
|
||||
gl_pathc size_t // number of matched paths
|
||||
gl_pathv &&char // list of matched pathnames
|
||||
gl_offs size_t // slots to reserve in gl_pathv
|
||||
}
|
||||
|
||||
struct C.utsname {
|
||||
mut:
|
||||
sysname &char
|
||||
|
@ -50,14 +59,13 @@ mut:
|
|||
machine &char
|
||||
}
|
||||
|
||||
[typedef]
|
||||
struct C.glob_t {
|
||||
mut:
|
||||
gl_pathc size_t // number of matched paths
|
||||
gl_pathv &&char // list of matched pathnames
|
||||
gl_offs size_t // slots to reserve in gl_pathv
|
||||
struct C.utimbuf {
|
||||
actime int
|
||||
modtime int
|
||||
}
|
||||
|
||||
fn C.utime(&char, voidptr) int
|
||||
|
||||
fn C.uname(name voidptr) int
|
||||
|
||||
fn C.symlink(&char, &char) int
|
||||
|
@ -108,6 +116,13 @@ pub fn glob(patterns ...string) ?[]string {
|
|||
return matches
|
||||
}
|
||||
|
||||
pub fn utime(path string, actime int, modtime int) ? {
|
||||
mut u := C.utimbuf{actime, modtime}
|
||||
if C.utime(&char(path.str), voidptr(&u)) != 0 {
|
||||
return error_with_code(posix_get_error_msg(C.errno), C.errno)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn uname() Uname {
|
||||
mut u := Uname{}
|
||||
utsize := sizeof(C.utsname)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import os
|
||||
import time
|
||||
|
||||
const (
|
||||
// tfolder will contain all the temporary files/subfolders made by
|
||||
|
@ -724,3 +725,18 @@ fn test_glob() {
|
|||
os.rm('test_dir/test') or { panic(err) }
|
||||
os.rmdir_all('test_dir') or { panic(err) }
|
||||
}
|
||||
|
||||
fn test_utime() {
|
||||
filename := './test_utime.txt'
|
||||
hello := 'hello world!'
|
||||
mut f := os.create(filename) or { panic(err) }
|
||||
defer {
|
||||
f.close()
|
||||
os.rm(filename) or { panic(err) }
|
||||
}
|
||||
f.write_string(hello) or { panic(err) }
|
||||
atime := time.now().add_days(2).unix_time()
|
||||
mtime := time.now().add_days(4).unix_time()
|
||||
os.utime(filename, atime, mtime) or { panic(err) }
|
||||
assert os.file_last_mod_unix(filename) == mtime
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import strings
|
|||
|
||||
#flag windows -l advapi32
|
||||
#include <process.h>
|
||||
#include <sys/utime.h>
|
||||
|
||||
// See https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsymboliclinkw
|
||||
fn C.CreateSymbolicLinkW(&u16, &u16, u32) int
|
||||
|
@ -86,6 +87,13 @@ mut:
|
|||
b_inherit_handle bool
|
||||
}
|
||||
|
||||
struct C._utimbuf {
|
||||
actime int
|
||||
modtime int
|
||||
}
|
||||
|
||||
fn C._utime(&char, voidptr) int
|
||||
|
||||
fn init_os_args_wide(argc int, argv &&byte) []string {
|
||||
mut args_ := []string{}
|
||||
for i in 0 .. argc {
|
||||
|
@ -145,6 +153,13 @@ fn windows_glob_pattern(pattern string, mut matches []string) ? {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn utime(path string, actime int, modtime int) ? {
|
||||
mut u := C._utimbuf{actime, modtime}
|
||||
if C._utime(&char(path.str), voidptr(&u)) != 0 {
|
||||
return error_with_code(posix_get_error_msg(C.errno), C.errno)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ls(path string) ?[]string {
|
||||
mut find_file_data := Win32finddata{}
|
||||
mut dir_files := []string{}
|
||||
|
|
Loading…
Reference in New Issue