Skip to content

Commit fa7f6b4

Browse files
committed
Replace UInt16 with Int16 in FilePermissions
1 parent fbe0466 commit fa7f6b4

5 files changed

+21
-9
lines changed

CotEditor/Sources/Document.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,9 @@ final class Document: NSDocument, AdditionalDocumentPreparing, EncodingChanging
478478

479479
// give the execute permission if user requested
480480
if self.saveOptions.isExecutable, !saveOperation.isAutosave {
481-
let permissions = self.fileAttributes?.permissions.mask ?? 0o644 // ???: Is the default permission really always 644?
482-
attributes[FileAttributeKey.posixPermissions] = permissions | S_IXUSR
481+
var permissions = self.fileAttributes?.permissions ?? FilePermissions(mask: 0o644) // ???: Is the default permission really always 644?
482+
permissions.user.insert(.execute)
483+
attributes[FileAttributeKey.posixPermissions] = permissions.mask
483484
}
484485

485486
// save document state to the extended file attributes

CotEditor/Sources/DocumentFile.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ extension DocumentFile.Attributes {
171171
self.creationDate = dictionary[.creationDate] as? Date
172172
self.modificationDate = dictionary[.modificationDate] as? Date
173173
self.size = dictionary[.size] as? Int64 ?? 0
174-
self.permissions = FilePermissions(mask: dictionary[.posixPermissions] as? UInt16 ?? 0)
174+
self.permissions = FilePermissions(mask: dictionary[.posixPermissions] as? Int16 ?? 0)
175175
self.owner = dictionary[.ownerAccountName] as? String
176176
}
177177
}

CotEditor/Sources/EditorTextView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ extension EditorTextView {
17671767
}
17681768
if movement == NSIllegalTextMovement,
17691769
let character = event.characters?.utf16.first,
1770-
character < 0xF700, character != UInt16(NSDeleteCharacter)
1770+
character < 0xF700, character != Int16(NSDeleteCharacter)
17711771
{ // standard key-input
17721772
self.needsRecompletion = true
17731773
}

CotEditor/Sources/FilePermissions.swift

+6-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct FilePermissions: Equatable {
3232

3333
struct Permission: OptionSet {
3434

35-
let rawValue: UInt16
35+
let rawValue: Int16
3636

3737
static let read = Self(rawValue: 0b100)
3838
static let write = Self(rawValue: 0b010)
@@ -48,15 +48,16 @@ struct FilePermissions: Equatable {
4848
}
4949

5050

51-
init(mask: UInt16) {
51+
init(mask: Int16) {
5252

5353
self.user = Permission(rawValue: (mask & 0b111 << 6) >> 6)
5454
self.group = Permission(rawValue: (mask & 0b111 << 3) >> 3)
5555
self.others = Permission(rawValue: (mask & 0b111))
5656
}
5757

5858

59-
var mask: UInt16 {
59+
/// The `Int16` value.
60+
var mask: Int16 {
6061

6162
let userMask = self.user.rawValue << 6
6263
let groupMask = self.group.rawValue << 3
@@ -66,14 +67,14 @@ struct FilePermissions: Equatable {
6667
}
6768

6869

69-
/// The human-readable permission expression like "rwxr--r--"
70+
/// The human-readable permission expression like rwxr--r--”.
7071
var symbolic: String {
7172

7273
self.user.symbolic + self.group.symbolic + self.others.symbolic
7374
}
7475

7576

76-
/// The octal value expression like "644"
77+
/// The octal value expression like 644”.
7778
var octal: String {
7879

7980
String(self.mask, radix: 8)

Tests/FilePermissionTests.swift

+10
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ final class FilePermissionTests: XCTestCase {
4444
XCTAssertEqual(FilePermissions(mask: 0o777).formatted(.filePermissions(.full)), "777 (-rwxrwxrwx)")
4545
XCTAssertEqual(FilePermissions(mask: 0o643).formatted(.filePermissions(.full)), "643 (-rw-r---wx)")
4646
}
47+
48+
49+
func testCalculation() {
50+
51+
var permissions = FilePermissions(mask: 0o644)
52+
permissions.user.insert(.execute)
53+
54+
XCTAssertTrue(permissions.user.contains(.execute))
55+
XCTAssertEqual(permissions.mask, 0o744)
56+
}
4757
}

0 commit comments

Comments
 (0)