1 /***
2 *
3 */
4 package com.fernsroth.squashfs;
5
6 import java.io.File;
7 import java.text.SimpleDateFormat;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.Date;
11
12 import com.fernsroth.squashfs.model.BaseFile;
13 import com.fernsroth.squashfs.model.Directory;
14 import com.fernsroth.squashfs.model.SFSSquashedFile;
15 import com.fernsroth.squashfs.model.SymLink;
16 import com.fernsroth.squashfs.model.squashfs.stat;
17
18 /***
19 *
20 * @author Joseph M. Ferner (Near Infinity Corporation)
21 */
22 public final class SquashFSUtils {
23
24 /***
25 * linux date format.
26 */
27 public static final SimpleDateFormat LINUX_DATE_FORMAT = new SimpleDateFormat(
28 "MMM dd HH:mm");
29
30 /***
31 * iso 8601 date formatter.
32 */
33 public static SimpleDateFormat ISO8601_FORMAT = new SimpleDateFormat(
34 "yyyy-MM-dd'T'HH:mm:ss");
35
36 /***
37 * hide constructor.
38 */
39 private SquashFSUtils() {
40
41 }
42
43 /***
44 * gets the mode string.
45 * @param baseFile the file to get the string from.
46 * @return the mode string.
47 */
48 public static String getModeString(BaseFile baseFile) {
49 String line = "";
50 if (baseFile instanceof Directory) {
51 line += "d";
52 } else if (baseFile instanceof SymLink) {
53 line += "l";
54 } else if (baseFile instanceof SFSSquashedFile) {
55 line += "-";
56 }
57 line += getPermissionsString((baseFile.getMode() >> 6) & 0x07);
58 line += getPermissionsString((baseFile.getMode() >> 3) & 0x07);
59 line += getPermissionsString((baseFile.getMode() >> 0) & 0x07);
60 return line;
61 }
62
63 /***
64 * gets the permissions.
65 * @param permissions
66 * @return the permissions string.
67 */
68 public static String getPermissionsString(long permissions) {
69 String line = "";
70 line += (permissions & 0x04) == 0x04 ? "r" : "-";
71 line += (permissions & 0x02) == 0x02 ? "w" : "-";
72 line += (permissions & 0x01) == 0x01 ? "x" : "-";
73 return line;
74 }
75
76 /***
77 * gets the mode from a string.
78 * @param value the value to convert.
79 * @return the mode.
80 */
81 public static int getModeFromString(String value) {
82 String type = value.substring(0, 1);
83 String ownerStr = value.substring(1, 4);
84 String groupStr = value.substring(4, 7);
85 String allStr = value.substring(7, 10);
86 return getTypeFromChar(type.charAt(0))
87 | (getPermissionsFromString(ownerStr) << 6)
88 | (getPermissionsFromString(groupStr) << 3)
89 | (getPermissionsFromString(allStr) << 0);
90 }
91
92 /***
93 * @param c the type char.
94 * @return the type.
95 */
96 private static int getTypeFromChar(char c) {
97 switch (c) {
98 case 'd':
99 return stat.S_IFDIR;
100 case 'l':
101 return stat.S_IFLNK;
102 case '-':
103 return stat.S_IFREG;
104 default:
105 throw new RuntimeException("unknwon type character '" + c + "'");
106 }
107 }
108
109 /***
110 * gets the permissions bit mask from a string.
111 * @param str the string to get it from.
112 * @return the permissions bitmask.
113 */
114 public static int getPermissionsFromString(String str) {
115 int result = 0;
116 if (str.charAt(0) == 'r') {
117 result |= 0x04;
118 }
119 if (str.charAt(1) == 'w') {
120 result |= 0x02;
121 }
122 if (str.charAt(2) == 'x') {
123 result |= 0x01;
124 }
125 return result;
126 }
127
128 /***
129 * gets a date from a mtime.
130 * @param mtime the mtime to convert.
131 * @return the date.
132 */
133 public static Date getDateFromMTime(long mtime) {
134 return new Date(((long) mtime) * 1000);
135 }
136
137 /***
138 * gets the mtime from the date.
139 * @param dateMTime the date to convert.
140 * @return the mtime.
141 */
142 public static long getMTimeFromDate(Date dateMTime) {
143 return (dateMTime.getTime() / 1000);
144 }
145
146 /***
147 * walks a squashfs directory.
148 * @param dir the directory to walk.
149 * @param handler the walk handler to get called for each entry.
150 * @throws Exception
151 */
152 public static void walk(Directory dir, WalkHandler handler)
153 throws Exception {
154 walk(new Directory[] {}, dir, handler);
155 }
156
157 /***
158 * walks a squashfs directory.
159 * @param path path to the file.
160 * @param file the file.
161 * @param handler the handler.
162 * @throws Exception
163 */
164 private static void walk(Directory[] path, BaseFile file,
165 WalkHandler handler) throws Exception {
166 handler.visit(path, file);
167
168 if (file instanceof Directory) {
169 ArrayList<Directory> pathList = new ArrayList<Directory>(Arrays
170 .asList(path));
171 pathList.add((Directory) file);
172 for (BaseFile f : ((Directory) file).getSubentries()) {
173 walk(pathList.toArray(new Directory[pathList.size()]), f,
174 handler);
175 }
176 }
177 }
178
179 /***
180 * gets the relative path of a file.
181 * @param root the root path to get relative from.
182 * @param file the file to get the relative path of.
183 * @return the relative path of the file.
184 */
185 public static String getRelativePath(File root, File file) {
186 String rel = file.getAbsolutePath().substring(
187 root.getAbsolutePath().length() + 1);
188 rel = rel.replaceAll("////", "/");
189 return rel;
190 }
191 }