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