Skip to content

Package: LibReader

LibReader

nameinstructionbranchcomplexitylinemethod
LibReader(ArchiveGenerator, File)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
addDeps(Module)
M: 38 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
addModule(Module)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
choosePath(Module)
M: 45 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
createLib(String)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
createProg(String)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getElf()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
read()
M: 45 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
resolveReferences(Module)
M: 31 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
searchLibFile(String)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
splitInDynamicEntries()
M: 35 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: *
3: */
4: package libraries;
5:
6: import java.io.File;
7: import java.io.FileNotFoundException;
8: import java.io.IOException;
9: import java.util.ArrayList;
10: import java.util.Iterator;
11: import java.util.List;
12:
13: import generator.ArchiveGenerator;
14: import nl.lxtreme.binutils.elf.Dynamic;
15: import nl.lxtreme.binutils.elf.Elf;
16: import nl.lxtreme.binutils.elf.Section;
17: import pruefskript.parser.exceptions.CheckScriptException;
18: import pruefskript.parser.nodes.AddToOptFacade;
19:
20: /**
21: * This class representes the reader for the libaries.
22: *
23: * @author Hendrik
24: *
25: */
26: class LibReader {
27:         /**
28:          * The elf.
29:          */
30:         private final nl.lxtreme.binutils.elf.Elf elf;
31:         /**
32:          * List of modules.
33:          */
34:         private final List<Module> modules;
35:         /**
36:          * An array of Dynamic objects. May be empty if no dynamic section is found.
37:          */
38:         private Dynamic[] dynamics;
39:         /**
40:          * The {@link ArchiveGenerator} to use.
41:          */
42:         private final ArchiveGenerator gen;
43:
44:         /**
45:          * constructor.
46:          *
47:          * @param gen
48:          * the {@link ArchiveGenerator} to use
49:          * @param aFile
50:          * file to read.
51:          * @throws IOException
52:          * file is empty.
53:          *
54:          */
55:         protected LibReader(final ArchiveGenerator gen, final File aFile) throws IOException {
56:                 super();
57:                 this.elf = new Elf(aFile);
58:                 this.modules = new ArrayList<Module>();
59:                 this.gen = gen;
60:         }
61:
62:         /**
63:          * reads the data.
64:          *
65:          * @throws IOException
66:          * from file
67:          * @throws LibReaderException
68:          * if no dynamic entries.
69:          *
70:          * @throws CheckScriptException
71:          * if it fails.
72:          *
73:          *
74:          */
75:         public void read() throws IOException, LibReaderException, CheckScriptException {
76:                 splitInDynamicEntries();
77:•                for (Dynamic entry : this.dynamics) {
78:•                        if (entry.getTag() == Dynamic.DT_SONAME) {
79:                                 this.createLib(entry.getName());
80:•                        } else if (entry.getTag() == Dynamic.DT_RPATH) { // RPath null?!
81:                                 this.createProg(entry.getName());
82:                         } else {
83:                                 throw new LibReaderException("No Dynamic Entry found!");
84:                         }
85:                 }
86:         }
87:
88:         /**
89:          * @return the elf
90:          */
91:         protected nl.lxtreme.binutils.elf.Elf getElf() {
92:                 return this.elf;
93:         }
94:
95:         /**
96:          * Reads all dynamic entries from an ELF file.
97:          *
98:          *
99:          * @throws IOException
100:          * if an I/O error occurs
101:          */
102:         private void splitInDynamicEntries() throws IOException {
103:                 final Section[] sections = this.elf.getSections();
104:•                for (Section section : sections) {
105:•                        if (section.getType() == Section.SHT_DYNAMIC) {
106:                                 this.dynamics = this.elf.getDynamicSections(section);
107:                         }
108:                 }
109:                 this.dynamics = new Dynamic[0];
110:         }
111:
112:         /**
113:          * add the LibReferences to the module.
114:          *
115:          * @param mod
116:          * the module
117:          * @throws ReferencedModuleNotFoundException
118:          * if module not found.
119:          * @throws CheckScriptException
120:          * if it fails
121:          * @throws FileNotFoundException
122:          * if no file is found.
123:          */
124:         private void addDeps(final Module mod) throws ReferencedModuleNotFoundException,
125:                         FileNotFoundException, CheckScriptException {
126:•                for (Dynamic entry : this.dynamics) {
127:•                        if (entry.getTag() == Dynamic.DT_NEEDED) {
128:                                 mod.addLibRefs(new LibReference(new LibRefUnresolved(),
129:                                                 LibManager.getTheInstance().getSoName(entry.getName())));
130:                                 this.resolveReferences(mod);
131:
132:                         }
133:                 }
134:
135:         }
136:
137:         /**
138:          * create lib an add to modules.
139:          *
140:          * @param name
141:          * the name of the SOName
142:          * @throws ReferencedModuleNotFoundException
143:          * if dependency not found.
144:          * @throws CheckScriptException
145:          * if it fails.
146:          * @throws FileNotFoundException
147:          * if no file is found
148:          */
149:         private void createLib(final String name) throws ReferencedModuleNotFoundException,
150:                         FileNotFoundException, CheckScriptException {
151:                 final Lib lib = new Lib(LibManager.getTheInstance().getSoName(name));
152:                 this.addModule(lib);
153:                 this.addDeps(lib);
154:
155:         }
156:
157:         /**
158:          * creates Program and add to modules.
159:          *
160:          * @param rPath
161:          * the rPathString
162:          * @throws ReferencedModuleNotFoundException
163:          * if dependency not found
164:          * @throws CheckScriptException
165:          * if it fails.
166:          * @throws FileNotFoundException
167:          * if no file is found
168:          */
169:         private void createProg(final String rPath) throws ReferencedModuleNotFoundException,
170:                         FileNotFoundException, CheckScriptException {
171:                 final Program program = new Program();
172:                 program.setRpath(new RPath(rPath)); // Was passiert wenn null?!!
173:                 this.addModule(program);
174:                 this.addDeps(program);
175:         }
176:
177:         /**
178:          * add to modules.
179:          *
180:          * @param entry
181:          * the entry to add.
182:          */
183:         private void addModule(final Module entry) {
184:•                if (!this.modules.contains(entry)) {
185:                         this.modules.add(entry);
186:                         LibManager.getTheInstance().addModule(entry);
187:                 }
188:
189:         }
190:
191:         /**
192:          * resolve the unresolved references.
193:          *
194:          * @param module
195:          * to resolve
196:          * @throws ReferencedModuleNotFoundException
197:          * if Module is not found.
198:          * @throws CheckScriptException
199:          * if it fails
200:          * @throws FileNotFoundException
201:          * if no file is found.
202:          */
203:         private void resolveReferences(final Module module) throws ReferencedModuleNotFoundException,
204:                         FileNotFoundException, CheckScriptException {
205:                 final Iterator<LibReference> i = module.getLibRefs().iterator();
206:•                if (i.hasNext()) {
207:                         final LibReference ref = i.next();
208:•                        if (LibManager.getTheInstance().getSONames().containsValue(ref.getSoName())) {
209:                                 // TODO: LIB aus der Liste suchen und State auf resolved.
210:                         } else {
211:                                 final String path = choosePath(module);
212:                                 this.gen.addToOpt(new AddToOptFacade(path, ""));
213:                         }
214:                 }
215:
216:                 // TODO: Resolve Dependencies indem vielleicht fehlende Libs einlesen werden
217:                 // Neuen Reader mit der einzulesenden Lib erstellen. Libs werden entweder im bevorzugten!!
218:                 // RPath oder in /lib oder in /usr/lib/ gefunden!
219:
220:         }
221:
222:         /**
223:          * choose the path to referenced File.
224:          *
225:          * @param module
226:          * the module to find the path
227:          * @return the path as String.
228:          * @throws ReferencedModuleNotFoundException
229:          * if module is not found
230:          */
231:         private String choosePath(final Module module) throws ReferencedModuleNotFoundException {
232:                 Boolean found = false;
233:                 final String path;
234:•                if (module instanceof Program) {
235:                         found = searchLibFile(((Program) module).getRpath().toString());
236:                 }
237:
238:•                if (!found) {
239:•                        if (!searchLibFile(ModuleConstants.SEARCHPATH_LIB)) {
240:•                                if (searchLibFile(ModuleConstants.SEARCHPATH_USRLIB)) {
241:                                         path = ModuleConstants.SEARCHPATH_USRLIB;
242:
243:                                 } else {
244:                                         throw new ReferencedModuleNotFoundException(module.toString());
245:                                 }
246:                         } else {
247:                                 path = ModuleConstants.SEARCHPATH_LIB;
248:                         }
249:                 } else {
250:                         path = ((Program) module).getRpath().toString();
251:                 }
252:                 return path;
253:         }
254:
255:         /**
256:          * Searches a directory for a library.
257:          *
258:          * @param string
259:          * The directory to search.
260:          *
261:          * @return true = found; false not found
262:          */
263:         private Boolean searchLibFile(final String string) {
264:                 // TODO: Wie geht das?
265:                 return false;
266:
267:         }
268:
269: }