1 | package net.oni2.aeinstaller.backend.oni.management.tools;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.util.Arrays;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.TreeSet;
|
---|
8 |
|
---|
9 | import net.oni2.aeinstaller.backend.CaseInsensitiveFile;
|
---|
10 | import net.oni2.aeinstaller.backend.FileChecksum;
|
---|
11 | import net.oni2.aeinstaller.backend.Paths;
|
---|
12 | import net.oni2.aeinstaller.backend.packages.Package;
|
---|
13 | import net.oni2.aeinstaller.backend.packages.PackageManager;
|
---|
14 |
|
---|
15 | import org.apache.commons.io.FileUtils;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * @author Christian Illy
|
---|
19 | */
|
---|
20 | public class ToolsManager {
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Verify integrity of installed tools
|
---|
24 | */
|
---|
25 | public static void verifyToolsIntegrity() {
|
---|
26 | final ToolInstallationList til = ToolInstallationList.getInstance();
|
---|
27 | for (final Package m : PackageManager.getInstance().getInstalledTools()) {
|
---|
28 | ToolFileIterator.iteratePlatformToolFiles(m,
|
---|
29 | new ToolFileIteratorEntry() {
|
---|
30 | @Override
|
---|
31 | public void toolFile(File source, File target) {
|
---|
32 | byte[] chkSrc = FileChecksum
|
---|
33 | .calculateFileMD5(source);
|
---|
34 | if (!target.exists()) {
|
---|
35 | til.markModified(m.getPackageNumber(), true);
|
---|
36 | } else {
|
---|
37 | byte[] chkTrg = FileChecksum
|
---|
38 | .calculateFileMD5(target);
|
---|
39 | if (!Arrays.equals(chkSrc, chkTrg))
|
---|
40 | til.markModified(m.getPackageNumber(), true);
|
---|
41 | }
|
---|
42 | }
|
---|
43 | });
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @param tools
|
---|
49 | * Tools to (un)install
|
---|
50 | * @param uninstall
|
---|
51 | * Uninstall tools or install?
|
---|
52 | */
|
---|
53 | public static void installTools(TreeSet<Package> tools, boolean uninstall) {
|
---|
54 | ToolInstallationList til = ToolInstallationList.getInstance();
|
---|
55 | for (Package m : tools) {
|
---|
56 | if (!uninstall) { // Install:
|
---|
57 | final HashSet<String> files = new HashSet<String>();
|
---|
58 | ToolFileIterator.iteratePlatformToolFiles(m,
|
---|
59 | new ToolFileIteratorEntry() {
|
---|
60 | @Override
|
---|
61 | public void toolFile(File source, File target) {
|
---|
62 | copyToolsFiles(source, target, files);
|
---|
63 | }
|
---|
64 | });
|
---|
65 | til.addTool(m.getPackageNumber(), files);
|
---|
66 | } else { // Uninstall:
|
---|
67 | if (til.isInstalled(m.getPackageNumber())) {
|
---|
68 | removeTool(til.getFiles(m.getPackageNumber()));
|
---|
69 | til.removeTool(m.getPackageNumber());
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | til.saveList();
|
---|
74 | }
|
---|
75 |
|
---|
76 | private static void copyToolsFiles(File src, File target,
|
---|
77 | HashSet<String> files) {
|
---|
78 | try {
|
---|
79 | File targetFile = CaseInsensitiveFile.getCaseInsensitiveFile(
|
---|
80 | target.getParentFile(), target.getName());
|
---|
81 |
|
---|
82 | // Case mismatch?
|
---|
83 | if (!targetFile.getName().equals(src.getName()))
|
---|
84 | targetFile.delete();
|
---|
85 |
|
---|
86 | files.add(target.getPath().replace(
|
---|
87 | Paths.getEditionBasePath().getPath(), ""));
|
---|
88 |
|
---|
89 | FileUtils.copyFile(src, target);
|
---|
90 | if (src.canExecute())
|
---|
91 | target.setExecutable(true);
|
---|
92 | } catch (IOException e) {
|
---|
93 | e.printStackTrace();
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | private static void removeTool(HashSet<String> files) {
|
---|
98 | for (String p : files) {
|
---|
99 | File targetFile = new File(Paths.getEditionBasePath().getPath() + p);
|
---|
100 | if (targetFile.getPath().contains(
|
---|
101 | Paths.getEditionBasePath().getPath())) {
|
---|
102 | File targetFolder = targetFile.getParentFile();
|
---|
103 |
|
---|
104 | if (targetFile.exists())
|
---|
105 | targetFile.delete();
|
---|
106 | if (targetFolder.list().length == 0)
|
---|
107 | targetFolder.delete();
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|