1c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair/*
2c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Copyright 2008 the original author or authors.
3c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
4c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Licensed under the Apache License, Version 2.0 (the "License");
5c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * you may not use this file except in compliance with the License.
6c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * You may obtain a copy of the License at
7c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
8c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *      http://www.apache.org/licenses/LICENSE-2.0
9c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
10c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Unless required by applicable law or agreed to in writing, software
11c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * distributed under the License is distributed on an "AS IS" BASIS,
12c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * See the License for the specific language governing permissions and
14c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * limitations under the License.
15c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */
16c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairpackage org.mockftpserver.fake.filesystem;
17c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
18c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairimport org.mockftpserver.core.util.Assert;
19c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
20c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair/**
21c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * Represents and encapsulates the read/write/execute permissions for a file or directory.
22c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * This is conceptually (and somewhat loosely) based on the permissions flags within the Unix
23c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * file system. An instance of this class is immutable.
24c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair *
25c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @author Chris Mair
26c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair * @version $Revision$ - $Date$
27c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair */
28c1de24f1bfa6699e54b069e300af5e4246b34a34chrismairpublic class Permissions {
29c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public static final Permissions ALL = new Permissions("rwxrwxrwx");
30c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public static final Permissions NONE = new Permissions("---------");
31c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public static final Permissions DEFAULT = ALL;
32c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
33c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    private static final char READ_CHAR = 'r';
34c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    private static final char WRITE_CHAR = 'w';
35c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    private static final char EXECUTE_CHAR = 'x';
36c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
37c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    private String rwxString;
38c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
39c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
40c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Costruct a new instance for the specified read/write/execute specification String
41c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     *
42c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @param rwxString - the read/write/execute specification String; must be 9 characters long, with chars
43c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     *                  at index 0,3,6 == '-' or 'r', chars at index 1,4,7 == '-' or 'w' and chars at index 2,5,8 == '-' or 'x'.
44c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
45c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public Permissions(String rwxString) {
46c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        Assert.isTrue(rwxString.length() == 9, "The permissions string must be exactly 9 characters");
47c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final String RWX = "(-|r)(-|w)(-|x)";
48c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        final String PATTERN = RWX + RWX + RWX;
49c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        Assert.isTrue(rwxString.matches(PATTERN), "The permissions string must match [" + PATTERN + "]");
50c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        this.rwxString = rwxString;
51c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
52c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
53c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
54c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Return the read/write/execute specification String representing the set of permissions. For example:
55c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * "rwxrwxrwx" or "rw-r-----".
56c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     *
57c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return the String containing 9 characters that represent the read/write/execute permissions.
58c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
59c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public String asRwxString() {
60c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString;
61c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
62c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
63c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
64c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return the RWX string for this instance
65c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
66c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public String getRwxString() {
67c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString;
68c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
69c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
70c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
71c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @see java.lang.Object#equals(java.lang.Object)
72c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
73c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public boolean equals(Object object) {
74c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return (object != null)
75c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair                && (object.getClass() == this.getClass())
76c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair                && (object.hashCode() == hashCode());
77c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
78c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
79c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
80c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * Return the hash code for this object.
81c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     *
82c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @see java.lang.Object#hashCode()
83c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
84c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public int hashCode() {
85c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString.hashCode();
86c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
87c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
88c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
89c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if and only if the user has read permission
90c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
91c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public boolean canUserRead() {
92c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString.charAt(0) == READ_CHAR;
93c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
94c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
95c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
96c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if and only if the user has write permission
97c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
98c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public boolean canUserWrite() {
99c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString.charAt(1) == WRITE_CHAR;
100c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
101c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
102c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
103c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if and only if the user has execute permission
104c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
105c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public boolean canUserExecute() {
106c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString.charAt(2) == EXECUTE_CHAR;
107c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
108c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
109c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
110c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if and only if the group has read permission
111c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
112c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public boolean canGroupRead() {
113c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString.charAt(3) == READ_CHAR;
114c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
115c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
116c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
117c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if and only if the group has write permission
118c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
119c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public boolean canGroupWrite() {
120c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString.charAt(4) == WRITE_CHAR;
121c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
122c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
123c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
124c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if and only if the group has execute permission
125c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
126c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public boolean canGroupExecute() {
127c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString.charAt(5) == EXECUTE_CHAR;
128c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
129c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
130c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
131c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if and only if the world has read permission
132c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
133c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public boolean canWorldRead() {
134c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString.charAt(6) == READ_CHAR;
135c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
136c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
137c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
138c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if and only if the world has write permission
139c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
140c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public boolean canWorldWrite() {
141c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString.charAt(7) == WRITE_CHAR;
142c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
143c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
144c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
145c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return true if and only if the world has execute permission
146c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
147c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public boolean canWorldExecute() {
148c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return rwxString.charAt(8) == EXECUTE_CHAR;
149c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
150c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair
151c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    /**
152c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     * @return the String representation of this object.
153c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair     */
154c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    public String toString() {
155c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair        return "Permissions[" + rwxString + "]";
156c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair    }
157c1de24f1bfa6699e54b069e300af5e4246b34a34chrismair}