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.util.Date;
022: import java.io.IOException;
023: import java.io.InputStream;
024:
025: import com.knowgate.jcifs.Config;
026: import com.knowgate.misc.Gadgets;
027:
028: class SmbComOpenAndX extends AndXServerMessageBlock {
029:
030: // flags (not the same as flags constructor argument)
031: private static final int FLAGS_RETURN_ADDITIONAL_INFO = 0x01;
032: private static final int FLAGS_REQUEST_OPLOCK = 0x02;
033: private static final int FLAGS_REQUEST_BATCH_OPLOCK = 0x04;
034:
035: // Access Mode Encoding for desiredAccess
036: private static final int SHARING_COMPATIBILITY = 0x00;
037: private static final int SHARING_DENY_READ_WRITE_EXECUTE = 0x10;
038: private static final int SHARING_DENY_WRITE = 0x20;
039: private static final int SHARING_DENY_READ_EXECUTE = 0x30;
040: private static final int SHARING_DENY_NONE = 0x40;
041:
042: private static final int DO_NOT_CACHE = 0x1000; // bit 12
043: private static final int WRITE_THROUGH = 0x4000; // bit 14
044:
045: private static final int OPEN_FN_CREATE = 0x10;
046: private static final int OPEN_FN_FAIL_IF_EXISTS = 0x00;
047: private static final int OPEN_FN_OPEN = 0x01;
048: private static final int OPEN_FN_TRUNC = 0x02;
049:
050: private static final int BATCH_LIMIT = Config.getInt(
051: "jcifs.smb.client.OpenAndX.ReadAndX", 1);
052:
053: int flags, desiredAccess, searchAttributes, fileAttributes,
054: creationTime, openFunction, allocationSize;
055:
056: // flags is NOT the same as flags member
057:
058: SmbComOpenAndX(String fileName, int flags, ServerMessageBlock andx) {
059: super (andx);
060: this .path = fileName;
061: command = SMB_COM_OPEN_ANDX;
062:
063: // desiredAccess
064: desiredAccess = (flags >>> 16) & 0x3;
065: if (desiredAccess == 0x3) {
066: desiredAccess = 0x2; /* Mmm, I thought 0x03 was RDWR */
067: }
068: desiredAccess |= SHARING_DENY_NONE;
069: desiredAccess &= ~0x1; // Win98 doesn't like GENERIC_READ ?! -- get Access Denied.
070:
071: // searchAttributes
072: searchAttributes = ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM;
073:
074: // fileAttributes
075: fileAttributes = 0;
076:
077: // openFunction
078: if ((flags & SmbFile.O_TRUNC) == SmbFile.O_TRUNC) {
079: // truncate the file
080: if ((flags & SmbFile.O_CREAT) == SmbFile.O_CREAT) {
081: // create it if necessary
082: openFunction = OPEN_FN_TRUNC | OPEN_FN_CREATE;
083: } else {
084: openFunction = OPEN_FN_TRUNC;
085: }
086: } else {
087: // don't truncate the file
088: if ((flags & SmbFile.O_CREAT) == SmbFile.O_CREAT) {
089: // create it if necessary
090: if ((flags & SmbFile.O_EXCL) == SmbFile.O_EXCL) {
091: // fail if already exists
092: openFunction = OPEN_FN_CREATE
093: | OPEN_FN_FAIL_IF_EXISTS;
094: } else {
095: openFunction = OPEN_FN_CREATE | OPEN_FN_OPEN;
096: }
097: } else {
098: openFunction = OPEN_FN_OPEN;
099: }
100: }
101: }
102:
103: int getBatchLimit(byte command) {
104: return command == SMB_COM_READ_ANDX ? BATCH_LIMIT : 0;
105: }
106:
107: int writeParameterWordsWireFormat(byte[] dst, int dstIndex) {
108: int start = dstIndex;
109:
110: writeInt2(flags, dst, dstIndex);
111: dstIndex += 2;
112: writeInt2(desiredAccess, dst, dstIndex);
113: dstIndex += 2;
114: writeInt2(searchAttributes, dst, dstIndex);
115: dstIndex += 2;
116: writeInt2(fileAttributes, dst, dstIndex);
117: dstIndex += 2;
118: creationTime = 0;
119: writeInt4(creationTime, dst, dstIndex);
120: dstIndex += 4;
121: writeInt2(openFunction, dst, dstIndex);
122: dstIndex += 2;
123: writeInt4(allocationSize, dst, dstIndex);
124: dstIndex += 4;
125: for (int i = 0; i < 8; i++) {
126: dst[dstIndex++] = 0x00;
127: }
128:
129: return dstIndex - start;
130: }
131:
132: int writeBytesWireFormat(byte[] dst, int dstIndex) {
133: int start = dstIndex;
134:
135: if (useUnicode) {
136: dst[dstIndex++] = (byte) '\0';
137: }
138: dstIndex += writeString(path, dst, dstIndex);
139:
140: return dstIndex - start;
141: }
142:
143: int readParameterWordsWireFormat(byte[] buffer, int bufferIndex) {
144: return 0;
145: }
146:
147: int readBytesWireFormat(byte[] buffer, int bufferIndex) {
148: return 0;
149: }
150:
151: int readBytesDirectWireFormat(InputStream in, int byteCount,
152: byte[] buffer, int bufferIndex) throws IOException {
153: return 0;
154: }
155:
156: public String toString() {
157: return new String("SmbComOpenAndX[" + super .toString()
158: + ",flags=0x" + Gadgets.toHexString(flags, 2)
159: + ",desiredAccess=0x"
160: + Gadgets.toHexString(desiredAccess, 4)
161: + ",searchAttributes=0x"
162: + Gadgets.toHexString(searchAttributes, 4)
163: + ",fileAttributes=0x"
164: + Gadgets.toHexString(fileAttributes, 4)
165: + ",creationTime=" + new Date(creationTime)
166: + ",openFunction=0x"
167: + Gadgets.toHexString(openFunction, 2)
168: + ",allocationSize=" + allocationSize + ",fileName="
169: + path + "]");
170: }
171: }
|