Test code for Circular-Dependency.
Change-Id: Ib18210320cc1ace5db79e97badaa58f99983c845
Signed-off-by: Moonkyu Kang <moonkyu82.kang@samsung.com>
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;
return unzipTargetDir;
}
+
+
/**
* Extract a SDK Image File.
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
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
}
}
-// 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) {
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
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
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
// 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
@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.
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;
+ }
}
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.");
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);
}
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;
- }
-
-
}
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{
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;
}
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){
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 {
}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;
}
}
--- /dev/null
+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);
+ }
+ }
+ }
+ }
+
+}