Skip to content

Commit 2dc9494

Browse files
authored
Fix setting permission on open for create to ugo+rw (+x if executable) which will then let umask adjust. (#86)
1 parent 0e7dda7 commit 2dc9494

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Sources/SWBUtil/PbxCp.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ fileprivate func copyRegular(_ srcPath: Path, _ srcParentPath: Path, _ dstPath:
298298

299299
func _copyFile(_ srcPath: Path, _ dstPath: Path) throws {
300300
do {
301-
let dstFd = try FileDescriptor.open(FilePath(dstPath.str), .writeOnly, options: [.create, .truncate], permissions: [.ownerReadWrite, .groupRead, .otherRead])
301+
var permissions: FilePermissions = [.ownerRead, .ownerWrite, .groupRead, .groupWrite, .otherRead, .otherWrite]
302+
if try localFS.isExecutable(srcPath) {
303+
permissions.insert([.ownerExecute, .groupExecute, .otherExecute])
304+
}
305+
let dstFd = try FileDescriptor.open(FilePath(dstPath.str), .writeOnly, options: [.create, .truncate], permissions: permissions)
302306
try dstFd.closeAfter {
303307
let srcFd = try FileDescriptor.open(FilePath(srcPath.str), .readOnly)
304308
try srcFd.closeAfter {
@@ -316,7 +320,6 @@ func _copyFile(_ srcPath: Path, _ dstPath: Path) throws {
316320
} while (bread > bwritten)
317321
}
318322
}
319-
try localFS.setFilePermissions(dstPath, permissions: localFS.getFilePermissions(srcPath))
320323
}
321324
} catch let error as Errno {
322325
throw POSIXError(error.rawValue, context: "copy", srcPath.str, dstPath.str)

Tests/SWBTaskExecutionTests/PBXCpTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ fileprivate struct PBXCpTests: CoreBasedTests {
426426
))
427427
let srcPerm = try fs.getFilePermissions(srcfile)
428428
let dstPerm = try fs.getFilePermissions(dst.join(fwkName).join(fName))
429-
#expect(srcPerm == dstPerm)
429+
#expect(dstPerm == 0o755) // files are created with u+rw, g+wr, o+rw (and +x if src is executable) permissions and umask will adjust
430430
}
431431
}
432432
}
@@ -452,7 +452,7 @@ fileprivate struct PBXCpTests: CoreBasedTests {
452452
size += try fd.writeAll(buffer1)
453453
}
454454
}
455-
try fs.setFilePermissions(sName, permissions: 0o777)
455+
try fs.setFilePermissions(sName, permissions: 0o600)
456456

457457
do {
458458
let result = await pbxcp(["builtin-copy", "-V", src.str + Path.pathSeparatorString, dst.str], cwd: Path("/"))
@@ -463,7 +463,7 @@ fileprivate struct PBXCpTests: CoreBasedTests {
463463
// Check permssions
464464
let srcPerm = try fs.getFilePermissions(sName)
465465
let dstPerm = try fs.getFilePermissions(dName)
466-
#expect(srcPerm == dstPerm)
466+
#expect(dstPerm == 0o644) // files are created with u+rw, g+wr, o+rw (and +x if src is executable) permissions and umask will adjust
467467
#expect(FileManager.default.contentsEqual(atPath: sName.str, andPath: dName.str))
468468
}
469469
}

0 commit comments

Comments
 (0)