KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 import com.knowgate.misc.Gadgets;
25
26 class SmbComNTCreateAndX extends AndXServerMessageBlock {
27
28     // access mask encoding
29
static final int FILE_READ_DATA = 0x00000001; // 1
30
static final int FILE_WRITE_DATA = 0x00000002; // 2
31
static final int FILE_APPEND_DATA = 0x00000004; // 3
32
static final int FILE_READ_EA = 0x00000008; // 4
33
static final int FILE_WRITE_EA = 0x00000010; // 5
34
static final int FILE_EXECUTE = 0x00000020; // 6
35
static final int FILE_DELETE = 0x00000040; // 7
36
static final int FILE_READ_ATTRIBUTES = 0x00000080; // 8
37
static final int FILE_WRITE_ATTRIBUTES = 0x00000100; // 9
38
static final int DELETE = 0x00010000; // 16
39
static final int READ_CONTROL = 0x00020000; // 17
40
static final int WRITE_DAC = 0x00040000; // 18
41
static final int WRITE_OWNER = 0x00080000; // 19
42
static final int SYNCHRONIZE = 0x00100000; // 20
43
static final int GENERIC_ALL = 0x10000000; // 28
44
static final int GENERIC_EXECUTE = 0x20000000; // 29
45
static final int GENERIC_WRITE = 0x40000000; // 30
46
static final int GENERIC_READ = 0x80000000; // 31
47

48     // share access specified in SmbFile
49

50     // create disposition
51

52     /* Creates a new file or supersedes the existing one
53      */

54
55     static final int FILE_SUPERSEDE = 0x0;
56
57     /* Open the file or fail if it does not exist
58      * aka OPEN_EXISTING
59      */

60
61     static final int FILE_OPEN = 0x1;
62
63     /* Create the file or fail if it does not exist
64      * aka CREATE_NEW
65      */

66
67     static final int FILE_CREATE = 0x2;
68
69     /* Open the file or create it if it does not exist
70      * aka OPEN_ALWAYS
71      */

72
73     static final int FILE_OPEN_IF = 0x3;
74
75     /* Open the file and overwrite it's contents or fail if it does not exist
76      * aka TRUNCATE_EXISTING
77      */

78
79     static final int FILE_OVERWRITE = 0x4;
80
81     /* Open the file and overwrite it's contents or create it if it does not exist
82      * aka CREATE_ALWAYS (according to the wire when calling CreateFile)
83      */

84
85     static final int FILE_OVERWRITE_IF = 0x5;
86
87
88     // create options
89
static final int FILE_WRITE_THROUGH = 0x00000002;
90     static final int FILE_SEQUENTIAL_ONLY = 0x00000004;
91     static final int FILE_SYNCHRONOUS_IO_ALERT = 0x00000010;
92     static final int FILE_SYNCHRONOUS_IO_NONALERT = 0x00000020;
93
94     // security flags
95
static final int SECURITY_CONTEXT_TRACKING = 0x01;
96     static final int SECURITY_EFFECTIVE_ONLY = 0x02;
97
98     private int flags,
99         rootDirectoryFid,
100         desiredAccess,
101         extFileAttributes,
102         shareAccess,
103         createDisposition,
104         createOptions,
105         impersonationLevel;
106     private long allocationSize;
107     private byte securityFlags;
108
109     SmbComNTCreateAndX( String JavaDoc name, int flags,
110                 int shareAccess,
111                 int extFileAttributes,
112                 int createOptions,
113                 ServerMessageBlock andx ) {
114         super( andx );
115         this.path = name;
116         command = SMB_COM_NT_CREATE_ANDX;
117
118         // desiredAccess
119
desiredAccess = ( flags >>> 16 ) & 0xFFFF;
120         desiredAccess |= FILE_READ_EA | FILE_READ_ATTRIBUTES;
121
122         // extFileAttributes
123
this.extFileAttributes = extFileAttributes;
124
125         // shareAccess
126
this.shareAccess = shareAccess;
127
128         // createDisposition
129
if(( flags & SmbFile.O_TRUNC ) == SmbFile.O_TRUNC ) {
130             // truncate the file
131
if(( flags & SmbFile.O_CREAT ) == SmbFile.O_CREAT ) {
132                 // create it if necessary
133
createDisposition = FILE_OVERWRITE_IF;
134             } else {
135                 createDisposition = FILE_OVERWRITE;
136             }
137         } else {
138             // don't truncate the file
139
if(( flags & SmbFile.O_CREAT ) == SmbFile.O_CREAT ) {
140                 // create it if necessary
141
if ((flags & SmbFile.O_EXCL ) == SmbFile.O_EXCL ) {
142                     // fail if already exists
143
createDisposition = FILE_CREATE;
144                 } else {
145                     createDisposition = FILE_OPEN_IF;
146                 }
147             } else {
148                 createDisposition = FILE_OPEN;
149             }
150         }
151
152         if(( createOptions & 0x01 ) == 0 ) {
153             this.createOptions = createOptions | 0x0040;
154         } else {
155             this.createOptions = createOptions;
156         }
157         impersonationLevel = 0x02; // As seen on NT :~)
158
securityFlags = (byte)0x03; // SECURITY_CONTEXT_TRACKING | SECURITY_EFFECTIVE_ONLY
159
}
160
161     int writeParameterWordsWireFormat( byte[] dst, int dstIndex ) {
162         int start = dstIndex;
163
164         dst[dstIndex++] = (byte)0x00;
165         // name length without counting null termination
166
writeInt2( ( useUnicode ? path.length() * 2 : path.length() ), dst, dstIndex );
167         dstIndex += 2;
168         writeInt4( flags, dst, dstIndex );
169         dstIndex += 4;
170         writeInt4( rootDirectoryFid, dst, dstIndex );
171         dstIndex += 4;
172         writeInt4( desiredAccess, dst, dstIndex );
173         dstIndex += 4;
174         writeInt8( allocationSize, dst, dstIndex );
175         dstIndex += 8;
176         writeInt4( extFileAttributes, dst, dstIndex );
177         dstIndex += 4;
178         writeInt4( shareAccess, dst, dstIndex );
179         dstIndex += 4;
180         writeInt4( createDisposition, dst, dstIndex );
181         dstIndex += 4;
182         writeInt4( createOptions, dst, dstIndex );
183         dstIndex += 4;
184         writeInt4( impersonationLevel, dst, dstIndex );
185         dstIndex += 4;
186         dst[dstIndex++] = securityFlags;
187
188         return dstIndex - start;
189     }
190     int writeBytesWireFormat( byte[] dst, int dstIndex ) {
191         return writeString( path, dst, dstIndex );
192     }
193     int readParameterWordsWireFormat( byte[] buffer, int bufferIndex ) {
194         return 0;
195     }
196     int readBytesWireFormat( byte[] buffer, int bufferIndex ) {
197         return 0;
198     }
199     int readBytesDirectWireFormat( InputStream JavaDoc in, int byteCount,
200                 byte[] buffer, int bufferIndex ) throws IOException JavaDoc {
201         return 0;
202     }
203     public String JavaDoc toString() {
204         return new String JavaDoc( "SmbComNTCreateAndX[" +
205             super.toString() +
206             ",flags=0x" + Gadgets.toHexString( flags, 2 ) +
207             ",rootDirectoryFid=" + rootDirectoryFid +
208             ",desiredAccess=0x" + Gadgets.toHexString( desiredAccess, 4 ) +
209             ",allocationSize=" + allocationSize +
210             ",extFileAttributes=0x" + Gadgets.toHexString( extFileAttributes, 4 ) +
211             ",shareAccess=0x" + Gadgets.toHexString( shareAccess, 4 ) +
212             ",createDisposition=0x" + Gadgets.toHexString( createDisposition, 4 ) +
213             ",createOptions=0x" + Gadgets.toHexString( createOptions, 8 ) +
214             ",impersonationLevel=0x" + Gadgets.toHexString( impersonationLevel, 4 ) +
215             ",securityFlags=0x" + Gadgets.toHexString( securityFlags, 2 ) +
216             ",name=" + path + "]" );
217     }
218 }
219
Popular Tags