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