package org.javay.ports; import java.io.*; import java.util.ArrayList; import java.util.Iterator; import java.util.StringTokenizer; public class LibsUtil { public static void main(String[] args) throws Exception { // String copyFiles = args[0]; String line = ""; ArrayList originalFiles = new ArrayList(); ArrayList allFiles = new ArrayList(); ArrayList proceededFiles = new ArrayList(); ArrayList notFound = new ArrayList(); LineNumberReader reader = new LineNumberReader(new FileReader(copyFiles)); while((line = reader.readLine())!= null) { if(line.trim().length() > 0) { originalFiles.add(line.trim()); } } allFiles.addAll(originalFiles); boolean haveNew = true; while(haveNew) { haveNew = false; ArrayList iterateCollection = new ArrayList(allFiles); for (Iterator iterator = iterateCollection.iterator(); iterator.hasNext();) { String s = (String) iterator.next(); if(!proceededFiles.contains(s)) { proceededFiles.add(s); String type = getOutput("file " + s +"").toLowerCase(); boolean executable = type.contains("executable"); boolean so = type.contains("shared object"); boolean linux = type.contains("linux"); boolean freebsd = type.contains("freebsd"); if(freebsd && (so || executable)) { if(proceedFile(s, "freebsd", "/usr/local/lib", allFiles, notFound)) { haveNew = true; } } //here is linux if(linux && (so || executable)) { if(proceedFile(s, "linux", "/compat/linux", allFiles, notFound)) { haveNew = true; } } //here is linux } } } if(!notFound.isEmpty()) { System.out.println("Not Found LIbs:"); for (Iterator iterator = notFound.iterator(); iterator.hasNext();) { String s = (String) iterator.next(); System.out.println("\t\t" + s); } } System.out.println("All LIbs:"); for (Iterator iterator = allFiles.iterator(); iterator.hasNext();) { String s = (String) iterator.next(); System.out.println("\t\t" + s); } System.out.println("originalFiles.size() = " + originalFiles.size()); System.out.println("allFiles.size() = " + allFiles.size()); System.out.println("notFound.size() = " + notFound.size()); } public static boolean proceedFile(String record, String fileType, String firstPlaceSearch, ArrayList allFiles, ArrayList notFound) throws Exception { boolean haveNew = false; String ldd = getOutput("ldd " + record); for (StringTokenizer st = new StringTokenizer(ldd, "\n"); st.hasMoreTokens();) { String s1 = st.nextToken(); if(s1.contains("=>")) { String libName = s1.substring(0, s1.indexOf("=>")); String status = s1.substring(s1.indexOf("=>") + 2); boolean notFoundLib = status.contains("not found"); if(!notFoundLib) { String path = status.substring(0, status.lastIndexOf("(")).trim(); //if this is linux, so we should add /compat/linux ? // or ignore what we have here found, and search. if(getOutput("file " + path +"").toLowerCase().contains(fileType)) { //yep, it's our if(!allFiles.contains(path)) { allFiles.add(path); haveNew = true; } } else { notFoundLib = true; } } if(notFoundLib) { //not found, need to find somewhere :) String locate = getOutput("find " + firstPlaceSearch + " -name " + libName); if(locate.trim().length() == 0) { locate = getOutput("find /usr/local -name " + libName); } if(locate.trim().length() == 0) { locate = getOutput("find / -name " + libName); } if(locate.trim().length() != 0) { String[] strings = locate.split("\n"); //we probably could find here not our system lib , checkit for (int i = 0; i < strings.length; i++) { String string = strings[i]; if(getOutput("file " + string +"").toLowerCase().contains(fileType)) { //yep, it's our if(!allFiles.contains(string)) { allFiles.add(string); haveNew = true; } } } } else { if(!notFound.contains(libName)) { notFound.add(libName); } } } } } return haveNew; } public static String getOutput(String command) throws Exception { StringBuffer sb = new StringBuffer(); Process exec = Runtime.getRuntime().exec(command); Thread.sleep(100); //waiting for results exec.waitFor(); InputStream inputStream = exec.getInputStream(); byte[] b = new byte[1024*8]; while(inputStream.available() > 0) { int cnt = inputStream.read(b); sb.append(new String(b, 0, cnt)); } return sb.toString(); } }