KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > DiskInfoPacker


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb.server;
18
19 import org.alfresco.filesys.server.filesys.DiskInfo;
20 import org.alfresco.filesys.server.filesys.SrvDiskInfo;
21 import org.alfresco.filesys.server.filesys.VolumeInfo;
22 import org.alfresco.filesys.smb.NTTime;
23 import org.alfresco.filesys.util.DataBuffer;
24 import org.alfresco.filesys.util.DataPacker;
25
26 /**
27  * Disk information packer class.
28  */

29 class DiskInfoPacker
30 {
31
32     // Disk information levels
33

34     public static final int InfoStandard = 1;
35     public static final int InfoVolume = 2;
36     public static final int InfoFsVolume = 0x102;
37     public static final int InfoFsSize = 0x103;
38     public static final int InfoFsDevice = 0x104;
39     public static final int InfoFsAttribute = 0x105;
40     public static final int InfoCifsUnix = 0x200;
41     public static final int InfoMacFsInfo = 0x301;
42     public static final int InfoFullFsSize = 0x3EF;
43
44     // Mac support flags
45

46     public static final int MacAccessControl = 0x0010;
47     public static final int MacGetSetComments = 0x0020;
48     public static final int MacDesktopDbCalls = 0x0040;
49     public static final int MacUniqueIds = 0x0080;
50     public static final int MacNoStreamsOrMacSupport = 0x0100;
51
52     /**
53      * Class constructor.
54      */

55     public DiskInfoPacker()
56     {
57         super();
58     }
59
60     /**
61      * Pack the standard disk information, InfoStandard.
62      *
63      * @param info SMBDiskInfo to be packed.
64      * @param buf Buffer to pack the data into.
65      */

66     public final static void packStandardInfo(DiskInfo info, DataBuffer buf)
67     {
68
69         // Information format :-
70
// ULONG File system identifier, always 0 ?
71
// ULONG Sectors per allocation unit.
72
// ULONG Total allocation units.
73
// ULONG Total available allocation units.
74
// USHORT Number of bytes per sector.
75

76         // Pack the file system identifier, 0 = NT file system
77

78         buf.putZeros(4);
79         // buf.putInt(999);
80

81         // Pack the disk unit information
82

83         buf.putInt(info.getBlocksPerAllocationUnit());
84         buf.putInt((int) info.getTotalUnits());
85         buf.putInt((int) info.getFreeUnits());
86         buf.putShort(info.getBlockSize());
87     }
88
89     /**
90      * Pack the volume label information, InfoVolume.
91      *
92      * @param info Volume information
93      * @param buf Buffer to pack data into.
94      * @param uni Use Unicode strings if true, else use ASCII strings
95      */

96     public final static void packVolumeInfo(VolumeInfo info, DataBuffer buf, boolean uni)
97     {
98
99         // Information format :-
100
// ULONG Volume serial number
101
// UCHAR Volume label length
102
// STRING Volume label
103

104         // Pack the volume serial number
105

106         buf.putInt(info.getSerialNumber());
107
108         // Pack the volume label length and string
109

110         buf.putByte(info.getVolumeLabel().length());
111         buf.putString(info.getVolumeLabel(), uni);
112     }
113
114     /**
115      * Pack the filesystem size information, InfoFsSize
116      *
117      * @param info Disk size information
118      * @param buf Buffer to pack data into.
119      */

120     public final static void packFsSizeInformation(SrvDiskInfo info, DataBuffer buf)
121     {
122
123         // Information format :-
124
// ULONG Disk size (in units)
125
// ULONG Free size (in units)
126
// UINT Unit size in blocks
127
// UINT Block size in bytes
128

129         buf.putLong(info.getTotalUnits());
130         buf.putLong(info.getFreeUnits());
131         buf.putInt(info.getBlocksPerAllocationUnit());
132         buf.putInt(info.getBlockSize());
133     }
134
135     /**
136      * Pack the filesystem volume information, InfoFsVolume
137      *
138      * @param info Volume information
139      * @param buf Buffer to pack data into.
140      * @param uni Use Unicode strings if true, else use ASCII strings
141      */

142     public final static void packFsVolumeInformation(VolumeInfo info, DataBuffer buf, boolean uni)
143     {
144
145         // Information format :-
146
// ULONG Volume creation date/time (NT 64bit time fomat)
147
// UINT Volume serial number
148
// UINT Volume label length
149
// SHORT Reserved
150
// STRING Volume label (no null)
151

152         if (info.hasCreationDateTime())
153             buf.putLong(NTTime.toNTTime(info.getCreationDateTime()));
154         else
155             buf.putZeros(8);
156
157         if (info.hasSerialNumber())
158             buf.putInt(info.getSerialNumber());
159         else
160             buf.putZeros(4);
161
162         int len = info.getVolumeLabel().length();
163         if (uni)
164             len *= 2;
165         buf.putInt(len);
166
167         buf.putZeros(2); // reserved
168
buf.putString(info.getVolumeLabel(), uni, false);
169     }
170
171     /**
172      * Pack the filesystem device information, InfoFsDevice
173      *
174      * @param typ Device type
175      * @param devChar Device characteristics
176      * @param buf Buffer to pack data into.
177      */

178     public final static void packFsDevice(int typ, int devChar, DataBuffer buf)
179     {
180
181         // Information format :-
182
// UINT Device type
183
// UINT Characteristics
184

185         buf.putInt(typ);
186         buf.putInt(devChar);
187     }
188
189     /**
190      * Pack the filesystem attribute information, InfoFsAttribute
191      *
192      * @param attr Attribute flags
193      * @param maxName Maximum file name component length
194      * @param fsType File system type name
195      * @param uni Unicode strings required
196      * @param buf Buffer to pack data into.
197      */

198     public final static void packFsAttribute(int attr, int maxName, String JavaDoc fsType, boolean uni, DataBuffer buf)
199     {
200
201         // Information format :-
202
// UINT Attribute flags
203
// UINT Maximum filename component length (usually 255)
204
// UINT Filesystem type length
205
// STRING Filesystem type string
206

207         buf.putInt(attr);
208         buf.putInt(maxName);
209
210         if (uni)
211             buf.putInt(fsType.length() * 2);
212         else
213             buf.putInt(fsType.length());
214         buf.putString(fsType, uni, false);
215     }
216
217     /**
218      * Pack the Mac filesystem information, InfoMacFsInfo
219      *
220      * @param diskInfo SMBDiskInfo to be packed.
221      * @param volInfo Volume information to be packed
222      * @param ntfs Filesystem supports NTFS streams
223      * @param buf Buffer to pack the data into.
224      */

225     public final static void packMacFsInformation(DiskInfo diskInfo, VolumeInfo volInfo, boolean ntfs, DataBuffer buf)
226     {
227
228         // Information format :-
229
// LARGE_INTEGER Volume creation time (NT format)
230
// LARGE_INTEGER Volume modify time (NT format)
231
// LARGE_INTEGER Volume backup time (NT format)
232
// ULONG Allocation blocks
233
// ULONG Allocation block size (multiple of 512)
234
// ULONG Free blocks on the volume
235
// UCHAR[32] Finder info
236
// LONG Number of files in root directory (zero if unknown)
237
// LONG Number of directories in the root directory (zero if unknown)
238
// LONG Number of files on the volume (zero if unknown)
239
// LONG Number of directories on the volume (zero if unknown)
240
// LONG Mac support flags (big endian)
241

242         // Pack the volume creation time
243

244         if (volInfo.hasCreationDateTime())
245         {
246             long ntTime = NTTime.toNTTime(volInfo.getCreationDateTime());
247             buf.putLong(ntTime);
248             buf.putLong(ntTime);
249             buf.putLong(ntTime);
250         }
251         else
252             buf.putZeros(24);
253
254         // Pack the number of allocation blocks, block size and free block count
255

256         buf.putInt((int) diskInfo.getTotalUnits());
257         buf.putInt(diskInfo.getBlockSize() * diskInfo.getBlocksPerAllocationUnit());
258         buf.putInt((int) diskInfo.getFreeUnits());
259
260         // Pack the finder information area
261

262         buf.putZeros(32);
263
264         // Pack the file/directory counts
265

266         buf.putInt(0);
267         buf.putInt(0);
268         buf.putInt(0);
269         buf.putInt(0);
270
271         // Pack the Mac support flags
272

273         DataPacker.putIntelInt(ntfs ? 0 : MacNoStreamsOrMacSupport, buf.getBuffer(), buf.getPosition());
274         buf.setPosition(buf.getPosition() + 4);
275     }
276
277     /**
278      * Pack the filesystem size information, InfoFsSize
279      *
280      * @param userLimit User free units
281      * @param info Disk size information
282      * @param buf Buffer to pack data into.
283      */

284     public final static void packFullFsSizeInformation(long userLimit, SrvDiskInfo info, DataBuffer buf)
285     {
286
287         // Information format :-
288
// ULONG Disk size (in units)
289
// ULONG User free size (in units)
290
// ULONG Free size (in units)
291
// UINT Unit size in blocks
292
// UINT Block size in bytes
293

294         buf.putLong(info.getTotalUnits());
295         buf.putLong(userLimit);
296         buf.putLong(info.getFreeUnits());
297         buf.putInt(info.getBlocksPerAllocationUnit());
298         buf.putInt(info.getBlockSize());
299     }
300 }
Popular Tags