[INST] Test code for zip library. 57/19857/1
authorMoonkyu Kang <moonkyu82.kang@samsung.com>
Tue, 22 Apr 2014 03:13:00 +0000 (12:13 +0900)
committerMoonkyu Kang <moonkyu82.kang@samsung.com>
Tue, 22 Apr 2014 03:13:00 +0000 (12:13 +0900)
Test code for Circular-Dependency.

Change-Id: Ib18210320cc1ace5db79e97badaa58f99983c845
Signed-off-by: Moonkyu Kang <moonkyu82.kang@samsung.com>
InstallManager_java/lib/zip4j_1.3.2.jar [new file with mode: 0644]
InstallManager_java/src/org/tizen/installmanager/cli/CliUnzipImage.java
InstallManager_java/src/org/tizen/installmanager/lib/linux/LinuxSDKPackageFormat.java
InstallManager_java/src/org/tizen/installmanager/lib/win/WindowsSDKPackageFormat.java
InstallManager_java/src/org/tizen/installmanager/pkg/lib/PackageManager.java
InstallManager_java/src/org/tizen/installmanager/pkg/model/PackageSet.java
InstallManager_java/src/org/tizen/installmanager/ui/dialog/UnzipImageDialog.java
InstallManager_java/src/org/tizen/installmanager/util/unzip/ApacheSdkUnzip.java
InstallManager_java/src/org/tizen/installmanager/util/unzip/ApacheUnzipThread.java
InstallManager_java/src/org/tizen/installmanager/util/unzip/UnzipUtil.java
InstallManager_java/src/org/tizen/installmanager/util/unzip/Zip4jSdkUnzip.java [new file with mode: 0644]

