Skip to content

Support for building with 64-bit time_t/off_t on 32-bit platforms #1395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/FoundationEssentials/FileManager/FileManager+Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,12 @@ extension _FileManagerImpl {
let blockSize = UInt64(result.f_bsize)
#else
let fsNumber = result.f_fsid
#if canImport(Glibc)
let blockSize = fsblkcnt_t(result.f_frsize) // support 64-bit block sizes on 32-bit platforms
#else
let blockSize = UInt(result.f_frsize)
#endif
#endif
var totalSizeBytes = result.f_blocks * blockSize
var availSizeBytes = result.f_bavail * blockSize
var totalFiles = result.f_files
Expand Down Expand Up @@ -963,6 +967,12 @@ extension _FileManagerImpl {
#endif
}

#if canImport(Glibc)
Copy link
Contributor

@jmschonfeld jmschonfeld Jul 2, 2025

Choose a reason for hiding this comment

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

Is there some #define that's enabled when building with 64bit values on a 32 bit platform that we should check before doing this? IIUC this would allow it to build when that is enabled, but would cause the build to fail in the situations where the build currently passes (the setting is disabled when building for 32 bit). I'm not sure if Swift has any Glibc-based 32 bit platforms that are officially supported but it might make sense to mitigate that risk by conditionalizing this to not just canImport(Glibc) but the specific scenario where it currently fails to build

Copy link
Author

Choose a reason for hiding this comment

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

The change doesn't break 32-bit timestamps on 32-bit platforms. We have verified this as we actually ended up backing out enabling 64-bit timestamps on 32-bit platforms in our build pending some other package updates.

If you're concerned about FreeBSD, maybe we also add an os(Linux) check.

There is a C #define for 64-bit timestamps (well, for whether time_t is wide on 32-bits in the current compilation unit) but that's not going to be visible in Swift.

Copy link
Author

Choose a reason for hiding this comment

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

Of course in a C header we could conditionally define a type of the appropriate width conditional on _TIME_BITS but, that's describing time_t.

// support for 64-bit timestamps on 32-bit platforms; unfortunately
// suseconds_t is not an alias of the appropriate type, but time_t is
typealias suseconds_t = time_t
#endif

if let date = attributes[.modificationDate] as? Date {
let (isecs, fsecs) = modf(date.timeIntervalSince1970)
if let tv_sec = time_t(exactly: isecs),
Expand Down