So I'm using the chown
C API (man 2 chown
, Ubuntu 16.04, Swift 4.1). And the man page says the chown call has the following signature:
int chown(const char *pathname, uid_t owner, gid_t group);
Then in the man page description section it says:
If the owner or group is specified as -1, then that ID is not changed.
So, I have this function I'm trying to use to call into the chown C API:
public func change(owner uid: uid_t = -1, group gid: gid_t = -1) throws {
guard chown(string, uid, gid) == 0 else {
throw ChangeOwnershipError.getError()
}
}
Which fails to compile with the following error message on the function declaration line:
integer literal '-1' overflows when stored into unsigned 'Builtin.Int32'
The uid_t
/gid_t
types are correctly being typecast as UInt32
.
I also tried this, with the same error message though.
public func change(owner uid: uid_t? = nil, group gid: gid_t? = nil) throws {
>> guard chown(string, uid ?? -1, gid ?? -1) == 0 else {
throw ChangeOwnershipError.getError()
}
}
I understand that -1 is not a valid unsigned integer and would cause an overflow. My frustration is that I can't literally follow the documentation, even though I realize I can technically just use UInt32.max
for the same result.
Is there some way around this by overriding the integer literal inits for UInt32? or will the stdlib implementation just be called anyways?