Skip to content

Commit 2550dd4

Browse files
authored
[os]: add FilePathOps utility type + initial absolute path function (apple#751)
- Closes apple#744 - Adds the initial `FilePathOps` utility type - Adds the `absolutePath` implementation - Adds the `FilePathOpsTests` file and initial test cases
1 parent 565e8a3 commit 2550dd4

3 files changed

Lines changed: 82 additions & 2 deletions

File tree

Sources/ContainerizationOS/FileDescriptorOps.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ private let os_S_IFLNK = mode_t(Glibc.S_IFLNK)
4646
/// All operations use `openat`/`mkdirat`/`unlinkat` anchored to the supplied
4747
/// file descriptor, preventing path traversal and TOCTOU races. The type is
4848
/// never instantiated; it exists solely as a namespace.
49-
public struct FileDescriptorOps {
50-
private init() {}
49+
public enum FileDescriptorOps {
5150

5251
// MARK: - Nested types
5352

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2026 Apple Inc. and the Containerization project authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
import Foundation
18+
import SystemPackage
19+
20+
/// Static utility functions for path operations.
21+
///
22+
/// The type is never instantiated; it exists solely as a namespace.
23+
public enum FilePathOps {
24+
/// Returns an absolute version of `path`.
25+
///
26+
/// This is a purely lexical operation: it does not resolve symlinks,
27+
/// perform tilde expansion, and does not access the file system. If `path`
28+
/// is already absolute, this returns `path` unchanged.
29+
public static func absolutePath(_ path: FilePath) -> FilePath {
30+
guard !path.isAbsolute else {
31+
return path
32+
}
33+
34+
return FilePath(FileManager.default.currentDirectoryPath)
35+
.appending(path.components)
36+
.lexicallyNormalized()
37+
}
38+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2026 Apple Inc. and the Containerization project authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
import Foundation
18+
import SystemPackage
19+
import Testing
20+
21+
@testable import ContainerizationOS
22+
23+
struct FilePathOpsTests {
24+
@Test("absolutePath returns absolute inputs unchanged")
25+
func absolutePathPreservesAbsolutePath() {
26+
let absolute = FilePath("/tmp/containerization/file.tar")
27+
let resolved = FilePathOps.absolutePath(absolute)
28+
29+
#expect(resolved == absolute)
30+
}
31+
32+
@Test("absolutePath resolves relative paths against cwd and normalizes lexically")
33+
func absolutePathResolvesRelativePath() {
34+
let relative = FilePath("./images/../image.tar")
35+
let expected = FilePath(FileManager.default.currentDirectoryPath)
36+
.appending(relative.components)
37+
.lexicallyNormalized()
38+
let resolved = FilePathOps.absolutePath(relative)
39+
40+
#expect(resolved == expected)
41+
#expect(resolved.isAbsolute)
42+
}
43+
}

0 commit comments

Comments
 (0)