/** * @param doOverwrite * @since 5.5 */ protected Command doCopy( Task task, Map<String, String> prefs, File fileToCopy, File dst, boolean doOverwrite) throws PackageException { String dstmd5; File bak = null; CompositeCommand rollbackCommand = new CompositeCommand(); if (fileToCopy.isDirectory()) { if (fileToCopy != file) { dst = new File(dst, fileToCopy.getName()); } dst.mkdirs(); for (File childFile : fileToCopy.listFiles()) { rollbackCommand.addCommand(doCopy(task, prefs, childFile, dst, doOverwrite)); } return rollbackCommand; } if (dst.isDirectory()) { dst = new File(dst, fileToCopy.getName()); } try { FileMatcher filenameMatcher = FileMatcher.getMatcher("{n:.*-}[0-9]+.*\\.jar"); if (filenameMatcher.match(fileToCopy.getName()) && (overwriteIfNewerVersion || upgradeOnly)) { // Compare source and destination versions set in filename FileVersion fileToCopyVersion, dstVersion = null; String filenameWithoutVersion = filenameMatcher.getValue(); FileMatcher versionMatcher = FileMatcher.getMatcher(filenameWithoutVersion + "{v:[0-9]+.*}\\.jar"); // Get new file version if (versionMatcher.match(fileToCopy.getName())) { fileToCopyVersion = new FileVersion(versionMatcher.getValue()); // Get original file name and version File dir = dst.getParentFile(); File[] list = dir.listFiles(); if (list != null) { for (File f : list) { if (versionMatcher.match(f.getName())) { dst = f; dstVersion = new FileVersion(versionMatcher.getValue()); break; } } } if (dstVersion == null) { if (upgradeOnly) { return null; } } else if (fileToCopyVersion.greaterThan(dstVersion)) { // backup dst and generate rollback command File oldDst = dst; dst = new File(dst.getParentFile(), fileToCopy.getName()); File backup = IOUtils.backup(task.getPackage(), oldDst); rollbackCommand.addCommand(new Copy(backup, oldDst, null, false)); // Delete old dst as its name differs from new version oldDst.delete(); } else if (fileToCopyVersion.isSnapshot() && fileToCopyVersion.equals(dstVersion)) { doOverwrite = true; } else if (!doOverwrite) { log.info( "Ignore " + fileToCopy + " because not newer than " + dstVersion + " and 'overwrite' is set to false."); return null; } } } if (dst.exists()) { // backup the destination file if exist. if (!doOverwrite && !append) { // force a rollback throw new PackageException( "Copy command has overwrite flag on false but destination file exists: " + dst); } if (task instanceof UninstallTask) { // no backup for uninstall task } else if (append) { bak = IOUtils.backup(task.getPackage(), fileToCopy); } else { bak = IOUtils.backup(task.getPackage(), dst); } } else { // target file doesn't exists - it will be created dst.getParentFile().mkdirs(); } // copy the file - use getContentToCopy to allow parameterization // for subclasses String content = getContentToCopy(fileToCopy, prefs); if (content != null) { if (append && dst.exists()) { RandomAccessFile rfile = new RandomAccessFile(dst, "r"); try { rfile.seek(dst.length()); if (!"".equals(rfile.readLine())) { content = System.getProperty("line.separator") + content; } } catch (IOException e) { log.error(e); } finally { rfile.close(); } } FileUtils.writeFile(dst, content, append); } else { File tmp = new File(dst.getPath() + ".tmp"); FileUtils.copy(fileToCopy, tmp); if (!tmp.renameTo(dst)) { tmp.delete(); FileUtils.copy(fileToCopy, dst); } } // check whether the copied or restored file was the launcher if (dst.getName().equals(LAUNCHER_JAR) || fileToCopy.getName().equals(LAUNCHER_JAR)) { Environment env = Environment.getDefault(); env.setProperty(LAUNCHER_CHANGED_PROPERTY, "true"); } // get the md5 of the copied file. dstmd5 = IOUtils.createMd5(dst); } catch (Exception e) { throw new PackageException("Failed to copy " + fileToCopy, e); } if (bak == null) { // no file was replaced rollbackCommand.addCommand(new Delete(dst, dstmd5, removeOnExit)); } else if (append) { rollbackCommand.addCommand(new UnAppend(bak, dst)); } else { rollbackCommand.addCommand(new Copy(bak, dst, dstmd5, true)); } return rollbackCommand; }
@Test public void test() { // test version containing only major part assertArrayEquals(new FileVersion("1").getSplitVersion(), new Integer[] {1}); FileVersion fv = new FileVersion("1-qualifier"); assertArrayEquals(fv.getSplitVersion(), new Integer[] {1}); assertEquals("-qualifier", fv.getQualifier()); fv = new FileVersion("1_qualifier"); assertArrayEquals(fv.getSplitVersion(), new Integer[] {1}); assertEquals("_qualifier", fv.getQualifier()); // test caja versions fv = new FileVersion("r1234"); assertArrayEquals(fv.getSplitVersion(), new Integer[] {1234}); fv = new FileVersion("r1234-qualifier"); assertArrayEquals(fv.getSplitVersion(), new Integer[] {1234}); assertEquals("-qualifier", fv.getQualifier()); fv = new FileVersion("r1234_qualifier"); assertArrayEquals(fv.getSplitVersion(), new Integer[] {1234}); assertEquals("_qualifier", fv.getQualifier()); assertArrayEquals(new FileVersion("1.3.3").getSplitVersion(), new Integer[] {1, 3, 3}); assertEquals(new FileVersion("1.3.3").getQualifier(), ""); assertArrayEquals(new FileVersion("1.3.3_01").getSplitVersion(), new Integer[] {1, 3, 3}); assertEquals(new FileVersion("1.3.3_01").getQualifier(), "_01"); assertArrayEquals( new FileVersion("1.1-BETA5-incubating").getSplitVersion(), new Integer[] {1, 1}); assertEquals(new FileVersion("1.1-BETA5-incubating").getQualifier(), "-BETA5-incubating"); assertArrayEquals(new FileVersion("3.3.1.GA-NX").getSplitVersion(), new Integer[] {3, 3, 1}); assertEquals(new FileVersion("3.3.1.GA-NX").getQualifier(), ".GA-NX"); assertArrayEquals(new FileVersion("1.3.1-NXP-7750").getSplitVersion(), new Integer[] {1, 3, 1}); assertEquals(new FileVersion("1.3.1-NXP-7750").getQualifier(), "-NXP-7750"); assertArrayEquals(new FileVersion("3.0-NX3.2").getSplitVersion(), new Integer[] {3, 0}); assertEquals(new FileVersion("3.0-NX3.2").getQualifier(), "-NX3.2"); assertArrayEquals(new FileVersion("0.4.0-r1096750").getSplitVersion(), new Integer[] {0, 4, 0}); assertEquals(new FileVersion("0.4.0-r1096750").getQualifier(), "-r1096750"); assertTrue( "5.5 must be greater than 5.4", new FileVersion("5.5").compareTo(new FileVersion("5.4")) > 0); assertTrue( "5.5-SNAPSHOT must be greater than 5.4", new FileVersion("5.5-SNAPSHOT").compareTo(new FileVersion("5.4")) > 0); assertTrue( "5.5 must be greater than 5.4-SNAPSHOT", new FileVersion("5.5").compareTo(new FileVersion("5.4-SNAPSHOT")) > 0); assertTrue( "5.5 must be greater than 5.5-SNAPSHOT", new FileVersion("5.5").compareTo(new FileVersion("5.5-SNAPSHOT")) > 0); assertTrue( "5.5-anything must be greater than 5.5", new FileVersion("5.5-anything").compareTo(new FileVersion("5.5")) > 0); assertTrue( "5.5_01 must be greater than 5.5", new FileVersion("5.5_01").compareTo(new FileVersion("5.5")) > 0); assertTrue( "5.5_01 must be lesser than 5.5.1", new FileVersion("5.5_01").compareTo(new FileVersion("5.5.1")) < 0); assertTrue( "5.5-NXP must be lesser than 5.5.1", new FileVersion("5.5-NXP").compareTo(new FileVersion("5.5.1")) < 0); assertTrue( "5.5-anything must be greater than 5.5-SNAPSHOT", new FileVersion("5.5-anything").compareTo(new FileVersion("5.5-SNAPSHOT")) > 0); assertTrue( "5.5.1 must be greater than 5.5", new FileVersion("5.5.1").compareTo(new FileVersion("5.5")) > 0); assertTrue( "5.5.1 must be greater than 5.5-SNAPSHOT", new FileVersion("5.5.1").compareTo(new FileVersion("5.5-SNAPSHOT")) > 0); assertTrue( "5.5.1 must be greater than 5.5-anything", new FileVersion("5.5.1").compareTo(new FileVersion("5.5-anything")) > 0); assertTrue( "5.5.1-SNAPSHOT must be greater than 5.5-SNAPSHOT", new FileVersion("5.5.1-SNAPSHOT").compareTo(new FileVersion("5.5-SNAPSHOT")) > 0); assertEquals( "5.5-SNAPSHOT must be equal to 5.5-SNAPSHOT", new FileVersion("5.5-SNAPSHOT"), new FileVersion("5.5-SNAPSHOT")); // Release candidate, alpha and beta versions must be lower than the // final version assertTrue( "5.5 must be greater than 5.5-RC1", new FileVersion("5.5").compareTo(new FileVersion("5.5-RC1")) > 0); assertTrue( "5.5-RC2 must be greater than 5.5-RC1", new FileVersion("5.5-RC2").compareTo(new FileVersion("5.5-RC1")) > 0); assertTrue( "5.5 must be greater than 5.5-alpha1", new FileVersion("5.5").compareTo(new FileVersion("5.5-alpha1")) > 0); assertTrue( "5.5 must be greater than 5.5-BETA6", new FileVersion("5.5").compareTo(new FileVersion("5.5-BETA6")) > 0); // Date-based versions must be lower than the final version assertTrue( "5.5 must be greater than 5.5-I20120101_0115", new FileVersion("5.5").compareTo(new FileVersion("5.5-I20120101_0115")) > 0); assertTrue( "5.5.1-I20120101_0115 must be greater than 5.5", new FileVersion("5.5.1-I20120101_0115").compareTo(new FileVersion("5.5")) > 0); assertTrue( "5.5.1-I20120101_0115 must be greater than 5.5.1-I20110101_0115", new FileVersion("5.5.1-I20120101_0115").compareTo(new FileVersion("5.5.1-I20110101_0115")) > 0); }