termux-app

Форк
0
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

26
package com.termux.shared.file.filesystem;
27

28
import static com.termux.shared.file.filesystem.FilePermission.*;
29

30
import 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

41
public final class FilePermissions {
42
    private FilePermissions() { }
43

44
    // Write string representation of permission bits to {@code sb}.
45
    private static void writeBits(StringBuilder sb, boolean r, boolean w, boolean x) {
46
        if (r) {
47
            sb.append('r');
48
        } else {
49
            sb.append('-');
50
        }
51
        if (w) {
52
            sb.append('w');
53
        } else {
54
            sb.append('-');
55
        }
56
        if (x) {
57
            sb.append('x');
58
        } else {
59
            sb.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
     */
76
    public static String toString(Set<FilePermission> perms) {
77
        StringBuilder sb = new StringBuilder(9);
78
        writeBits(sb, perms.contains(OWNER_READ), perms.contains(OWNER_WRITE),
79
            perms.contains(OWNER_EXECUTE));
80
        writeBits(sb, perms.contains(GROUP_READ), perms.contains(GROUP_WRITE),
81
            perms.contains(GROUP_EXECUTE));
82
        writeBits(sb, perms.contains(OTHERS_READ), perms.contains(OTHERS_WRITE),
83
            perms.contains(OTHERS_EXECUTE));
84
        return sb.toString();
85
    }
86

87
    private static boolean isSet(char c, char setValue) {
88
        if (c == setValue)
89
            return true;
90
        if (c == '-')
91
            return false;
92
        throw new IllegalArgumentException("Invalid mode");
93
    }
94
    private static boolean isR(char c) { return isSet(c, 'r'); }
95
    private static boolean isW(char c) { return isSet(c, 'w'); }
96
    private 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&lt;FilePermission&gt; 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
     */
129
    public static Set<FilePermission> fromString(String perms) {
130
        if (perms.length() != 9)
131
            throw new IllegalArgumentException("Invalid mode");
132
        Set<FilePermission> result = EnumSet.noneOf(FilePermission.class);
133
        if (isR(perms.charAt(0))) result.add(OWNER_READ);
134
        if (isW(perms.charAt(1))) result.add(OWNER_WRITE);
135
        if (isX(perms.charAt(2))) result.add(OWNER_EXECUTE);
136
        if (isR(perms.charAt(3))) result.add(GROUP_READ);
137
        if (isW(perms.charAt(4))) result.add(GROUP_WRITE);
138
        if (isX(perms.charAt(5))) result.add(GROUP_EXECUTE);
139
        if (isR(perms.charAt(6))) result.add(OTHERS_READ);
140
        if (isW(perms.charAt(7))) result.add(OTHERS_WRITE);
141
        if (isX(perms.charAt(8))) result.add(OTHERS_EXECUTE);
142
        return result;
143
    }
144

145
}
146

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.