diff --git a/InstallManager_java/lib/zip4j_1.3.2.jar b/InstallManager_java/lib/zip4j_1.3.2.jar
new file mode 100644 (file)
index 0000000..66a19e4
Binary files /dev/null and b/InstallManager_java/lib/zip4j_1.3.2.jar differ
index 97e0d8a..89474be 100644 (file)
@@ -98,22 +98,11 @@ public class CliUnzipImage {
                
                Log.log("unzip to " + unzipTargetDir);
                
-               // Remove directory or file as 'binary' just in case.
-               File uselessFile = new File(PathUtil.get(unzipTargetDir, Config.BINARY_DIRECTORY));
-               if (uselessFile.exists()) {
-                       if (uselessFile.isDirectory()) {
-                               File[] files = uselessFile.listFiles();
-                               if (files != null) {
-                                       for (File useless : files) {
-                                               if (!useless.delete()) {
-                                                       Log.err("Fail to delete useless file ==> " + useless);
-                                               }
-                                       }
-                               }
-                       }
-                       if (!uselessFile.delete()) {
-                               Log.err("Fail to delete file ==> " + uselessFile);
-                       }
+
+               if(UnzipUtil.removeUsleseFile(unzipTargetDir) == ISdkConstant.SUCCESS){
+                       Log.log("Success to remove \'binary\' directory.");
+               }else{
+                       Log.err("Fail to remove \'binary\' directory.");
                }
                
                sdkTempDir = unzipTargetDir;
@@ -121,6 +110,8 @@ public class CliUnzipImage {
                
                return unzipTargetDir;
        }
+
+
        
     /**
      * Extract a SDK Image File.
index 777ff8d..485dac5 100644 (file)
@@ -39,6 +39,7 @@ import org.tizen.installmanager.util.IMShellCommandFail;
 import org.tizen.installmanager.util.ShellUtil;\r
 import org.tizen.installmanager.util.unzip.ApacheSdkUnzip;\r
 import org.tizen.installmanager.util.unzip.UnzipSDK;\r
+import org.tizen.installmanager.util.unzip.Zip4jSdkUnzip;\r
 \r
 /**\r
  * Manages to unpack package of tizen sdk for linux.\r
@@ -79,9 +80,8 @@ public class LinuxSDKPackageFormat extends SDKPackageFormat {
                int exitValue = ERROR;\r
 \r
                if (checkExtension == PackageType.ZIP){\r
-                       UnzipSDK unzipSdk = new ApacheSdkUnzip();\r
+                       UnzipSDK unzipSdk = new Zip4jSdkUnzip();                                \r
                        return unzipSdk.unzipPackageFile(fileFullPath.getAbsolutePath(), targetDir.getAbsolutePath(),monitor);                  \r
-                       \r
                } else if (checkExtension == PackageType.TAR_GZ) {\r
                        try {\r
                                exitValue = sendCommand(\r
index bf62407..46a4ee7 100644 (file)
@@ -85,16 +85,12 @@ public class WindowsSDKPackageFormat extends SDKPackageFormat{
                        }
                }
 
-//             long startTime = System.currentTimeMillis();
-//             long ret = unZipPackage(fileFullPath, targetDir, monitor);
-//             long finishedTime = System.currentTimeMillis();
-//             System.out.println("extracting time : "
-//                             + (finishedTime - startTime));
                UnzipSDK unzipSdk = new ApacheSdkUnzip();
-               long ret = unzipSdk.unzipPackageFile(fileFullPath.getAbsolutePath(), targetDir.getAbsolutePath(),monitor);      
+               return unzipSdk.unzipPackageFile(fileFullPath.getAbsolutePath(), targetDir.getAbsolutePath(),monitor);  
         
+                //long ret = unZipPackage(fileFullPath, targetDir, monitor);
         //closeFileOutput(); 
-        return ret;
+        //return ret;
     }
     
        private long unZipPackage(File fileFullPath, File targetDir, IIMProgressMonitor monitor) {
index 712eead..9bed7fe 100644 (file)
@@ -35,6 +35,7 @@ import java.net.URL;
 import java.util.ArrayList;\r
 import java.util.Collection;\r
 import java.util.HashMap;\r
+import java.util.LinkedHashSet;\r
 import java.util.LinkedList;\r
 import java.util.List;\r
 import java.util.Map;\r
@@ -1159,7 +1160,8 @@ public abstract class PackageManager {
                                for(Package depPkg : depPackageSet) {\r
                                        if(p.equals(depPkg)) {\r
                                                Log.err("Circular dependency found: "+p);\r
-                                               throw new IMFatalException(ErrorCode.CIRCULAR_PACKAGE_DEPENDENCY);\r
+                                               //throw new IMFatalException(ErrorCode.CIRCULAR_PACKAGE_DEPENDENCY);\r
+                                               dep.addCircularPackageSet(depPkg);\r
                                        }\r
                                        pkgStack.push(depPkg);\r
                                }\r
@@ -1382,6 +1384,13 @@ public abstract class PackageManager {
        public boolean isMetaUpdatable(Package m) {             \r
                // if one of the meta package's dependents is installed and updatable, the meta package is updatable.\r
                PackageSet dep = dep(m, State.INSTALL);\r
+               \r
+               // if circular package is exists, return false;\r
+               LinkedHashSet<Package> circularPackage = dep.getCircularPackage();\r
+               if(circularPackage != null && !circularPackage.isEmpty()){\r
+                       return false;\r
+               }\r
+               \r
 \r
                for(Package p : dep) {\r
                        \r
@@ -1433,6 +1442,11 @@ public abstract class PackageManager {
                // if one of the meta package's dependents is installed and updatable, the meta package is updatable.\r
                PackageSet dep = dep(meta, State.INSTALL);\r
                \r
+               LinkedHashSet<Package> circularPackageSet = dep.getCircularPackage();\r
+               if(circularPackageSet != null && !circularPackageSet.isEmpty()){\r
+                       return false;\r
+               }\r
+               \r
                for(Package p : dep) {\r
                        //Do not check meta package. It is only used by install-type or category.\r
                        if (p.isMeta()) {\r
index 02d7b6b..0a743ae 100644 (file)
@@ -41,7 +41,7 @@ import java.util.LinkedHashSet;
 @SuppressWarnings("serial")
 public class PackageSet extends LinkedHashSet<Package>{
        public final static PackageSet EMPTY = new PackageSet();
-       
+       private LinkedHashSet<Package> circularPackage = new LinkedHashSet<Package>(); 
        /**
         * Finds and returns a package in this set with the same name of the given package.
         * You have to check the existence with <code>contains()</code> method before calling this method.
@@ -121,4 +121,20 @@ public class PackageSet extends LinkedHashSet<Package>{
        public int getSize() {
                return this.size();
        }
+
+       // Check Circular Package Set
+
+       
+       
+       public boolean addCircularPackageSet(Package pkg){
+               return circularPackage.add(pkg);
+       }
+
+       public LinkedHashSet<Package> getCircularPackage() {
+               return circularPackage;
+       }
+
+       public void setCircularPackage(LinkedHashSet<Package> circularPackage) {
+               this.circularPackage = circularPackage;
+       }
 }
index 9cf5850..8391e49 100644 (file)
@@ -225,7 +225,12 @@ public class UnzipImageDialog extends Dialog {
                public void run() {
                        try {
                                while (!Thread.currentThread().isInterrupted()) {
-                                       unzipImageFile(composite);                                      
+                                       
+                                       long startTime = System.currentTimeMillis();
+                                       unzipImageFile(composite);
+                                       long finishedTime = System.currentTimeMillis();
+                                       System.out.println("SDK Image extracting time : "
+                                                       + (finishedTime - startTime));
                                }
                        } catch (IMFatalException e) {
                                Log.err("Cannot work to extract SDK Image File.");
index 385d065..683d5cc 100644 (file)
@@ -41,15 +41,25 @@ public class ApacheSdkUnzip extends UnzipSDK {
                        ExecutorService executor = Executors.newFixedThreadPool(5);
                        long startTime = System.currentTimeMillis();
                        while (entries.hasMoreElements()) {
-                               ZipArchiveEntry element = entries.nextElement();
-                               String fileNameToUnzip = element.getName();
-                               File targetFile = new File(targetFullPath, fileNameToUnzip);
+                               ZipArchiveEntry element = entries.nextElement();                                
+                               File targetFile = new File(targetFullPath, element.getName());
                                if (element.isDirectory()) {// Directory case
-                                       if (!targetFile.mkdir()) { // Make directory
-                                               System.out.println("Fail to create file ==> "
-                                                               + targetFile);
+                                       if (!targetFile.exists()) {
+                                               if (!targetFile.mkdirs()) { // Make directory
+                                                       if (targetFile.exists()) {
+                                                               System.out.println("Already exists : "
+                                                                               + targetFile.getAbsolutePath());
+                                                       } else {
+                                                               System.out
+                                                                               .println("Not file exists!!!,Fail to create file ==> "
+                                                                                               + targetFile.getAbsolutePath());
+                                                       }
+                                               }
+                                       } else {
+                                               System.out.println("Already exists : "
+                                                               + targetFile.getAbsolutePath());
                                        }
-                               } else {
+                               } else {                                        
                                        Runnable worker = new ApacheUnzipThread(targetFile,
                                                        element, zipFile);
                                        executor.execute(worker);
@@ -79,34 +89,4 @@ public class ApacheSdkUnzip extends UnzipSDK {
                }               
                return ISdkConstant.SUCCESS;
        }
-       
-       private long copyFile(File targetFile,ZipArchiveEntry element,ZipFile zipFile){
-               OutputStream fos = null;
-               InputStream inputStream = null;
-               try {
-                       fos = new FileOutputStream(targetFile);
-                       inputStream = zipFile.getInputStream(element);
-                       IOUtils.copy(inputStream, fos);
-                       if ((element.getUnixMode() & 0100) != 0) {
-                               targetFile.setExecutable(true);                                         
-                       }
-               } catch (FileNotFoundException e) {
-                       e.printStackTrace();
-                       return ISdkConstant.ERROR;
-               } catch (ZipException e) {
-                       return ISdkConstant.ERROR;
-               } catch (IOException e) {
-                       return ISdkConstant.ERROR;
-               }finally{
-                       if(inputStream != null){
-                               IOUtils.closeQuietly(inputStream);
-                       }
-                       if(fos != null){
-                               IOUtils.closeQuietly(fos);
-                       }
-               }
-               return ISdkConstant.SUCCESS;
-       }
-
-
 }
index e8ef222..8c752c3 100644 (file)
@@ -5,16 +5,12 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
 import java.util.zip.ZipException;
 
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
-import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 import org.apache.commons.compress.archivers.zip.ZipFile;
 import org.apache.commons.compress.utils.IOUtils;
-
-import sun.nio.ch.ThreadPool;
+import org.tizen.installmanager.lib.Log;
 
 public class ApacheUnzipThread implements Runnable{
 
@@ -28,6 +24,16 @@ public class ApacheUnzipThread implements Runnable{
        
        public ApacheUnzipThread(File targetFile,ZipArchiveEntry element,ZipFile zipFile){
                this.targetFile = targetFile;
+               File parent = new File(targetFile.getParent());
+               parent.mkdirs();                
+               if(!this.targetFile.exists()){
+                       try {
+                               targetFile.createNewFile();
+                       } catch (IOException e) {
+                               // TODO Auto-generated catch block
+                               e.printStackTrace();
+                       }
+               }
                this.element = element;
                this.zipFile = zipFile;
        }
@@ -44,18 +50,22 @@ public class ApacheUnzipThread implements Runnable{
                try {
                        fos = new FileOutputStream(targetFile);
                        inputStream = zipFile.getInputStream(element);
-                       long copy = IOUtils.copy(inputStream, fos);
-                       System.out.println("Copy : " + copy);
+                       long copy = IOUtils.copy(inputStream, fos);                     
                        if ((element.getUnixMode() & 0100) != 0) {
                                targetFile.setExecutable(true);                                         
                        }
                        
-               } catch (FileNotFoundException e) {
-                       e.printStackTrace();
+               } catch (FileNotFoundException e) {     
+                       Log.err("FileNotFoundException - Path: " + targetFile.getAbsolutePath());
+                       System.out.println("FileNotFoundException - Path: " + targetFile.getAbsolutePath());
                        return ERROR;
                } catch (ZipException e) {
+                       System.out.println("ZipException - Path: " + targetFile.getAbsolutePath());
+                       Log.err("ZipException - Path: " + targetFile.getAbsolutePath());
                        return ERROR;
                } catch (IOException e) {
+                       System.out.println("IOException - Path: " + targetFile.getAbsolutePath());
+                       Log.err("IOException - Path: " + targetFile.getAbsolutePath());
                        return ERROR;
                }finally{
                        if(inputStream != null){
index d1f06e1..7a40b8f 100644 (file)
@@ -8,8 +8,10 @@ import java.util.zip.ZipException;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipInputStream;
 
+import org.tizen.installmanager.core.Config;
 import org.tizen.installmanager.lib.Log;
 import org.tizen.installmanager.util.ISdkConstant;
+import org.tizen.installmanager.util.PathUtil;
 import org.tizen.installmanager.util.ResourceHandler;
 
 public class UnzipUtil {
@@ -82,7 +84,33 @@ public class UnzipUtil {
                }else{
                        Log.err("Fail to create SDK temporary directory.");
                        return ISdkConstant.ERROR;
+               }               
+       }
+       
+       /**
+        * Remove directory or file as 'binary' just in case.
+        * @param unzipTargetDir
+        * @return Result of remove 'binary'
+        */
+       public static int removeUsleseFile(String unzipTargetDir) {
+               File uselessFile = new File(PathUtil.get(unzipTargetDir, Config.BINARY_DIRECTORY));
+               if (uselessFile.exists()) {
+                       if (uselessFile.isDirectory()) {
+                               File[] files = uselessFile.listFiles();
+                               if (files != null) {
+                                       for (File useless : files) {
+                                               if (!useless.delete()) {
+                                                       Log.err("Fail to delete useless file ==> " + useless);
+                                               }
+                                       }
+                               }
+                       }
+                       if (!uselessFile.delete()) {
+                               Log.err("Fail to delete file ==> " + uselessFile);
+                               return ISdkConstant.ERROR;
+                       }
                }
+               return ISdkConstant.SUCCESS;
                
        }
 }
diff --git a/InstallManager_java/src/org/tizen/installmanager/util/unzip/Zip4jSdkUnzip.java b/InstallManager_java/src/org/tizen/installmanager/util/unzip/Zip4jSdkUnzip.java
new file mode 100644 (file)
index 0000000..d0828b6
--- /dev/null
@@ -0,0 +1,59 @@
+package org.tizen.installmanager.util.unzip;
+
+import java.io.File;
+import java.util.List;
+
+import net.lingala.zip4j.core.ZipFile;
+import net.lingala.zip4j.exception.ZipException;
+import net.lingala.zip4j.model.FileHeader;
+
+import org.tizen.installmanager.lib.IIMProgressMonitor;
+import org.tizen.installmanager.lib.Log;
+import org.tizen.installmanager.util.ISdkConstant;
+
+
+
+public class Zip4jSdkUnzip extends UnzipSDK {
+
+       @Override
+       public long unzipPackageFile(String fileFullPath, String targetFullPath,IIMProgressMonitor monitor) {
+               try {                   
+                       ZipFile zipfile = new ZipFile(fileFullPath);
+                       long startTime = System.currentTimeMillis();
+                       @SuppressWarnings("unchecked")
+                       List<FileHeader> fileHeaders = (List<FileHeader>)zipfile.getFileHeaders();
+                       if(fileHeaders != null){                                
+                               for (FileHeader fileHeader : fileHeaders) {                                     
+                                       unzip(targetFullPath, zipfile, fileHeader);
+                                       monitor.workedInstallSize(fileHeader.getUncompressedSize());
+                               }
+                               long finishedTime = System.currentTimeMillis();
+                               System.out.println("extracting time : "+ (finishedTime - startTime));
+                       }else{                                  
+                               return ISdkConstant.ERROR;
+                       }                       
+               } catch (ZipException e) {
+                       Log.ExceptionLog(e);
+                       return ISdkConstant.ERROR;
+               }
+               return ISdkConstant.SUCCESS;            
+       }
+
+       private void unzip(String targetFullPath, ZipFile zipfile,
+                       FileHeader fileHeader) throws ZipException {
+               zipfile.extractFile(fileHeader, targetFullPath);
+               if(!fileHeader.isDirectory()){
+                       byte[] externalFileAttr = fileHeader.getExternalFileAttr();
+                       if(externalFileAttr != null && externalFileAttr.length > 0){
+                               short bytesToShort = BytesToShort(externalFileAttr,
+                                               Little_EDIAN);
+                               if ((bytesToShort & 0100) != 0) {
+                                       File f = new File(targetFullPath,
+                                                       fileHeader.getFileName());
+                                       f.setExecutable(true);
+                               }
+                       }
+               }
+       }
+
+}