-
Notifications
You must be signed in to change notification settings - Fork 126
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
[Experimental][DNM] Use SwiftSystem for file operations #221
Draft
stevapple
wants to merge
9
commits into
swiftlang:main
Choose a base branch
from
stevapple:filepath-fileops
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d7ec183
Reimplement Path API with SwiftSystem
stevapple b127725
Drop Pathcch dependency
stevapple d8888c6
Fix style
stevapple 49264e3
Clarify the change of `Path.components`
stevapple 76ae479
Deprecate static method AbsolutePath.root
stevapple 55565ba
Minor style changes
stevapple 39704db
Minor update
stevapple 44f7f25
Fix build
stevapple 14356cd
[Experimental] Use SwiftSystem for LocalFileSystem
stevapple File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"object": { | ||
"pins": [ | ||
{ | ||
"package": "swift-system", | ||
"repositoryURL": "https://github.com/apple/swift-system.git", | ||
"state": { | ||
"branch": null, | ||
"revision": "2bc160bfe34d843ae5ff47168080add24dfd7eac", | ||
"version": "0.0.2" | ||
} | ||
} | ||
] | ||
}, | ||
"version": 1 | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors | ||
Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See http://swift.org/LICENSE.txt for license information | ||
See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import TSCLibc | ||
import Foundation | ||
import Dispatch | ||
import SystemPackage | ||
|
||
public struct FileSystemError: Error, Equatable { | ||
public enum Kind: Equatable { | ||
|
@@ -86,23 +86,34 @@ extension FileSystemError: CustomNSError { | |
} | ||
} | ||
|
||
public extension FileSystemError { | ||
init(errno: Int32, _ path: AbsolutePath) { | ||
extension FileSystemError.Kind { | ||
public static func errno(_ errno: Errno) -> FileSystemError.Kind { | ||
switch errno { | ||
case TSCLibc.EACCES: | ||
self.init(.invalidAccess, path) | ||
case TSCLibc.EISDIR: | ||
self.init(.isDirectory, path) | ||
case TSCLibc.ENOENT: | ||
self.init(.noEntry, path) | ||
case TSCLibc.ENOTDIR: | ||
self.init(.notDirectory, path) | ||
default: | ||
self.init(.unknownOSError, path) | ||
case .permissionDenied: return .invalidAccess | ||
case .isDirectory: return.isDirectory | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing space? |
||
case .noSuchFileOrDirectory: return .noEntry | ||
case .notDirectory: return .notDirectory | ||
default: return .unknownOSError | ||
} | ||
} | ||
} | ||
|
||
public extension FileSystemError { | ||
init(errno: Errno, _ path: AbsolutePath) { | ||
self.init(.errno(errno), path) | ||
} | ||
init(errno: Int32, _ path: AbsolutePath) { | ||
self.init(errno: Errno(rawValue: errno), path) | ||
} | ||
} | ||
|
||
func withFileSystemError<Result>(path: AbsolutePath, _ body: () throws -> Result) throws -> Result { | ||
do { return try body() } | ||
catch let errno as Errno { | ||
throw FileSystemError(errno: errno, path) | ||
} | ||
} | ||
|
||
/// Defines the file modes. | ||
public enum FileMode { | ||
|
||
|
@@ -284,7 +295,7 @@ private class LocalFileSystem: FileSystem { | |
|
||
func isExecutableFile(_ path: AbsolutePath) -> Bool { | ||
// Our semantics doesn't consider directories. | ||
return (self.isFile(path) || self.isSymlink(path)) && FileManager.default.isExecutableFile(atPath: path.pathString) | ||
return (self.isFile(path) || self.isSymlink(path)) && FileManager.default.isExecutableFile(atPath: path.pathString) | ||
} | ||
|
||
func exists(_ path: AbsolutePath, followSymlink: Bool) -> Bool { | ||
|
@@ -377,60 +388,39 @@ private class LocalFileSystem: FileSystem { | |
} | ||
|
||
func createSymbolicLink(_ path: AbsolutePath, pointingAt destination: AbsolutePath, relative: Bool) throws { | ||
let destString = relative ? destination.relative(to: path.parentDirectory).pathString : destination.pathString | ||
let destString = relative ? try destination.relative(to: path.parentDirectory).pathString : destination.pathString | ||
try FileManager.default.createSymbolicLink(atPath: path.pathString, withDestinationPath: destString) | ||
} | ||
|
||
func readFileContents(_ path: AbsolutePath) throws -> ByteString { | ||
// Open the file. | ||
let fp = fopen(path.pathString, "rb") | ||
if fp == nil { | ||
throw FileSystemError(errno: errno, path) | ||
} | ||
defer { fclose(fp) } | ||
|
||
// Read the data one block at a time. | ||
let data = BufferedOutputByteStream() | ||
var tmpBuffer = [UInt8](repeating: 0, count: 1 << 12) | ||
while true { | ||
let n = fread(&tmpBuffer, 1, tmpBuffer.count, fp) | ||
if n < 0 { | ||
if errno == EINTR { continue } | ||
throw FileSystemError(.ioError(code: errno), path) | ||
} | ||
if n == 0 { | ||
let errno = ferror(fp) | ||
if errno != 0 { | ||
throw FileSystemError(.ioError(code: errno), path) | ||
try withFileSystemError(path: path) { | ||
let data = BufferedOutputByteStream() | ||
// Open the file. | ||
let fd = try FileDescriptor.open(path.filepath, .readOnly) | ||
var tmpBuffer = [UInt8](repeating: 0, count: 1 << 12) | ||
try fd.closeAfter { | ||
while true { | ||
// Read the data one block at a time. | ||
let n = try tmpBuffer.withUnsafeMutableBytes { | ||
try fd.read(into: $0) | ||
} | ||
guard n > 0 else { break } | ||
data <<< tmpBuffer[0..<n] | ||
} | ||
break | ||
} | ||
data <<< tmpBuffer[0..<n] | ||
return data.bytes | ||
} | ||
|
||
return data.bytes | ||
} | ||
|
||
func writeFileContents(_ path: AbsolutePath, bytes: ByteString) throws { | ||
// Open the file. | ||
let fp = fopen(path.pathString, "wb") | ||
if fp == nil { | ||
throw FileSystemError(errno: errno, path) | ||
} | ||
defer { fclose(fp) } | ||
|
||
// Write the data in one chunk. | ||
var contents = bytes.contents | ||
while true { | ||
let n = fwrite(&contents, 1, contents.count, fp) | ||
if n < 0 { | ||
if errno == EINTR { continue } | ||
throw FileSystemError(.ioError(code: errno), path) | ||
} | ||
if n != contents.count { | ||
throw FileSystemError(.unknownOSError, path) | ||
try withFileSystemError(path: path) { | ||
// Open the file. | ||
let fd = try FileDescriptor.open(path.filepath, .writeOnly, options: [.create, .truncate], permissions: [ | ||
.ownerReadWrite, .groupReadWrite, .otherRead | ||
]) | ||
_ = try fd.closeAfter { | ||
try fd.writeAll(bytes.contents) | ||
} | ||
break | ||
} | ||
} | ||
|
||
|
@@ -803,7 +793,7 @@ public class InMemoryFileSystem: FileSystem { | |
throw FileSystemError(.alreadyExistsAtDestination, path) | ||
} | ||
|
||
let destination = relative ? destination.relative(to: path.parentDirectory).pathString : destination.pathString | ||
let destination = relative ? try destination.relative(to: path.parentDirectory).pathString : destination.pathString | ||
|
||
contents.entries[path.basename] = Node(.symlink(destination)) | ||
} | ||
|
@@ -981,11 +971,11 @@ public class RerootedFileSystemView: FileSystem { | |
|
||
/// Adjust the input path for the underlying file system. | ||
private func formUnderlyingPath(_ path: AbsolutePath) -> AbsolutePath { | ||
if path == AbsolutePath.root { | ||
if path.isRoot { | ||
return root | ||
} else { | ||
// FIXME: Optimize? | ||
return root.appending(RelativePath(String(path.pathString.dropFirst(1)))) | ||
return root.appending(RelativePath(path.filepath.removingRoot())) | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove this file