Skip to content

Conversation

@jrflat
Copy link
Contributor

@jrflat jrflat commented Aug 12, 2025

This PR adds a Swift Stat implementation for the C stat types and system calls.

See the pitch at https://forums.swift.org/t/pitch-stat-types-for-swift-system/81616
See the full proposal in #257

@jrflat
Copy link
Contributor Author

jrflat commented Aug 12, 2025

Note that the implementation uses typed throws which require Swift 6.0+, so earlier Swift versions are failing.

Package.swift Outdated

let cSettings: [CSetting] = [
.define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])),
.define("_GNU_SOURCE", .when(platforms: [.linux])),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vaguely remember that this may cause an issue with the glibc module if another package imports it without this define? I.e. either everyone has to import it this way or no one can (in which case, we really want the C importer to set this instead of setting it in the package). @DougGregor?

@jrflat jrflat force-pushed the stat-implementation branch 2 times, most recently from 266636c to 2f0f045 Compare August 26, 2025 23:54
@rauhul
Copy link
Contributor

rauhul commented Aug 27, 2025

I would really like native TimeSpec and TimeVal wrappers to be provided by this API and conversions to/from duration be based on them.

These type represent the granularity the system thinks in and papering over that with Duration/UTCClock.instant is silly. The system cannot represent all of these values. Swift-system is supported to provide type-safe access to the system api not high level abstractions, thats best left to Foundation.

@jrflat
Copy link
Contributor Author

jrflat commented Aug 28, 2025

I would really like native TimeSpec and TimeVal wrappers to be provided by this API and conversions to/from duration be based on them.

The stdlib already provides initializers to convert timespec and timeval to/from Duration, so I'm not sure if it's worth it to have wrappers in these cases. I think exposing the raw timespec as we're doing here is a decent compromise to allow access to the system's granularity while later providing a more ergonomic UTCClock.Instant property.

@jrflat jrflat force-pushed the stat-implementation branch from 2f0f045 to 2f5b12b Compare September 29, 2025 18:06
@jrflat jrflat marked this pull request as ready for review September 29, 2025 18:39
@jrflat
Copy link
Contributor Author

jrflat commented Sep 29, 2025

@swift-ci please test

@jrflat
Copy link
Contributor Author

jrflat commented Oct 6, 2025

@swift-ci please test

// MARK: - fstatat Flags

@_alwaysEmitIntoClient
internal var _AT_SYMLINK_NOFOLLOW: CInt { AT_SYMLINK_FOLLOW }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
internal var _AT_SYMLINK_NOFOLLOW: CInt { AT_SYMLINK_FOLLOW }
internal var _AT_SYMLINK_NOFOLLOW: CInt { AT_SYMLINK_NOFOLLOW }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, thanks! Fixed in 90dba1d

/// Total size, in bytes
///
/// The semantics of this property are tied to the underlying C `st_size` field,
/// which can have file system-dependent behavior. For example, this property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// which can have file system-dependent behavior. For example, this property
/// which can have filesystem-dependent behavior. For example, this property

We use the one-word "filesystem" elsewhere, so this seems like a typo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from where I used "filesystem" below, it actually looks like all prior uses in System are written "file system" or capitalized "FileSystem" (except for one), so I settled on "file system" in eee0145.

After some research, I did find that "file system-dependent" is not correct grammar and instead should be written "file-system-dependent" or "file-system–dependent" (with an en dash in the second gap) if we want to match the use of "file system." I've opted for "file-system–dependent" since it's a neat way to use an en dash to highlight the grouping of "file-system" in a larger compound modifier, but I don't feel super strongly about it if we want to change it (or even replace all uses with "filesystem") 😄

}
#endif

// TODO: Investigate changing time properties to UTCClock.Instant once available.
Copy link
Contributor

@glessard glessard Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove the commented-out lines in a standalone commit so that they're easy to find later, instead of leaving commented-out code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! 501bb7d

@jrflat
Copy link
Contributor Author

jrflat commented Oct 29, 2025

@swift-ci please test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants