Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 48e00e438ce03da490cc72c007f468ea726f73d6 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2005, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * 	Martin Oberhuber (Wind River) - [170317] add symbolic link support to API
 *******************************************************************************/
package org.eclipse.core.filesystem.provider;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.internal.filesystem.local.LocalFileNativesManager;

/**
 * This class should be used by file system providers in their implementation
 * of API methods that return {@link IFileInfo} objects.
 * 
 * @since org.eclipse.core.filesystem 1.0
 * @noextend This class is not intended to be subclassed by clients.
 */
public class FileInfo implements IFileInfo {
	/**
	 * Internal attribute indicating if the file is a directory
	 */
	private static final int ATTRIBUTE_DIRECTORY = 1 << 0;

	/**
	 * Internal attribute indicating if the file exists.
	 */
	private static final int ATTRIBUTE_EXISTS = 1 << 16;

	/**
	 * Bit field of file attributes. Initialized to specify a writable resource.
	 */
	private int attributes = EFS.ATTRIBUTE_OWNER_WRITE | EFS.ATTRIBUTE_OWNER_READ;

	/**
	 * The last modified time.
	 */
	private long lastModified = EFS.NONE;

	/**
	 * The file length.
	 */
	private long length = EFS.NONE;

	/**
	 * The file name.
	 */
	private String name = ""; //$NON-NLS-1$

	/**
	 * The target file name if this is a symbolic link
	 */
	private String linkTarget = null;

	/**
	 * Creates a new file information object with default values.
	 */
	public FileInfo() {
		super();
	}

	/**
	 * Creates a new file information object. All values except the file name
	 * will have default values.
	 * 
	 * @param name The name of this file
	 */
	public FileInfo(String name) {
		super();
		this.name = name;
	}

	/**
	 * Convenience method to clear a masked region of the attributes bit field.
	 * 
	 * @param mask The mask to be cleared
	 */
	private void clear(int mask) {
		attributes &= ~mask;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			//we know this object is cloneable
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Object o) {
		return name.compareTo(((FileInfo) o).name);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.filesystem.IFileInfo#exists()
	 */
	public boolean exists() {
		return getAttribute(ATTRIBUTE_EXISTS);
	}

	public boolean getAttribute(int attribute) {
		if (attribute == EFS.ATTRIBUTE_READ_ONLY && isAttributeSuported(EFS.ATTRIBUTE_OWNER_WRITE))
			return (!isSet(EFS.ATTRIBUTE_OWNER_WRITE)) || isSet(EFS.ATTRIBUTE_IMMUTABLE);
		else if (attribute == EFS.ATTRIBUTE_EXECUTABLE && isAttributeSuported(EFS.ATTRIBUTE_OWNER_EXECUTE))
			return isSet(EFS.ATTRIBUTE_OWNER_EXECUTE);
		else
			return isSet(attribute);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.filesystem.IFileInfo#getStringAttribute(int)
	 */
	public String getStringAttribute(int attribute) {
		if (attribute == EFS.ATTRIBUTE_LINK_TARGET)
			return this.linkTarget;
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.filesystem.IFileInfo#lastModified()
	 */
	public long getLastModified() {
		return lastModified;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.filesystem.IFileInfo#length()
	 */
	public long getLength() {
		return length;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.filesystem.IFileInfo#getName()
	 */
	public String getName() {
		return name;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.filesystem.IFileInfo#isDirectory()
	 */
	public boolean isDirectory() {
		return isSet(ATTRIBUTE_DIRECTORY);
	}

	private boolean isSet(long mask) {
		return (attributes & mask) != 0;
	}

	private void set(int mask) {
		attributes |= mask;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.filesystem.IFileInfo#setAttribute(int, boolean)
	 */
	public void setAttribute(int attribute, boolean value) {
		if (attribute == EFS.ATTRIBUTE_READ_ONLY && isAttributeSuported(EFS.ATTRIBUTE_OWNER_WRITE)) {
			if (value) {
				clear(EFS.ATTRIBUTE_OWNER_WRITE | EFS.ATTRIBUTE_OTHER_WRITE | EFS.ATTRIBUTE_GROUP_WRITE);
				set(EFS.ATTRIBUTE_IMMUTABLE);
			} else {
				set(EFS.ATTRIBUTE_OWNER_WRITE | EFS.ATTRIBUTE_OWNER_READ);
				clear(EFS.ATTRIBUTE_IMMUTABLE);
			}
		} else if (attribute == EFS.ATTRIBUTE_EXECUTABLE && isAttributeSuported(EFS.ATTRIBUTE_OWNER_EXECUTE)) {
			if (value)
				set(EFS.ATTRIBUTE_OWNER_EXECUTE);
			else
				clear(EFS.ATTRIBUTE_OWNER_EXECUTE | EFS.ATTRIBUTE_GROUP_EXECUTE | EFS.ATTRIBUTE_OTHER_EXECUTE);
		} else {
			if (value)
				set(attribute);
			else
				clear(attribute);
		}
	}

	private static boolean isAttributeSuported(int value) {
		return (LocalFileNativesManager.getSupportedAttributes() & value) != 0;
	}

	/**
	 * Sets whether this is a file or directory.
	 * 
	 * @param value <code>true</code> if this is a directory, and <code>false</code>
	 * if this is a file.
	 */
	public void setDirectory(boolean value) {
		if (value)
			set(ATTRIBUTE_DIRECTORY);
		else
			clear(ATTRIBUTE_DIRECTORY);
	}

	/**
	 * Sets whether this file or directory exists.
	 * 
	 * @param value <code>true</code> if this file exists, and <code>false</code>
	 * otherwise.
	 */
	public void setExists(boolean value) {
		if (value)
			set(ATTRIBUTE_EXISTS);
		else
			clear(ATTRIBUTE_EXISTS);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.filesystem.IFileInfo#setLastModified(long)
	 */
	public void setLastModified(long value) {
		lastModified = value;
	}

	/**
	 * Sets the length of this file. A value of {@link EFS#NONE}
	 * indicates the file does not exist, is a directory, or the length could not be computed.
	 * 
	 * @param value the length of this file, or {@link EFS#NONE}
	 */
	public void setLength(long value) {
		this.length = value;
	}

	/**
	 * Sets the name of this file.
	 * 
	 * @param name The file name
	 */
	public void setName(String name) {
		if (name == null)
			throw new IllegalArgumentException();
		this.name = name;
	}

	/**
	 * Sets or clears a String attribute, e.g. symbolic link target.
	 * 
	 * @param attribute The kind of attribute to set. Currently only
	 * {@link EFS#ATTRIBUTE_LINK_TARGET} is supported.
	 * @param value The String attribute, or <code>null</code> to clear
	 * the attribute
	 * @since org.eclipse.core.filesystem 1.1
	 */
	public void setStringAttribute(int attribute, String value) {
		if (attribute == EFS.ATTRIBUTE_LINK_TARGET)
			this.linkTarget = value;
	}

	/**
	 * For debugging purposes only.
	 */
	public String toString() {
		return name;
	}
}

Back to the top