fix the build on macos

Stat structs gain methods to abstract over the platform differences with
regards to mtime, ctime, atime.
This commit is contained in:
Andrew Kelley 2019-07-15 12:28:39 -04:00
parent 0cd660462f
commit 51a3938b04
6 changed files with 110 additions and 11 deletions

View File

@ -248,12 +248,15 @@ pub const File = struct {
}
const st = try os.fstat(self.handle);
const atime = st.atime();
const mtime = st.mtime();
const ctime = st.ctime();
return Stat{
.size = @bitCast(u64, st.size),
.mode = st.mode,
.atime = st.atim.tv_sec * std.time.ns_per_s + st.atim.tv_nsec,
.mtime = st.mtim.tv_sec * std.time.ns_per_s + st.mtim.tv_nsec,
.ctime = st.ctim.tv_sec * std.time.ns_per_s + st.ctim.tv_nsec,
.atime = atime.tv_sec * std.time.ns_per_s + atime.tv_nsec,
.mtime = mtime.tv_sec * std.time.ns_per_s + mtime.tv_nsec,
.ctime = ctime.tv_sec * std.time.ns_per_s + ctime.tv_nsec,
};
}

View File

@ -44,6 +44,11 @@ pub const mach_timebase_info_data = extern struct {
};
/// Renamed to Stat to not conflict with the stat function.
/// atime, mtime, and ctime have functions to return `timespec`,
/// because although this is a POSIX API, the layout and names of
/// the structs are inconsistent across operating systems, and
/// in C, macros are used to hide the differences. Here we use
/// methods to accomplish this.
pub const Stat = extern struct {
dev: i32,
mode: u16,
@ -52,14 +57,14 @@ pub const Stat = extern struct {
uid: u32,
gid: u32,
rdev: i32,
atime: usize,
atimensec: usize,
mtime: usize,
mtimensec: usize,
ctime: usize,
ctimensec: usize,
birthtime: usize,
birthtimensec: usize,
atimesec: isize,
atimensec: isize,
mtimesec: isize,
mtimensec: isize,
ctimesec: isize,
ctimensec: isize,
birthtimesec: isize,
birthtimensec: isize,
size: i64,
blocks: i64,
blksize: i32,
@ -67,6 +72,27 @@ pub const Stat = extern struct {
gen: u32,
lspare: i32,
qspare: [2]i64,
pub fn atime(self: Stat) timespec {
return timespec{
.tv_sec = self.atimesec,
.tv_nsec = self.atimensec,
};
}
pub fn mtime(self: Stat) timespec {
return timespec{
.tv_sec = self.mtimesec,
.tv_nsec = self.mtimensec,
};
}
pub fn ctime(self: Stat) timespec {
return timespec{
.tv_sec = self.ctimesec,
.tv_nsec = self.ctimensec,
};
}
};
pub const timespec = extern struct {

View File

@ -73,6 +73,12 @@ pub const msghdr_const = extern struct {
msg_flags: i32,
};
/// Renamed to Stat to not conflict with the stat function.
/// atime, mtime, and ctime have functions to return `timespec`,
/// because although this is a POSIX API, the layout and names of
/// the structs are inconsistent across operating systems, and
/// in C, macros are used to hide the differences. Here we use
/// methods to accomplish this.
pub const Stat = extern struct {
dev: u64,
ino: u64,
@ -96,6 +102,18 @@ pub const Stat = extern struct {
flags: u32,
gen: u64,
__spare: [10]u64,
pub fn atime(self: Stat) timespec {
return self.atim;
}
pub fn mtime(self: Stat) timespec {
return self.mtim;
}
pub fn ctime(self: Stat) timespec {
return self.ctim;
}
};
pub const timespec = extern struct {

View File

@ -396,6 +396,11 @@ pub const msghdr_const = extern struct {
};
/// Renamed to Stat to not conflict with the stat function.
/// atime, mtime, and ctime have functions to return `timespec`,
/// because although this is a POSIX API, the layout and names of
/// the structs are inconsistent across operating systems, and
/// in C, macros are used to hide the differences. Here we use
/// methods to accomplish this.
pub const Stat = extern struct {
dev: u64,
ino: u64,
@ -414,6 +419,18 @@ pub const Stat = extern struct {
mtim: timespec,
ctim: timespec,
__unused: [3]isize,
pub fn atime(self: Stat) timespec {
return self.atim;
}
pub fn mtime(self: Stat) timespec {
return self.mtim;
}
pub fn ctime(self: Stat) timespec {
return self.ctim;
}
};
pub const timespec = extern struct {

View File

@ -493,6 +493,11 @@ pub const msghdr_const = extern struct {
};
/// Renamed to Stat to not conflict with the stat function.
/// atime, mtime, and ctime have functions to return `timespec`,
/// because although this is a POSIX API, the layout and names of
/// the structs are inconsistent across operating systems, and
/// in C, macros are used to hide the differences. Here we use
/// methods to accomplish this.
pub const Stat = extern struct {
dev: u64,
ino: u64,
@ -511,6 +516,18 @@ pub const Stat = extern struct {
mtim: timespec,
ctim: timespec,
__unused: [3]isize,
pub fn atime(self: Stat) timespec {
return self.atim;
}
pub fn mtime(self: Stat) timespec {
return self.mtim;
}
pub fn ctime(self: Stat) timespec {
return self.ctim;
}
};
pub const timespec = extern struct {

View File

@ -73,6 +73,12 @@ pub const msghdr_const = extern struct {
msg_flags: i32,
};
/// Renamed to Stat to not conflict with the stat function.
/// atime, mtime, and ctime have functions to return `timespec`,
/// because although this is a POSIX API, the layout and names of
/// the structs are inconsistent across operating systems, and
/// in C, macros are used to hide the differences. Here we use
/// methods to accomplish this.
pub const Stat = extern struct {
dev: u64,
mode: u32,
@ -94,6 +100,18 @@ pub const Stat = extern struct {
flags: u32,
gen: u32,
__spare: [2]u32,
pub fn atime(self: Stat) timespec {
return self.atim;
}
pub fn mtime(self: Stat) timespec {
return self.mtim;
}
pub fn ctime(self: Stat) timespec {
return self.ctim;
}
};
pub const timespec = extern struct {