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 | boolean isDir) {
|
---|
33 | byte[] chkSrc = FileChecksum
|
---|
34 | .calculateFileMD5(source);
|
---|
35 | if (!target.exists()) {
|
---|
36 | til.markModified(m.getPackageNumber(), true);
|
---|
37 | } else {
|
---|
38 | if (!isDir) {
|
---|
39 | byte[] chkTrg = FileChecksum
|
---|
40 | .calculateFileMD5(target);
|
---|
41 | if (!Arrays.equals(chkSrc, chkTrg))
|
---|
42 | til.markModified(m.getPackageNumber(), true);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 | });
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @param tools
|
---|
52 | * Tools to (un)install
|
---|
53 | * @param uninstall
|
---|
54 | * Uninstall tools or install?
|
---|
55 | */
|
---|
56 | public static void installTools(TreeSet<Package> tools, boolean uninstall) {
|
---|
57 | ToolInstallationList til = ToolInstallationList.getInstance();
|
---|
58 | for (Package m : tools) {
|
---|
59 | if (!uninstall) { // Install:
|
---|
60 | final HashSet<String> files = new HashSet<String>();
|
---|
61 | ToolFileIterator.iteratePlatformToolFiles(m,
|
---|
62 | new ToolFileIteratorEntry() {
|
---|
63 | @Override
|
---|
64 | public void toolFile(File source, File target,
|
---|
65 | boolean isDir) {
|
---|
66 | copyToolsFiles(source, target, isDir, files);
|
---|
67 | }
|
---|
68 | });
|
---|
69 | til.addTool(m.getPackageNumber(), files);
|
---|
70 | } else { // Uninstall:
|
---|
71 | if (til.isInstalled(m.getPackageNumber())) {
|
---|
72 | removeTool(til.getFiles(m.getPackageNumber()));
|
---|
73 | til.removeTool(m.getPackageNumber());
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | til.saveList();
|
---|
78 | }
|
---|
79 |
|
---|
80 | private static void copyToolsFiles(File src, File target, boolean isDir,
|
---|
81 | HashSet<String> files) {
|
---|
82 | try {
|
---|
83 | File targetFile = CaseInsensitiveFile.getCaseInsensitiveFile(
|
---|
84 | target.getParentFile(), target.getName());
|
---|
85 |
|
---|
86 | if (isDir && src.getAbsolutePath().endsWith(".app")) {
|
---|
87 | files.add(target.getPath().replace(
|
---|
88 | Paths.getEditionBasePath().getPath(), ""));
|
---|
89 |
|
---|
90 | if (targetFile.exists()) {
|
---|
91 | FileUtils.deleteDirectory(targetFile);
|
---|
92 | }
|
---|
93 |
|
---|
94 | FileUtils.copyDirectory(src, target, true);
|
---|
95 | } else {
|
---|
96 | if (!src.getAbsolutePath().contains(".app/")) {
|
---|
97 | files.add(target.getPath().replace(
|
---|
98 | Paths.getEditionBasePath().getPath(), ""));
|
---|
99 |
|
---|
100 | // Case mismatch?
|
---|
101 | if (!targetFile.getName().equals(src.getName()))
|
---|
102 | targetFile.delete();
|
---|
103 |
|
---|
104 | FileUtils.copyFile(src, target);
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (src.canExecute() && targetFile.exists())
|
---|
108 | targetFile.setExecutable(true);
|
---|
109 | }
|
---|
110 | } catch (IOException e) {
|
---|
111 | e.printStackTrace();
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | private static void removeTool(HashSet<String> files) {
|
---|
116 | for (String p : files) {
|
---|
117 | File targetFile = new File(Paths.getEditionBasePath().getPath() + p);
|
---|
118 | if (targetFile.getPath().contains(
|
---|
119 | Paths.getEditionBasePath().getPath())) {
|
---|
120 | File targetFolder = targetFile.getParentFile();
|
---|
121 |
|
---|
122 | if (targetFile.exists()) {
|
---|
123 | if (targetFile.isDirectory()) {
|
---|
124 | try {
|
---|
125 | FileUtils.deleteDirectory(targetFile);
|
---|
126 | } catch (IOException e) {
|
---|
127 | e.printStackTrace();
|
---|
128 | }
|
---|
129 | } else
|
---|
130 | targetFile.delete();
|
---|
131 | }
|
---|
132 | if (targetFolder.list().length == 0)
|
---|
133 | targetFolder.delete();
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|