termux-app
145 строк · 5.7 Кб
1/*
2* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*
5* This code is free software; you can redistribute it and/or modify it
6* under the terms of the GNU General Public License version 2 only, as
7* published by the Free Software Foundation. Oracle designates this
8* particular file as subject to the "Classpath" exception as provided
9* by Oracle in the LICENSE file that accompanied this code.
10*
11* This code is distributed in the hope that it will be useful, but WITHOUT
12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14* version 2 for more details (a copy is included in the LICENSE file that
15* accompanied this code).
16*
17* You should have received a copy of the GNU General Public License version
18* 2 along with this work; if not, write to the Free Software Foundation,
19* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20*
21* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22* or visit www.oracle.com if you need additional information or have any
23* questions.
24*/
25
26package com.termux.shared.file.filesystem;
27
28import static com.termux.shared.file.filesystem.FilePermission.*;
29
30import java.util.*;
31
32/**
33* This class consists exclusively of static methods that operate on sets of
34* {@link FilePermission} objects.
35*
36* https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:libcore/ojluni/src/main/java/java/nio/file/attribute/PosixFilePermissions.java
37*
38* @since 1.7
39*/
40
41public final class FilePermissions {
42private FilePermissions() { }
43
44// Write string representation of permission bits to {@code sb}.
45private static void writeBits(StringBuilder sb, boolean r, boolean w, boolean x) {
46if (r) {
47sb.append('r');
48} else {
49sb.append('-');
50}
51if (w) {
52sb.append('w');
53} else {
54sb.append('-');
55}
56if (x) {
57sb.append('x');
58} else {
59sb.append('-');
60}
61}
62
63/**
64* Returns the {@code String} representation of a set of permissions. It
65* is guaranteed that the returned {@code String} can be parsed by the
66* {@link #fromString} method.
67*
68* <p> If the set contains {@code null} or elements that are not of type
69* {@code FilePermission} then these elements are ignored.
70*
71* @param perms
72* the set of permissions
73*
74* @return the string representation of the permission set
75*/
76public static String toString(Set<FilePermission> perms) {
77StringBuilder sb = new StringBuilder(9);
78writeBits(sb, perms.contains(OWNER_READ), perms.contains(OWNER_WRITE),
79perms.contains(OWNER_EXECUTE));
80writeBits(sb, perms.contains(GROUP_READ), perms.contains(GROUP_WRITE),
81perms.contains(GROUP_EXECUTE));
82writeBits(sb, perms.contains(OTHERS_READ), perms.contains(OTHERS_WRITE),
83perms.contains(OTHERS_EXECUTE));
84return sb.toString();
85}
86
87private static boolean isSet(char c, char setValue) {
88if (c == setValue)
89return true;
90if (c == '-')
91return false;
92throw new IllegalArgumentException("Invalid mode");
93}
94private static boolean isR(char c) { return isSet(c, 'r'); }
95private static boolean isW(char c) { return isSet(c, 'w'); }
96private static boolean isX(char c) { return isSet(c, 'x'); }
97
98/**
99* Returns the set of permissions corresponding to a given {@code String}
100* representation.
101*
102* <p> The {@code perms} parameter is a {@code String} representing the
103* permissions. It has 9 characters that are interpreted as three sets of
104* three. The first set refers to the owner's permissions; the next to the
105* group permissions and the last to others. Within each set, the first
106* character is {@code 'r'} to indicate permission to read, the second
107* character is {@code 'w'} to indicate permission to write, and the third
108* character is {@code 'x'} for execute permission. Where a permission is
109* not set then the corresponding character is set to {@code '-'}.
110*
111* <p> <b>Usage Example:</b>
112* Suppose we require the set of permissions that indicate the owner has read,
113* write, and execute permissions, the group has read and execute permissions
114* and others have none.
115* <pre>
116* Set<FilePermission> perms = FilePermissions.fromString("rwxr-x---");
117* </pre>
118*
119* @param perms
120* string representing a set of permissions
121*
122* @return the resulting set of permissions
123*
124* @throws IllegalArgumentException
125* if the string cannot be converted to a set of permissions
126*
127* @see #toString(Set)
128*/
129public static Set<FilePermission> fromString(String perms) {
130if (perms.length() != 9)
131throw new IllegalArgumentException("Invalid mode");
132Set<FilePermission> result = EnumSet.noneOf(FilePermission.class);
133if (isR(perms.charAt(0))) result.add(OWNER_READ);
134if (isW(perms.charAt(1))) result.add(OWNER_WRITE);
135if (isX(perms.charAt(2))) result.add(OWNER_EXECUTE);
136if (isR(perms.charAt(3))) result.add(GROUP_READ);
137if (isW(perms.charAt(4))) result.add(GROUP_WRITE);
138if (isX(perms.charAt(5))) result.add(GROUP_EXECUTE);
139if (isR(perms.charAt(6))) result.add(OTHERS_READ);
140if (isW(perms.charAt(7))) result.add(OTHERS_WRITE);
141if (isX(perms.charAt(8))) result.add(OTHERS_EXECUTE);
142return result;
143}
144
145}
146