1 6 package net.sourceforge.cvsgrab; 7 8 import org.netbeans.lib.cvsclient.admin.AdminHandler; 9 import org.netbeans.lib.cvsclient.admin.Entry; 10 import org.netbeans.lib.cvsclient.command.GlobalOptions; 11 12 import java.io.BufferedInputStream ; 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.util.Date ; 19 import java.util.Iterator ; 20 import java.util.Vector ; 21 22 29 30 public class LocalRepository { 31 32 public static final int UPDATE_NEEDED = 1; 33 public static final int UPDATE_NO_CHANGES = 2; 34 public static final int UPDATE_LOCAL_CHANGE = 3; 35 public static final int UPDATE_MERGE_NEEDED = 4; 36 public static final int UPDATE_IMPOSSIBLE = 5; 37 38 private AdminHandler _handler; 39 private GlobalOptions _globalOptions = new GlobalOptions(); 40 43 private File _localRootDir; 44 47 private File _localProjectDir; 48 private int _newFiles = 0; 49 private int _updatedFiles = 0; 50 private int _removedFiles = 0; 51 private int _failedUpdates = 0; 52 private boolean _cleanUpdate; 53 54 59 public LocalRepository(CVSGrab cvsGrab) { 60 _handler = new CVSGrabAdminHandler(cvsGrab); 61 _globalOptions.setCVSRoot(cvsGrab.getCvsRoot()); 62 _globalOptions.setCheckedOutFilesReadOnly(true); 63 _localRootDir = new File (cvsGrab.getDestDir()); 64 _cleanUpdate = cvsGrab.isCleanUpdate(); 65 _localProjectDir = new File (_localRootDir, cvsGrab.getPackagePath()); 66 } 67 68 73 public File getLocalRootDir() { 74 return _localRootDir; 75 } 76 77 82 public File getLocalDir(RemoteDirectory remoteDir) { 83 return new File (getLocalRootDir(), remoteDir.getLocalDir()); 84 } 85 86 91 public File getLocalFile(RemoteFile remoteFile) { 92 File dir = getLocalDir(remoteFile.getDirectory()); 93 File file = new File (dir, remoteFile.getName()); 94 return file; 95 } 96 97 102 public int getNewFileCount() { 103 return _newFiles; 104 } 105 106 111 public int getUpdatedFileCount() { 112 return _updatedFiles; 113 } 114 115 120 public int getRemovedFileCount() { 121 return _removedFiles; 122 } 123 124 129 public int getFailedUpdateCount() { 130 return _failedUpdates; 131 } 132 133 138 public boolean isCleanUpdate() { 139 return _cleanUpdate; 140 } 141 142 145 public void resetFileCounts() { 146 _newFiles = 0; 147 _updatedFiles = 0; 148 _removedFiles = 0; 149 _failedUpdates = 0; 150 } 151 152 158 public int checkUpdateStatus(RemoteFile remoteFile) { 159 boolean needUpdate = true; 160 File file = getLocalFile(remoteFile); 161 162 if (file.exists()) { 163 try { 164 Entry entry = _handler.getEntry(file); 165 if (entry == null) { 166 CVSGrab.getLog().debug("No entry for file " + file); 167 _failedUpdates++; 168 return UPDATE_IMPOSSIBLE; 169 } else { 170 needUpdate = !remoteFile.getVersion().equals(entry.getRevision()); 171 boolean locallyModified = isLocallyModified(file, entry); 172 if (locallyModified) { 173 CVSGrab.getLog().debug("File " + file + " is locally modified"); 174 } 175 if (needUpdate) { 176 if (locallyModified) { 177 CVSGrab.getLog().debug("File " + file + " was modified since last update, cannot upload the new version of this file"); 178 CVSGrab.getLog().debug("Last modified date on disk: " + new Date (file.lastModified())); 179 CVSGrab.getLog().debug("Last modified date on cvs: " + entry.getLastModified()); 180 _failedUpdates++; 182 return UPDATE_MERGE_NEEDED; 183 } else { 184 CVSGrab.getLog().debug("New version available on the remote repository for file " + file); 185 return UPDATE_NEEDED; 186 } 187 } else { 188 if (locallyModified) { 189 CVSGrab.getLog().debug("File " + file + " was modified since last update, cannot upload the new version of this file"); 190 CVSGrab.getLog().debug("Last modified date on disk: " + new Date (file.lastModified())); 191 CVSGrab.getLog().debug("Last modified date on cvs: " + entry.getLastModified()); 192 } 193 return locallyModified ? UPDATE_LOCAL_CHANGE : UPDATE_NO_CHANGES; 194 } 195 } 196 } catch (IOException ex) { 197 ex.printStackTrace(); 199 _failedUpdates++; 200 return UPDATE_IMPOSSIBLE; 201 } 202 } else { 203 return UPDATE_NEEDED; 204 } 205 } 206 207 212 public String getLocalVersion(RemoteFile remoteFile) { 213 File file = getLocalFile(remoteFile); 214 215 if (file.exists()) { 216 try { 217 Entry entry = _handler.getEntry(file); 218 if (entry == null) { 219 return null; 220 } else { 221 return entry.getRevision(); 222 } 223 } catch (IOException ex) { 224 ex.printStackTrace(); 226 return null; 227 } 228 } else { 229 return null; 230 } 231 } 232 233 private boolean isLocallyModified(File file, Entry entry) { 234 return (file.lastModified() > entry.getLastModified().getTime() + 60*1000); 236 } 237 238 243 public synchronized void updateFileVersion(RemoteFile remoteFile) { 245 File dir = getLocalDir(remoteFile.getDirectory()); 246 File file = getLocalFile(remoteFile); 247 Entry entry = null; 248 Date lastModified = remoteFile.getLastModified(); 249 if (lastModified == null) { 250 lastModified = new Date (); 251 } 252 try { 253 entry = _handler.getEntry(file); 254 if (entry == null) { 255 throw new IOException ("Entry not found"); 256 } 257 entry.setRevision(remoteFile.getVersion()); 258 entry.setDate(lastModified); 259 _updatedFiles++; 260 } catch (IOException ex) { 261 boolean binary = remoteFile.isBinary(); 262 String lastModifiedStr = Entry.getLastModifiedDateFormatter().format(lastModified); 263 entry = new Entry("/" + remoteFile.getName() + "/" + remoteFile.getVersion() + "/" 264 + lastModifiedStr + "/" + (binary ? "-kb/" : "/")); 265 _newFiles++; 266 } 267 268 String localDirectory = WebBrowser.removeFinalSlash(dir.getAbsolutePath()); 269 String repositoryPath = remoteFile.getDirectory().getDirectoryPath(); 270 try { 271 _handler.updateAdminData(localDirectory, repositoryPath, entry, _globalOptions); 272 } catch (IOException ex) { 273 _failedUpdates++; 274 CVSGrab.getLog().error("Cannot update CVS entry for file " + file, ex); 275 throw new RuntimeException ("Cannot update CVS entry for file " + file); 276 } 277 } 278 279 284 public void backupFile(RemoteFile remoteFile) { 285 File dir = getLocalDir(remoteFile.getDirectory()); 286 File file = getLocalFile(remoteFile); 287 File backupFile = new File (dir, ".#" + remoteFile.getName() + "." + remoteFile.getVersion()); 288 CVSGrab.getLog().info("Move " + remoteFile.getName() + " to " + backupFile.getName()); 289 InputStream in = null; 291 FileOutputStream out = null; 292 try { 293 try { 294 in = new BufferedInputStream (new FileInputStream (file)); 295 out = new FileOutputStream (backupFile); 296 297 byte[] buffer = new byte[8 * 1024]; 298 int count = 0; 299 do { 300 out.write(buffer, 0, count); 301 count = in.read(buffer, 0, buffer.length); 302 } while (count != -1); 303 } finally { 304 if (out != null) { 305 out.close(); 306 } 307 if (in != null) { 308 in.close(); 309 } 310 } 311 } catch (IOException ex) { 312 _failedUpdates++; 313 ex.printStackTrace(); 314 CVSGrab.getLog().error("Cannot create backup for file " + file); 315 throw new RuntimeException ("Cannot create backup for file " + file); 316 } 317 } 318 319 325 public void unregisterFile(RemoteFile remoteFile) { 326 File file = getLocalFile(remoteFile); 327 try { 328 _handler.removeEntry(file); 329 _failedUpdates++; 330 } catch (IOException ex) { 331 ex.printStackTrace(); 332 } 334 } 335 336 342 public synchronized void cleanRemovedFiles(RemoteDirectory remoteDirectory) { 344 try { 345 File dir = getLocalDir(remoteDirectory); 346 Entry dirEntry = _handler.getEntry(dir); 347 Vector dirFiles = new Vector (); 348 if (dirEntry == null) { 349 return; 351 } 352 for (Iterator i = _handler.getEntries(dir); i.hasNext(); ) { 353 Entry cvsFile = (Entry) i.next(); 354 if (cvsFile.isDirectory()) { 355 continue; 356 } 357 dirFiles.add(cvsFile.getName()); 358 } 359 RemoteFile[] lastDirFiles = remoteDirectory.getRemoteFiles(); 360 for (int i = 0; i < lastDirFiles.length; i++) { 361 dirFiles.remove(lastDirFiles[i].getName()); 362 } 363 _removedFiles += dirFiles.size(); 364 for (Iterator i = dirFiles.iterator(); i.hasNext(); ) { 365 String fileName = (String ) i.next(); 366 File file = new File (getLocalDir(remoteDirectory), fileName); 367 CVSGrab.getLog().debug("Removing " + file); 368 _handler.removeEntry(file); 369 file.delete(); 370 } 371 } catch (IOException ex) { 372 _failedUpdates++; 373 ex.printStackTrace(); 374 CVSGrab.getLog().error("Error while removing files marked for deletion"); 375 } 376 } 377 378 381 public void pruneEmptyDirectories() throws IOException { 382 pruneEmptyDirectory(_localProjectDir); 383 } 384 385 389 private boolean pruneEmptyDirectory(File directory) { 390 boolean empty = true; 391 392 final File [] contents = directory.listFiles(); 393 394 if (contents != null) { 396 for (int i = 0; i < contents.length; i++) { 397 if (contents[i].isFile()) { 398 empty = false; 399 } else { 400 if (!contents[i].getName().equals("CVS")) { empty = pruneEmptyDirectory(contents[i]); 402 } 403 } 404 } 405 406 if (empty) { 407 final File entriesFile = new File (directory, "CVS/Entries"); if (entriesFile.exists()) { 411 final File adminDir = new File (directory, "CVS"); final File [] adminFiles = adminDir.listFiles(); 413 for (int i = 0; i < adminFiles.length; i++) { 414 adminFiles[i].delete(); 415 } 416 CVSGrab.getLog().debug("Removing empty directory " + directory); 417 adminDir.delete(); 418 try { 419 _handler.removeEntry(directory); 421 } catch (IOException ex) { 422 _failedUpdates++; 423 ex.printStackTrace(); 424 CVSGrab.getLog().error("Error while removing empty directory"); 425 } 426 directory.delete(); 427 _removedFiles++; 428 } 429 } 430 } 431 432 return empty; 433 } 434 435 440 public synchronized void add(RemoteDirectory remoteDir) { 442 File dir = getLocalDir(remoteDir); 443 Entry entry = null; 444 String dirName = WebBrowser.removeFinalSlash(dir.getName()); 445 try { 446 entry = _handler.getEntry(dir); 447 if (entry == null) { 448 throw new IOException ("Entry not found"); 449 } 450 } catch (IOException ex) { 451 entry = new Entry("D/" + dirName + "////"); 452 } 453 454 String localDirectory = WebBrowser.removeFinalSlash(dir.getParent()); 455 String repositoryPath = WebBrowser.removeFinalSlash(remoteDir.getDirectoryPath()); 456 int lastSlash = repositoryPath.lastIndexOf('/'); 457 if (lastSlash > 0) { 458 repositoryPath = repositoryPath.substring(0, lastSlash); 459 } 460 try { 461 _handler.updateAdminData(localDirectory, repositoryPath, entry, _globalOptions); 462 } catch (IOException ex) { 463 _failedUpdates++; 464 ex.printStackTrace(); 465 CVSGrab.getLog().error("Cannot update CVS entry for directory " + dir); 466 throw new RuntimeException ("Cannot update CVS entry for directory " + dir); 467 } 468 } 469 470 } 471 | Popular Tags |