001: /* jcifs smb client library in Java
002: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package jcifs.smb;
020:
021: import java.io.UnsupportedEncodingException;
022: import java.util.Date;
023:
024: class Trans2FindFirst2Response extends SmbComTransactionResponse {
025:
026: // information levels
027:
028: static final int SMB_INFO_STANDARD = 1;
029: static final int SMB_INFO_QUERY_EA_SIZE = 2;
030: static final int SMB_INFO_QUERY_EAS_FROM_LIST = 3;
031: static final int SMB_FIND_FILE_DIRECTORY_INFO = 0x101;
032: static final int SMB_FIND_FILE_FULL_DIRECTORY_INFO = 0x102;
033: static final int SMB_FILE_NAMES_INFO = 0x103;
034: static final int SMB_FILE_BOTH_DIRECTORY_INFO = 0x104;
035:
036: class SmbFindFileBothDirectoryInfo implements FileEntry {
037: int nextEntryOffset;
038: int fileIndex;
039: long creationTime;
040: long lastAccessTime;
041: long lastWriteTime;
042: long changeTime;
043: long endOfFile;
044: long allocationSize;
045: int extFileAttributes;
046: int fileNameLength;
047: int eaSize;
048: int shortNameLength;
049: String shortName;
050: String filename;
051:
052: public String getName() {
053: return filename;
054: }
055:
056: public int getType() {
057: return SmbFile.TYPE_FILESYSTEM;
058: }
059:
060: public int getAttributes() {
061: return extFileAttributes;
062: }
063:
064: public long createTime() {
065: return creationTime;
066: }
067:
068: public long lastModified() {
069: return lastWriteTime;
070: }
071:
072: public long length() {
073: return endOfFile;
074: }
075:
076: public String toString() {
077: return new String("SmbFindFileBothDirectoryInfo["
078: + "nextEntryOffset="
079: + nextEntryOffset
080: + ",fileIndex="
081: + fileIndex
082: + ",creationTime="
083: + new Date(creationTime)
084: + ",lastAccessTime="
085: + new Date(lastAccessTime)
086: + ",lastWriteTime="
087: + new Date(lastWriteTime)
088: + ",changeTime="
089: + new Date(changeTime)
090: + ",endOfFile="
091: + endOfFile
092: + ",allocationSize="
093: + allocationSize
094: + ",extFileAttributes="
095: + extFileAttributes
096: + ",fileNameLength="
097: + fileNameLength
098: + ",eaSize="
099: + eaSize
100: + ",shortNameLength="
101: + shortNameLength
102: + ",shortName="
103: + shortName
104: + ",filename="
105: + filename + "]");
106: }
107: }
108:
109: int sid;
110: boolean isEndOfSearch;
111: int eaErrorOffset;
112: int lastNameOffset, lastNameBufferIndex;
113: String lastName;
114: int resumeKey;
115:
116: Trans2FindFirst2Response() {
117: command = SMB_COM_TRANSACTION2;
118: subCommand = SmbComTransaction.TRANS2_FIND_FIRST2;
119: }
120:
121: String readString(byte[] src, int srcIndex, int len) {
122: String str = null;
123: try {
124: if (useUnicode) {
125: // should Unicode alignment be corrected for here?
126: str = new String(src, srcIndex, len, "UnicodeLittle");
127: } else {
128:
129: /* On NT without Unicode the fileNameLength
130: * includes the '\0' whereas on win98 it doesn't. I
131: * guess most clients only support non-unicode so
132: * they don't run into this.
133: */
134:
135: /* UPDATE: Maybe not! Could this be a Unicode alignment issue. I hope
136: * so. We cannot just comment out this method and use readString of
137: * ServerMessageBlock.java because the arguments are different, however
138: * one might be able to reduce this.
139: */
140:
141: if (len > 0 && src[srcIndex + len - 1] == '\0') {
142: len--;
143: }
144: str = new String(src, srcIndex, len,
145: ServerMessageBlock.OEM_ENCODING);
146: }
147: } catch (UnsupportedEncodingException uee) {
148: if (log.level > 1)
149: uee.printStackTrace(log);
150: }
151: return str;
152: }
153:
154: int writeSetupWireFormat(byte[] dst, int dstIndex) {
155: return 0;
156: }
157:
158: int writeParametersWireFormat(byte[] dst, int dstIndex) {
159: return 0;
160: }
161:
162: int writeDataWireFormat(byte[] dst, int dstIndex) {
163: return 0;
164: }
165:
166: int readSetupWireFormat(byte[] buffer, int bufferIndex, int len) {
167: return 0;
168: }
169:
170: int readParametersWireFormat(byte[] buffer, int bufferIndex, int len) {
171: int start = bufferIndex;
172:
173: if (subCommand == SmbComTransaction.TRANS2_FIND_FIRST2) {
174: sid = readInt2(buffer, bufferIndex);
175: bufferIndex += 2;
176: }
177: numEntries = readInt2(buffer, bufferIndex);
178: bufferIndex += 2;
179: isEndOfSearch = (buffer[bufferIndex] & 0x01) == 0x01 ? true
180: : false;
181: bufferIndex += 2;
182: eaErrorOffset = readInt2(buffer, bufferIndex);
183: bufferIndex += 2;
184: lastNameOffset = readInt2(buffer, bufferIndex);
185: bufferIndex += 2;
186:
187: return bufferIndex - start;
188: }
189:
190: int readDataWireFormat(byte[] buffer, int bufferIndex, int len) {
191: int start = bufferIndex;
192: SmbFindFileBothDirectoryInfo e;
193:
194: lastNameBufferIndex = bufferIndex + lastNameOffset;
195:
196: results = new SmbFindFileBothDirectoryInfo[numEntries];
197: for (int i = 0; i < numEntries; i++) {
198: results[i] = e = new SmbFindFileBothDirectoryInfo();
199:
200: e.nextEntryOffset = readInt4(buffer, bufferIndex);
201: e.fileIndex = readInt4(buffer, bufferIndex + 4);
202: e.creationTime = readTime(buffer, bufferIndex + 8);
203: // e.lastAccessTime = readTime( buffer, bufferIndex + 16 );
204: e.lastWriteTime = readTime(buffer, bufferIndex + 24);
205: // e.changeTime = readTime( buffer, bufferIndex + 32 );
206: e.endOfFile = readInt8(buffer, bufferIndex + 40);
207: // e.allocationSize = readInt8( buffer, bufferIndex + 48 );
208: e.extFileAttributes = readInt4(buffer, bufferIndex + 56);
209: e.fileNameLength = readInt4(buffer, bufferIndex + 60);
210: // e.eaSize = readInt4( buffer, bufferIndex + 64 );
211: // e.shortNameLength = buffer[bufferIndex + 68] & 0xFF;
212:
213: /* With NT, the shortName is in Unicode regardless of what is negotiated.
214: */
215:
216: // e.shortName = readString( buffer, bufferIndex + 70, e.shortNameLength );
217: e.filename = readString(buffer, bufferIndex + 94,
218: e.fileNameLength);
219:
220: /* lastNameOffset ends up pointing to either to
221: * the exact location of the filename(e.g. Win98)
222: * or to the start of the entry containing the
223: * filename(e.g. NT). Ahhrg! In either case the
224: * lastNameOffset falls between the start of the
225: * entry and the next entry.
226: */
227:
228: if (lastNameBufferIndex >= bufferIndex
229: && (e.nextEntryOffset == 0 || lastNameBufferIndex < (bufferIndex + e.nextEntryOffset))) {
230: lastName = e.filename;
231: resumeKey = e.fileIndex;
232: }
233:
234: bufferIndex += e.nextEntryOffset;
235: }
236:
237: /* last nextEntryOffset for NT 4(but not 98) is 0 so we must
238: * use dataCount or our accounting will report an error for NT :~(
239: */
240:
241: //return bufferIndex - start;
242: return dataCount;
243: }
244:
245: public String toString() {
246: String c;
247: if (subCommand == SmbComTransaction.TRANS2_FIND_FIRST2) {
248: c = "Trans2FindFirst2Response[";
249: } else {
250: c = "Trans2FindNext2Response[";
251: }
252: return new String(c + super .toString() + ",sid=" + sid
253: + ",searchCount=" + numEntries + ",isEndOfSearch="
254: + isEndOfSearch + ",eaErrorOffset=" + eaErrorOffset
255: + ",lastNameOffset=" + lastNameOffset + ",lastName="
256: + lastName + "]");
257: }
258: }
|