KickJava   Java API By Example, From Geeks To Geeks.

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

45     // share access specified in SmbFile
46

47     // create disposition
48

49     /* Creates a new file or supersedes the existing one
50      */

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

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

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

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

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

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