1 11 package org.eclipse.update.configuration; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.update.internal.core.UpdateCore; 17 import org.eclipse.update.internal.core.Volume; 18 19 34 public class LocalSystemInfo { 35 36 48 public static final long SIZE_UNKNOWN = -1; 49 50 61 public static final int VOLUME_UNKNOWN = -1; 62 63 74 public static final int VOLUME_INVALID_PATH = -2; 75 76 87 public static final int VOLUME_REMOVABLE = 1; 88 89 100 public static final int VOLUME_FIXED = 2; 101 102 113 public static final int VOLUME_REMOTE = 3; 114 115 126 public static final int VOLUME_CDROM = 4; 127 128 139 public static final int VOLUME_RAMDISK = 5; 140 141 152 public static final int VOLUME_FLOPPY_5 = 6; 153 154 165 public static final int VOLUME_FLOPPY_3 = 7; 166 167 177 public static final int VOLUME_ADDED = 0; 178 179 189 public static final int VOLUME_REMOVED = 1; 190 191 201 public static final int VOLUME_CHANGED = 2; 202 203 204 private static ArrayList listeners = new ArrayList (); 205 private static boolean hasNatives = false; 206 static { 207 try { 208 System.loadLibrary("update"); hasNatives = true; 210 } catch (UnsatisfiedLinkError e) { 211 UpdateCore.warn("Unable to load native library 'update'."); hasNatives = false; 213 } 214 } 215 216 236 public static long getFreeSpace(File path) { 237 if (hasNatives) { 238 try { 239 long bytes = nativeGetFreeSpace(path); 240 return (bytes!=0)?bytes/1024:0; 241 } catch (UnsatisfiedLinkError e) { 242 } 243 } 244 return SIZE_UNKNOWN; 245 } 246 247 248 260 public static IVolume[] getVolumes() { 261 String [] mountPoints = listMountPoints(); 262 Volume[] vol = new Volume[0]; 263 if (mountPoints!=null){ 264 vol = new Volume[mountPoints.length]; 265 for (int i = 0; i < mountPoints.length; i++) { 266 File root = new File (mountPoints[i]); 267 String label = getLabel(root); 268 int type = getType(root); 269 long size = getFreeSpace(root); 270 vol[i] = new Volume(root,label,type,size); 271 vol[i].markReadOnly(); 272 } 273 } else { 274 UpdateCore.warn("Unable to find mount points"); File [] roots = File.listRoots(); 277 if (roots.length == 1) { 278 File root = roots[0]; 280 roots = root.listFiles(); 281 } 282 vol = new Volume[roots.length]; 283 for (int i = 0; i < roots.length; i++) { 284 vol[i] = new Volume(roots[i],null,LocalSystemInfo.VOLUME_UNKNOWN,LocalSystemInfo.SIZE_UNKNOWN); 285 vol[i].markReadOnly(); 286 } 287 } 288 return vol; 289 } 290 291 292 310 public static void addInfoListener(ILocalSystemInfoListener listener) { 311 if (!listeners.contains(listener)) 312 listeners.add(listener); 313 } 314 315 326 public static void removeInfoListener(ILocalSystemInfoListener listener) { 327 listeners.remove(listener); 328 } 329 330 347 public static void fireSystemInfoChanged(IVolume volume, int changeType) { 348 for (int i=0; i< listeners.size(); i++) { 349 ((ILocalSystemInfoListener)listeners.get(i)).systemInfoChanged(volume,changeType); 350 } 351 } 352 353 363 private static String getLabel(File path) { 364 if (hasNatives) { 365 try { 366 return nativeGetLabel(path); 367 } catch (UnsatisfiedLinkError e) { 368 } 369 } 370 return null; 371 } 372 373 390 private static int getType(File path) { 391 if (hasNatives) { 392 try { 393 return nativeGetType(path); 394 } catch (UnsatisfiedLinkError e) { 395 } 396 } 397 return VOLUME_UNKNOWN; 398 } 399 400 406 private static String [] listMountPoints() { 407 if (hasNatives) { 408 try { 409 String [] mountPoints = nativeListMountPoints(); 410 return mountPoints; 411 } catch (UnsatisfiedLinkError e) { 412 } 413 } 414 return null; 415 } 416 417 420 private static native long nativeGetFreeSpace(File path); 421 private static native String nativeGetLabel(File path); 422 private static native int nativeGetType(File path); 423 private static native String [] nativeListMountPoints(); 424 } 425 | Popular Tags |