Last active
May 22, 2022 14:03
-
-
Save seaneagan/e01ae92825694a4f4dc4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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