os: add utime() (#9796)
parent
b351aa1289
commit
8cc49b5e9e
|
@ -9,6 +9,7 @@ import strings
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/ptrace.h>
|
#include <sys/ptrace.h>
|
||||||
#include <glob.h>
|
#include <glob.h>
|
||||||
|
#include <utime.h>
|
||||||
|
|
||||||
pub const (
|
pub const (
|
||||||
path_separator = '/'
|
path_separator = '/'
|
||||||
|
@ -41,6 +42,14 @@ pub const (
|
||||||
s_ixoth = 0o0001 // Execute by others
|
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 {
|
struct C.utsname {
|
||||||
mut:
|
mut:
|
||||||
sysname &char
|
sysname &char
|
||||||
|
@ -50,14 +59,13 @@ mut:
|
||||||
machine &char
|
machine &char
|
||||||
}
|
}
|
||||||
|
|
||||||
[typedef]
|
struct C.utimbuf {
|
||||||
struct C.glob_t {
|
actime int
|
||||||
mut:
|
modtime int
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn C.utime(&char, voidptr) int
|
||||||
|
|
||||||
fn C.uname(name voidptr) int
|
fn C.uname(name voidptr) int
|
||||||
|
|
||||||
fn C.symlink(&char, &char) int
|
fn C.symlink(&char, &char) int
|
||||||
|
@ -108,6 +116,13 @@ pub fn glob(patterns ...string) ?[]string {
|
||||||
return matches
|
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 {
|
pub fn uname() Uname {
|
||||||
mut u := Uname{}
|
mut u := Uname{}
|
||||||
utsize := sizeof(C.utsname)
|
utsize := sizeof(C.utsname)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// tfolder will contain all the temporary files/subfolders made by
|
// 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.rm('test_dir/test') or { panic(err) }
|
||||||
os.rmdir_all('test_dir') 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
|
#flag windows -l advapi32
|
||||||
#include <process.h>
|
#include <process.h>
|
||||||
|
#include <sys/utime.h>
|
||||||
|
|
||||||
// See https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsymboliclinkw
|
// See https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsymboliclinkw
|
||||||
fn C.CreateSymbolicLinkW(&u16, &u16, u32) int
|
fn C.CreateSymbolicLinkW(&u16, &u16, u32) int
|
||||||
|
@ -86,6 +87,13 @@ mut:
|
||||||
b_inherit_handle bool
|
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 {
|
fn init_os_args_wide(argc int, argv &&byte) []string {
|
||||||
mut args_ := []string{}
|
mut args_ := []string{}
|
||||||
for i in 0 .. argc {
|
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 {
|
pub fn ls(path string) ?[]string {
|
||||||
mut find_file_data := Win32finddata{}
|
mut find_file_data := Win32finddata{}
|
||||||
mut dir_files := []string{}
|
mut dir_files := []string{}
|
||||||
|
|
Loading…
Reference in New Issue