1 /***
2 *
3 */
4 package com.fernsroth.squashfs.model;
5
6 /***
7 *
8 * @author Joseph M. Ferner (Near Infinity Corporation)
9 */
10 public class Manifest {
11
12 /***
13 * the root directory.
14 */
15 private Directory root;
16
17 /***
18 * gets the root directory.
19 * @return the root directory.
20 */
21 public Directory getRoot() {
22 return this.root;
23 }
24
25 /***
26 * @param root the root to set
27 */
28 public void setRoot(Directory root) {
29 this.root = root;
30 }
31
32 /***
33 * @param filename the file name to find.
34 * @return the BaseFile with that path.
35 */
36 public BaseFile find(String filename) {
37 return find(getRoot(), filename);
38 }
39
40 /***
41 * @param start the start node.
42 * @param filename the filename to find.
43 * @return the base file with that path.
44 */
45 private BaseFile find(Directory start, String filename) {
46 int i = filename.indexOf('/');
47 String toFind;
48 String left;
49 if (i == -1) {
50 toFind = filename;
51 left = null;
52 } else {
53 toFind = filename.substring(0, i);
54 left = filename.substring(i + 1);
55 }
56 if (toFind.equals(".")) {
57 return find(start, left);
58 } else {
59 for (BaseFile bf : start.getSubentries()) {
60 if (bf.getName().equals(toFind)) {
61 if (left == null) {
62 return bf;
63 } else {
64 return find((Directory) bf, left);
65 }
66 }
67 }
68 }
69 return null;
70 }
71
72 /***
73 * get the path to a file.
74 * @param bf the file to find path to.
75 * @return the path.
76 */
77 public String getPath(BaseFile bf) {
78 return getPath(getRoot(), bf);
79 }
80
81 /***
82 * get the path to a file.
83 * @param start the start point.
84 * @param bf the file to find.
85 * @return the path.
86 */
87 private String getPath(Directory start, BaseFile bf) {
88 for (BaseFile f : start.getSubentries()) {
89 if (f == bf) {
90 return bf.getName();
91 }
92 if (f instanceof Directory) {
93 String path = getPath((Directory) f, bf);
94 if (path != null) {
95 return f.getName() + "/" + path;
96 }
97 }
98 }
99 return null;
100 }
101 }