DiskSession.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (C) 2006-2010 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.alfresco.jlan.client;
  20. import java.io.FileNotFoundException;
  21. import java.io.IOException;
  22. import org.alfresco.jlan.client.info.DiskInfo;
  23. import org.alfresco.jlan.client.info.FileInfo;
  24. import org.alfresco.jlan.client.info.VolumeInfo;
  25. import org.alfresco.jlan.server.filesys.FileAttribute;
  26. import org.alfresco.jlan.smb.PCShare;
  27. import org.alfresco.jlan.smb.SMBDeviceType;
  28. import org.alfresco.jlan.smb.SMBException;
  29. /**
  30. * <p>The DiskSession class provides disk, directory and file related methods on
  31. * a remote disk share.
  32. *
  33. * <p>The disk session maintains a current working directory, initially set from the
  34. * PCShare object that was used to open the disk session. Methods such as CreateDirectory(),
  35. * DeleteDirectory(), OpenFile() etc. will prepend the working directory string to the
  36. * specified file or directory string, unless the specified file or directory contains a
  37. * path. The current working directory can be changed using the setWorkingDirectory() method.
  38. *
  39. * <p>A disk session is created using the SessionFactory.OpenDiskSession() method. The
  40. * SessionFactory negotiates the appropriate SMB dialect with the remote server and creates
  41. * the appropriate DiskSession derived object.
  42. *
  43. * @see SessionFactory
  44. *
  45. * @author gkspencer
  46. */
  47. public abstract class DiskSession extends Session {
  48. // Default information level to be returned by a directory search
  49. public static final int DefaultInformationLevel = 1;
  50. // Flags for the setFileInformation() method
  51. public final static int Attributes = 0x0001;
  52. public final static int WriteTime = 0x0002;
  53. public final static int WriteDate = 0x0004;
  54. /**
  55. * Class constructor
  56. *
  57. * @param shr PCShare
  58. * @param dialect int
  59. */
  60. protected DiskSession(PCShare shr, int dialect) {
  61. super(shr, dialect, null);
  62. // Set the device type
  63. this.setDeviceType(SMBDeviceType.Disk);
  64. }
  65. /**
  66. * Close this connection with the remote server share.
  67. *
  68. * @exception java.io.IOException If an I/O error occurs.
  69. * @exception SMBException If an SMB level error occurs
  70. */
  71. public void CloseSession()
  72. throws IOException, SMBException {
  73. // Close the network session
  74. super.CloseSession();
  75. }
  76. /**
  77. * Create a new directory on the remote file server.
  78. *
  79. * @param dir Directory name string. If the directory name does not have a leading '\' the
  80. * current working directory for this session will be prepended to the string.
  81. * @exception java.io.IOException If an I/O error occurs.
  82. * @exception SMBException If an SMB level error occurs
  83. */
  84. public abstract void CreateDirectory(String dir)
  85. throws IOException, SMBException;
  86. /**
  87. * Create and open a file on the remote file server.
  88. *
  89. * @param fname Remote file name string.
  90. * @return SMBFile for the opened file, else null.
  91. * @exception java.io.IOException If an I/O error occurs
  92. * @exception SMBException If an SMB error occurs
  93. */
  94. public abstract SMBFile CreateFile(String fname)
  95. throws IOException, SMBException;
  96. /**
  97. * Delete the specified directory on the remote file server.
  98. *
  99. * @param dir Directory name string. If the directory name does not have a leading '\' the
  100. * current working directory for this session will be preprended to the string.
  101. * @exception java.io.IOException If an I/O error occurs.
  102. * @exception SMBException If an SMB level error occurs
  103. */
  104. public abstract void DeleteDirectory(String dir)
  105. throws IOException, SMBException;
  106. /**
  107. * Delete the specified file on the remote file server.
  108. *
  109. * @param fname File name of the remote file to delete. If the file name does not have a leading
  110. * '\' the current working directory for this session will be prepended to the
  111. * string. The string may contain wildcard characters to delete multiple files. '?'
  112. * matches a single character and '*' matches none, one or more characters.
  113. * @exception java.io.IOException If an I/O error occurs.
  114. * @exception SMBException If an SMB level error occurs
  115. */
  116. public void DeleteFile(String fname)
  117. throws IOException, SMBException {
  118. // Call the delete file method for normal files
  119. DeleteFile(fname, FileAttribute.Normal);
  120. }
  121. /**
  122. * Delete the specified file on the remote file server.
  123. *
  124. * @param fname File name of the remote file to delete. If the file name does not have a leading
  125. * '\' the current working directory for this session will be prepended to the
  126. * string. The string may contain wildcard characters to delete multiple files. '?'
  127. * matches a single character and '*' matches none, one or more characters.
  128. * @param attr File attributes of the file(s) to delete.
  129. * @exception java.io.IOException If an I/O error occurs.
  130. * @exception SMBException If an SMB level error occurs
  131. */
  132. public abstract void DeleteFile(String fname, int attr)
  133. throws IOException, SMBException;
  134. /**
  135. * Check if a file exists on the remote file server.
  136. *
  137. * @param fname File name to test for on the remote file server. If the file name does not start
  138. * with a '\' then the working directory is prepended to the file name string.
  139. * @return true if the file exists, else false.
  140. */
  141. public boolean FileExists(String fname) {
  142. // Try and read the file attributes of the specified file on the remote server
  143. boolean sts = false;
  144. try {
  145. // Check if the file name has a path
  146. if ( fname.startsWith("\\")) {
  147. // Read the file information for the remote file
  148. if ( getFileInformation(fname) != null)
  149. sts = true;
  150. }
  151. else {
  152. // Add the current working directory to the file name string
  153. if ( getFileInformation(PCShare.makePath(getWorkingDirectory(), fname)) != null)
  154. sts = true;
  155. }
  156. }
  157. catch (SMBException ex) {
  158. }
  159. catch (FileNotFoundException ex) {
  160. }
  161. catch (IOException ex) {
  162. }
  163. // Return the file status
  164. return sts;
  165. }
  166. /**
  167. * Finalize the object
  168. */
  169. protected void finalize() {
  170. // Make sure the session is closed
  171. if ( !isClosed()) {
  172. // Close the disk session
  173. try {
  174. CloseSession();
  175. }
  176. catch (SMBException ex) {
  177. }
  178. catch (IOException ex) {
  179. }
  180. }
  181. }
  182. /**
  183. * Get disk information for this remote disk.
  184. *
  185. * @return Disk information object, or null.
  186. * @exception java.io.IOException If an I/O error occurs.
  187. * @exception SMBException If an SMB level error occurs
  188. */
  189. public abstract DiskInfo getDiskInformation()
  190. throws IOException, SMBException;
  191. /**
  192. * Get file information for the specified file.
  193. *
  194. * @param fname File name of the file to return information for.
  195. * @param level Information level required
  196. * @return SMBFileInfo if the request was successful, else null.
  197. * @exception java.io.IOException If an I/O error occurs.
  198. * @exception java.io.FileNotFoundException If the remote file does not exist.
  199. * @exception SMBException If an SMB level error occurs
  200. */
  201. public abstract FileInfo getFileInformation(String fname, int level)
  202. throws IOException, FileNotFoundException, SMBException;
  203. /**
  204. * Get file information for the specified file, returning the default information level
  205. *
  206. * @param fname File name of the file to return information for.
  207. * @return SMBFileInfo if the request was successful, else null.
  208. * @exception java.io.IOException If an I/O error occurs.
  209. * @exception java.io.FileNotFoundException If the remote file does not exist.
  210. * @exception SMBException If an SMB level error occurs
  211. */
  212. public final FileInfo getFileInformation(String fname)
  213. throws IOException, FileNotFoundException, SMBException {
  214. // Return the default information level
  215. return getFileInformation(fname, DefaultInformationLevel);
  216. }
  217. /**
  218. * Get the disk volume information
  219. *
  220. * @return VolumeInfo, or null
  221. * @exception java.io.FileNotFoundException If the remote file does not exist.
  222. * @exception SMBException If an SMB level error occurs
  223. */
  224. public abstract VolumeInfo getVolumeInformation()
  225. throws IOException, SMBException;
  226. /**
  227. * Get the current working directory, relative to the share that is being accessed.
  228. *
  229. * @return Current working directory path string.
  230. */
  231. public final String getWorkingDirectory() {
  232. return this.getPCShare().getPath();
  233. }
  234. /**
  235. * Detemine if the disk session has been closed.
  236. *
  237. * @return true if the disk session has been closed, else false.
  238. */
  239. public final boolean isClosed() {
  240. return m_treeid == Closed ? true : false;
  241. }
  242. /**
  243. * Check if the specified file name is a directory.
  244. *
  245. * @param dir Directory name string. If the directory name does not have a leading '\' the
  246. * current working directory for this session will be prepended to the string.
  247. * @return true if the specified file name is a directory, else false.
  248. * @exception java.io.IOException If an I/O error occurs.
  249. * @exception SMBException If an SMB level error occurs
  250. */
  251. public abstract boolean isDirectory(String dir)
  252. throws IOException, SMBException;
  253. /**
  254. * Open a file on the remote file server.
  255. *
  256. * @param fname Remote file name string.
  257. * @param flags File open option flags.
  258. * @return SMBFile for the opened file, else null.
  259. * @exception java.io.IOException If an I/O error occurs
  260. * @exception SMBException If an SMB error occurs
  261. */
  262. public abstract SMBFile OpenFile(String fname, int flags)
  263. throws IOException, SMBException;
  264. /**
  265. * Open a file as an input stream.
  266. *
  267. * @param fname Remote file name string.
  268. * @param flags File open option flags.
  269. * @return SMBInputStream for the opened file, else null.
  270. * @exception java.io.IOException If an I/O error occurs
  271. * @exception SMBException If an SMB error occurs
  272. */
  273. public SMBInputStream OpenInputStream(String fname, int flags)
  274. throws IOException, SMBException {
  275. // Open an SMBFile first
  276. SMBFile sfile = OpenFile(fname, flags);
  277. if ( sfile == null)
  278. return null;
  279. // Create an input stream attached to the SMBFile
  280. return new SMBInputStream(sfile);
  281. }
  282. /**
  283. * Open a file as an output stream.
  284. *
  285. * @param fname Remote file name string.
  286. * @param flags File open option flags.
  287. * @return SMBOutputStream for the opened file, else null.
  288. * @exception java.io.IOException If an I/O error occurs
  289. * @exception SMBException If an SMB error occurs
  290. */
  291. public SMBOutputStream OpenOutputStream(String fname, int flags)
  292. throws IOException, SMBException {
  293. // Open an SMBFile first
  294. SMBFile sfile = OpenFile(fname, flags);
  295. if ( sfile == null)
  296. return null;
  297. // Create an output stream attached to the SMBFile
  298. return new SMBOutputStream(sfile);
  299. }
  300. /**
  301. * Rename a file, or set of files, on the remote file server.
  302. *
  303. * @param curnam Current file name string, may contain wildcards.
  304. * @param newnam New file name.
  305. * @return true if the file(s) were renamed, else false
  306. * @exception java.io.IOException If an I/O error occurs.
  307. * @exception SMBException If an SMB level error occurs
  308. */
  309. public boolean RenameFile(String curnam, String newnam)
  310. throws IOException, SMBException {
  311. // Rename the normal attribute file(s)
  312. return RenameFile(curnam, newnam, FileAttribute.Normal);
  313. }
  314. /**
  315. * Rename a file, or set of files, on the remote file server.
  316. *
  317. * @param curnam Current file name string, may contain wildcards.
  318. * @param newnam New file name.
  319. * @param attr Search attributes, to determine which file(s) to rename.
  320. * @see org.alfresco.jlan.server.filesys.FileAttribute
  321. * @return true if the file(s) were renamed, else false
  322. * @exception java.io.IOException If an I/O error occurs.
  323. * @exception SMBException If an SMB level error occurs
  324. */
  325. public abstract boolean RenameFile(String curnam, String newnam, int attr)
  326. throws IOException, SMBException;
  327. /**
  328. * Set file information for the specified file.
  329. *
  330. * @param fname File name of the file to set information for.
  331. * @param finfo File information containing the new values.
  332. * @exception java.io.IOException If an I/O error occurs.
  333. * @exception SMBException If an SMB level error occurs
  334. */
  335. public abstract void setFileInformation(String fname, FileInfo finfo)
  336. throws IOException, SMBException;
  337. /**
  338. * Set file information for the specified file, using the file id
  339. *
  340. * @param file File to set information for
  341. * @param finfo File information containing new values
  342. * @exception java.io.IOException If an I/O error occurs.
  343. * @exception SMBException If an SMB level error occurs
  344. */
  345. public abstract void setFileInformation(SMBFile file, FileInfo finfo)
  346. throws IOException, SMBException;
  347. /**
  348. * Set file attributes for the specified file, using the file name
  349. *
  350. * @param fname File name of the file to set information for.
  351. * @param attrib File attributes mask. @see org.alfresco.jlan.server.filesys.FileAttribute
  352. * @exception java.io.IOException If an I/O error occurs.
  353. * @exception SMBException If an SMB level error occurs
  354. */
  355. public abstract void setFileAttributes(String fname, int attrib)
  356. throws IOException, SMBException;
  357. /**
  358. * Set the current working directory, relative to the share that is being accessed.
  359. *
  360. * @param wd Working directory path string.
  361. */
  362. public final void setWorkingDirectory(String wd) {
  363. this.getPCShare().setPath(wd);
  364. }
  365. /**
  366. * Start a search of the specified directory returning information for each file/directory
  367. * found.
  368. *
  369. * @param dir Directory/file name string, which may contain wildcards. If the directory string
  370. * does not start with a '\' then the directory name is prepended with the current
  371. * working directory.
  372. * @param attr Search attributes, to determine the types of files/directories returned. @see
  373. * org.alfresco.jlans.server.filesys.FileAttribute
  374. * @param level Information level to be returned. @see org.alfresco.jlan.smb.FileInfoLevel
  375. * @return SearchContext for this search, else null
  376. * @exception java.io.IOException If an I/O error occurs
  377. * @exception SMBException If an SMB level error occurs
  378. */
  379. public abstract SearchContext StartSearch(String dir, int attr, int level)
  380. throws IOException, SMBException;
  381. /**
  382. * Start a search of the specified directory returning the default information level
  383. *
  384. * @param dir Directory/file name string, which may contain wildcards. If the directory string
  385. * does not start with a '\' then the directory name is prepended with the current
  386. * working directory.
  387. * @param attr Search attributes, to determine the types of files/directories returned. @see
  388. * org.alfresco.jlan.server.filesys.FileAttribute
  389. * @return SearchContext for this search, else null
  390. * @exception java.io.IOException If an I/O error occurs
  391. * @exception SMBException If an SMB level error occurs
  392. */
  393. public final SearchContext StartSearch(String dir, int attr)
  394. throws IOException, SMBException {
  395. // Start a search using the default information level
  396. return StartSearch(dir, attr, DefaultInformationLevel);
  397. }
  398. /**
  399. * Check if a path looks like a valid file path
  400. *
  401. * @param path String
  402. * @return boolean
  403. */
  404. protected final boolean isValidFilePath(String path) {
  405. // Check the path
  406. if ( path == null || path.endsWith("\\"))
  407. return false;
  408. return true;
  409. }
  410. }