001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.net.ftp;
019 import java.io.Serializable;
020 import java.util.Calendar;
021
022 /***
023 * The FTPFile class is used to represent information about files stored
024 * on an FTP server.
025 * <p>
026 * <p>
027 * @author Daniel F. Savarese
028 * @see FTPFileEntryParser
029 * @see FTPClient#listFiles
030 ***/
031
032 public class FTPFile implements Serializable
033 {
034 /** A constant indicating an FTPFile is a file. ***/
035 public static final int FILE_TYPE = 0;
036 /** A constant indicating an FTPFile is a directory. ***/
037 public static final int DIRECTORY_TYPE = 1;
038 /** A constant indicating an FTPFile is a symbolic link. ***/
039 public static final int SYMBOLIC_LINK_TYPE = 2;
040 /** A constant indicating an FTPFile is of unknown type. ***/
041 public static final int UNKNOWN_TYPE = 3;
042
043 /** A constant indicating user access permissions. ***/
044 public static final int USER_ACCESS = 0;
045 /** A constant indicating group access permissions. ***/
046 public static final int GROUP_ACCESS = 1;
047 /** A constant indicating world access permissions. ***/
048 public static final int WORLD_ACCESS = 2;
049
050 /** A constant indicating file/directory read permission. ***/
051 public static final int READ_PERMISSION = 0;
052 /** A constant indicating file/directory write permission. ***/
053 public static final int WRITE_PERMISSION = 1;
054 /**
055 * A constant indicating file execute permission or directory listing
056 * permission.
057 ***/
058 public static final int EXECUTE_PERMISSION = 2;
059
060 int _type, _hardLinkCount;
061 long _size;
062 String _rawListing, _user, _group, _name, _link;
063 Calendar _date;
064 boolean[] _permissions[];
065
066 /*** Creates an empty FTPFile. ***/
067 public FTPFile()
068 {
069 _permissions = new boolean[3][3];
070 _rawListing = null;
071 _type = UNKNOWN_TYPE;
072 _hardLinkCount = 0;
073 _size = 0;
074 _user = null;
075 _group = null;
076 _date = null;
077 _name = null;
078 }
079
080
081 /***
082 * Set the original FTP server raw listing from which the FTPFile was
083 * created.
084 * <p>
085 * @param rawListing The raw FTP server listing.
086 ***/
087 public void setRawListing(String rawListing)
088 {
089 _rawListing = rawListing;
090 }
091
092 /***
093 * Get the original FTP server raw listing used to initialize the FTPFile.
094 * <p>
095 * @return The original FTP server raw listing used to initialize the
096 * FTPFile.
097 ***/
098 public String getRawListing()
099 {
100 return _rawListing;
101 }
102
103
104 /***
105 * Determine if the file is a directory.
106 * <p>
107 * @return True if the file is of type <code>DIRECTORY_TYPE</code>, false if
108 * not.
109 ***/
110 public boolean isDirectory()
111 {
112 return (_type == DIRECTORY_TYPE);
113 }
114
115 /***
116 * Determine if the file is a regular file.
117 * <p>
118 * @return True if the file is of type <code>FILE_TYPE</code>, false if
119 * not.
120 ***/
121 public boolean isFile()
122 {
123 return (_type == FILE_TYPE);
124 }
125
126 /***
127 * Determine if the file is a symbolic link.
128 * <p>
129 * @return True if the file is of type <code>UNKNOWN_TYPE</code>, false if
130 * not.
131 ***/
132 public boolean isSymbolicLink()
133 {
134 return (_type == SYMBOLIC_LINK_TYPE);
135 }
136
137 /***
138 * Determine if the type of the file is unknown.
139 * <p>
140 * @return True if the file is of type <code>UNKNOWN_TYPE</code>, false if
141 * not.
142 ***/
143 public boolean isUnknown()
144 {
145 return (_type == UNKNOWN_TYPE);
146 }
147
148
149 /***
150 * Set the type of the file (<code>DIRECTORY_TYPE</code>,
151 * <code>FILE_TYPE</code>, etc.).
152 * <p>
153 * @param type The integer code representing the type of the file.
154 ***/
155 public void setType(int type)
156 {
157 _type = type;
158 }
159
160
161 /***
162 * Return the type of the file (one of the <code>_TYPE</code> constants),
163 * e.g., if it is a directory, a regular file, or a symbolic link.
164 * <p>
165 * @return The type of the file.
166 ***/
167 public int getType()
168 {
169 return _type;
170 }
171
172
173 /***
174 * Set the name of the file.
175 * <p>
176 * @param name The name of the file.
177 ***/
178 public void setName(String name)
179 {
180 _name = name;
181 }
182
183 /***
184 * Return the name of the file.
185 * <p>
186 * @return The name of the file.
187 ***/
188 public String getName()
189 {
190 return _name;
191 }
192
193
194 /**
195 * Set the file size in bytes.
196 * @param size The file size in bytes.
197 */
198 public void setSize(long size)
199 {
200 _size = size;
201 }
202
203
204 /***
205 * Return the file size in bytes.
206 * <p>
207 * @return The file size in bytes.
208 ***/
209 public long getSize()
210 {
211 return _size;
212 }
213
214
215 /***
216 * Set the number of hard links to this file. This is not to be
217 * confused with symbolic links.
218 * <p>
219 * @param links The number of hard links to this file.
220 ***/
221 public void setHardLinkCount(int links)
222 {
223 _hardLinkCount = links;
224 }
225
226
227 /***
228 * Return the number of hard links to this file. This is not to be
229 * confused with symbolic links.
230 * <p>
231 * @return The number of hard links to this file.
232 ***/
233 public int getHardLinkCount()
234 {
235 return _hardLinkCount;
236 }
237
238
239 /***
240 * Set the name of the group owning the file. This may be
241 * a string representation of the group number.
242 * <p>
243 * @param group The name of the group owning the file.
244 ***/
245 public void setGroup(String group)
246 {
247 _group = group;
248 }
249
250
251 /***
252 * Returns the name of the group owning the file. Sometimes this will be
253 * a string representation of the group number.
254 * <p>
255 * @return The name of the group owning the file.
256 ***/
257 public String getGroup()
258 {
259 return _group;
260 }
261
262
263 /***
264 * Set the name of the user owning the file. This may be
265 * a string representation of the user number;
266 * <p>
267 * @param user The name of the user owning the file.
268 ***/
269 public void setUser(String user)
270 {
271 _user = user;
272 }
273
274 /***
275 * Returns the name of the user owning the file. Sometimes this will be
276 * a string representation of the user number.
277 * <p>
278 * @return The name of the user owning the file.
279 ***/
280 public String getUser()
281 {
282 return _user;
283 }
284
285
286 /***
287 * If the FTPFile is a symbolic link, use this method to set the name of the
288 * file being pointed to by the symbolic link.
289 * <p>
290 * @param link The file pointed to by the symbolic link.
291 ***/
292 public void setLink(String link)
293 {
294 _link = link;
295 }
296
297
298 /***
299 * If the FTPFile is a symbolic link, this method returns the name of the
300 * file being pointed to by the symbolic link. Otherwise it returns null.
301 * <p>
302 * @return The file pointed to by the symbolic link (null if the FTPFile
303 * is not a symbolic link).
304 ***/
305 public String getLink()
306 {
307 return _link;
308 }
309
310
311 /***
312 * Set the file timestamp. This usually the last modification time.
313 * The parameter is not cloned, so do not alter its value after calling
314 * this method.
315 * <p>
316 * @param date A Calendar instance representing the file timestamp.
317 ***/
318 public void setTimestamp(Calendar date)
319 {
320 _date = date;
321 }
322
323
324 /***
325 * Returns the file timestamp. This usually the last modification time.
326 * <p>
327 * @return A Calendar instance representing the file timestamp.
328 ***/
329 public Calendar getTimestamp()
330 {
331 return _date;
332 }
333
334
335 /***
336 * Set if the given access group (one of the <code> _ACCESS </code>
337 * constants) has the given access permission (one of the
338 * <code> _PERMISSION </code> constants) to the file.
339 * <p>
340 * @param access The access group (one of the <code> _ACCESS </code>
341 * constants)
342 * @param permission The access permission (one of the
343 * <code> _PERMISSION </code> constants)
344 * @param value True if permission is allowed, false if not.
345 ***/
346 public void setPermission(int access, int permission, boolean value)
347 {
348 _permissions[access][permission] = value;
349 }
350
351
352 /***
353 * Determines if the given access group (one of the <code> _ACCESS </code>
354 * constants) has the given access permission (one of the
355 * <code> _PERMISSION </code> constants) to the file.
356 * <p>
357 * @param access The access group (one of the <code> _ACCESS </code>
358 * constants)
359 * @param permission The access permission (one of the
360 * <code> _PERMISSION </code> constants)
361 ***/
362 public boolean hasPermission(int access, int permission)
363 {
364 return _permissions[access][permission];
365 }
366
367
368 /***
369 * Returns a string representation of the FTPFile information. This
370 * will be the raw FTP server listing that was used to initialize the
371 * FTPFile instance.
372 * <p>
373 * @return A string representation of the FTPFile information.
374 ***/
375 @Override
376 public String toString()
377 {
378 return _rawListing;
379 }
380
381 }