aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/filemanager/model/Permissions.java
blob: 95000865ca8d27fd539f6140c566c59b3dd116a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright (C) 2012 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cyanogenmod.filemanager.model;

import com.cyanogenmod.filemanager.util.ParseHelper;

import java.io.Serializable;
import java.text.ParseException;

/**
 * Permissions of a filesystem object.
 *
 * @see Permission
 * @see FileSystemObject
 */
public class Permissions implements Serializable, Comparable<Permissions> {

    private static final long serialVersionUID = -8268598363293965341L;

    private UserPermission mUser;
    private GroupPermission mGroup;
    private OthersPermission mOthers;

    /**
     * Constructor of <code>Permissions</code>.
     *
     * @param user The permissions for the proprietary user of the filesystem object
     * @param group The permissions for the proprietary group of the filesystem object
     * @param others The permissions for the non proprietary users of the filesystem object
     */
    public Permissions(UserPermission user, GroupPermission group, OthersPermission others) {
        super();
        this.mUser = user;
        this.mGroup = group;
        this.mOthers = others;
    }

    /**
     * Method that returns the permissions for the proprietary user of the filesystem object.
     *
     * @return UserPermission The permissions for the proprietary user of the filesystem object
     */
    public UserPermission getUser() {
        return this.mUser;
    }

    /**
     * Method that returns the permissions for the proprietary user of the filesystem object.
     *
     * @param user The permissions for the proprietary user of the filesystem object
     */
    public void setUser(UserPermission user) {
        this.mUser = user;
    }

    /**
     * Method that returns the permissions for the proprietary group of the filesystem object.
     *
     * @return GroupPermission The permissions for the proprietary group of the filesystem object
     */
    public GroupPermission getGroup() {
        return this.mGroup;
    }

    /**
     * Method that returns the permissions for the proprietary group of the filesystem object.
     *
     * @param group The permissions for the proprietary group of the filesystem object
     */
    public void setGroup(GroupPermission group) {
        this.mGroup = group;
    }

    /**
     * Method that returns the permissions for the non proprietary users of the filesystem object.
     *
     * @return Permission The permissions for the non proprietary users of the filesystem object
     */
    public OthersPermission getOthers() {
        return this.mOthers;
    }

    /**
     * Method that returns the permissions for the non proprietary users of the filesystem object.
     *
     * @param others The permissions for the non proprietary users of the filesystem object
     */
    public void setOthers(OthersPermission others) {
        this.mOthers = others;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int compareTo(Permissions another) {
        String o1 = this.toRawString();
        String o2 = another.toRawString();
        return o1.compareTo(o2);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.mGroup == null) ? 0 : this.mGroup.hashCode());
        result = prime * result + ((this.mOthers == null) ? 0 : this.mOthers.hashCode());
        result = prime * result + ((this.mUser == null) ? 0 : this.mUser.hashCode());
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Permissions other = (Permissions) obj;
        if (this.mGroup == null) {
            if (other.mGroup != null) {
                return false;
            }
        } else if (!this.mGroup.equals(other.mGroup)) {
            return false;
        }
        if (this.mOthers == null) {
            if (other.mOthers != null) {
                return false;
            }
        } else if (!this.mOthers.equals(other.mOthers)) {
            return false;
        }
        if (this.mUser == null) {
            if (other.mUser != null) {
                return false;
            }
        } else if (!this.mUser.equals(other.mUser)) {
            return false;
        }
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "Permissions [user=" + this.mUser //$NON-NLS-1$
                + ", group=" + this.mGroup +  //$NON-NLS-1$
                ", others=" + this.mOthers + "]"; //$NON-NLS-1$//$NON-NLS-2$
    }

    /**
     * Method that returns a string representation of the permissions,
     * conforming with the unix style (rwx).
     *
     * @return String The string representation of the permissions
     */
    public String toRawString() {
        return this.mUser.toRawString()
                + this.mGroup.toRawString()
                + this.mOthers.toRawString();
    }

    /**
     * Method that converts every permission into octal numbers,
     * conforming with the unix style (xe: 0755).
     *
     * @return String The octal numbers string for the permissions
     */
    @SuppressWarnings("boxing")
    public String toOctalString() {
        //SetUID/SetGID/Sticky Bit
        int b = 0;
        if (this.mUser.isSetUID()) {
            b = b | 0x04;
        }
        if (this.mGroup.isSetGID()) {
            b = b | 0x02;
        }
        if (this.mOthers.isStickybit()) {
            b = b | 0x01;
        }
        //User
        int u = 0;
        if (this.mUser.isRead()) {
            u = u | 0x04;
        }
        if (this.mUser.isWrite()) {
            u = u | 0x02;
        }
        if (this.mUser.isExecute()) {
            u = u | 0x01;
        }
        //Group
        int g = 0;
        if (this.mGroup.isRead()) {
            g = g | 0x04;
        }
        if (this.mGroup.isWrite()) {
            g = g | 0x02;
        }
        if (this.mGroup.isExecute()) {
            g = g | 0x01;
        }
        //Others
        int o = 0;
        if (this.mOthers.isRead()) {
            o = o | 0x04;
        }
        if (this.mOthers.isWrite()) {
            o = o | 0x02;
        }
        if (this.mOthers.isExecute()) {
            o = o | 0x01;
        }

        //Return octal string
        return String.format("%d%d%d%d", b, u, g, o); //$NON-NLS-1$
    }

    /**
     * Method that parses and extracts the permissions from a unix string format.
     *
     * @param rawPermissions The raw permissions
     * @return Permissions An object with all the permissions
     * @throws ParseException If the permissions can't be parsed
     * @see ParseHelper#parsePermission(String)
     */
    public static Permissions fromRawString(String rawPermissions) throws ParseException {
        return ParseHelper.parsePermission(rawPermissions);
    }

    /**
     * Method that converts the unix style octal number into a Permissions reference
     *
     * @param octalPermissions The octal permissions
     * @return Permissions An object with all the permissions
     * @throws ParseException If the permissions can't be parsed
     */
    public static Permissions fromOctalString(String octalPermissions) throws ParseException {
        int size = octalPermissions.length();
        if (size != 3 && size != 4) {
            throw new ParseException(
                    "Invalid permissions string length: !=3 or != 4", 0); //$NON-NLS-1$
        }

        // Extract the data into char
        int cc = 0;
        char b = 0;
        if (size == 4) {
            b = octalPermissions.charAt(cc);
            cc++;
        }
        char u = octalPermissions.charAt(cc);
        cc++;
        char g = octalPermissions.charAt(cc);
        cc++;
        char o = octalPermissions.charAt(cc);
        cc++;

        //Get permissions
        UserPermission user =
                new UserPermission(
                        (u & 0x04) == 0x04,
                        (u & 0x02) == 0x02,
                        (u & 0x01) == 0x01,
                        (b & 0x01) == 0x01);
        GroupPermission group =
                new GroupPermission(
                        (g & 0x04) == 0x04,
                        (g & 0x02) == 0x02,
                        (g & 0x01) == 0x01,
                        (b & 0x02) == 0x02);
        OthersPermission other =
                new OthersPermission(
                        (o & 0x04) == 0x04,
                        (o & 0x02) == 0x02,
                        (o & 0x01) == 0x01,
                        (b & 0x04) == 0x04);
        return new Permissions(user, group, other);
    }

}