Skip to content

Instantly share code, notes, and snippets.

@seaneagan
Last active May 22, 2022 14:03
Show Gist options
  • Save seaneagan/e01ae92825694a4f4dc4 to your computer and use it in GitHub Desktop.
Save seaneagan/e01ae92825694a4f4dc4 to your computer and use it in GitHub Desktop.
/// See http://dartbug.com/22036
library has_permission;
import 'dart:io';
enum FilePermission { READ, WRITE, EXECUTE, SET_UID, SET_GID, STICKY }
enum FilePermissionRole { WORLD, GROUP, USER }
bool hasPermission(FileStat stat, FilePermission permission, {FilePermissionRole role: FilePermissionRole.WORLD}) {
var bitIndex = _getPermissionBitIndex(permission, role);
return (stat.mode & (1 << bitIndex)) != 0;
}
int _getPermissionBitIndex(FilePermission permission, FilePermissionRole role) {
switch (permission) {
case FilePermission.SET_UID: return 11;
case FilePermission.SET_GID: return 10;
case FilePermission.STICKY: return 9;
default: return (role.index * 3) + permission.index;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment