1 12 package org.eclipse.core.internal.localstore; 13 14 import java.io.File ; 15 import org.eclipse.core.internal.resources.ResourceException; 16 import org.eclipse.core.internal.resources.ResourceStatus; 17 import org.eclipse.core.internal.utils.Convert; 18 import org.eclipse.core.internal.utils.Messages; 19 import org.eclipse.core.resources.*; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.osgi.util.NLS; 22 23 public abstract class CoreFileSystemLibrary { 24 25 26 private static final boolean caseSensitive = Platform.OS_MACOSX.equals(Platform.getOS()) ? false : new File ("a").compareTo(new File ("A")) != 0; private static boolean hasNatives = false; 28 private static boolean isUnicode = false; 29 30 31 private static final String LIBRARY_NAME = "core_3_1_0"; 35 private static boolean loggedFailedGetAttributes = false; 36 37 46 47 48 private static final long STAT_FOLDER = 0x2000000000000000l; 49 50 private static final long STAT_READ_ONLY = 0x1000000000000000l; 51 52 private static final long STAT_RESERVED = 0x8000000000000000l; 53 55 private static final long STAT_VALID = 0x4000000000000000l; 56 57 private static final long STAT_LASTMODIFIED = ~(STAT_RESERVED | STAT_VALID | STAT_FOLDER | STAT_READ_ONLY); 58 59 static { 60 try { 61 System.loadLibrary(LIBRARY_NAME); 62 hasNatives = true; 63 isUnicode = internalIsUnicode(); 64 } catch (UnsatisfiedLinkError e) { 65 logMissingNativeLibrary(e); 66 } 67 } 68 69 73 public static boolean copyAttributes(String source, String destination, boolean copyLastModified) { 74 if (hasNatives) 75 return isUnicode ? internalCopyAttributesW(source.toCharArray(), destination.toCharArray(), copyLastModified) : internalCopyAttributes(Convert.toPlatformBytes(source), Convert.toPlatformBytes(destination), copyLastModified); 77 return false; } 79 80 public static long getLastModified(long stat) { 81 return (stat & STAT_LASTMODIFIED); 82 } 83 84 public static long getLastModified(String fileName) { 85 if (hasNatives) 86 return getLastModified(getStat(fileName)); 87 88 return new File (fileName).lastModified(); 90 } 91 92 public static ResourceAttributes getResourceAttributes(String fileName) { 93 try { 94 ResourceAttributes attributes = new ResourceAttributes(); 95 if (!hasNatives) { 96 attributes.setReadOnly(isReadOnly(fileName)); 98 return attributes; 99 } 100 if (isUnicode ? internalGetResourceAttributesW(fileName.toCharArray(), attributes) : internalGetResourceAttributes(Convert.toPlatformBytes(fileName), attributes)) 102 return attributes; 103 } catch (UnsatisfiedLinkError e) { 104 if (!loggedFailedGetAttributes) { 105 loggedFailedGetAttributes = true; 106 String message = NLS.bind(Messages.resources_getResourceAttributesFailed, fileName); 107 ResourceStatus status = new ResourceStatus(IStatus.INFO, new Path(fileName), message); 108 ResourcesPlugin.getPlugin().getLog().log(status); 109 } 110 } 111 return null; 112 } 113 114 public static long getStat(String fileName) { 115 if (hasNatives) 116 return isUnicode ? internalGetStatW(fileName.toCharArray()) : internalGetStat(Convert.toPlatformBytes(fileName)); 117 118 File target = new File (fileName); 120 long result = target.lastModified(); 121 if (result == 0) return result; 123 result |= STAT_VALID; 124 if (target.isDirectory()) 125 result |= STAT_FOLDER; 126 if (!(new File (fileName).canWrite())) 127 result |= STAT_READ_ONLY; 128 return result; 129 } 130 131 135 private static final native boolean internalCopyAttributes(byte[] source, byte[] destination, boolean copyLastModified); 136 137 143 private static final native boolean internalCopyAttributesW(char[] source, char[] destination, boolean copyLastModified); 144 145 147 private static final native boolean internalGetResourceAttributes(byte[] fileName, ResourceAttributes attribute); 148 149 152 private static final native boolean internalGetResourceAttributesW(char[] fileName, ResourceAttributes attribute); 153 154 159 private static final native long internalGetStat(byte[] fileName); 160 161 167 private static final native long internalGetStatW(char[] fileName); 168 169 173 private static final native boolean internalIsUnicode(); 174 175 177 private static final native boolean internalSetResourceAttributes(byte[] fileName, ResourceAttributes attribute); 178 179 182 private static final native boolean internalSetResourceAttributesW(char[] fileName, ResourceAttributes attribute); 183 184 public static boolean isCaseSensitive() { 185 return caseSensitive; 186 } 187 188 public static boolean isFile(long stat) { 189 return isSet(stat, STAT_VALID) && !isSet(stat, STAT_FOLDER); 190 } 191 192 public static boolean isFolder(long stat) { 193 return isSet(stat, STAT_VALID) && isSet(stat, STAT_FOLDER); 194 } 195 196 public static boolean isReadOnly(String fileName) { 197 return isSet(getStat(fileName), STAT_READ_ONLY); 201 } 202 203 public static boolean isReadOnly(long stat) { 204 return isSet(stat, STAT_READ_ONLY); 205 } 206 207 private static boolean isSet(long stat, long mask) { 208 return (stat & mask) != 0; 209 } 210 211 private static void logMissingNativeLibrary(UnsatisfiedLinkError e) { 212 String libName = System.mapLibraryName(LIBRARY_NAME); 213 String message = NLS.bind(Messages.localstore_couldNotLoadLibrary, libName); 214 ResourceStatus status = new ResourceStatus(IStatus.INFO, null, message, null); 215 ResourcesPlugin.getPlugin().getLog().log(status); 216 } 217 218 public static boolean setReadOnly(String fileName, boolean readonly) { 219 ResourceAttributes attributes = getResourceAttributes(fileName); 220 if (attributes == null) 221 return false; 222 attributes.setReadOnly(readonly); 223 try { 224 setResourceAttributes(fileName, attributes); 225 } catch (CoreException e) { 226 return false; 228 } 229 return true; 230 } 231 232 public static void setResourceAttributes(String fileName, ResourceAttributes attributes) throws CoreException { 233 if (!hasNatives) { 234 return; 236 } 237 if (isUnicode ? internalSetResourceAttributesW(fileName.toCharArray(), attributes) : internalSetResourceAttributes(Convert.toPlatformBytes(fileName), attributes)) 238 return; 239 String message = NLS.bind(Messages.resources_setResourceAttributesFailed, fileName); 240 throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, new Path(fileName), message, null); 241 } 242 243 247 public static boolean usingNatives() { 248 return hasNatives; 249 } 250 } 251 | Popular Tags |