1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net.ftp;
  17. import java.io.Serializable;
  18. import java.util.Calendar;
  19. /***
  20. * The FTPFile class is used to represent information about files stored
  21. * on an FTP server. Because there is no standard representation for
  22. * file information on FTP servers, it may not always be possible to
  23. * extract all the information that can be represented by FTPFile, or
  24. * it may even be possible to extract more information. In cases where
  25. * more information can be extracted, you will want to subclass FTPFile
  26. * and implement your own <a href="org.apache.commons.net.ftp.FTPFileListParser.html">
  27. * FTPFileListParser </a> to extract the information.
  28. * However, most FTP servers return file information in a format that
  29. * can be completely parsed by
  30. * <a href="org.apache.commons.net.ftp.DefaultFTPFileListParser.html">
  31. * DefaultFTPFileListParser </a> and stored in FTPFile.
  32. * <p>
  33. * <p>
  34. * @author Daniel F. Savarese
  35. * @see FTPFileListParser
  36. * @see DefaultFTPFileListParser
  37. * @see FTPClient#listFiles
  38. ***/
  39. public class FTPFile implements Serializable
  40. {
  41. /** A constant indicating an FTPFile is a file. ***/
  42. public static final int FILE_TYPE = 0;
  43. /** A constant indicating an FTPFile is a directory. ***/
  44. public static final int DIRECTORY_TYPE = 1;
  45. /** A constant indicating an FTPFile is a symbolic link. ***/
  46. public static final int SYMBOLIC_LINK_TYPE = 2;
  47. /** A constant indicating an FTPFile is of unknown type. ***/
  48. public static final int UNKNOWN_TYPE = 3;
  49. /** A constant indicating user access permissions. ***/
  50. public static final int USER_ACCESS = 0;
  51. /** A constant indicating group access permissions. ***/
  52. public static final int GROUP_ACCESS = 1;
  53. /** A constant indicating world access permissions. ***/
  54. public static final int WORLD_ACCESS = 2;
  55. /** A constant indicating file/directory read permission. ***/
  56. public static final int READ_PERMISSION = 0;
  57. /** A constant indicating file/directory write permission. ***/
  58. public static final int WRITE_PERMISSION = 1;
  59. /**
  60. * A constant indicating file execute permission or directory listing
  61. * permission.
  62. ***/
  63. public static final int EXECUTE_PERMISSION = 2;
  64. int _type, _hardLinkCount;
  65. long _size;
  66. String _rawListing, _user, _group, _name, _link;
  67. Calendar _date;
  68. boolean[] _permissions[];
  69. /*** Creates an empty FTPFile. ***/
  70. public FTPFile()
  71. {
  72. _permissions = new boolean[3][3];
  73. _rawListing = null;
  74. _type = UNKNOWN_TYPE;
  75. _hardLinkCount = 0;
  76. _size = 0;
  77. _user = null;
  78. _group = null;
  79. _date = null;
  80. _name = null;
  81. }
  82. /***
  83. * Set the original FTP server raw listing from which the FTPFile was
  84. * created.
  85. * <p>
  86. * @param rawListing The raw FTP server listing.
  87. ***/
  88. public void setRawListing(String rawListing)
  89. {
  90. _rawListing = rawListing;
  91. }
  92. /***
  93. * Get the original FTP server raw listing used to initialize the FTPFile.
  94. * <p>
  95. * @return The original FTP server raw listing used to initialize the
  96. * FTPFile.
  97. ***/
  98. public String getRawListing()
  99. {
  100. return _rawListing;
  101. }
  102. /***
  103. * Determine if the file is a directory.
  104. * <p>
  105. * @return True if the file is of type <code>DIRECTORY_TYPE</code>, false if
  106. * not.
  107. ***/
  108. public boolean isDirectory()
  109. {
  110. return (_type == DIRECTORY_TYPE);
  111. }
  112. /***
  113. * Determine if the file is a regular file.
  114. * <p>
  115. * @return True if the file is of type <code>FILE_TYPE</code>, false if
  116. * not.
  117. ***/
  118. public boolean isFile()
  119. {
  120. return (_type == FILE_TYPE);
  121. }
  122. /***
  123. * Determine if the file is a symbolic link.
  124. * <p>
  125. * @return True if the file is of type <code>UNKNOWN_TYPE</code>, false if
  126. * not.
  127. ***/
  128. public boolean isSymbolicLink()
  129. {
  130. return (_type == SYMBOLIC_LINK_TYPE);
  131. }
  132. /***
  133. * Determine if the type of the file is unknown.
  134. * <p>
  135. * @return True if the file is of type <code>UNKNOWN_TYPE</code>, false if
  136. * not.
  137. ***/
  138. public boolean isUnknown()
  139. {
  140. return (_type == UNKNOWN_TYPE);
  141. }
  142. /***
  143. * Set the type of the file (<code>DIRECTORY_TYPE</code>,
  144. * <code>FILE_TYPE</code>, etc.).
  145. * <p>
  146. * @param type The integer code representing the type of the file.
  147. ***/
  148. public void setType(int type)
  149. {
  150. _type = type;
  151. }
  152. /***
  153. * Return the type of the file (one of the <code>_TYPE</code> constants),
  154. * e.g., if it is a directory, a regular file, or a symbolic link.
  155. * <p>
  156. * @return The type of the file.
  157. ***/
  158. public int getType()
  159. {
  160. return _type;
  161. }
  162. /***
  163. * Set the name of the file.
  164. * <p>
  165. * @param name The name of the file.
  166. ***/
  167. public void setName(String name)
  168. {
  169. _name = name;
  170. }
  171. /***
  172. * Return the name of the file.
  173. * <p>
  174. * @return The name of the file.
  175. ***/
  176. public String getName()
  177. {
  178. return _name;
  179. }
  180. /**
  181. * Set the file size in bytes.
  182. * @param size The file size in bytes.
  183. */
  184. public void setSize(long size)
  185. {
  186. _size = size;
  187. }
  188. /***
  189. * Return the file size in bytes.
  190. * <p>
  191. * @return The file size in bytes.
  192. ***/
  193. public long getSize()
  194. {
  195. return _size;
  196. }
  197. /***
  198. * Set the number of hard links to this file. This is not to be
  199. * confused with symbolic links.
  200. * <p>
  201. * @param links The number of hard links to this file.
  202. ***/
  203. public void setHardLinkCount(int links)
  204. {
  205. _hardLinkCount = links;
  206. }
  207. /***
  208. * Return the number of hard links to this file. This is not to be
  209. * confused with symbolic links.
  210. * <p>
  211. * @return The number of hard links to this file.
  212. ***/
  213. public int getHardLinkCount()
  214. {
  215. return _hardLinkCount;
  216. }
  217. /***
  218. * Set the name of the group owning the file. This may be
  219. * a string representation of the group number.
  220. * <p>
  221. * @param group The name of the group owning the file.
  222. ***/
  223. public void setGroup(String group)
  224. {
  225. _group = group;
  226. }
  227. /***
  228. * Returns the name of the group owning the file. Sometimes this will be
  229. * a string representation of the group number.
  230. * <p>
  231. * @return The name of the group owning the file.
  232. ***/
  233. public String getGroup()
  234. {
  235. return _group;
  236. }
  237. /***
  238. * Set the name of the user owning the file. This may be
  239. * a string representation of the user number;
  240. * <p>
  241. * @param user The name of the user owning the file.
  242. ***/
  243. public void setUser(String user)
  244. {
  245. _user = user;
  246. }
  247. /***
  248. * Returns the name of the user owning the file. Sometimes this will be
  249. * a string representation of the user number.
  250. * <p>
  251. * @return The name of the user owning the file.
  252. ***/
  253. public String getUser()
  254. {
  255. return _user;
  256. }
  257. /***
  258. * If the FTPFile is a symbolic link, use this method to set the name of the
  259. * file being pointed to by the symbolic link.
  260. * <p>
  261. * @param link The file pointed to by the symbolic link.
  262. ***/
  263. public void setLink(String link)
  264. {
  265. _link = link;
  266. }
  267. /***
  268. * If the FTPFile is a symbolic link, this method returns the name of the
  269. * file being pointed to by the symbolic link. Otherwise it returns null.
  270. * <p>
  271. * @return The file pointed to by the symbolic link (null if the FTPFile
  272. * is not a symbolic link).
  273. ***/
  274. public String getLink()
  275. {
  276. return _link;
  277. }
  278. /***
  279. * Set the file timestamp. This usually the last modification time.
  280. * The parameter is not cloned, so do not alter its value after calling
  281. * this method.
  282. * <p>
  283. * @param date A Calendar instance representing the file timestamp.
  284. ***/
  285. public void setTimestamp(Calendar date)
  286. {
  287. _date = date;
  288. }
  289. /***
  290. * Returns the file timestamp. This usually the last modification time.
  291. * <p>
  292. * @return A Calendar instance representing the file timestamp.
  293. ***/
  294. public Calendar getTimestamp()
  295. {
  296. return _date;
  297. }
  298. /***
  299. * Set if the given access group (one of the <code> _ACCESS </code>
  300. * constants) has the given access permission (one of the
  301. * <code> _PERMISSION </code> constants) to the file.
  302. * <p>
  303. * @param access The access group (one of the <code> _ACCESS </code>
  304. * constants)
  305. * @param permission The access permission (one of the
  306. * <code> _PERMISSION </code> constants)
  307. * @param value True if permission is allowed, false if not.
  308. ***/
  309. public void setPermission(int access, int permission, boolean value)
  310. {
  311. _permissions[access][permission] = value;
  312. }
  313. /***
  314. * Determines if the given access group (one of the <code> _ACCESS </code>
  315. * constants) has the given access permission (one of the
  316. * <code> _PERMISSION </code> constants) to the file.
  317. * <p>
  318. * @param access The access group (one of the <code> _ACCESS </code>
  319. * constants)
  320. * @param permission The access permission (one of the
  321. * <code> _PERMISSION </code> constants)
  322. ***/
  323. public boolean hasPermission(int access, int permission)
  324. {
  325. return _permissions[access][permission];
  326. }
  327. /***
  328. * Returns a string representation of the FTPFile information. This
  329. * will be the raw FTP server listing that was used to initialize the
  330. * FTPFile instance.
  331. * <p>
  332. * @return A string representation of the FTPFile information.
  333. ***/
  334. public String toString()
  335. {
  336. return _rawListing;
  337. }
  338. }