Exemplo n.º 1
0
        internal static bool ApplyModifiers(string modifer, FilePermissions permissions)
        {
            if (Regex.IsMatch(modifer, "^[0-7]{1,3}$"))
            {
                permissions.PermissionValue = int.Parse(modifer);
                return(true);
            }

            Match match = Regex.Match(modifer, "^([augo]*)([+=-][rwx]*)+$");

            if (match.Success && match.Groups.Count >= 2)
            {
                string permissionTypeChars = match.Groups[1].Captures[0].Value;

                HashSet <FilePermissions.PermissionType> permissionTypes = new HashSet <FilePermissions.PermissionType>();

                int lengthOfTypes = Enum.GetValues(typeof(FilePermissions.PermissionType)).Length;

                if (permissionTypeChars.Length > 0)
                {
                    foreach (char permissionTypeChar in permissionTypeChars)
                    {
                        if (permissionTypes.Count > lengthOfTypes)
                        {
                            break;
                        }
                        switch (permissionTypeChar)
                        {
                        case 'u':
                            permissionTypes.Add(FilePermissions.PermissionType.User);
                            continue;

                        case 'g':
                            permissionTypes.Add(FilePermissions.PermissionType.Group);
                            continue;

                        case 'o':
                            permissionTypes.Add(FilePermissions.PermissionType.Others);
                            continue;

                        case 'a':
                            permissionTypes.UnionWith(Enum.GetValues(typeof(FilePermissions.PermissionType)).OfType <FilePermissions.PermissionType>());
                            continue;

                        default:
                            return(false);
                        }
                    }
                }
                else
                {
                    //No type specified, default to all types
                    permissionTypes.UnionWith(Enum.GetValues(typeof(FilePermissions.PermissionType)).OfType <FilePermissions.PermissionType>());
                }


                foreach (Capture permissionCapture in match.Groups[2].Captures)
                {
                    string permission = permissionCapture.Value;
                    Logger.Debug($"permission {permission}");
                    foreach (FilePermissions.PermissionType type in permissionTypes)
                    {
                        int permissionDigit = FilePermissions.CalculatePermissionDigit(permission.Contains('r'), permission.Contains('w'), permission.Contains('x'));
                        switch (permission[0])
                        {
                        case '+':
                            permissionDigit = permissions.GetPermissionDigit(type) | permissionDigit;
                            break;

                        case '=':
                            break;

                        case '-':
                            permissionDigit = permissions.GetPermissionDigit(type) & ~permissionDigit;
                            break;

                        // Logically there will never be anything but a '+', '=', or '-' here, so default throws exceptions in case of future bugs.
                        default:
                            throw new InvalidOperationException($"Invalid permission Modifier '{permission[0]}'");
                        }
                        permissions.SetPermission(type, permissionDigit);
                    }
                }
                return(true);
            }

            return(false);
        }