CIFSSearchContext.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 org.alfresco.jlan.client.info.FileInfo;
  21. import org.alfresco.jlan.smb.DataType;
  22. import org.alfresco.jlan.smb.PCShare;
  23. import org.alfresco.jlan.smb.PacketType;
  24. import org.alfresco.jlan.smb.SMBException;
  25. import org.alfresco.jlan.smb.SMBStatus;
  26. import org.alfresco.jlan.util.DataPacker;
  27. /**
  28. * SMB CIFS protocol search context class
  29. *
  30. * @author gkspencer
  31. */
  32. final class CIFSSearchContext extends SearchContext {
  33. // Directory entry offsets
  34. private static final int DirResumeLength = 21;
  35. private static final int DirAttrLength = 1;
  36. private static final int DirDateLength = 2;
  37. private static final int DirTimeLength = 2;
  38. private static final int DirSizeLength = 4;
  39. private static final int DirNameLength = 13;
  40. private static final int DirNameOffset = 30;
  41. private static final int DirInfoLen = 43;
  42. // SMB packet used for the search
  43. private SMBPacket m_pkt = null;
  44. // Current directory entry index
  45. private int m_dirIdx;
  46. /**
  47. * Construct an SMB search context on the specified disk session.
  48. *
  49. * @param sess Disk session that this search is associated with.
  50. */
  51. protected CIFSSearchContext(DiskSession sess) {
  52. super(sess);
  53. }
  54. /**
  55. * Continue the current search, request another packet of directory entries.
  56. *
  57. * @return true if more files were returned, else false
  58. * @exception java.io.IOException If an I/O error occurs.
  59. * @exception SMBException If an SMB level error occurs
  60. */
  61. protected final boolean ContinueSearch()
  62. throws java.io.IOException, SMBException {
  63. // Build the continue search packet using the last resume key from
  64. // the current search packet.
  65. int pos = m_pkt.getByteOffset() + 2;
  66. byte[] buf = m_pkt.getBuffer();
  67. buf[pos++] = (byte) DataType.ASCII;
  68. buf[pos++] = (byte) 0x00;
  69. buf[pos++] = (byte) DataType.VariableBlock;
  70. DataPacker.putIntelShort(DirResumeLength, buf, pos);
  71. pos += 2;
  72. int respos = getDirEntryOffset(getDirEntryCount() - 1);
  73. for (int i = 0; i < DirResumeLength; i++)
  74. buf[pos++] = buf[respos++];
  75. buf[pos++] = (byte) 0x00;
  76. DataPacker.putIntelShort((DirResumeLength + 6), buf, m_pkt.getByteOffset());
  77. // Reset the parameter words, can't do this until the resume key has been
  78. // moved into place.
  79. m_pkt.setParameterCount(2);
  80. m_pkt.setParameter(0, 50);
  81. m_pkt.setParameter(1, getSearchAttributes());
  82. m_pkt.setFlags(getSession().getDefaultFlags());
  83. m_pkt.setFlags2(getSession().getDefaultFlags2());
  84. // Send/receive the search SMB packet
  85. m_pkt.ExchangeSMB(getSession(), m_pkt);
  86. // Check if we received a valid response
  87. if ( m_pkt.isValidResponse() == false) {
  88. // Check if the error is 'no more files'
  89. if ( m_pkt.getErrorClass() == SMBStatus.ErrDos && m_pkt.getErrorCode() == SMBStatus.DOSNoMoreFiles)
  90. return false;
  91. else
  92. throw new java.io.IOException("Continue search failed");
  93. }
  94. // Reset the current directory entry index
  95. m_dirIdx = 0;
  96. return true;
  97. }
  98. /**
  99. * Return the number of directory entries in the SMB search response packet.
  100. *
  101. * @return Number of directory entries in the current SMB search packet.
  102. */
  103. protected final int getDirEntryCount() {
  104. return m_pkt.getParameter(0);
  105. }
  106. /**
  107. * Return the buffer offset of the specified directory entry.
  108. *
  109. * @param idx Directory entry index.
  110. * @return Offset within the SMB packet buffer that the directory entry is stored.
  111. */
  112. protected final int getDirEntryOffset(int idx) {
  113. // Calculate the offset of the directory entry within the SMB packet
  114. int pos = m_pkt.getByteOffset() + 3; // data type + data length
  115. pos += (idx * DirInfoLen);
  116. return pos;
  117. }
  118. /**
  119. * Return the next file in this search as an SMB file information object.
  120. *
  121. * @return SMBFileInfo object, or null if there are no more files.
  122. * @exception java.io.IOException If an I/O error occurs.
  123. * @exception SMBException If an SMB level error occurs
  124. */
  125. public final FileInfo nextFileInfo()
  126. throws java.io.IOException, SMBException {
  127. // Check if the current search packet has been exhausted
  128. if ( m_dirIdx >= getDirEntryCount())
  129. if ( !ContinueSearch())
  130. return null;
  131. // Get the offset of the required directory entry
  132. int pos = getDirEntryOffset(m_dirIdx++) + DirResumeLength;
  133. byte[] buf = m_pkt.getBuffer();
  134. // Extract the various data fields from the directory entry
  135. int attr = (int) buf[pos++];
  136. int wrtime = (int) DataPacker.getIntelShort(buf, pos);
  137. pos += DirTimeLength;
  138. int wrdate = (int) DataPacker.getIntelShort(buf, pos);
  139. pos += DirDateLength;
  140. int fsize = DataPacker.getIntelInt(buf, pos);
  141. pos += DirSizeLength;
  142. int fnamelen = 0;
  143. while (fnamelen < DirNameLength && buf[pos + fnamelen] != 0x00)
  144. fnamelen++;
  145. String fname = new String(buf, pos, fnamelen);
  146. // Create an SMB file information object
  147. return new FileInfo(fname, fsize, attr, wrdate, wrtime);
  148. }
  149. /**
  150. * Return the next file name in this search.
  151. *
  152. * @return Next file name string, or null if there are no more files.
  153. * @exception java.io.IOException If an I/O error occurs.
  154. * @exception SMBException If an SMB level error occurs
  155. */
  156. public final String nextFileName()
  157. throws java.io.IOException, SMBException {
  158. // Check if the current search packet has been exhausted
  159. if ( m_dirIdx >= getDirEntryCount())
  160. if ( !ContinueSearch())
  161. return null;
  162. // Get the offset of the required directory entry
  163. int pos = getDirEntryOffset(m_dirIdx++) + DirNameOffset;
  164. byte[] buf = m_pkt.getBuffer();
  165. // Find the end of the file name string
  166. int fnamelen = 0;
  167. while (fnamelen < DirNameLength && buf[pos + fnamelen] != 0x00)
  168. fnamelen++;
  169. // Return the file name string
  170. return new String(buf, pos, fnamelen);
  171. }
  172. /**
  173. * Start a new search using the default directory name and attributes.
  174. *
  175. * @param fname File name string, may contain wilcards.
  176. * @param attr File attributes bit mask.
  177. * @param level Information level to be returned
  178. * @exception java.io.IOException If an I/O error occurs.
  179. * @exception SMBException If an SMB level error occurs
  180. */
  181. public final void StartSearch(String fname, int attr, int level)
  182. throws java.io.IOException, SMBException {
  183. // Save the search parameters
  184. setSearchParameters(fname, attr, level);
  185. // Check if an SMB packet has been allocated for the directory search
  186. if ( m_pkt == null) {
  187. // Allocate an SMB packet for the search, and initialize it
  188. m_pkt = new SMBPacket();
  189. m_pkt.setCommand(PacketType.Search);
  190. m_pkt.setUserId(getSession().getUserId());
  191. m_pkt.setTreeId(getSession().getTreeId());
  192. }
  193. // Initialize the search SMB packet
  194. m_pkt.setFlags(getSession().getDefaultFlags());
  195. m_pkt.setFlags2(getSession().getDefaultFlags2());
  196. m_pkt.setParameterCount(2);
  197. m_pkt.setParameter(0, 50); // number of directory entries to return
  198. m_pkt.setParameter(1, getSearchAttributes());
  199. // Check if the directory path has a leading '\', if not then the directory
  200. // is relative to the current working directory
  201. String searchPath = getSearchPath();
  202. if ( searchPath.startsWith("\\") == false)
  203. searchPath = PCShare.makePath(getSession().getWorkingDirectory(), getSearchPath());
  204. // Pack the search string
  205. m_pkt.resetBytePointer();
  206. m_pkt.packByte(DataType.ASCII);
  207. m_pkt.packString(searchPath, m_pkt.isUnicode());
  208. // Append a null resume key, to indicate the start of a new search
  209. m_pkt.packByte(DataType.VariableBlock);
  210. m_pkt.packWord(0);
  211. m_pkt.setByteCount();
  212. // Send/receive the search SMB packet
  213. m_pkt.ExchangeSMB(getSession(), m_pkt);
  214. // Check if we received a valid response
  215. if ( m_pkt.isValidResponse() == false)
  216. throw new java.io.IOException("Search failed");
  217. // Reset the current directory entry index
  218. m_dirIdx = 0;
  219. }
  220. }