KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 jcifs.smb;
20
21 import java.util.Date JavaDoc;
22 import jcifs.Config;
23 import jcifs.util.Hexdump;
24
25 class SmbComOpenAndX extends AndXServerMessageBlock {
26
27     // flags (not the same as flags constructor argument)
28
private static final int FLAGS_RETURN_ADDITIONAL_INFO = 0x01;
29     private static final int FLAGS_REQUEST_OPLOCK = 0x02;
30     private static final int FLAGS_REQUEST_BATCH_OPLOCK = 0x04;
31
32     // Access Mode Encoding for desiredAccess
33
private static final int SHARING_COMPATIBILITY = 0x00;
34     private static final int SHARING_DENY_READ_WRITE_EXECUTE = 0x10;
35     private static final int SHARING_DENY_WRITE = 0x20;
36     private static final int SHARING_DENY_READ_EXECUTE = 0x30;
37     private static final int SHARING_DENY_NONE = 0x40;
38
39     private static final int DO_NOT_CACHE = 0x1000; // bit 12
40
private static final int WRITE_THROUGH = 0x4000; // bit 14
41

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

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

72         // searchAttributes
73
searchAttributes = ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM;
74
75         // fileAttributes
76
fileAttributes = 0;
77
78         // openFunction
79
if(( flags & SmbFile.O_TRUNC ) == SmbFile.O_TRUNC ) {
80             // truncate the file
81
if(( flags & SmbFile.O_CREAT ) == SmbFile.O_CREAT ) {
82                 // create it if necessary
83
openFunction = OPEN_FN_TRUNC | OPEN_FN_CREATE;
84             } else {
85                 openFunction = OPEN_FN_TRUNC;
86             }
87         } else {
88             // don't truncate the file
89
if(( flags & SmbFile.O_CREAT ) == SmbFile.O_CREAT ) {
90                 // create it if necessary
91
if(( flags & SmbFile.O_EXCL ) == SmbFile.O_EXCL ) {
92                     // fail if already exists
93
openFunction = OPEN_FN_CREATE | OPEN_FN_FAIL_IF_EXISTS;
94                 } else {
95                     openFunction = OPEN_FN_CREATE | OPEN_FN_OPEN;
96                 }
97             } else {
98                 openFunction = OPEN_FN_OPEN;
99             }
100         }
101     }
102
103     int getBatchLimit( byte command ) {
104         return command == SMB_COM_READ_ANDX ? BATCH_LIMIT : 0;
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     int writeBytesWireFormat( byte[] dst, int dstIndex ) {
131         int start = dstIndex;
132
133         if( useUnicode ) {
134             dst[dstIndex++] = (byte)'\0';
135         }
136         dstIndex += writeString( path, dst, dstIndex );
137
138         return dstIndex - start;
139     }
140     int readParameterWordsWireFormat( byte[] buffer, int bufferIndex ) {
141         return 0;
142     }
143     int readBytesWireFormat( byte[] buffer, int bufferIndex ) {
144         return 0;
145     }
146     public String JavaDoc toString() {
147         return new String JavaDoc( "SmbComOpenAndX[" +
148             super.toString() +
149             ",flags=0x" + Hexdump.toHexString( flags, 2 ) +
150             ",desiredAccess=0x" + Hexdump.toHexString( desiredAccess, 4 ) +
151             ",searchAttributes=0x" + Hexdump.toHexString( searchAttributes, 4 ) +
152             ",fileAttributes=0x" + Hexdump.toHexString( fileAttributes, 4 ) +
153             ",creationTime=" + new Date JavaDoc( creationTime ) +
154             ",openFunction=0x" + Hexdump.toHexString( openFunction, 2 ) +
155             ",allocationSize=" + allocationSize +
156             ",fileName=" + path + "]" );
157     }
158 }
159
Popular Tags