summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/far/Far.java
blob: ad04dc7fcdb836256e2e5e843b6e556e763a50c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/** @file
 
 The file is used to create far file
 
 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
package org.tianocore.frameworkwizard.far;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;

import org.tianocore.frameworkwizard.common.Tools;
import org.tianocore.frameworkwizard.packaging.PackageIdentification;
import org.tianocore.frameworkwizard.platform.PlatformIdentification;
import org.tianocore.frameworkwizard.workspace.Workspace;

public class Far {
    //
    // Class member Manifest
    //
    public Manifest manifest = null;

    //
    // Jar file
    //
    private JarFile jf = null;

    private File jarFile = null;

    //
    // Jar outputStream.
    //
    static public JarOutputStream jos = null;

    public Far(File jarFile) {
        this.jarFile = jarFile;
    }

    //
    // For install/updat jar
    //
    public Far(JarFile farFile) throws Exception {
        jf = farFile;
        this.manifest = new Manifest(getManifestFile());
    }

    public void creatFar(List<PackageIdentification> pkgList, List<PlatformIdentification> plfList,
                         Set<String> fileFilter, FarHeader fHeader) throws Exception {
        jos = new JarOutputStream(new FileOutputStream(jarFile));

        //
        // Generate Manifest and get file lists
        //
        this.manifest = new Manifest();
        this.manifest.setFarHeader(fHeader);
        this.manifest.createManifest(pkgList, plfList, fileFilter);

        this.manifest.hibernateToFile();

        //
        // Write Mainifest file to JAR.
        //
        if (this.manifest.mfFile != null) {
            writeToJar(this.manifest.mfFile, jos);
        }

        //
        // Write all files to JAR
        //
        Set<File> files = this.manifest.files;
        Iterator<File> iter = files.iterator();
        while (iter.hasNext()) {
            writeToJar(iter.next(), jos);
        }
        jos.close();
    }

    private void writeToJar(File file, JarOutputStream jos) throws Exception {
        byte[] buffer = new byte[(int) file.length()];
        FileInputStream fInput = new FileInputStream(file);
        JarEntry entry = new JarEntry(
                                      Tools
                                           .convertPathToUnixType(Tools
                                                                       .getRelativePath(file.getPath(),
                                                                                        Workspace.getCurrentWorkspace())));
        jos.putNextEntry(entry);
        fInput.read(buffer);
        jos.write(buffer);
        fInput.close();
    }

    public void InstallFar(String dir) throws Exception {
        List<FarFileItem> allFile = manifest.getAllFileItem();
        extract(allFile, dir);
    }

    public void InstallFar(Map<PlatformIdentification, File> plfMap, Map<PackageIdentification, File> pkgMap)
                                                                                                             throws Exception {
        Set<PlatformIdentification> plfKeys = plfMap.keySet();
        Iterator<PlatformIdentification> plfIter = plfKeys.iterator();
        while (plfIter.hasNext()) {
            PlatformIdentification item = plfIter.next();
            extract(this.manifest.getPlatformContents(item), plfMap.get(item).getPath());
        }

        Set<PackageIdentification> pkgKeys = pkgMap.keySet();
        Iterator<PackageIdentification> pkgIter = pkgKeys.iterator();
        while (pkgIter.hasNext()) {
            PackageIdentification item = pkgIter.next();
            installPackage(item, pkgMap.get(item));
        }
        jf.close();
    }

    public void installPackage(PackageIdentification packageId, File installPath) throws Exception {
        List<FarFileItem> contents = this.manifest.getPackageContents(packageId);
        extract(contents, installPath.getPath());
    }

    public InputStream getManifestFile() throws Exception {
        JarEntry je = null;
        for (Enumeration e = jf.entries(); e.hasMoreElements();) {
            je = (JarEntry) e.nextElement();
            if (je.getName().equalsIgnoreCase(Manifest.mfFileName))
                return jf.getInputStream(je);
        }
        return null;
    }

    public void createFar() {

    }

    public boolean hibernateToFile() {
        return true;
    }

    public void extract(List<FarFileItem> allFile, String dir) throws Exception {

        Iterator filesItem = allFile.iterator();
        FarFileItem ffItem;
        JarEntry je;
        new File(dir).mkdirs();
        dir += File.separatorChar;
        while (filesItem.hasNext()) {
            try {
                ffItem = (FarFileItem) filesItem.next();
                je = jf.getJarEntry(Tools.convertPathToUnixType(ffItem.getDefaultPath()));
                InputStream entryStream = jf.getInputStream(je);
                File file = new File(dir + ffItem.getRelativeFilename());
                file.getParentFile().mkdirs();
                try {
                    //
                    // Create the output file (clobbering the file if it
                    // exists).
                    //
                    FileOutputStream outputStream = new FileOutputStream(file);

                    try {
                        //
                        // Read the entry data and write it to the output
                        // file.
                        //
                        int fileLen = (int)je.getSize();
                        byte[] buffer = new byte[fileLen];
                        int len = entryStream.read(buffer);
                        if (len < fileLen){
                            File tempFile = new File("tempFile");
                            FileOutputStream fos = new FileOutputStream(tempFile);
                            fos.write(buffer, 0, len);
                            while (len < fileLen){
                                int tempLen = entryStream.read(buffer);
                                len = len + tempLen;
                                fos.write(buffer, 0, tempLen);
                            }
                            fos.close();
                            FileInputStream fin = new FileInputStream(tempFile);
                            fin.read(buffer);
                            tempFile.delete();
                        }
                        //
                        //  Check Md5
                        //
                        if (!ffItem.getMd5Value().equalsIgnoreCase(FarMd5.md5(buffer))){
                            throw new Exception (ffItem.getRelativeFilename() + " MD5 Checksum is invaild!");
                        }
                        outputStream.write(buffer);
                    } finally {
                        outputStream.close();
                    }
                } finally {
                    entryStream.close();
                }

            } finally {
            }
        }

    }

    public void addFileToFar(File file, JarOutputStream farOuputStream, String workDir) {

    }

    /**
     * 
     * @param pkgId
     * @return
     */
    public List<PackageIdentification> getPackageDependencies(PackageIdentification pkgId) {
        String[] entry = new String[2];
        PackageQuery pkgQ = new PackageQuery();
        List<PackageIdentification> result = new ArrayList<PackageIdentification>();

        entry = this.manifest.getPackgeSpd(pkgId);
        if (entry == null) {
            return result;
        }
        if (entry[0] != null) {
            try {
                JarEntry je;
                je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + entry[0]));
                List<String> masList = pkgQ.getPackageMsaList(jf.getInputStream(je));
                Iterator item = masList.iterator();
                while (item.hasNext()) {
                    je = jf.getJarEntry(Tools.convertPathToUnixType(entry[1] + File.separatorChar + item.next()));
                    List<PackageIdentification> pkgIdList = pkgQ.getModuleDependencies(jf.getInputStream(je));
                    Iterator pkgItem = pkgIdList.iterator();
                    while (pkgItem.hasNext()) {
                        PackageIdentification id = (PackageIdentification) pkgItem.next();
                        if (!AggregationOperation.belongs(id, result)) {
                            result.add(id);
                        }
                    }
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return result;
    }
}