CoreSearchContext.java 8.4 KB

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