GrayJack / coreutils

Core utils re-implementation for UNIX/UNIX-like systems written in Rust
Mozilla Public License 2.0
108 stars 40 forks source link

Date: Implement settimeofday #86

Closed kegesch closed 4 years ago

kegesch commented 5 years ago

Currently date is lacking of the ability to set the datetime of the OS. This could be implemented by utilising the Linux function settimeofday.

GrayJack commented 5 years ago

That would function well on other platforms? Like BSDs and Solaris

kegesch commented 5 years ago

BSD yes. Solaris i don't know. But i think this is a basic unix function so it should work.

GrayJack commented 5 years ago

I was looking at the libc crate to see what platforms expose that function, looks like it's missing on Solaris, OpenBSD and Haiku

Note that libc not exposing it doesn't mean that it doesn't exist on the platform, as example, I filled 2 issues on libc crate to expose utmpx on NetBSD and Solaris, cause they have that structc and methods, but they are not present on libc crate

kegesch commented 5 years ago

Alright. OpenBSD at least has a man page for that. I couldn't find anything about Solaris.

GrayJack commented 5 years ago

I found for illumos: https://illumos.org/man/3c/settimeofday Most of the time their API are the same, so I'm guessing Solaris has it too

GrayJack commented 5 years ago

I have opened 2 issues on libc crate

GrayJack commented 5 years ago

Update: I made pull request to add that function on libc, hopefully they will be merged easilly

GrayJack commented 5 years ago

Update: OpenBSD now expose that function: https://github.com/rust-lang/libc/pull/1548

GrayJack commented 5 years ago

Update: Solaris/Illumos now expose that function, next libc crate release they will have it

kegesch commented 5 years ago

I started working on this since they released a new version. You can have look here

GrayJack commented 5 years ago

Timezone in libc is specifies as a empty enum, probably cause they don't want anyone creating it

GrayJack commented 5 years ago

Aha, found it, timezone struct is historical and should be just a NULL pointer when calling the functions

As for problem 2, time crate implements a Tm struct, the represents in a higher level the tm struct in c, inside it has the thing you wanted. ref: https://docs.rs/time/0.1.42/time/struct.Tm.html

My recommendation is, implement safe abstraction on coreutils_core and have it as dependency on the crate (note that coreutils_core re-export time crate)

kegesch commented 5 years ago

Okay, thanks a lot. I give it a shot.