Skip to content

Instantly share code, notes, and snippets.

@bdx0
Forked from seaneagan/has_permission.dart
Created May 22, 2022 14:03
Show Gist options
  • Save bdx0/516f50af36e080209f3f704339e165e8 to your computer and use it in GitHub Desktop.
Save bdx0/516f50af36e080209f3f704339e165e8 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