Skip to content

Check renameat2() is available on Linux #2337

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

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include <sys/stat.h>
#include <sys/syscall.h>
#elif TARGET_OS_LINUX
#include <errno.h>
#include <features.h>

#if __GLIBC_PREREQ(2, 28) == 0
Expand Down Expand Up @@ -594,10 +595,21 @@ _stat_with_btime(const char *filename, struct stat *buffer, struct timespec *bti
#endif // __NR_statx

static unsigned int const _CF_renameat2_RENAME_EXCHANGE = 1 << 1;
static int _CF_renameat2(int olddirfd, const char *_Nonnull oldpath,
int newdirfd, const char *_Nonnull newpath, unsigned int flags) {
#ifdef SYS_renameat2
static _Bool const _CFHasRenameat2 = 1;
static inline int _CF_renameat2(int olddirfd, const char *_Nonnull oldpath,
int newdirfd, const char *_Nonnull newpath, unsigned int flags) {
return syscall(SYS_renameat2, olddirfd, oldpath, newdirfd, newpath, flags);
}
#else
static _Bool const _CFHasRenameat2 = 0;
static inline int _CF_renameat2(int olddirfd, const char *_Nonnull oldpath,
int newdirfd, const char *_Nonnull newpath, unsigned int flags) {
return ENOSYS;
}
#endif // __SYS_renameat2


#endif // TARGET_OS_LINUX

#if __HAS_STATX
Expand Down
22 changes: 16 additions & 6 deletions Foundation/FileManager+POSIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ internal func _contentsEqual(atPath path1: String, andPath path2: String) -> Boo
throw _NSErrorWithErrno(error, reading: false, path: path)
}
}

internal func _replaceItem(at originalItemURL: URL, withItemAt newItemURL: URL, backupItemName: String?, options: ItemReplacementOptions = [], allowPlatformSpecificSyscalls: Bool = true) throws -> URL? {

// 1. Make a backup, if asked to.
Expand Down Expand Up @@ -1245,11 +1245,21 @@ internal func _contentsEqual(atPath path1: String, andPath path2: String) -> Boo
return newItemURL.withUnsafeFileSystemRepresentation { (newItemFS) -> Int32? in
if let originalFS = originalFS,
let newItemFS = newItemFS {
if _CF_renameat2(AT_FDCWD, originalFS, AT_FDCWD, newItemFS, _CF_renameat2_RENAME_EXCHANGE) == 0 {
return nil
} else {
return errno
}

#if os(Linux)
if _CFHasRenameat2 && kernelSupportsRenameat2 {
if _CF_renameat2(AT_FDCWD, originalFS, AT_FDCWD, newItemFS, _CF_renameat2_RENAME_EXCHANGE) == 0 {
return nil
} else {
return errno
}
}
#endif
if renameat(AT_FDCWD, originalFS, AT_FDCWD, newItemFS) == 0 {
return nil
} else {
return errno
}
} else {
return Int32(EINVAL)
}
Expand Down
6 changes: 6 additions & 0 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,12 @@ open class FileManager : NSObject {
let requiredVersion = OperatingSystemVersion(majorVersion: 4, minorVersion: 11, patchVersion: 0)
return ProcessInfo.processInfo.isOperatingSystemAtLeast(requiredVersion)
}()

// renameat2() is only supported by Linux kernels >= 3.15
internal lazy var kernelSupportsRenameat2: Bool = {
let requiredVersion = OperatingSystemVersion(majorVersion: 3, minorVersion: 15, patchVersion: 0)
return ProcessInfo.processInfo.isOperatingSystemAtLeast(requiredVersion)
}()
#endif

/* -contentsEqualAtPath:andPath: does not take into account data stored in the resource fork or filesystem extended attributes.
Expand Down