KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > jcifs > smb > SmbComOpenAndX


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package com.knowgate.jcifs.smb;
20
21 import java.util.Date JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 import com.knowgate.jcifs.Config;
26 import com.knowgate.misc.Gadgets;
27
28 class SmbComOpenAndX extends AndXServerMessageBlock {
29
30     // flags (not the same as flags constructor argument)
31
private static final int FLAGS_RETURN_ADDITIONAL_INFO = 0x01;
32     private static final int FLAGS_REQUEST_OPLOCK = 0x02;
33     private static final int FLAGS_REQUEST_BATCH_OPLOCK = 0x04;
34
35     // Access Mode Encoding for desiredAccess
36
private static final int SHARING_COMPATIBILITY = 0x00;
37     private static final int SHARING_DENY_READ_WRITE_EXECUTE = 0x10;
38     private static final int SHARING_DENY_WRITE = 0x20;
39     private static final int SHARING_DENY_READ_EXECUTE = 0x30;
40     private static final int SHARING_DENY_NONE = 0x40;
41
42     private static final int DO_NOT_CACHE = 0x1000; // bit 12
43
private static final int WRITE_THROUGH = 0x4000; // bit 14
44

45     private static final int OPEN_FN_CREATE = 0x10;
46     private static final int OPEN_FN_FAIL_IF_EXISTS = 0x00;
47     private static final int OPEN_FN_OPEN = 0x01;
48     private static final int OPEN_FN_TRUNC = 0x02;
49
50     private static final int BATCH_LIMIT = Config.getInt( "jcifs.smb.client.OpenAndX.ReadAndX", 1 );
51
52     int flags,
53         desiredAccess,
54         searchAttributes,
55         fileAttributes,
56         creationTime,
57         openFunction,
58         allocationSize;
59
60     // flags is NOT the same as flags member
61

62     SmbComOpenAndX( String JavaDoc fileName, int flags, ServerMessageBlock andx ) {
63         super( andx );
64         this.path = fileName;
65         command = SMB_COM_OPEN_ANDX;
66
67         // desiredAccess
68
desiredAccess = ( flags >>> 16 ) & 0x3;
69         if( desiredAccess == 0x3 ) {
70             desiredAccess = 0x2; /* Mmm, I thought 0x03 was RDWR */
71         }
72         desiredAccess |= SHARING_DENY_NONE;
73         desiredAccess &= ~0x1; // Win98 doesn't like GENERIC_READ ?! -- get Access Denied.
74

75         // searchAttributes
76
searchAttributes = ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM;
77
78         // fileAttributes
79
fileAttributes = 0;
80
81         // openFunction
82
if(( flags & SmbFile.O_TRUNC ) == SmbFile.O_TRUNC ) {
83             // truncate the file
84
if(( flags & SmbFile.O_CREAT ) == SmbFile.O_CREAT ) {
85                 // create it if necessary
86
openFunction = OPEN_FN_TRUNC | OPEN_FN_CREATE;
87             } else {
88                 openFunction = OPEN_FN_TRUNC;
89             }
90         } else {
91             // don't truncate the file
92
if(( flags & SmbFile.O_CREAT ) == SmbFile.O_CREAT ) {
93                 // create it if necessary
94
if(( flags & SmbFile.O_EXCL ) == SmbFile.O_EXCL ) {
95                     // fail if already exists
96
openFunction = OPEN_FN_CREATE | OPEN_FN_FAIL_IF_EXISTS;
97                 } else {
98                     openFunction = OPEN_FN_CREATE | OPEN_FN_OPEN;
99                 }
100             } else {
101                 openFunction = OPEN_FN_OPEN;
102             }
103         }
104     }
105
106     int getBatchLimit( byte command ) {
107         return command == SMB_COM_READ_ANDX ? BATCH_LIMIT : 0;
108     }
109     int writeParameterWordsWireFormat( byte[] dst, int dstIndex ) {
110         int start = dstIndex;
111
112         writeInt2( flags, dst, dstIndex );
113         dstIndex += 2;
114         writeInt2( desiredAccess, dst, dstIndex );
115         dstIndex += 2;
116         writeInt2( searchAttributes, dst, dstIndex );
117         dstIndex += 2;
118         writeInt2( fileAttributes, dst, dstIndex );
119         dstIndex += 2;
120         creationTime = 0;
121         writeInt4( creationTime, dst, dstIndex );
122         dstIndex += 4;
123         writeInt2( openFunction, dst, dstIndex );
124         dstIndex += 2;
125         writeInt4( allocationSize, dst, dstIndex );
126         dstIndex += 4;
127         for( int i = 0; i < 8; i++ ) {
128             dst[dstIndex++] = 0x00;
129         }
130
131         return dstIndex - start;
132     }
133     int writeBytesWireFormat( byte[] dst, int dstIndex ) {
134         int start = dstIndex;
135
136         if( useUnicode ) {
137             dst[dstIndex++] = (byte)'\0';
138         }
139         dstIndex += writeString( path, dst, dstIndex );
140
141         return dstIndex - start;
142     }
143     int readParameterWordsWireFormat( byte[] buffer, int bufferIndex ) {
144         return 0;
145     }
146     int readBytesWireFormat( byte[] buffer, int bufferIndex ) {
147         return 0;
148     }
149     int readBytesDirectWireFormat( InputStream JavaDoc in, int byteCount,
150                 byte[] buffer, int bufferIndex ) throws IOException JavaDoc {
151         return 0;
152     }
153     public String JavaDoc toString() {
154         return new String JavaDoc( "SmbComOpenAndX[" +
155             super.toString() +
156             ",flags=0x" + Gadgets.toHexString( flags, 2 ) +
157             ",desiredAccess=0x" + Gadgets.toHexString( desiredAccess, 4 ) +
158             ",searchAttributes=0x" + Gadgets.toHexString( searchAttributes, 4 ) +
159             ",fileAttributes=0x" + Gadgets.toHexString( fileAttributes, 4 ) +
160             ",creationTime=" + new Date JavaDoc( creationTime ) +
161             ",openFunction=0x" + Gadgets.toHexString( openFunction, 2 ) +
162             ",allocationSize=" + allocationSize +
163             ",fileName=" + path + "]" );
164     }
165 }
166
Popular Tags