1 19 20 package org.netbeans.modules.versioning.system.cvss; 21 22 import org.netbeans.modules.versioning.spi.VCSInterceptor; 23 import org.netbeans.modules.versioning.util.Utils; 24 import org.netbeans.lib.cvsclient.admin.StandardAdminHandler; 25 import org.netbeans.lib.cvsclient.admin.Entry; 26 import org.netbeans.lib.cvsclient.admin.AdminHandler; 27 import org.openide.ErrorManager; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.util.*; 32 33 38 class FilesystemHandler extends VCSInterceptor { 39 40 private final FileStatusCache cache; 41 private static Thread ignoredThread; 42 43 public FilesystemHandler(CvsVersioningSystem cvs) { 44 cache = cvs.getStatusCache(); 45 } 46 47 52 public boolean beforeDelete(File file) { 53 if (ignoringEvents()) return false; 54 return org.netbeans.modules.versioning.system.cvss.util.Utils.isPartOfCVSMetadata(file) || file.isDirectory() && hasMetadata(file); 55 } 56 57 public void doDelete(File file) throws IOException { 58 if (file.isDirectory() && hasMetadata(file)) { 59 new File (file, "CVS/.nb-removed").createNewFile(); 60 cache.refresh(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN, true); 61 return; 62 } 63 if (!org.netbeans.modules.versioning.system.cvss.util.Utils.isPartOfCVSMetadata(file)) { 64 if (!file.delete()) { 65 throw new IOException ("Failed to delete file: " + file.getAbsolutePath()); 66 } 67 } 68 } 69 70 public void afterDelete(final File file) { 71 if (ignoringEvents()) return; 72 Utils.post(new Runnable () { 73 public void run() { 74 fileDeletedImpl(file); 75 } 76 }); 77 } 78 79 82 public boolean beforeMove(File from, File to) { 83 File destDir = to.getParentFile(); 84 if (from != null && destDir != null && from.isDirectory()) { 85 FileInformation info = cache.getStatus(from); 86 return (info.getStatus() & FileInformation.STATUS_MANAGED) != 0; 87 } 88 return false; 89 } 90 91 97 public void doMove(File from, File to) throws IOException { 98 List<File > affectedFiles = new ArrayList<File >(); 99 moveRecursively(affectedFiles, from, to); 100 cvsRemoveRecursively(from); 101 refresh(affectedFiles); 102 } 103 104 private void moveRecursively(List<File > affectedFiles, File from, File to) throws IOException { 105 File [] files = from.listFiles(); 106 if (files != null) { 107 for (File file : files) { 108 String fileName = file.getName(); 109 if (file.isDirectory()) { 110 if (fileName.equals("CVS")) { 111 new File (file, ".nb-removed").createNewFile(); 112 continue; 113 } 114 File toFile = new File (to, fileName); 115 moveRecursively(affectedFiles, file, toFile); 116 affectedFiles.add(file); 117 affectedFiles.add(toFile); 118 } else { 119 to.mkdirs(); 120 File toFile = new File (to, fileName); 121 file.renameTo(toFile); 122 affectedFiles.add(file); 123 affectedFiles.add(toFile); 124 } 125 } 126 } 127 } 128 129 private void cvsRemoveRecursively(File dir) { 130 StandardAdminHandler sah = new StandardAdminHandler(); 131 Entry [] entries = null; 132 try { 133 entries = sah.getEntriesAsArray(dir); 134 } catch (IOException e) { 135 } 137 138 if (entries != null) { 139 for (Entry entry : entries) { 140 if (entry != null && !entry.isDirectory() && !entry.isUserFileToBeRemoved()) { 141 File file = new File (dir, entry.getName()); 142 cvsRemoveLocally(sah, file, entry); 143 } 144 } 145 } 146 147 File [] files = dir.listFiles(); 148 for (File file : files) { 149 if (file.isDirectory()) cvsRemoveRecursively(file); 150 } 151 } 152 153 public void afterMove(final File from, final File to) { 154 if (ignoringEvents()) return; 155 Utils.post(new Runnable () { 156 public void run() { 157 fileDeletedImpl(from); 158 fileCreatedImpl(to); 159 } 160 }); 161 } 162 163 public boolean beforeCreate(File file, boolean isDirectory) { 164 if (ignoringEvents()) return false; 165 if (file.getName().equals(CvsVersioningSystem.FILENAME_CVS)) { 166 if (file.isDirectory()) { 167 File f = new File (file, CvsLiteAdminHandler.INVALID_METADATA_MARKER); 168 try { 169 f.createNewFile(); 170 } catch (IOException e) { 171 ErrorManager.getDefault().log(ErrorManager.ERROR, "Unable to create marker: " + f.getAbsolutePath()); } 173 } 174 return true; 175 } else { 176 return false; 177 } 178 } 179 180 public void doCreate(File file, boolean isDirectory) throws IOException { 181 file.mkdir(); 182 File f = new File (file, CvsLiteAdminHandler.INVALID_METADATA_MARKER); 183 try { 184 f.createNewFile(); 185 } catch (IOException e) { 186 ErrorManager.getDefault().log(ErrorManager.ERROR, "Unable to create marker: " + f.getAbsolutePath()); } 188 } 189 190 public void afterCreate(final File file) { 191 if (ignoringEvents()) return; 192 Utils.post(new Runnable () { 193 public void run() { 194 fileCreatedImpl(file); 195 } 196 }); 197 } 198 199 public void afterChange(final File file) { 200 if (ignoringEvents()) return; 201 Utils.post(new Runnable () { 202 public void run() { 203 cache.refreshCached(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN); 204 if (file.getName().equals(CvsVersioningSystem.FILENAME_CVSIGNORE)) cache.directoryContentChanged(file.getParentFile()); 205 } 206 }); 207 } 208 209 211 private void fileCreatedImpl(File file) { 212 if (file == null) return; 213 int status = cache.refresh(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN).getStatus(); 214 215 if ((status & FileInformation.STATUS_MANAGED) == 0) return; 216 217 if (status == FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY) { 218 StandardAdminHandler sah = new StandardAdminHandler(); 219 Entry entry = null; 220 try { 221 entry = sah.getEntry(file); 222 } catch (IOException e) { 223 } 225 if (entry != null && !entry.isDirectory() && entry.isUserFileToBeRemoved()) { 226 cvsUndoRemoveLocally(sah, file, entry); 227 } 228 cache.refresh(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN); 229 } 230 if (file.getName().equals(CvsVersioningSystem.FILENAME_CVSIGNORE)) cache.directoryContentChanged(file.getParentFile()); 231 if (file.isDirectory()) cache.directoryContentChanged(file); 232 } 233 234 239 private void fileDeletedImpl(File file) { 240 if (file == null) return; 241 242 StandardAdminHandler sah = new StandardAdminHandler(); 243 Entry entry = null; 244 try { 245 entry = sah.getEntry(file); 246 } catch (IOException e) { 247 } 249 if (entry != null && !entry.isDirectory() && !entry.isUserFileToBeRemoved()) { 250 cvsRemoveLocally(sah, file, entry); 251 } 252 253 cache.refresh(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN); 254 if (file.getName().equals(CvsVersioningSystem.FILENAME_CVSIGNORE)) cache.directoryContentChanged(file.getParentFile()); 255 } 256 257 265 private void cvsRemoveLocally(AdminHandler ah, File file, Entry entry) { 266 try { 267 if (entry.isNewUserFile()) { 268 ah.removeEntry(file); 269 } else { 270 entry.setRevision("-" + entry.getRevision()); entry.setConflict(Entry.DUMMY_TIMESTAMP); 272 ah.setEntry(file, entry); 273 } 274 } catch (IOException e) { 275 } 277 } 278 279 private void cvsUndoRemoveLocally(AdminHandler ah, File file, Entry entry) { 280 entry.setRevision(entry.getRevision().substring(1)); 281 entry.setConflict(Entry.getLastModifiedDateFormatter().format(new Date(System.currentTimeMillis() - 1000))); 282 try { 283 ah.setEntry(file, entry); 284 } catch (IOException e) { 285 } 287 } 288 289 private void refresh(List<File > files) { 290 for (File file : files) { 291 cache.refresh(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN, true); 292 } 293 } 294 295 private boolean hasMetadata(File file) { 296 return new File (file, "CVS/Repository").canRead(); 297 } 298 299 314 static void ignoreEvents(boolean ignore) { 315 if (ignore) { 316 ignoredThread = Thread.currentThread(); 317 } else { 318 ignoredThread = null; 319 } 320 } 321 322 325 private static boolean ignoringEvents() { 326 return ignoredThread == Thread.currentThread(); 327 } 328 } 329 | Popular Tags |