Source code: com/flexstor/common/io/NativeFileProperties.java
1 /*
2 * NativeFileProperties.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:30 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.io;
12
13 import java.util.Date;
14
15 import com.flexstor.common.errorlogger.FlexError;
16 import com.flexstor.common.settings.Settings;
17 import com.flexstor.common.util.Diagnostic;
18
19 //import com.jconfig.*;
20
21 /**
22 *
23 *
24 */
25 public class NativeFileProperties
26 {
27 public final static String IDENTIFIER="$Id: NativeFileProperties.java,v 1.2 2003/08/11 02:22:30 aleric Exp $";
28
29 // Permissions Types (User, Group, Other)
30 public final int U = 0;
31 public final int G = 1;
32 public final int O = 2;
33 public final int UG = 3;
34 public final int UO = 4;
35 public final int GO = 5;
36 public final int UGO = 6;
37
38 // Permissions Modes (Execute, Read, Write)
39 public final int X = 1;
40 public final int W = 2;
41 public final int WX = 3;
42 public final int R = 4;
43 public final int RX = 5;
44 public final int RW = 6;
45 public final int RXW = 7;
46
47
48
49 protected boolean bTest = false; // Debug
50 protected String fileSeparator = "";
51 protected int nPermissions = 0;
52 protected NativeUtils refNativeUtils = null;
53
54 /**
55 *
56 *
57 */
58 public NativeFileProperties()
59 {
60 fileSeparator = System.getProperty("file.separator");
61
62 refNativeUtils = new NativeUtils();
63 } // constructor
64
65 /**
66 *
67 *
68 */
69 public boolean isSpaceAvailable(String sPath, long lSpaceRequested)
70 {
71 String sPathSeparator = ":";
72 long lFreeSpace = 0;
73
74 diagnosticTrace("Disk space path: " + sPath);
75
76 boolean bDiskSpaceEnabled = Settings.getBoolean( Settings.DISKSPACE_ENABLED );
77 // If diskspace is disabled, always return true
78 if (bDiskSpaceEnabled == false)
79 {
80 diagnosticTrace("Disk space check is DISABLED");
81 return true;
82 }
83
84 if (lSpaceRequested == 0)
85 {
86 return false;
87 }
88
89 // Remove the filename from the path if the path has more than
90 // one separator char
91 int nSep1 = sPath.indexOf(fileSeparator);
92 if (nSep1 != -1)
93 {
94 int nSep2 = sPath.lastIndexOf(fileSeparator);
95 if ((nSep2 != -1) && (nSep2 != nSep1))
96 {
97 sPath = sPath.substring(0, nSep2);
98 }
99 }
100
101 lFreeSpace = refNativeUtils.getDiskSpace(sPath);
102 diagnosticTrace("Free space: " + lFreeSpace + " for " + sPath);
103 // Is the requested space available?
104 if (lFreeSpace > lSpaceRequested)
105 {
106 return true;
107 }
108
109 return false;
110 } // isSpaceAvailable
111
112
113
114 /**
115 *
116 *
117 */
118 public Date getCreationDate(String sFile)
119 {
120 Date dCreateDate = refNativeUtils.getFileDate(sFile);
121 return dCreateDate;
122 } // getCreationDate
123
124
125 /**
126 *
127 *
128 */
129 protected void logError(String sMsg)
130 {
131 new FlexError(FlexError.CRITICAL, sMsg, IDENTIFIER, "");
132 diagnosticTrace(sMsg);
133 } // logError
134
135 /**
136 *
137 *
138 */
139 public void diagnosticTrace(String sMsg)
140 {
141 if ( Diagnostic.isOutputEnabled( Diagnostic.APPSERVER_IMPORT ) )
142 {
143 Diagnostic.trace(Diagnostic.APPSERVER_IMPORT, sMsg);
144 }
145
146 if (bTest == true)
147 {
148 System.out.println(sMsg);
149 }
150 }
151
152 /**
153 *
154 *
155 */
156 public void addPermission(int nType, int nMode)
157 {
158 // Add permissions to the nPermissions parameter for nType
159 // User Group Other
160 // RWX RWX RWX
161 //
162 if ((nType == O) || (nType == GO) || (nType == UO) || (nType == UGO))
163 {
164 nPermissions |= nMode;
165 }
166
167 if ((nType == G) || (nType == GO) || (nType == UG) || (nType == UGO))
168 {
169 nPermissions |= nMode << 3;
170 }
171
172 if ((nType == U) || (nType == UG) || (nType == UO) || (nType == UGO))
173 {
174 nPermissions |= nMode << 6;
175 }
176
177 } // addPermission
178
179
180 /**
181 *
182 *
183 */
184 public void addAllPermissions()
185 {
186 addPermission(UGO, RXW);
187 }
188
189
190 /**
191 *
192 *
193 */
194 public void addAllUserPermissions()
195 {
196 addPermission(U, RXW);
197 }
198
199
200 /**
201 *
202 *
203 */
204 public void addAllGroupPermissions()
205 {
206 addPermission(G, RXW);
207 }
208
209
210 /**
211 *
212 *
213 */
214 public void addAllOtherPermissions()
215 {
216 addPermission(O, RXW);
217 }
218
219
220 /**
221 *
222 *
223 */
224 public int setAddedPermissions(String sFilePath)
225 {
226 // Set the permissions from the nPermissions variable
227 // Only call this once after addxxxPermission(s) has been called one or more times
228
229 // Get the current file permissions integer for the file and mask off the lower order
230 // 9 permissions bits, holding the higher order non-permissions bits so they don't get
231 // changed.
232 int nCurrentPermissions = getPermissions(sFilePath);
233 if (nCurrentPermissions == -1)
234 {
235 nCurrentPermissions = 0;
236 }
237
238 nCurrentPermissions &= ~0x1ff;
239 nPermissions |= nCurrentPermissions;
240
241 // Set the permissions
242 int nResult = refNativeUtils.setFilePermissions(sFilePath, nPermissions);
243 if (nResult == -1)
244 {
245 logError("Could not set permissions for " + sFilePath);
246 }
247
248 // Clear the global permissions parameter
249 nPermissions = 0;
250
251 return nResult;
252 } // setAddedPermissions
253
254
255 /**
256 *
257 *
258 */
259 public int setPermissions(String sFilePath, String sPerms)
260 {
261 return setPermissions(sFilePath, sPerms, -1, -1);
262 } // setPermissions 1
263
264 /**
265 *
266 *
267 */
268 public int setPermissions(String sFilePath, String sPerms, int nUserId, int nGroupId)
269 {
270 int nResult = -1;
271 int nPerms = -1;
272
273 // Set the Owner if Id's were passsed in
274 if ((nUserId != -1) && (nGroupId != -1))
275 {
276 if (setOwner(sFilePath, nUserId, nGroupId) == -1)
277 {
278 return -1;
279 }
280 }
281
282 // Convert permissions from octal/hex to decimal
283 if (sPerms.startsWith("0"))
284 {
285 if ((sPerms.startsWith("0x")) || (sPerms.startsWith("0X")))
286 {
287 // Its hex
288 int nPos = sPerms.toUpperCase().indexOf("X");
289 sPerms = sPerms.substring(nPos + 1);
290
291 try
292 {
293 Integer nIntPerms = Integer.valueOf(sPerms, 16);
294 nPerms = nIntPerms.intValue();
295 }
296
297 catch(NumberFormatException e)
298 {
299 logError("Incorrect hex number format for permission: " + sPerms);
300 return -1;
301 }
302 } // "0x" if
303
304 else
305 {
306 // Its octal
307 try
308 {
309 Integer nIntPerms = Integer.valueOf(sPerms, 8);
310 nPerms = nIntPerms.intValue();
311 }
312
313 catch(NumberFormatException e)
314 {
315 logError("Incorrect octal number format for permission: " + sPerms);
316 return -1;
317 }
318 } // "0x" else
319 } // "0" if
320
321 else
322 {
323 // Its decimal
324 try
325 {
326 Integer nIntPerms = Integer.valueOf(sPerms);
327 nPerms = nIntPerms.intValue();
328 }
329
330 catch(NumberFormatException e)
331 {
332 logError("Incorrect decimal number format for permission: " + sPerms);
333 return -1;
334 }
335 } // "0" else
336
337 // Mask off higher order bits of the requested input permissions
338 nPerms &= 0x1ff;
339
340 // Set the global permissions variable
341 nPermissions = nPerms;
342 setAddedPermissions(sFilePath);
343
344 return nResult;
345 } // setPermissions 2
346
347
348 /**
349 *
350 *
351 */
352 public int setOwner(String sPath, int nUser, int nGroup)
353 {
354 return refNativeUtils.setFileOwner(sPath, nUser, nGroup);
355 }
356
357 /**
358 *
359 *
360 */
361 public int getPermissions(String sPath)
362 {
363 return refNativeUtils.getFilePermissions(sPath);
364 }
365
366 } // NativeFileProperties